resolveConfig.js 3.4 KB

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