Repository navigation
fix(fs): follow symlinked directories when changing files in InMemoryFs - #782
FredAmartey wants to merge 4 commits into
Conversation
|
@FredAmartey is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
🤖 auto-maintain reviewAutomated, advisory triage for
Review panel: 🟡 medium highest severity just-bash maintainer code review: 🟢 low
General code review: 🟢 low
Adversarial security: 🟢 low
Adversarial security (second opinion): 🟢 low
Standard Bash and host portability: 🟡 medium
Posted by auto-maintain. This automated code review is advisory; a human maintainer makes the call. |
privatenumber
left a comment
There was a problem hiding this comment.
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 withENOENT/ENOTDIRinstead of redirecting operations. - Prevent file writes, copies, and moves from overwriting directories (
EISDIR), and prevent directorymvfrom recursing through a final destination symlink. - Use
setEntrywhen copying symlinks (quota accounting), check source existence before the same-pathmvreturn, detect self-copy via aliases, avoid argument spread on long symlink targets, and propagate the caller syscall name inELOOP.
Tests (in-memory-fs, symlinked-dir.comparison.test.ts, src/fs/ + cp/mv/mkdir/mktemp), typecheck, and lint all pass. Looks good to merge.
Problem
On an
InMemoryFs, a path that goes through a symlinked directory can be read but not changed: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,readdirWithFileTypesandreadlinkused 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.createExclusivealready resolves its parent for this reason.Summary
lstatdoes.writeFileLazydoes too.resolveIntermediateSymlinksandresolvePathWithSymlinks) and the follow loop inreaddirWithFileTypesare 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 atalias/dir, so reads,lsand writes all missed, and a symlink to/produced//namekeys thatreaddirnever lists.rmandmvact on the link, and writing to a file symlink still replaces the link, whichin-memory-fs.security.test.tsasserts.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/andwriteFileLazy. All but the last-part case fail on main.symlinked-dir.comparison.test.ts: recorded bash fixtures for writes, appends,mkdir,touch,cp,mvandrmthrough the link, and for reads,lsand 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:bannedandtypecheckpass.Not in this PR
Bash writes through a symlink to a file (
echo x > flnkchanges the target) andInMemoryFsreplaces 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, solnk/../xmeans/xrather 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