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
Prev Previous commit
Next Next commit
fix: removed multiple slash replacement;
  • Loading branch information
DigitalBrainJS committed Dec 26, 2023
commit 290b89f9f3b8685c3734d27e5b5fb19e4e9eff05
11 changes: 3 additions & 8 deletions lib/helpers/combineURLs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
* @returns {string} The combined URL
*/
export default function combineURLs(baseURL, relativeURL) {
if (!relativeURL) return baseURL;

const baseURLRegex = new RegExp(/\/+$/);
const relativeURLRegex = new RegExp(/^\/+/);
const multipleSlashes = new RegExp(/\/{3,}/);
return baseURL.replace(multipleSlashes, '').replace(baseURLRegex, '')
+ '/'
+ relativeURL.replace(multipleSlashes, '').replace(relativeURLRegex, '');
return relativeURL
? baseURL.replace(/\/?\/$/, '') + '/' + relativeURL.replace(/^\/+/, '')
: baseURL;
}
5 changes: 3 additions & 2 deletions test/specs/defaults.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,14 @@ describe('defaults', function () {
it('should resistent to ReDoS attack', function (done) {
const instance = axios.create();
const start = performance.now();
instance.defaults.baseURL = '/'.repeat(100000) + 'bar/';
const slashes = '/'.repeat(100000);
instance.defaults.baseURL = '/' + slashes + 'bar/';
instance.get('/foo');

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