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

fix: Regular Expression Denial of Service (ReDoS) #6132

Merged
merged 11 commits into from
Dec 26, 2023
2 changes: 1 addition & 1 deletion lib/helpers/combineURLs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
*/
export default function combineURLs(baseURL, relativeURL) {
return relativeURL
? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
? baseURL.replace(/\/?\/$/, '') + '/' + relativeURL.replace(/^\/+/, '')
: baseURL;
}
17 changes: 16 additions & 1 deletion test/specs/defaults.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,25 @@ describe('defaults', function () {
const instance = axios.create();
axios.defaults.baseURL = 'http://example.org/';

instance.get('/foo/users');

getAjaxRequest().then(function (request) {
expect(request.url).toBe('/foo/users');
done();
});
});

it('should resistent to ReDoS attack', function (done) {
const instance = axios.create();
const start = performance.now();
const slashes = '/'.repeat(100000);
instance.defaults.baseURL = '/' + slashes + 'bar/';
instance.get('/foo');

getAjaxRequest().then(function (request) {
expect(request.url).toBe('/foo');
const elapsedTimeMs = performance.now() - start;
expect(elapsedTimeMs).toBeLessThan(20);
expect(request.url).toBe('/' + slashes + 'bar/foo');
done();
});
});
Expand Down