(requestOptions, policyContext = {})
| 94 | } |
| 95 | |
| 96 | async function safeRequest(requestOptions, policyContext = {}) { |
| 97 | const baseOptions = { ...requestOptions }; |
| 98 | const followRedirect = baseOptions.followRedirect !== false; |
| 99 | const followAllRedirects = baseOptions.followAllRedirects === true; |
| 100 | const maxRedirects = Number.isInteger(baseOptions.maxRedirects) |
| 101 | ? baseOptions.maxRedirects |
| 102 | : DEFAULT_MAX_REDIRECTS; |
| 103 | |
| 104 | let redirectCount = 0; |
| 105 | let currentUrl = baseOptions.url; |
| 106 | let currentMethod = baseOptions.method || "GET"; |
| 107 | let currentBody = baseOptions.body; |
| 108 | let currentForm = baseOptions.form; |
| 109 | let currentFormData = baseOptions.formData; |
| 110 | |
| 111 | while (true) { // oxlint-disable-line no-constant-condition |
| 112 | const optionsForRequest = sanitizeOutboundRequestOptions( |
| 113 | { |
| 114 | ...baseOptions, |
| 115 | url: currentUrl, |
| 116 | method: currentMethod, |
| 117 | body: currentBody, |
| 118 | form: currentForm, |
| 119 | formData: currentFormData, |
| 120 | followRedirect: false, |
| 121 | }, |
| 122 | currentUrl |
| 123 | ); |
| 124 | |
| 125 | // Redirect validation must run sequentially to validate each hop in order. |
| 126 | // oxlint-disable-next-line no-await-in-loop |
| 127 | const validationResult = await validateOutboundUrl(optionsForRequest.url, { |
| 128 | ...policyContext, |
| 129 | redirectCount, |
| 130 | }); |
| 131 | |
| 132 | // oxlint-disable-next-line no-await-in-loop |
| 133 | const response = await request({ |
| 134 | ...optionsForRequest, |
| 135 | lookup: createPinnedLookup( |
| 136 | validationResult.hostname, |
| 137 | validationResult.resolvedAddresses |
| 138 | ), |
| 139 | }); |
| 140 | const statusCode = response && response.statusCode; |
| 141 | const location = response && response.headers && response.headers.location; |
| 142 | |
| 143 | if (!location || !shouldFollowRedirect({ |
| 144 | statusCode, |
| 145 | method: currentMethod, |
| 146 | followRedirect, |
| 147 | followAllRedirects, |
| 148 | })) { |
| 149 | return response; |
| 150 | } |
| 151 | |
| 152 | if (redirectCount >= maxRedirects) { |
| 153 | const redirectError = new Error("Exceeded maximum redirect limit for outbound request."); |
no test coverage detected