Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c77691d

Browse filesBrowse files
committed
fix: remediate ReDOS further
1 parent 76ca93c commit c77691d
Copy full SHA for c77691d

File tree

Expand file treeCollapse file tree

2 files changed

+76
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+76
-4
lines changed

‎index.js

Copy file name to clipboard
+11-2Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
'use strict';
22

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

55
module.exports = function (str) {
6-
return str.replace(regex, '');
6+
var result = str.match(regex);
7+
if (!result) {
8+
return '';
9+
}
10+
var firstIndex = result.index;
11+
var lastIndex = str.length - 1;
12+
while (str[lastIndex] === '\r' || str[lastIndex] === '\n') {
13+
lastIndex--;
14+
}
15+
return str.substring(firstIndex, lastIndex + 1);
716
};

‎test.js

Copy file name to clipboardExpand all lines: test.js
+65-2Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,71 @@ it('should trim off \\r\\n', function () {
2121
});
2222

2323
it('should not be susceptible to exponential backtracking', function () {
24+
var redosString = 'a';
25+
var count = 1000;
26+
while (count) {
27+
redosString += '\r\n';
28+
count--;
29+
}
30+
redosString += 'a';
31+
32+
var longerRedosString = redosString;
33+
count = 1000;
34+
while (count) {
35+
longerRedosString += redosString;
36+
count--;
37+
}
38+
39+
var start = Date.now();
40+
trimOffNewlines(redosString);
41+
trimOffNewlines(longerRedosString);
42+
var end = Date.now();
43+
assert.ok(end - start < 1000, 'took too long, susceptible to ReDoS?');
44+
});
45+
46+
it('should be performant on very long strings', function () {
47+
var longOrdinaryString = 'aa';
48+
var count = 27;
49+
while (count) {
50+
longOrdinaryString += longOrdinaryString;
51+
count--;
52+
}
53+
assert.strictEqual(longOrdinaryString.length, 268435456);
54+
2455
var start = Date.now();
25-
trimOffNewlines('a' + '\r\n'.repeat(1000) + 'a');
56+
trimOffNewlines(longOrdinaryString);
2657
var end = Date.now();
27-
assert.ok(end - start < 1000, 'took too long, probably susceptible to ReDOS');
58+
assert.ok(end - start < 1000, 'took too long, performance issue?');
59+
});
60+
61+
it('should be performant in worst-case', function () {
62+
// In the current algorithm, this is likely a worst-case:
63+
// non-newline character followed by many newline characters.
64+
65+
this.timeout(10000);
66+
67+
var worstCaseString = '\r\n';
68+
var count = 27;
69+
while (count) {
70+
worstCaseString += worstCaseString;
71+
count--;
72+
}
73+
worstCaseString = 'a' + worstCaseString;
74+
assert.strictEqual(worstCaseString.length, 268435457);
75+
var start = Date.now();
76+
trimOffNewlines(worstCaseString);
77+
var end = Date.now();
78+
assert.ok(end - start < 5000, 'worst case took too long, performance issue?');
79+
});
80+
81+
it('should leave newlines in the middle of a string alone', function () {
82+
assert.strictEqual(trimOffNewlines('Come on,\nFhqwhgads.'), 'Come on,\nFhqwhgads.');
83+
});
84+
85+
it('should leave spaces at start and end alone', function () {
86+
assert.strictEqual(trimOffNewlines(' fhqwhgads '), ' fhqwhgads ');
87+
});
88+
89+
it('should return an empty string if there are only \\r and \\n', function () {
90+
assert.strictEqual(trimOffNewlines('\r\n\r\r\n\n'), '');
2891
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.