-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid an unlikely but theoretically possible redos
When stripping the trailing slash from `files` arguments, we were using `f.replace(/\/+$/, '')`, which can get exponentially slow when `f` contains many `/` characters. Tested a few variants and found one that's faster than the regexp replacement for short strings, long strings, and long strings containing many instances of repeated `/` characters. This is "unlikely but theoretically possible" because it requires that the user is passing untrusted input into the `tar.extract()` or `tar.list()` array of entries to parse/extract, which would be quite unusual.
- Loading branch information
Showing
4 changed files
with
39 additions
and
6 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
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// this is the only approach that was significantly faster than using | ||
// str.replace(/\/+$/, '') for strings ending with a lot of / chars and | ||
// containing multiple / chars. | ||
const batchStrings = [ | ||
'/'.repeat(1024), | ||
'/'.repeat(512), | ||
'/'.repeat(256), | ||
'/'.repeat(128), | ||
'/'.repeat(64), | ||
'/'.repeat(32), | ||
'/'.repeat(16), | ||
'/'.repeat(8), | ||
'/'.repeat(4), | ||
'/'.repeat(2), | ||
'/', | ||
] | ||
|
||
module.exports = str => { | ||
for (const s of batchStrings) { | ||
while (str.length >= s.length && str.slice(-1 * s.length) === s) | ||
str = str.slice(0, -1 * s.length) | ||
} | ||
return str | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const t = require('tap') | ||
const stripSlash = require('../lib/strip-trailing-slashes.js') | ||
const short = '///a///b///c///' | ||
const long = short.repeat(10) + '/'.repeat(1000000) | ||
|
||
t.equal(stripSlash(short), '///a///b///c') | ||
t.equal(stripSlash(long), short.repeat(9) + '///a///b///c') |