Skip to content

fix(fs): follow symlinked directories when changing files in InMemoryFs - #782

Open
FredAmartey wants to merge 4 commits into
vercel-labs:mainfrom
FredAmartey:fix-inmemory-symlink-writes
Open

FredAmartey wants to merge 4 commits into
vercel-labs:mainfrom
FredAmartey:fix-inmemory-symlink-writes

Conversation

@FredAmartey

@FredAmartey FredAmartey commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On an InMemoryFs, a path that goes through a symlinked directory can be read but not changed:

mkdir real; ln -s real lnk
echo hi > lnk/a.txt; echo "rc=$?"   # rc=0
cat real/a.txt                      # No such file or directory
rm lnk/old.txt                      # rm: cannot remove 'lnk/old.txt': No such file or directory

The write reports success and the data is gone. Reads resolve every symlink in the path, but writeFileSync, appendFile, mkdirSync, rm, cp, mv, chmod, symlink, link, readdirWithFileTypes and readlink used the normalized path as is. A write stored an entry under /…/lnk/a.txt, which no lookup can reach because lookups go through the link. createExclusive already resolves its parent for this reason.

Summary

  • Those methods now resolve symlinks in every part of the path except the last, as lstat does. writeFileLazy does too.
  • The two resolvers (resolveIntermediateSymlinks and resolvePathWithSymlinks) and the follow loop in readdirWithFileTypes are now one walker. A symlink's target is walked part by part, like the kernel does. Before, a target that went through another symlink (chain -> alias/dir, alias -> real) stopped at alias/dir, so reads, ls and writes all missed, and a symlink to / produced //name keys that readdir never lists.
  • A symlink in the last part is handled as before: rm and mv act on the link, and writing to a file symlink still replaces the link, which in-memory-fs.security.test.ts asserts.

Validation

  • in-memory-fs.symlink-paths.test.ts: 13 cases, one per method plus the last-part case, a target through another symlink, a target that leads to / and writeFileLazy. All but the last-part case fail on main.
  • symlinked-dir.comparison.test.ts: recorded bash fixtures for writes, appends, mkdir, touch, cp, mv and rm through the link, and for reads, ls and writes through a chained link and a link to ... Both fail on main.
  • pnpm test:run: 16,341 passed. pnpm test:comparison: 1,061 passed.
  • biome check, lint:banned and typecheck pass.

Not in this PR

Bash writes through a symlink to a file (echo x > flnk changes the target) and InMemoryFs replaces the link instead. The security test pins that behavior, so changing it is a separate call.

Two more differences from bash that were already on main and are unchanged here: .. is collapsed before symlinks are followed, so lnk/../x means /x rather than /real/x, and writing through a link to a missing directory creates it, where bash fails with ENOENT.

Downstream: vercel/ai#25591

Fixes #785

@vercel

vercel Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

@FredAmartey is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@vercel-internal-security-reviewer

Copy link
Copy Markdown

Security review details

@auto-maintain

auto-maintain Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

🤖 auto-maintain review

Automated, advisory triage for @FredAmartey's PR. Facts below are read from the GitHub API.

Check Result
Author's merged PRs (this repo) 4
Account established ✅ (age 2993d · 8 followers · 32 public repos)
Commits signed/verified ⚠️ 2/4
Changeset included ✅ (.changeset/in-memory-fs-symlinked-dir.md)

Review panel: 🟡 medium highest severity

just-bash maintainer code review: 🟢 low

No actionable correctness or maintainability issues found in the complete diff.

General code review: 🟢 low

No actionable correctness or maintainability issues found in the complete diff.

Adversarial security: 🟢 low

No actionable security issues found in the complete diff.

Adversarial security (second opinion): 🟢 low

Change is confined to InMemoryFs path resolution: a single symlink walker now resolves intermediate components for mutating operations, matching the read path and the real-FS backends. No network, process, dependency, CI, or sandbox-trap surface is touched, and the fixtures/tests contain no injected instructions or obfuscation. The security-critical invariants are preserved and newly tested: createExclusive still refuses to follow a final symlink (EEXIST on collision), rm/mv still act on the link rather than its target so rm -rf cannot descend through a planted symlink, symlink hops are globally capped at MAX_SYMLINK_DEPTH with recursion bounded accordingly, and target parsing avoids argument spread on attacker-sized targets. Resolution stays inside the instance's own key-space, so MountableFs/OverlayFs mount boundaries are unaffected. New EISDIR/ENOTDIR/ENOENT/ENOTEMPTY guards close prior cases where writes created unreachable keys or clobbered directory entries, including writeFileSync('/'). No actionable security findings.

Standard Bash and host portability: 🟡 medium

Symlinked-directory resolution still mishandles final symlinks for chmod and file-copy destinations.

  • packages/just-bash/src/fs/in-memory-fs/in-memory-fs.ts:932 — `chmod` resolves only intermediate symlinks, so `chmod 600 alias/link` changes the symlink entry rather than its target; standard `chmod` follows the final symlink, leaving the target mode incorrect.
  • packages/just-bash/src/fs/in-memory-fs/in-memory-fs.ts:838 — Copying a file onto a destination symlink replaces that symlink via `setEntry`; e.g. `cp src alias/out` should retain `out` and overwrite its target, as standard `cp` does.

Posted by auto-maintain. This automated code review is advisory; a human maintainer makes the call.

@privatenumber privatenumber added awaiting-review Maintainer review or re-review is next. Does not imply approval or merge readiness. bug Something isn't working filesystem Filesystem implementations, mounts, links, and file operations p1 Prompt action for substantial impact. Priority is not acceptance or a delivery promise. labels Dec 1, 2026

@privatenumber privatenumber left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Pushed a couple of hardening follow-ups on top of the unified resolveSymlinks walker:

  • Validate traversed symlink target components before collapsing .. so invalid targets fail with ENOENT/ENOTDIR instead of redirecting operations.
  • Prevent file writes, copies, and moves from overwriting directories (EISDIR), and prevent directory mv from recursing through a final destination symlink.
  • Use setEntry when copying symlinks (quota accounting), check source existence before the same-path mv return, detect self-copy via aliases, avoid argument spread on long symlink targets, and propagate the caller syscall name in ELOOP.

Tests (in-memory-fs, symlinked-dir.comparison.test.ts, src/fs/ + cp/mv/mkdir/mktemp), typecheck, and lint all pass. Looks good to merge.

This branch has not been deployed

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

Labels

awaiting-review Maintainer review or re-review is next. Does not imply approval or merge readiness. bug Something isn't working filesystem Filesystem implementations, mounts, links, and file operations p1 Prompt action for substantial impact. Priority is not acceptance or a delivery promise.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InMemoryFs: writes and deletes through symlinked directories fail or are silently lost

2 participants