v0.3
This commit is contained in:
1 parent
d1ee8edbbd
commit
7733a44362
429 files changed
+75840
-94
No files matched your search
+120
@@ -0,0 +1,120 @@
|
||||
import platform from '../platform/index.js';
|
||||
import utils from '../utils.js';
|
||||
import AxiosError from '../core/AxiosError.js';
|
||||
import isURLSameOrigin from './isURLSameOrigin.js';
|
||||
import cookies from './cookies.js';
|
||||
import buildFullPath from '../core/buildFullPath.js';
|
||||
import mergeConfig from '../core/mergeConfig.js';
|
||||
import AxiosHeaders from '../core/AxiosHeaders.js';
|
||||
import setFormDataHeaders from '../core/setFormDataHeaders.js';
|
||||
import buildURL from './buildURL.js';
|
||||
|
||||
/**
|
||||
* Encode a UTF-8 string to a Latin-1 byte string for use with btoa().
|
||||
* This is a modern replacement for the deprecated unescape(encodeURIComponent(str)) pattern.
|
||||
*
|
||||
* @param {string} str The string to encode
|
||||
*
|
||||
* @returns {string} UTF-8 bytes as a Latin-1 string
|
||||
*/
|
||||
const encodeUTF8 = (str) =>
|
||||
encodeURIComponent(str).replace(/%([0-9A-F]{2})/gi, (_, hex) =>
|
||||
String.fromCharCode(parseInt(hex, 16))
|
||||
);
|
||||
|
||||
const readCookie = (name) => cookies.read(name);
|
||||
|
||||
function resolveConfig(config, readXsrfCookie = readCookie) {
|
||||
const newConfig = mergeConfig({}, config);
|
||||
|
||||
// Read only own properties to prevent prototype pollution gadgets
|
||||
// (e.g. Object.prototype.baseURL = 'https://evil.com').
|
||||
const own = (key) => (utils.hasOwnProp(newConfig, key) ? newConfig[key] : undefined);
|
||||
|
||||
const data = own('data');
|
||||
let withXSRFToken = own('withXSRFToken');
|
||||
const xsrfHeaderName = own('xsrfHeaderName');
|
||||
const xsrfCookieName = own('xsrfCookieName');
|
||||
let headers = own('headers');
|
||||
const auth = own('auth');
|
||||
const baseURL = own('baseURL');
|
||||
const allowAbsoluteUrls = own('allowAbsoluteUrls');
|
||||
const url = own('url');
|
||||
|
||||
newConfig.headers = headers = AxiosHeaders.from(headers);
|
||||
|
||||
newConfig.url = buildURL(
|
||||
buildFullPath(baseURL, url, allowAbsoluteUrls, newConfig),
|
||||
own('params'),
|
||||
own('paramsSerializer')
|
||||
);
|
||||
|
||||
// HTTP basic authentication
|
||||
if (auth) {
|
||||
const username = utils.getSafeProp(auth, 'username') || '';
|
||||
const password = utils.getSafeProp(auth, 'password') || '';
|
||||
const transitional = own('transitional');
|
||||
const encodeUsername =
|
||||
transitional &&
|
||||
utils.hasOwnProp(transitional, 'encodeBasicAuthUsername') &&
|
||||
transitional.encodeBasicAuthUsername === true;
|
||||
|
||||
try {
|
||||
headers.set(
|
||||
'Authorization',
|
||||
// Preserve legacy username bytes unless the caller opts in to UTF-8.
|
||||
'Basic ' +
|
||||
btoa(
|
||||
(encodeUsername ? encodeUTF8(username) : username) +
|
||||
':' +
|
||||
(password ? encodeUTF8(password) : '')
|
||||
)
|
||||
);
|
||||
} catch (e) {
|
||||
throw AxiosError.from(e, AxiosError.ERR_BAD_OPTION_VALUE, config);
|
||||
}
|
||||
}
|
||||
|
||||
if (utils.isFormData(data)) {
|
||||
const getHeaders = utils.getSafeProp(data, 'getHeaders');
|
||||
|
||||
if (
|
||||
platform.hasStandardBrowserEnv ||
|
||||
platform.hasStandardBrowserWebWorkerEnv ||
|
||||
utils.isReactNative(data)
|
||||
) {
|
||||
headers.setContentType(undefined); // browser/web worker/RN handles it
|
||||
} else if (utils.isFunction(getHeaders)) {
|
||||
// Node.js FormData (like form-data package)
|
||||
setFormDataHeaders(headers, getHeaders.call(data), own('formDataHeaderPolicy'));
|
||||
}
|
||||
}
|
||||
|
||||
// Add xsrf header
|
||||
// This is only done if running in a standard browser environment.
|
||||
// Specifically not if we're in a web worker, or react-native.
|
||||
|
||||
if (platform.hasStandardBrowserEnv) {
|
||||
if (utils.isFunction(withXSRFToken)) {
|
||||
withXSRFToken = withXSRFToken(newConfig);
|
||||
}
|
||||
|
||||
// Strict boolean check — prevents proto-pollution gadgets (e.g. Object.prototype.withXSRFToken = 1)
|
||||
// and misconfigurations (e.g. "false") from short-circuiting the same-origin check and leaking
|
||||
// the XSRF token cross-origin.
|
||||
const shouldSendXSRF =
|
||||
withXSRFToken === true || (withXSRFToken == null && isURLSameOrigin(newConfig.url));
|
||||
|
||||
if (shouldSendXSRF) {
|
||||
const xsrfValue = xsrfHeaderName && xsrfCookieName && readXsrfCookie(xsrfCookieName);
|
||||
|
||||
if (xsrfValue) {
|
||||
headers.set(xsrfHeaderName, xsrfValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
export default resolveConfig;
|
||||
Reference in new issue
Block a user