fix(security): pin worktree chat authorization to the resolved cwd - #4550
Merged
Conversation
The `.worktrees/<branch>` carve-out in chatProjectAccessId authorized on the client-supplied projectRoot alone. That only proves the client SPELLED a path under a registered project: a symlink at `<project>/.worktrees/<name>` pointing anywhere else handed the caller that project's grant while the harness ran outside it. Require the runtime's resolved cwd — realpathed by resolveLocalRuntimeCwd — to sit under the same `<project>/.worktrees/` prefix. Canonicalize the project root with realpathOrResolve for the same reason. A project registered through a symlinked path, or any root with a symlinked ancestor (/var -> /private/var on macOS), builds a prefix the realpathed cwd can never start with, so the new check would fail-close every legitimate worktree chat under it. Both sides now share one namespace; containment is not widened, since a symlink escaping the project resolves outside the prefix. Re-lands #4548, whose branch is ~1.8k commits behind main and conflicts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Pins worktree-scoped chat authorization to the runtime-resolved cwd (not just the client-supplied requested root), closing a symlink-escape path where a .worktrees/<name> entry could point outside the registered project while still borrowing the parent project’s grant.
Changes:
- Update
worktreeParentProject/chatProjectAccessIdto require bothrequestedProjectRootandresolvedCwdto canonicalize under the same<project>/.worktrees/prefix (viarealpathOrResolve). - Add regression coverage for (1) symlink-escape fail-close and (2) symlink-registered project roots (e.g.
/var -> /private/varclass of issues).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/lib/chat-project-access.ts | Tightens .worktrees/ carve-out by validating canonicalized requested root and runtime-resolved cwd against the same parent-project prefix. |
| src/lib/chat-project-access.test.ts | Adds regression tests for symlink escape and for symlink-registered project roots to avoid false negatives. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| @@ -211,3 +214,64 @@ assert.equal( | |||
| ); | |||
|
|
|||
| console.log("chat-project-access tests passed"); | |||
Comment on lines
+240
to
+241
| const linkedProject = path.join(realBase, "linked-cave"); | ||
| fs.symlinkSync(realProject, linkedProject); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Re-lands #4548 on current
main. That branch is ~1768 commits behind and conflicts withmain(git merge-treereports a conflict insrc/lib/chat-project-access.ts— the file was substantially reworked since the branch point, and the branch still carries a familiar-workspace exemptionmainhas since removed).The hole
The
.worktrees/<branch>carve-out inchatProjectAccessIdauthorized on the client-suppliedprojectRootalone. That only proves the client spelled a path under a registered project — a symlink at<project>/.worktrees/<name>pointing elsewhere borrowed that project's grant while the harness actually ran outside it.Fix: require
resolvedCwd(realpathed byresolveLocalRuntimeCwd) to sit under the same prefix.The reviewer's point on #4548, addressed
copilot-pull-request-reviewer flagged that comparing a realpathed
resolvedCwdagainst a lexicallypath.resolvedproject.rootfail-closes legitimate worktree chats whenever a project root is symlinked. Confirmed reachable, with a test that fails on #4548's head —/var -> /private/varon macOS makes this the everyday case, not a corner. Both sides are now canonicalized through the existingrealpathOrResolvehelper.Containment is not widened: a symlink escaping the project resolves outside the prefix and still misses. There is a test for that too.
Verification
node --experimental-strip-types src/lib/chat-project-access.test.ts— passes (3 new assertions; the symlink regression test was proven to fail against fix(security): validate resolved worktree cwd #4548's unpatched head first)node --experimental-strip-types src/app/api/chat/send/harness-routing-host-session.test.ts— passespnpm typecheck— clean