Skip to content

Commit

Permalink
Fix ReDoS vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 13, 2022
1 parent b5894c1 commit d8ba39a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function semverRegex() {
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;
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;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"semantic"
],
"devDependencies": {
"ava": "^3.15.0",
"ava": "^4.2.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
}
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,12 @@ test('invalid version does not cause catatrophic backtracking', t => {
const difference = Date.now() - start;
t.true(difference < 10, `Execution time: ${difference}`);
}

for (let index = 1; index <= 20; index++) {
const start = Date.now();
const fixture = `0.0.1-${'-.--'.repeat(index)} `;
semverRegex().test(fixture);
const difference = Date.now() - start;
t.true(difference < 10, `Execution time: ${difference}`);
}
});

0 comments on commit d8ba39a

Please # to comment.