| [9af201e] | 1 | import platform from '../platform/index.js';
|
|---|
| 2 | import utils from '../utils.js';
|
|---|
| 3 | import isURLSameOrigin from './isURLSameOrigin.js';
|
|---|
| 4 | import cookies from './cookies.js';
|
|---|
| 5 | import buildFullPath from '../core/buildFullPath.js';
|
|---|
| 6 | import mergeConfig from '../core/mergeConfig.js';
|
|---|
| 7 | import AxiosHeaders from '../core/AxiosHeaders.js';
|
|---|
| 8 | import buildURL from './buildURL.js';
|
|---|
| 9 |
|
|---|
| 10 | const FORM_DATA_CONTENT_HEADERS = ['content-type', 'content-length'];
|
|---|
| 11 |
|
|---|
| 12 | function setFormDataHeaders(headers, formHeaders, policy) {
|
|---|
| 13 | if (policy !== 'content-only') {
|
|---|
| 14 | headers.set(formHeaders);
|
|---|
| 15 | return;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | Object.entries(formHeaders).forEach(([key, val]) => {
|
|---|
| 19 | if (FORM_DATA_CONTENT_HEADERS.includes(key.toLowerCase())) {
|
|---|
| 20 | headers.set(key, val);
|
|---|
| 21 | }
|
|---|
| 22 | });
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Encode a UTF-8 string to a Latin-1 byte string for use with btoa().
|
|---|
| 27 | * This is a modern replacement for the deprecated unescape(encodeURIComponent(str)) pattern.
|
|---|
| 28 | *
|
|---|
| 29 | * @param {string} str The string to encode
|
|---|
| 30 | *
|
|---|
| 31 | * @returns {string} UTF-8 bytes as a Latin-1 string
|
|---|
| 32 | */
|
|---|
| 33 | const encodeUTF8 = (str) =>
|
|---|
| 34 | encodeURIComponent(str).replace(/%([0-9A-F]{2})/gi, (_, hex) =>
|
|---|
| 35 | String.fromCharCode(parseInt(hex, 16))
|
|---|
| 36 | );
|
|---|
| 37 |
|
|---|
| 38 | export default (config) => {
|
|---|
| 39 | const newConfig = mergeConfig({}, config);
|
|---|
| 40 |
|
|---|
| 41 | // Read only own properties to prevent prototype pollution gadgets
|
|---|
| 42 | // (e.g. Object.prototype.baseURL = 'https://evil.com').
|
|---|
| 43 | const own = (key) => (utils.hasOwnProp(newConfig, key) ? newConfig[key] : undefined);
|
|---|
| 44 |
|
|---|
| 45 | const data = own('data');
|
|---|
| 46 | let withXSRFToken = own('withXSRFToken');
|
|---|
| 47 | const xsrfHeaderName = own('xsrfHeaderName');
|
|---|
| 48 | const xsrfCookieName = own('xsrfCookieName');
|
|---|
| 49 | let headers = own('headers');
|
|---|
| 50 | const auth = own('auth');
|
|---|
| 51 | const baseURL = own('baseURL');
|
|---|
| 52 | const allowAbsoluteUrls = own('allowAbsoluteUrls');
|
|---|
| 53 | const url = own('url');
|
|---|
| 54 |
|
|---|
| 55 | newConfig.headers = headers = AxiosHeaders.from(headers);
|
|---|
| 56 |
|
|---|
| 57 | newConfig.url = buildURL(
|
|---|
| 58 | buildFullPath(baseURL, url, allowAbsoluteUrls),
|
|---|
| 59 | config.params,
|
|---|
| 60 | config.paramsSerializer
|
|---|
| 61 | );
|
|---|
| 62 |
|
|---|
| 63 | // HTTP basic authentication
|
|---|
| 64 | if (auth) {
|
|---|
| 65 | headers.set(
|
|---|
| 66 | 'Authorization',
|
|---|
| 67 | 'Basic ' +
|
|---|
| 68 | btoa((auth.username || '') + ':' + (auth.password ? encodeUTF8(auth.password) : ''))
|
|---|
| 69 | );
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if (utils.isFormData(data)) {
|
|---|
| 73 | if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
|
|---|
| 74 | headers.setContentType(undefined); // browser handles it
|
|---|
| 75 | } else if (utils.isFunction(data.getHeaders)) {
|
|---|
| 76 | // Node.js FormData (like form-data package)
|
|---|
| 77 | setFormDataHeaders(headers, data.getHeaders(), own('formDataHeaderPolicy'));
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // Add xsrf header
|
|---|
| 82 | // This is only done if running in a standard browser environment.
|
|---|
| 83 | // Specifically not if we're in a web worker, or react-native.
|
|---|
| 84 |
|
|---|
| 85 | if (platform.hasStandardBrowserEnv) {
|
|---|
| 86 | if (utils.isFunction(withXSRFToken)) {
|
|---|
| 87 | withXSRFToken = withXSRFToken(newConfig);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | // Strict boolean check — prevents proto-pollution gadgets (e.g. Object.prototype.withXSRFToken = 1)
|
|---|
| 91 | // and misconfigurations (e.g. "false") from short-circuiting the same-origin check and leaking
|
|---|
| 92 | // the XSRF token cross-origin.
|
|---|
| 93 | const shouldSendXSRF =
|
|---|
| 94 | withXSRFToken === true || (withXSRFToken == null && isURLSameOrigin(newConfig.url));
|
|---|
| 95 |
|
|---|
| 96 | if (shouldSendXSRF) {
|
|---|
| 97 | const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName);
|
|---|
| 98 |
|
|---|
| 99 | if (xsrfValue) {
|
|---|
| 100 | headers.set(xsrfHeaderName, xsrfValue);
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return newConfig;
|
|---|
| 106 | };
|
|---|