Skip to content

Commit bec32f7

Browse files
authored
Merge pull request #5971 from mmmsssttt404/master
fix:Potential ReDoS Vulnerability or Inefficient Regular Expression in Project: Need for Assessment and Mitigation
2 parents 3b3b547 + d0e7801 commit bec32f7

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

lib/tools/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Config._valid = function(key, value, sch){
201201
if(scht.length > 1 && type != scht[0] && type == '[object String]'){
202202
if(scht[0] == '[object Array]') {
203203
// unfortunately, js does not support lookahead RegExp (/(?<!\\)\s+/) now (until next ver).
204-
value = value.split(/([\w\-]+\="[^"]*")|([\w\-]+\='[^']*')|"([^"]*)"|'([^']*)'|\s/)
204+
value = value.split(/((?<![\w\-])([\w\-]+\="[^"]*")|(?<![\w\-])([\w\-]+\='[^']*')|"([^"]*)"|'([^']*)'|\s )/)
205205
.filter(function(v){
206206
return v && v.trim();
207207
});

test/interface/redos.mocha.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
process.chdir(__dirname)
2+
3+
const config = require('../../lib/tools/Config')
4+
const { performance } = require('perf_hooks')
5+
let expect;
6+
before(async () => {
7+
const chai = await import('chai')
8+
expect = chai.expect
9+
})
10+
11+
12+
describe('ReDos Test', function () {
13+
it('should done in 1 s', function () {
14+
// 构造 schema,期望值为数组或者字符串
15+
const schemaEntry = {
16+
type: ['array', 'string']
17+
}
18+
// 构造测试用的长字符串
19+
const value = "a".repeat(100000) + "="
20+
21+
const startTime = performance.now()
22+
const result = config._valid('dummyKey', value, schemaEntry)
23+
const endTime = performance.now()
24+
const timeTaken = endTime - startTime
25+
26+
// 输出匹配结果和耗时(调试用)
27+
console.log(`Time taken: ${timeTaken.toFixed(3)} ms`)
28+
29+
30+
// 并断言耗时在合理范围内(比如小于1000毫秒)
31+
expect(timeTaken).to.be.lessThan(1000)
32+
})
33+
})

test/unit.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,7 @@ runUnitTest $D/bus.spec.mocha.js
100100
runUnitTest $D/bus.fork.spec.mocha.js
101101
runUnitTest $D/utility.mocha.js
102102

103+
runUnitTest $D/redos.mocha.js
104+
103105
echo "============== unit test finished =============="
104106
cat unit_time

0 commit comments

Comments
 (0)