Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(withXSRFToken): added withXSRFToken option as a workaround to achieve the old withCredentials behavior; #6046

Merged
merged 36 commits into from Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3f2f496
chore(ci): Add release-it script;
DigitalBrainJS Dec 10, 2022
1bfc41b
Merge branch 'chore/release-it' into v1.x
DigitalBrainJS Dec 10, 2022
acaf0f4
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 15, 2022
8a5de86
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 15, 2022
cff9271
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 17, 2022
67afe20
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 19, 2022
dc7b66d
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 22, 2022
716eb59
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 23, 2022
10216bf
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Dec 29, 2022
3b199f4
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 7, 2023
f3d444f
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 19, 2023
077c381
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 26, 2023
f598657
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Jan 31, 2023
7bf5713
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Feb 5, 2023
81a8bd6
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Mar 8, 2023
f8bb158
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 5, 2023
3f1c768
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 18, 2023
a9b0418
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 25, 2023
1a16f4e
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Apr 27, 2023
5e22e2f
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS May 10, 2023
b7c77b0
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 25, 2023
2200038
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 25, 2023
df38e0c
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 26, 2023
878548a
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 26, 2023
7a33bcd
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Aug 28, 2023
5cafdeb
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Sep 13, 2023
f0e6b28
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Sep 26, 2023
98371b0
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Sep 28, 2023
bee6e45
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Oct 4, 2023
c97df04
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Oct 26, 2023
62274b7
Merge branch 'v1.x' of https://github.com/axios/axios into v1.x
DigitalBrainJS Oct 28, 2023
2bf3ff5
chore(draft): draft;
DigitalBrainJS Oct 28, 2023
60e3c4c
feat(withXSRFToken): added `withXSRFToken` option as a workaround to …
DigitalBrainJS Oct 30, 2023
f73bc4b
feat(withXSRFToken): added ability for `withXSRFToken` to take a func…
DigitalBrainJS Oct 31, 2023
ad8f512
fix(withXSRFToken): fixed the ability to accept a function as a value;
DigitalBrainJS Nov 14, 2023
beddb23
Merge branch 'v1.x' of https://github.com/axios/axios into feat/xsrf
DigitalBrainJS Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -452,6 +452,9 @@ These are the available config options for making requests. Only the `url` is re

// `xsrfHeaderName` is the name of the http header that carries the xsrf token value
xsrfHeaderName: 'X-XSRF-TOKEN', // default

// `undefined` (default) - set XSRF header only for the same origin requests
withXSRFToken: boolean | undefined | ((config: InternalAxiosRequestConfig) => boolean | undefined),

// `onUploadProgress` allows handling of progress events for uploads
// browser & node.js
Expand Down
1 change: 1 addition & 0 deletions index.d.cts
Expand Up @@ -414,6 +414,7 @@ declare namespace axios {
family?: AddressFamily;
lookup?: ((hostname: string, options: object, cb: (err: Error | null, address: LookupAddress | LookupAddress[], family?: AddressFamily) => void) => void) |
((hostname: string, options: object) => Promise<[address: LookupAddressEntry | LookupAddressEntry[], family?: AddressFamily] | LookupAddress>);
withXSRFToken?: boolean | ((config: InternalAxiosRequestConfig) => boolean | undefined);
}

// Alias
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -355,6 +355,7 @@ export interface AxiosRequestConfig<D = any> {
family?: AddressFamily;
lookup?: ((hostname: string, options: object, cb: (err: Error | null, address: LookupAddress | LookupAddress[], family?: AddressFamily) => void) => void) |
((hostname: string, options: object) => Promise<[address: LookupAddressEntry | LookupAddressEntry[], family?: AddressFamily] | LookupAddress>);
withXSRFToken?: boolean | ((config: InternalAxiosRequestConfig) => boolean | undefined);
}

// Alias
Expand Down
17 changes: 10 additions & 7 deletions lib/adapters/xhr.js
Expand Up @@ -49,7 +49,7 @@ export default isXHRAdapterSupported && function (config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
let requestData = config.data;
const requestHeaders = AxiosHeaders.from(config.headers).normalize();
const responseType = config.responseType;
let {responseType, withXSRFToken} = config;
let onCanceled;
function done() {
if (config.cancelToken) {
Expand Down Expand Up @@ -185,13 +185,16 @@ export default isXHRAdapterSupported && function (config) {
// 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) {
// Add xsrf header
// regarding CVE-2023-45857 config.withCredentials condition was removed temporarily
const xsrfValue = isURLSameOrigin(fullPath) && config.xsrfCookieName && cookies.read(config.xsrfCookieName);
if(platform.hasStandardBrowserEnv) {
withXSRFToken && utils.isFunction(withXSRFToken) && (withXSRFToken = withXSRFToken(config));

if (xsrfValue) {
requestHeaders.set(config.xsrfHeaderName, xsrfValue);
if (withXSRFToken || (withXSRFToken !== false && isURLSameOrigin(fullPath))) {
// Add xsrf header
const xsrfValue = config.xsrfHeaderName && config.xsrfCookieName && cookies.read(config.xsrfCookieName);

if (xsrfValue) {
requestHeaders.set(config.xsrfHeaderName, xsrfValue);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/core/mergeConfig.js
Expand Up @@ -75,6 +75,7 @@ export default function mergeConfig(config1, config2) {
timeout: defaultToConfig2,
timeoutMessage: defaultToConfig2,
withCredentials: defaultToConfig2,
withXSRFToken: defaultToConfig2,
adapter: defaultToConfig2,
responseType: defaultToConfig2,
xsrfCookieName: defaultToConfig2,
Expand Down
84 changes: 37 additions & 47 deletions lib/helpers/cookies.js
@@ -1,52 +1,42 @@
'use strict';

import utils from './../utils.js';
import platform from '../platform/index.js';

export default platform.hasStandardBrowserEnv ?

// Standard browser envs support document.cookie
(function standardBrowserEnv() {
return {
write: function write(name, value, expires, path, domain, secure) {
const cookie = [];
cookie.push(name + '=' + encodeURIComponent(value));

if (utils.isNumber(expires)) {
cookie.push('expires=' + new Date(expires).toGMTString());
}

if (utils.isString(path)) {
cookie.push('path=' + path);
}

if (utils.isString(domain)) {
cookie.push('domain=' + domain);
}

if (secure === true) {
cookie.push('secure');
}

document.cookie = cookie.join('; ');
},

read: function read(name) {
const match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
return (match ? decodeURIComponent(match[3]) : null);
},

remove: function remove(name) {
this.write(name, '', Date.now() - 86400000);
}
};
})() :

// Non standard browser env (web workers, react-native) lack needed support.
(function nonStandardBrowserEnv() {
return {
write: function write() {},
read: function read() { return null; },
remove: function remove() {}
};
})();
// Standard browser envs support document.cookie
{
write(name, value, expires, path, domain, secure) {
const cookie = [name + '=' + encodeURIComponent(value)];

utils.isNumber(expires) && cookie.push('expires=' + new Date(expires).toGMTString());

utils.isString(path) && cookie.push('path=' + path);

utils.isString(domain) && cookie.push('domain=' + domain);

secure === true && cookie.push('secure');

document.cookie = cookie.join('; ');
},

read(name) {
const match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
return (match ? decodeURIComponent(match[3]) : null);
},

remove(name) {
this.write(name, '', Date.now() - 86400000);
}
}

:

// Non-standard browser env (web workers, react-native) lack needed support.
{
write() {},
read() {
return null;
},
remove() {}
};

2 changes: 1 addition & 1 deletion lib/helpers/isURLSameOrigin.js
Expand Up @@ -13,7 +13,7 @@ export default platform.hasStandardBrowserEnv ?
let originURL;

/**
* Parse a URL to discover it's components
* Parse a URL to discover its components
*
* @param {String} url The URL to be parsed
* @returns {Object}
Expand Down
64 changes: 64 additions & 0 deletions test/specs/xsrf.spec.js
Expand Up @@ -79,4 +79,68 @@ describe('xsrf', function () {
done();
});
});

describe('withXSRFToken option', function(){

it('should set xsrf header for cross origin when withXSRFToken = true', function (done) {
const token = '12345';

document.cookie = axios.defaults.xsrfCookieName + '=' + token;

axios('http://example.com/', {
withXSRFToken: true
});

getAjaxRequest().then(function (request) {
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(token);
done();
});
});

it('should not set xsrf header for the same origin when withXSRFToken = false', function (done) {
const token = '12345';

document.cookie = axios.defaults.xsrfCookieName + '=' + token;

axios('/foo', {
withXSRFToken: false
});

getAjaxRequest().then(function (request) {
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined);
done();
});
});

it('should not set xsrf header for the same origin when withXSRFToken = false', function (done) {
const token = '12345';

document.cookie = axios.defaults.xsrfCookieName + '=' + token;

axios('/foo', {
withXSRFToken: false
});

getAjaxRequest().then(function (request) {
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined);
done();
});
});

it('should support function resolver', (done) => {
const token = '12345';

document.cookie = axios.defaults.xsrfCookieName + '=' + token;

axios('/foo', {
withXSRFToken: (config) => config.userFlag === 'yes',
userFlag: 'yes'
});

getAjaxRequest().then(function (request) {
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(token);
done();
});
});
});
});