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
8 changes: 5 additions & 3 deletions lib/helpers/combineURLs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* @returns {string} The combined URL
*/
export default function combineURLs(baseURL, relativeURL) {
return relativeURL
? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
: baseURL;
if (!relativeURL) return baseURL;

return baseURL.replace(/\/{3,}/, '').replace(/\/+$/, '')
Fixed Show fixed Hide fixed
+ '/'
+ relativeURL.replace(/\/{3,}/, '').replace(/^\/+/, '');
}
16 changes: 15 additions & 1 deletion test/specs/defaults.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,24 @@ 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();
instance.defaults.baseURL = '/'.repeat(100000) + '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('bar/foo');
done();
});
});
Expand Down