Skip to content

Commit 4cdf34e

Browse files
committed
fix: Potential ReDos vulnerability in url validator
1 parent 801727d commit 4cdf34e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/utils/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export function isURL(urlStr: any): boolean {
244244
}
245245
// Validate hostname: Can contain letters, numbers, underscore and dashes separated by a dot.
246246
// Each zone must not start with a hyphen or underscore.
247-
if (!hostname || !/^[a-zA-Z0-9]+[\w-]*([.]?[a-zA-Z0-9]+[\w-]*)*$/.test(hostname)) {
247+
if (!hostname || !/^[a-zA-Z0-9]+[\w-]*(\.[a-zA-Z0-9]+[\w-]*)*$/.test(hostname)) {
248248
return false;
249249
}
250250
// Allow for pathnames: (/chars+)*/?

test/unit/utils/validator.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,28 @@ describe('isISODateString()', () => {
530530
expect(isISODateString(validISODateString)).to.be.true;
531531
});
532532
});
533+
534+
describe('isURL() ReDoS and Long Inputs', () => {
535+
it('should handle long valid URLs quickly', function () {
536+
this.timeout(1000);
537+
const longUrl = 'https://' + Array(50).fill('a').join('.') + '.com';
538+
expect(isURL(longUrl)).to.be.true;
539+
});
540+
541+
it('should handle long invalid URLs quickly (ReDoS check)', function () {
542+
this.timeout(1000);
543+
const longInvalid = 'https://' + 'a'.repeat(22) + '!';
544+
expect(isURL(longInvalid)).to.be.false;
545+
});
546+
547+
it('should handle very long domain with many segments', function () {
548+
this.timeout(1000);
549+
const manySegments = 'https://' + Array(100).fill('a').join('.') + '.com';
550+
expect(isURL(manySegments)).to.be.true;
551+
});
552+
553+
it('should reject invalid dot usage caught by strict regex', function () {
554+
expect(isURL('https://a.b')).to.be.true;
555+
expect(isURL('https://a..b')).to.be.false;
556+
});
557+
});

0 commit comments

Comments
 (0)