resolveConfig.js 4.0 KB

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