Skip to content

Commit 0b6a978

Browse files
Fix potential ReDoS Vulnerability or Inefficient Regular Expression (#65)
* Create redos.js * Update index.js * Update test/redos.js --------- Co-authored-by: Julian Gruber <[email protected]>
1 parent 6a39bdd commit 0b6a978

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function expand (str, isTop) {
154154
const isOptions = m.body.indexOf(',') >= 0
155155
if (!isSequence && !isOptions) {
156156
// {a},b}
157-
if (m.post.match(/,.*\}/)) {
157+
if (m.post.match(/,(?!,).*\}/)) {
158158
str = m.pre + '{' + m.body + escClose + m.post
159159
return expand(str)
160160
}

test/redos.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import test from 'node:test'
2+
import assert from 'assert'
3+
import expand from '../index.js'
4+
5+
test('redos', function () {
6+
let str = "{a}" + ",".repeat(100000) + "\u0000";
7+
let startTime = performance.now();
8+
expand(str)
9+
let endTime = performance.now();
10+
let timeTaken = endTime - startTime;
11+
assert.ok(timeTaken < 1000, `Expected time (${timeTaken}ms) to be less than 1000ms`);
12+
})
13+
14+
15+

0 commit comments

Comments
 (0)