fix(decompress): harden toFile against symlink path-escape - #140
Open
ranxianglei wants to merge 3 commits into
Open
fix(decompress): harden toFile against symlink path-escape#140ranxianglei wants to merge 3 commits into
ranxianglei wants to merge 3 commits into
Conversation
resolveToFilePath checked containment against the literal resolved path, so a symlink inside an allowed root (e.g. /tmp) pointing elsewhere could pass the check and write outside the allowed roots. Resolve symlinks in the longest existing ancestor of the target before the containment check, and compare against realpath'd roots (tmpdir() itself often sits behind a symlink, e.g. /var -> /private/var on macOS). Also null-guard args.blockId before trim() for robustness. Extracted from #119 (lsmir2).
…k (Windows) existsSync follows symlinks, so a symlink whose target does not (yet) exist reads as non-existent and was skipped by the ancestor walk — the unresolved link component then passed the containment check even though writing through it would land outside the allowed roots. Resolve each suffix component via lstatSync/readlinkSync so dangling symlinks (e.g. to a non-existent /etc on Windows) are caught. Adds a dedicated dangling-symlink test.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
resolveToFilePathchecked path containment against the literal resolved path. A symlink inside an allowed root (e.g./tmp) that points outside the allowed roots could pass the check and letdecompress --toFilewrite to an arbitrary filesystem location.Fix
tmpdir()itself often sits behind a symlink (/var -> /private/varon macOS), so the string forms would otherwise diverge and reject legitimate writes.args.blockIdbeforetrim()for robustness.Test
New regression test
decompress toFile rejects paths that escape an allowed root via a symlink: creates a symlink inside an allowed jail pointing at/etc, asserts the write is rejected. Fails on the old code (literal-path check), passes with the fix.Validation
Scope
Minimal, isolated, no cross-repo dependency. Extracted from #119 (thanks @21307369 for the original implementation). This is the high-priority security item from the #119 review split — the rest of #119 (the
/compactpipeline, clean-output, i18n) is being split into separate PRs.