Repository navigation
fix(files): never write through a dangling symlink - #28385
Conversation
A path that doesn't exist yet was authorized by walking up to the nearest existing folder. A dangling symlink fails realpath with ENOENT, so the walk stepped over it and approved the link's own path; the write then followed the link and created its target outside the allowed folders. The walk now continues from the link's target, and returns that real destination.
Following a dangling link by hand resolved relative targets against the lexical parent, not the link's real folder: through a folder link the walk could loop forever or authorize the wrong file. Refuse instead: realpath can't say where a dangling link leads, but a write would follow it.
📝 Walkthrough
Priority: ➖ Normal Merge Risk: 🟡 Moderate · up to
|
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
75e83f22-a439-4c0e-a548-06f0a0aeaec5
📒 Files selected for processing: 2
src/main/ipc/filesystem-auth.test.tssrc/main/ipc/filesystem-auth.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
ELI5
A file save can be tricked into creating a file outside the folders Orca is allowed to write to. If a project contains a "broken" shortcut (a symbolic link whose target doesn't exist yet) that points outside the project, saving to that shortcut creates the file at the outside location. Orca's permission check approved the shortcut's own location (inside the project) instead of where the save actually lands. Now a save through any broken shortcut is refused.
PR 1 of 6 in the chat-scratch stack. It depends on nothing; PR 2 (rename the chat visuals folder to the chat scratch folder) builds on this branch. The bug already exists on
mainand the fix stands on its own.What Changed
Before: saving to
repo/escape.txt, whereescape.txtis a broken link to/somewhere/outside/new.txt, was allowed and created/somewhere/outside/new.txt. (A broken link to a folder in the middle of the path was also approved, but the write then failed when creating the missing folders, so only the last-segment case created a file outside.)After: that save (and the folder-link one) is refused with the usual "outside allowed directories" error, and nothing is created outside. Creating new files under missing folders inside an allowed folder (
repo/new/nested/file.ts) is unchanged, and links whose targets exist are handled exactly as before.One behaviour change versus main: a broken link that points somewhere inside the project is now refused too. On main, saving to it created the in-project file it pointed to; now that save shows the same "outside allowed directories" error, and opening such a link shows that error instead of "file not found". Creating the target file first (or saving to the target directly) still works.
Mechanism:
resolveAuthorizedPath(src/main/ipc/filesystem-auth.ts) handles a path that doesn't exist yet by walking up to the nearest folder that does exist, resolving that folder's real location, and checking the full path against the allowed folders. A broken link made that walk wrong: resolving its real location fails with "not found" (because its target is missing), so the walk treated the link as a missing name, stepped over it, and approved the link's own path. The write then followed the link out. Now, when a step in the walk fails with "not found" but a link is actually there (so the link is broken), the request is refused. This applies to the last part of the path and to any folder in the middle of it. Every caller ofresolveAuthorizedPathgets the fix: the desktop file IPC, the local filesystem provider, and runtime file commands on a paired host.Why
The approval and the write have to agree on where the bytes go. For a broken link, the operating system is the only thing that knows where a write would land, and it won't say until the write happens. Refusing in the one place that already resolves real locations fixes every write path at once, instead of adding a second check inside each writer.
Alternatives considered:
No difference from the common pattern: a path whose real location can't be resolved is not authorized.
Linked Issue
None (found while planning the chat scratch folder work; the bug predates it).
Visual Proof
N/A: no UI change. The only visible difference is that saving to or opening a broken link shows the existing "outside allowed directories" error.
Testing
Extended the existing real-filesystem symlink test in
src/main/ipc/filesystem-auth.test.ts: a broken link pointing outside and a broken link pointing inside are both refused. Verified it fails onmain's code (the outside link was approved) and passes with the fix. The existing "missing nested descendants" test keeps nested creation covered.Ran every test file that imports
filesystem-authplus the provider tests (1,279 passed). Node typecheck clean. Symlink tests skip on Windows like the existing ones (creating links needs elevated rights there); the check useslstat, which reports Windows symbolic links and junctions as links (not verified on Windows in this PR).macOS only locally. SSH: the relay authorizes paths on its own side and is not changed here; a paired Orca host runs this same function, so it is covered.
I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
Review
Agent skill upstream boundary
Notes
Security: closes a write outside the allowed folders. Cross-platform: no new path handling; the only new call is
lstat. Performance: one extralstatonly when a path segment is missing.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)