Skip to content

Commit

Permalink
fix regex in utils.stackTraceFilter to prevent ReDoS #3416
Browse files Browse the repository at this point in the history
if the stack trace begins with a large error message (>= 20k charactors), and user leaves `--full-trace` disabled, `utils.stackTraceFilter()` takes ages to finish. Large error messages is quite possible when user makes containment assertions such as `expect(content).to.contain(word)`.
  • Loading branch information
cyjake committed Jan 28, 2019
1 parent 0f95a7d commit 8fe5c23
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,6 @@ exports.stackTraceFilter = function() {
function isMochaInternal(line) {
return (
~line.indexOf('node_modules' + slash + 'mocha' + slash) ||
~line.indexOf('node_modules' + slash + 'mocha.js') ||
~line.indexOf('bower_components' + slash + 'mocha.js') ||
~line.indexOf(slash + 'mocha.js')
);
}
Expand Down Expand Up @@ -677,7 +675,7 @@ exports.stackTraceFilter = function() {
}

// Clean up cwd(absolute)
if (/\(?.+:\d+:\d+\)?$/.test(line)) {
if (/:\d+:\d+\)?$/.test(line)) {
line = line.replace('(' + cwd, '(');
}

Expand Down
26 changes: 26 additions & 0 deletions test/unit/runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,32 @@ describe('Runner', function() {
});
runner.failHook(hook, err);
});

it('should not hang if the error message is ridiculously long', function(done) {
var hook = new Hook();
var message = [];
// mock a long message
for (var i = 0; i < 40000; i++) {
var line = stack[i % stack.length];
message[i] = line[Math.floor(Math.random() * line.length)];
}
var err = new Error();
// Fake stack-trace
err.stack = [message.join('')].concat(stack).join('\n');

runner.on('fail', function(hook, err) {
expect(
err.stack
.split('\n')
.slice(1)
.join('\n'),
'to be',
stack.slice(0, 3).join('\n')
);
done();
});
runner.failHook(hook, err);
});
});
});

Expand Down

0 comments on commit 8fe5c23

Please sign in to comment.