Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tools/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Config._valid = function(key, value, sch){
if(scht.length > 1 && type != scht[0] && type == '[object String]'){
if(scht[0] == '[object Array]') {
// unfortunately, js does not support lookahead RegExp (/(?<!\\)\s+/) now (until next ver).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be removed because the commit uses lookbehind assertions. The NodeJS implementation of the feature is in 8.10+ (2018-03-06) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookbehind_assertion

value = value.split(/([\w\-]+\="[^"]*")|([\w\-]+\='[^']*')|"([^"]*)"|'([^']*)'|\s/)
value = value.split(/((?<![\w\-])([\w\-]+\="[^"]*")|(?<![\w\-])([\w\-]+\='[^']*')|"([^"]*)"|'([^']*)'|\s )/)
.filter(function(v){
return v && v.trim();
});
Expand Down
33 changes: 33 additions & 0 deletions test/interface/redos.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
process.chdir(__dirname)

const config = require('../../lib/tools/Config')
const { performance } = require('perf_hooks')
let expect;
before(async () => {
const chai = await import('chai')
expect = chai.expect
})


describe('ReDos Test', function () {
it('should done in 1 s', function () {
// 构造 schema,期望值为数组或者字符串
const schemaEntry = {
type: ['array', 'string']
}
// 构造测试用的长字符串
const value = "a".repeat(100000) + "="

const startTime = performance.now()
const result = config._valid('dummyKey', value, schemaEntry)
const endTime = performance.now()
const timeTaken = endTime - startTime

// 输出匹配结果和耗时(调试用)
console.log(`Time taken: ${timeTaken.toFixed(3)} ms`)


// 并断言耗时在合理范围内(比如小于1000毫秒)
expect(timeTaken).to.be.lessThan(1000)
})
})
2 changes: 2 additions & 0 deletions test/unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ runUnitTest $D/bus.spec.mocha.js
runUnitTest $D/bus.fork.spec.mocha.js
runUnitTest $D/utility.mocha.js

runUnitTest $D/redos.mocha.js

echo "============== unit test finished =============="
cat unit_time