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 24, 2019
1 parent 0f95a7d commit 81d9525
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
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
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 < 20000; i++) {
var line = stack[i % stack.length];
message[i] = line[Math.floor(Math.random() * line.length)];
}
var err = new Error(message);
// Fake stack-trace
err.stack = [message].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 81d9525

Please sign in to comment.