Skip to content

fix(security): pin worktree chat authorization to the resolved cwd - #4550

Merged
BunsDev merged 1 commit into
mainfrom
fix/reland-4548-worktree-cwd-containment
Aug 11, 2026
Merged

fix(security): pin worktree chat authorization to the resolved cwd#4550
BunsDev merged 1 commit into
mainfrom
fix/reland-4548-worktree-cwd-containment

Conversation

@BunsDev

@BunsDev BunsDev commented Aug 11, 2026

Copy link
Copy Markdown
Member

Re-lands #4548 on current main. That branch is ~1768 commits behind and conflicts with main (git merge-tree reports a conflict in src/lib/chat-project-access.ts — the file was substantially reworked since the branch point, and the branch still carries a familiar-workspace exemption main has since removed).

The hole

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 elsewhere borrowed that project's grant while the harness actually ran outside it.

Fix: require resolvedCwd (realpathed by resolveLocalRuntimeCwd) to sit under the same prefix.

The reviewer's point on #4548, addressed

copilot-pull-request-reviewer flagged that comparing a realpathed resolvedCwd against a lexically path.resolved project.root fail-closes legitimate worktree chats whenever a project root is symlinked. Confirmed reachable, with a test that fails on #4548's head/var -> /private/var on macOS makes this the everyday case, not a corner. Both sides are now canonicalized through the existing realpathOrResolve helper.

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 — passes
  • pnpm typecheck — clean

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>
Copilot AI lite review requested due to automatic review settings August 11, 2026 07:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 / chatProjectAccessId to require both requestedProjectRoot and resolvedCwd to canonicalize under the same <project>/.worktrees/ prefix (via realpathOrResolve).
  • Add regression coverage for (1) symlink-escape fail-close and (2) symlink-registered project roots (e.g. /var -> /private/var class 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);
@BunsDev
BunsDev merged commit da04c6b into main Aug 11, 2026
7 of 8 checks passed
@BunsDev
BunsDev deleted the fix/reland-4548-worktree-cwd-containment branch August 11, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants