Repository navigation
fix: resolve directory symlinks at every level of archive lookups - #502
Conversation
Read lookups only followed a directory symlink when it was the immediate parent of the requested entry, so paths such as `link/sub/file` failed with "Unexpected directory state while traversing". searchNodeFromDirectory now follows link nodes at every component of a read-only lookup, continuing from the link's target with the remaining components and sharing the existing circular/depth guards with getNode and getFile. Packing (create=true) is unchanged. Fixes #499 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01PiGRtB1rbGBBVHSbsGRUQ5
| const node = this.searchNodeFromDirectory(path.dirname(p), false); | ||
| // Directory symlinks in the parent path are always followed (like lstat); | ||
| // followLinks only applies to the last component, in getFile. | ||
| const node = this.searchNodeFromDirectory(path.dirname(p), false, depth, visited); |
There was a problem hiding this comment.
🟣 pre-existing, not blocking: Pre-existing: unpacked files reached through a packed directory symlink still fail with a raw ENOENT, so #499 stays unfixed for them. getNode now finds the entry via the resolved target, but readFileSync rebuilds the .unpacked path from the unresolved filename. Fix: have the lookup also return the link-resolved archive path, and read unpacked contents from <archive>.unpacked/<resolved path> in extractFile (and readFileWithFd). Packed entries and paths with no links must keep reading as they do now. Example: pnpm node_modules/foo -> .pnpm/... with *.node unpacked.
A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
Take an archive where directory link linkdir -> real is packed and real/sub/f.node is unpacked, for example via unpack: '*.node' or an unpackDir covering only real. Insertion goes through insertLink, src/filesystem.ts:295; the link is not unpacked, so createSymlink never writes linkdir into .unpacked. extractFile(archive, 'linkdir/sub/f.node') now reaches getNode, whose searchNodeFromDirectory call at src/filesystem.ts:344 follows the link and returns the unpacked entry. extractFile (src/asar.ts:368) then calls readFileSync, which reads ensureWithin(unpackedDir, filename) with the original linkdir/sub/f.node (src/disk.ts:386). That file does not exist, so the caller gets ENOENT instead of the contents. On the base branch the same call threw 'Unexpected directory state' (and one-level paths already hit this ENOENT), so the failure is pre-existing. The PR description notes the gap. Even so, the PR's stated fix (#499: read files below a directory symlink) does not hold for unpacked files, such as native modules under pnpm-style symlinked node_modules.
Verification: The trigger is an archive where a directory link is packed but the file reached through it (real/sub/f.node) is unpacked, read as linkdir/sub/f.node. readFileSync builds the read path from the unresolved name (src/disk.ts:383-386). A packed link is never written into .unpacked, so the read throws a raw ENOENT. On the base, these reads already failed.
There was a problem hiding this comment.
Agreed this is real, but it is pre-existing: on main the same unpacked read already fails with ENOENT one level below a packed directory link, and the PR description calls it out as a separate gap. It is better handled as a follow-up that has the lookup return the link-resolved archive path and reads unpacked contents from <archive>.unpacked/<resolved path> in extractFile and readFileWithFd, so I am leaving it out of this PR.
Generated by Claude Code
getNode handed getFile's shared `visited` set to searchNodeFromDirectory, which records each directory link's resolved prefix in it. A file link whose target passes back through the same directory link (`a -> d`, `d/x -> a/y`) or through a second link to that directory (`b -> d`, `d/x -> b/y`) then failed with a false "circular symlink detected" error. Give each directory walk its own cycle set while still carrying `depth`, so real directory cycles are still caught and the hop cap still holds. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01PiGRtB1rbGBBVHSbsGRUQ5
There was a problem hiding this comment.
The false "circular symlink" error I flagged earlier is fixed in 78dd633: each directory walk now starts with a fresh cycle set. This pass found no new bugs. A maintainer should still look, because the change alters what the public statFile/getFile lookups return when followLinks=false.
What was reviewed:
- Depth limit across hops: each file-link hop passes
depthinto the directory walk, and getFile still checks it against SYMLINK_MAX_DEPTH, so resolution always ends. - Escape guard: link targets that are absolute or start with
..return not-found before any recursion. This also rejects a target directory whose name merely begins with..(e.g...foo), which is an edge case. - The
create=truepacking path is unchanged, because links are followed only whencreateis false.
Extended reasoning...
The change rewrites symlink resolution in Filesystem.searchNodeFromDirectory/getNode (src/filesystem.ts) so lookups follow directory links at every path component, and it adds tests in test/filesystem-spec.ts. The relevant security surface is path resolution inside the archive, which is guarded against absolute and .. link targets. The follow-up commit addressed my earlier inline finding. I deferred rather than approved because followLinks=false now follows links in the parent path (lstat semantics). That is an intentional change to public API behaviour, and the maintainer's approval came before the latest commit.
Still open from earlier reviews (1):
- Unresolved: 1 minor or pre-existing.
|
🎉 This PR is included in version 4.3.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Requested via Slack thread
Files more than one level below a directory symlink inside an archive couldn't be read, because lookups only followed a link at the immediate parent. Read lookups now resolve links at every path component, reusing the existing circular-symlink and depth guards; packing is unchanged. Thanks to @trackoor for the reproducer.
For reviewers:
followLinks=falsenow applies only to the final component (like lstat), sostatFile(archive, 'link/file', false)now returns the entry instead of not-found.A separate, pre-existing gap is not fixed here: an unpacked file read through a directory link fails with ENOENT, because extractFile reads the
.unpackedpath as requested rather than the resolved path.Fixes #499
Generated by Claude Code