-
-
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.
fix: prevent path escape using drive-relative paths
On Windows, a path like `c:foo` is not considered "absolute", but if the cwd it's being resolved against is on a different drive letter, then `resolve(cwd, path)` will not end up contained within `cwd`, even in the absence of `..` portions. This change strips path roots from all paths prior to being resolved against the extraction target folder, even if such paths are not "absolute". Additionally, a path starting with a drive letter and then two dots, like `c:../`, would bypass the check for `..` path portions. This is now being checked properly. Finally, a defense in depth check is added, such that if the entry.absolute is outside of the extraction taret, and we are not in preservePaths:true mode, a warning is raised on that entry, and it is skipped. Currently, it is believed that this check is redundant, but it did catch some oversights in development.
- Loading branch information
Showing
4 changed files
with
161 additions
and
15 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 |
---|---|---|
@@ -1,14 +1,51 @@ | ||
const t = require('tap') | ||
const stripAbsolutePath = require('../lib/strip-absolute-path.js') | ||
const cwd = process.cwd() | ||
|
||
const cases = { | ||
'/': ['/', ''], | ||
'////': ['////', ''], | ||
'c:///a/b/c': ['c:///', 'a/b/c'], | ||
'\\\\foo\\bar\\baz': ['\\\\foo\\bar\\', 'baz'], | ||
'//foo//bar//baz': ['//', 'foo//bar//baz'], | ||
'c:\\c:\\c:\\c:\\\\d:\\e/f/g': ['c:\\c:\\c:\\c:\\\\d:\\', 'e/f/g'], | ||
} | ||
t.test('basic', t => { | ||
const cases = { | ||
'/': ['/', ''], | ||
'////': ['////', ''], | ||
'c:///a/b/c': ['c:///', 'a/b/c'], | ||
'\\\\foo\\bar\\baz': ['\\\\foo\\bar\\', 'baz'], | ||
'//foo//bar//baz': ['//', 'foo//bar//baz'], | ||
'c:\\c:\\c:\\c:\\\\d:\\e/f/g': ['c:\\c:\\c:\\c:\\\\d:\\', 'e/f/g'], | ||
} | ||
|
||
for (const [input, [root, stripped]] of Object.entries(cases)) | ||
t.strictSame(stripAbsolutePath(input), [root, stripped], input) | ||
for (const [input, [root, stripped]] of Object.entries(cases)) | ||
t.strictSame(stripAbsolutePath(input, cwd), [root, stripped], input) | ||
t.end() | ||
}) | ||
|
||
t.test('drive-local paths', t => { | ||
const env = process.env | ||
t.teardown(() => process.env = env) | ||
const cwd = 'D:\\safety\\land' | ||
const realPath = require('path') | ||
// be windowsy | ||
const path = { | ||
...realPath.win32, | ||
win32: realPath.win32, | ||
posix: realPath.posix, | ||
} | ||
const stripAbsolutePath = t.mock('../lib/strip-absolute-path.js', { path }) | ||
const cases = { | ||
'/': ['/', ''], | ||
'////': ['////', ''], | ||
'c:///a/b/c': ['c:///', 'a/b/c'], | ||
'\\\\foo\\bar\\baz': ['\\\\foo\\bar\\', 'baz'], | ||
'//foo//bar//baz': ['//', 'foo//bar//baz'], | ||
'c:\\c:\\c:\\c:\\\\d:\\e/f/g': ['c:\\c:\\c:\\c:\\\\d:\\', 'e/f/g'], | ||
'c:..\\system\\explorer.exe': ['c:', '..\\system\\explorer.exe'], | ||
'd:..\\..\\unsafe\\land': ['d:', '..\\..\\unsafe\\land'], | ||
'c:foo': ['c:', 'foo'], | ||
'D:mark': ['D:', 'mark'], | ||
'//?/X:/y/z': ['//?/X:/', 'y/z'], | ||
'\\\\?\\X:\\y\\z': ['\\\\?\\X:\\', 'y\\z'], | ||
} | ||
for (const [input, [root, stripped]] of Object.entries(cases)) { | ||
if (!t.strictSame(stripAbsolutePath(input, cwd), [root, stripped], input)) | ||
break | ||
} | ||
t.end() | ||
}) |
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