Skip to content

Commit 6d89476

Browse files
committed
fix: update regular expression to remove ReDOS
Fixes: #2
1 parent 0cd87f5 commit 6d89476

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var regex = /^(?:\r\n|\n|\r)+|(?:\r\n|\n|\r)+$/g;
3+
var regex = /^(?:\r|\n)+|(?:\r|\n)+$/g;
44

55
module.exports = function (str) {
66
return str.replace(regex, '');

test.js

+7
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ it('should trim off \\r\\n', function () {
1919
assert.strictEqual(trimOffNewlines('\r\nunicorns\r\n'), 'unicorns');
2020
assert.strictEqual(trimOffNewlines('unicorns\r\n\r\n\r\n\r\n\r\n\r\n'), 'unicorns');
2121
});
22+
23+
it('should not be susceptible to exponential backtracking', function () {
24+
var start = Date.now();
25+
trimOffNewlines('a' + '\r\n'.repeat(1000) + 'a');
26+
var end = Date.now();
27+
assert.ok(end - start < 1000, 'took too long, probably susceptible to ReDOS');
28+
});

0 commit comments

Comments
 (0)