fix(permissions): match normalized paths for read and bash deny rules

Merged
AN
2mo ago
+1226-355

Issue for this PR

Closes #1496

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Why: permission deny rules written with absolute paths could be sidestepped because the read and bash tools matched rules against whatever spelling the model happened to use (relative path, .., quoted operand, path after cd, or a bare ls).

Root cause: read requested permission only for the worktree-relative spelling, and bash matched rules against the raw command text. Absolute rules therefore never saw an equivalent relative spelling. Separately, the legacy permission ask returned at the first ask result, so a later denied command in the same request could be skipped once the earlier prompt was approved.

Before -> after:

  • read and bash now submit the authored spelling plus normalized absolute aliases (literal and quoted operands, straight-line cd, relative workdir, ls including bare ls; symlink-resolved targets for read).
  • Aliases are used only for deny/ask rules. allow rules still match only the authored spelling, so normalization can tighten a decision but never broadens an allow. Rule precedence (last match wins) is unchanged.
  • Independent invocations (same text in different directories) are each evaluated; one allowed spelling cannot mask another denied one.
  • Legacy ask now rejects any denied pattern before prompting; both permission implementations share one evaluator.
  • Background & lists no longer propagate cd to later commands.

Review notes: an independent review found that an allow rule could match the normalized spelling (and quoting/../background cases made that unsafe); this was fixed by making aliases deny/ask-only, with regression tests.

Scope and limits

  • Bash permission patterns are best-effort matching on command text. They are not a security boundary.
  • Normalization only ever tightens decisions: aliases apply to deny/ask rules, never to allow rules, and the result is the strictest of the authored and alias-aware match. Rules remain last-match-wins, so a later authored allow (including an "always" approval) still overrides an earlier rule for the same command.
  • Filesystem isolation (a sandbox or OS-level permissions) is the boundary for untrusted commands. Structural enforcement is out of scope for this change.
  • read additionally matches the symlink-resolved real path of its target for deny/ask rules.
  • Remaining reported bypass classes, intentionally not chased here: shell expansions (variables, CDPATH, HOME, globs), paths used only by redirections, wrapper commands and sh -c, directory-stack builtins, loop iterations and repeated failing cd, symlinks traversed before .. collapsing, reference-file operands, and further operand or whitespace spelling variants beyond those enumerated.
  • Not changed: the debug agent handler still uses the scalar evaluator.

How did you verify your code works?

From packages/opencode:

  • bun test test/tool/read.test.ts test/tool/bash.test.ts test/permission: 226 pass, 0 fail (new tests: read 3, bash 14, permission 9+; red before fix).
  • bun test test/tool test/permission: 680 pass, 1 fail (detectDataTools in project-scan.test.ts, probes external tools, unrelated to touched modules).
  • bun test test/permission-yolo.test.ts test/permission-task.test.ts test/agent/agent.test.ts test/file/security-e2e.test.ts test/server/httpapi-session.test.ts test/cli/run: 470 pass, 0 fail.
  • bun run typecheck: clean. git diff --check: clean.
  • Marker audit against the v1.4.14 baseline tag: permission/evaluate.ts, permission/index.ts, tool/bash.ts, tool/read.ts exist there; all changed lines are inside altimate_change markers (a pre-existing nested marker in bash.ts is unchanged). permission/next.ts is not in the tag and is marked as well.

Screenshots / recordings

N/A (no UI change).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

🤖 Generated with Claude Code


[!NOTE]
High Risk
Changes core permission evaluation and bash/read tool gating; mistakes could wrongly allow sensitive paths or block legitimate tool use.

Overview
Fixes permission bypasses where deny rules on absolute paths or exact command text did not apply when read or bash used a different spelling (relative paths, cd chains, quoting, etc.).

A shared PermissionEvaluate module evaluates rules against the authored pattern plus optional permissionAliases in request metadata. Aliases only affect deny and ask matches (never allow), and the outcome is the stricter of the authored rule and alias-aware matches. Effect and legacy permission ask paths use this evaluator; legacy ask sorts patterns so denies are checked before any prompt.

read resolves paths from the workspace, then supplies aliases for absolute and symlink-resolved paths. bash parses static shell operands (including cd / ls, mixed spellings, caps on alias explosion), attaches command-text aliases, and resolves workdir relative to the instance directory.

Regression tests cover alias precedence, multi-pattern requests, and “always” approvals that must not cross spellings.

Reviewed by Cursor Bugbot for commit 26380ee7a91ace441c9b4a3c4480ff58b6167a55. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

read and bash now match permission rules against authored paths and normalized absolute aliases, closing bypasses where equivalent spellings evaded deny rules.

  • Bash aliases cover quoted and escaped operands, mixed path spellings, cd sequences, relative workdir, ls, and symlink-resolved targets.
  • Aliases affect only deny and ask rules; allow rules still match only the authored spelling.
  • Legacy permission requests check all denied patterns before prompting, and directory tracking stays local to subshells, substitutions, pipelines, and background lists.
  • Matching is static: shell expansions, redirection-only paths, wrapper commands, and .. through symlinks remain unnormalized.

Summary by CodeRabbit

  • Bug Fixes
    • Permission checks now recognize relative paths, absolute paths, and paths resolved through symlinks when accessing files or running shell commands.
    • Deny rules take precedence when equivalent path spellings match different rules; allow rules do not apply solely through an alternate spelling.
    • Shell permission checks account for directory changes and literal file-command paths.
    • Relative working directories are resolved from the current workspace.
    • Permission requests evaluate all requested patterns and prioritize denied results.