Unify shell auto-allow and path-restriction workspace containment - #388
Merged
Conversation
escapesWorkspace/argEscapesWorkspace in classify.ts computed workspace containment as a bare realpath-prefix check against cwd, with no knowledge of registered git worktrees, while the restriction check in gate.ts/path-restriction.ts already resolved containment through the worktree-aware rootsProvider. A path inside a sibling worktree could therefore be judged auto-allow-eligible by one check and restricted by the other for the identical request. escapesWorkspace now delegates to path-restriction.ts's resolveWorkspacePath, and a RootsProvider is threaded through the shell auto-allow call chain down to it; gate.ts passes its own rootsProvider at both call sites so the two checks always consult the same roots. Callers that omit the provider keep today's cwd-only behavior via a no-op default, so no existing caller widens. resolveWorkspacePath also resolves a not-yet-existing path against its nearest existing ancestor rather than falling back to the raw string, so a symlink pointing outside the workspace is now caught for a not-yet-created target under it, closing a narrower gap the old prefix check missed.
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.
Summary
Two permission checks disagreed on whether a path is "inside the workspace," so an identical request could get opposite verdicts.
escapesWorkspace/argEscapesWorkspace(src/permission/classify.ts) — drives shell auto-allow eligibility — computed containment with a bare realpath-prefix check against a singlecwd, with no knowledge of git worktrees.bindRestrictedToProcessCwd(src/permission/gate.ts:89-97), viacreatePathRestriction/resolveWorkspacePathin src/permission/path-restriction.ts, already resolved containment againstcwdplus every registered git worktree root (viarootsProvider, backed bygit worktree list).A path inside a sibling worktree was auto-allow-eligible under one check and restricted under the other for the same argument.
Fix
escapesWorkspacenow delegates toresolveWorkspacePath(src/permission/path-restriction.ts) instead of reimplementing prefix comparison. ARootsProvider(src/permission/worktree-roots.ts) is threaded through the call chain —isAutoAllowedShellCall→isAutoAllowedShellCommand/isAutoAllowedShellSegment→isAutoAllowedSegment→argEscapesWorkspace→escapesWorkspace— defaulting to a no-op() => []roots list, so any caller that omits it (every existing test call site) keeps exactly today's cwd-only behavior.src/permission/gate.ts's two call sites now pass the gate's ownrootsProvider— the same instancecreatePathRestrictionalready uses — so both checks always consult identical roots.This builds on #386 (CL-5662), which added
cwdMatchesGrantinsrc/permission/authz-grants.tsto resolve worktree membership for grant-cwd matching, but deliberately left classify.ts's containment logic unreconciled because the call shapes differ (per-shell-token containment vs per-request cwd identity). This PR completes that unification for the containment side: classify.ts now calls the one existing path-containment authority instead of reimplementing it.Containment definitions remaining after this change: two, each owning a distinct question —
resolveWorkspacePath/createPathRestriction(src/permission/path-restriction.ts) for "is this path argument inside the workspace," andcwdMatchesGrant(src/permission/authz-grants.ts) for "does this request's cwd match a stored grant's cwd." classify.ts contributes zero new definitions; it is now purely a caller of the first.Security
This is a permission boundary. The unification cannot auto-allow anything the old split checks refused:
rootsProvidergets theNO_ROOTS = () => []default, which reducesresolveWorkspacePathto the exact samereal === realCwd || real.startsWith(realCwd + sep)check the old code did — identical result set.rootsProviderare gate.ts's own call sites, and they pass the identical instance already used to build the restriction check — never a broader one.wt-1-evilnext towt-1) is still refused.Tests
New file
src/permission/workspace-containment.test.ts:wt-1-evilvswt-1) is refused by both.resolveWorkspacePath's nearest-existing-ancestor resolution.Test plan
bun run typecheck— cleanbun run test(full suite via lock script) — 4143 pass, 0 failbun run build— cleanhttps://linear.app/abklabs/issue/CL-5671/shell-auto-allow-and-path-restriction-disagree-on-worktree-containment