resolveConfig.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. export default (config) => {
  10. const newConfig = mergeConfig({}, config);
  11. // Read only own properties to prevent prototype pollution gadgets
  12. // (e.g. Object.prototype.baseURL = 'https://evil.com'). See GHSA-q8qp-cvcw-x6jj.
  13. const own = (key) => (utils.hasOwnProp(newConfig, key) ? newConfig[key] : undefined);
  14. const data = own('data');
  15. let withXSRFToken = own('withXSRFToken');
  16. const xsrfHeaderName = own('xsrfHeaderName');
  17. const xsrfCookieName = own('xsrfCookieName');
  18. let headers = own('headers');
  19. const auth = own('auth');
  20. const baseURL = own('baseURL');
  21. const allowAbsoluteUrls = own('allowAbsoluteUrls');
  22. const url = own('url');
  23. newConfig.headers = headers = AxiosHeaders.from(headers);
  24. newConfig.url = buildURL(
  25. buildFullPath(baseURL, url, allowAbsoluteUrls),
  26. config.params,
  27. config.paramsSerializer
  28. );
  29. // HTTP basic authentication
  30. if (auth) {
  31. headers.set(
  32. 'Authorization',
  33. 'Basic ' +
  34. btoa(
  35. (auth.username || '') +
  36. ':' +
  37. (auth.password ? unescape(encodeURIComponent(auth.password)) : '')
  38. )
  39. );
  40. }
  41. if (utils.isFormData(data)) {
  42. if (platform.hasStandardBrowserEnv || platform.hasStandardBrowserWebWorkerEnv) {
  43. headers.setContentType(undefined); // browser handles it
  44. } else if (utils.isFunction(data.getHeaders)) {
  45. // Node.js FormData (like form-data package)
  46. const formHeaders = data.getHeaders();
  47. // Only set safe headers to avoid overwriting security headers
  48. const allowedHeaders = ['content-type', 'content-length'];
  49. Object.entries(formHeaders).forEach(([key, val]) => {
  50. if (allowedHeaders.includes(key.toLowerCase())) {
  51. headers.set(key, val);
  52. }
  53. });
  54. }
  55. }
  56. // Add xsrf header
  57. // This is only done if running in a standard browser environment.
  58. // Specifically not if we're in a web worker, or react-native.
  59. if (platform.hasStandardBrowserEnv) {
  60. if (utils.isFunction(withXSRFToken)) {
  61. withXSRFToken = withXSRFToken(newConfig);
  62. }
  63. // Strict boolean check — prevents proto-pollution gadgets (e.g. Object.prototype.withXSRFToken = 1)
  64. // and misconfigurations (e.g. "false") from short-circuiting the same-origin check and leaking
  65. // the XSRF token cross-origin. See GHSA-xx6v-rp6x-q39c.
  66. const shouldSendXSRF =
  67. withXSRFToken === true ||
  68. (withXSRFToken == null && isURLSameOrigin(newConfig.url));
  69. if (shouldSendXSRF) {
  70. const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.read(xsrfCookieName);
  71. if (xsrfValue) {
  72. headers.set(xsrfHeaderName, xsrfValue);
  73. }
  74. }
  75. }
  76. return newConfig;
  77. };