Skip to content

Commit 15f9b3c

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 b01a637 commit 15f9b3c

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
@@ -144,7 +144,7 @@ function expand (str, isTop) {
144144
const isOptions = m.body.indexOf(',') >= 0
145145
if (!isSequence && !isOptions) {
146146
// {a},b}
147-
if (m.post.match(/,.*\}/)) {
147+
if (m.post.match(/,(?!,).*\}/)) {
148148
str = m.pre + '{' + m.body + escClose + m.post
149149
return expand(str)
150150
}

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)