-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ReDoS vulnerability for the
.end()
method
The other methods do not have this vulnerability, but I’m refactoring them while I’m at it.
- Loading branch information
1 parent
7bd2272
commit 25246c6
Showing
2 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
export default function trimNewlines(string) { | ||
return string.replace(/^[\r\n]+|[\r\n]+$/g, ''); | ||
let start = 0; | ||
let end = string.length; | ||
|
||
while (start < end && (string[start] === '\r' || string[start] === '\n')) { | ||
start++; | ||
} | ||
|
||
while (end > 0 && (string[end - 1] === '\r' || string[end - 1] === '\n')) { | ||
end--; | ||
} | ||
|
||
return (start > 0 || end < string.length) ? string.slice(start, end) : string; | ||
} | ||
|
||
trimNewlines.start = string => string.replace(/^[\r\n]+/, ''); | ||
trimNewlines.end = string => string.replace(/[\r\n]+$/, ''); | ||
trimNewlines.start = string => { | ||
const end = string.length; | ||
let start = 0; | ||
|
||
while (start < end && (string[start] === '\r' || string[start] === '\n')) { | ||
start++; | ||
} | ||
|
||
return start > 0 ? string.slice(start, end) : string; | ||
}; | ||
|
||
trimNewlines.end = string => { | ||
let end = string.length; | ||
|
||
while (end > 0 && (string[end - 1] === '\r' || string[end - 1] === '\n')) { | ||
end--; | ||
} | ||
|
||
return end < string.length ? string.slice(0, end) : string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters