Skip to content

fix: resolve directory symlinks at every level of archive lookups - #502

Merged
MarshallOfSound merged 2 commits into
mainfrom
claude/issue-499-symlink-traversal
Nov 24, 2026
Merged

MarshallOfSound merged 2 commits into
mainfrom
claude/issue-499-symlink-traversal

Conversation

@claude

@claude claude Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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=false now applies only to the final component (like lstat), so statFile(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 .unpacked path as requested rather than the resolved path.

Fixes #499


Generated by Claude Code

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
@MarshallOfSound
MarshallOfSound marked this pull request as ready for review November 24, 2026 18:43
@MarshallOfSound
MarshallOfSound requested a review from a team as a code owner November 24, 2026 18:43
Comment thread src/filesystem.ts Resolved
Comment thread src/filesystem.ts Outdated
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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟣 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 depth into 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=true packing path is unchanged, because links are followed only when create is 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.

@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) November 24, 2026 19:44
@MarshallOfSound
MarshallOfSound merged commit 928fd3f into main Nov 24, 2026
10 checks passed
@MarshallOfSound
MarshallOfSound deleted the claude/issue-499-symlink-traversal branch November 24, 2026 23:31
@electron-npm-package-publisher

Copy link
Copy Markdown

🎉 This PR is included in version 4.3.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unpacked files lose +x on extract; nested paths under directory symlinks fail; missing-path lookups mutate the header

3 participants