Skip to content

Commit 11c6624

Browse files
committed
Fix ReDoS vulnerability
1 parent 80cd2f2 commit 11c6624

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default function semverRegex() {
2-
return /(?<=^v?|\sv?)(?:(?:0|[1-9]\d*)\.){2}(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*)(?:\.(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?\b/gi;
2+
return /(?:(?<=^v?|\sv?)(?:(?:0|[1-9]\d{0,9})\.){2}(?:0|[1-9]\d{0,9})(?:-(?:0|[1-9]\d*?|[\da-z-]*?[a-z-][\da-z-]*?){0,100}(?:\.(?:0|[1-9]\d*?|[\da-z-]*?[a-z-][\da-z-]*?))*?){0,100}(?:\+[\da-z-]+?(?:\.[\da-z-]+?)*?){0,100}\b){1,200}/gi;
33
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ semverRegex().exec('unicorn 1.0.0 rainbow')[0];
2626
//=> ['1.0.0', '2.1.3']
2727
```
2828

29-
**Note:** For versions coming from user-input, it's up to you to truncate the string to a sensible length to prevent abuse. For example, 100 length.
29+
**Note:** For versions coming from user-input, you are recommended to truncate the string to a sensible length to prevent abuse. For example, 100 length.
3030

3131
## Related
3232

test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const fixtures = [
1212
'2.7.2-foo+bar',
1313
'1.2.3-alpha.10.beta',
1414
'1.2.3-alpha.10.beta+build.unicorn.rainbow',
15-
'foo 0.0.0 bar 0.0.0'
15+
'foo 0.0.0 bar 0.0.0',
16+
'99999.99999.99999'
1617
];
1718

1819
test('matches semver versions on test', t => {
@@ -110,4 +111,12 @@ test('invalid version does not cause catatrophic backtracking', t => {
110111
`v1.1.3-0aa${postfix}$`,
111112
semverRegex()
112113
);
114+
115+
for (let index = 1; index <= 50000; index++) {
116+
const start = Date.now();
117+
const fixture = `0.0.0-0${'.-------'.repeat(index)}@`;
118+
semverRegex().test(fixture);
119+
const difference = Date.now() - start;
120+
t.true(difference < 10, `Execution time: ${difference}`);
121+
}
113122
});

0 commit comments

Comments
 (0)