Skip to content

fix(agent): close three permission leaks (early read, race key, patch targets) - #481

Merged
mrnasil merged 6 commits into
mainfrom
fix/agent-permission-leaks
Nov 25, 2026
Merged

mrnasil merged 6 commits into
mainfrom
fix/agent-permission-leaks

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

Three findings from the code audit, each a way past the permission / key boundaries.

1. Speculative read skipped every gate

While the model's reply streams, a read whose path is complete starts early (speculative_read). When the final call matched, that result was returned instead of running execute_with_permission, so tool rules (read: deny / ask), path rules (read(secrets/**): deny), the agent's tool allow-list, the intent check and secret-placeholder restoration never ran. Only hooks were honoured.

Fix: read_runs_ungated — the early result is used only when every gate would let this read through without asking; otherwise the call goes through the gates as usual. Also, in a sequential batch, a call that is not parallel-safe (edit, write, bash…) now aborts pending early reads, so [edit a.rs, read a.rs] returns the edited file, not the content read before the edit.

2. Model race sent the turn's API key to another provider

race_partner can pick another provider (model_race = "openai/…" while on anthropic), but the race target reused the turn's api_key: the Anthropic key went to the OpenAI endpoint on every step. The title-refine path already avoided this; the race path never got the fix.

Fix: shared side_request_key(target, provider, key): the same provider gets the turn's key, another gets its own {PROVIDER}_API_KEY or none (a keyless local provider still works). Title refine uses it too.

3. apply_patch header targets bypassed path rules

+++ b/<file> / diff --git headers override path, but path rules and the outside-workspace (important-mode) check looked at path only — and at nothing when path was omitted. {path: "src/a.rs", patch: "+++ b/.env …"} wrote .env past edit(.env): deny.

Fix: whycodes_tools::apply_patch::patch_targets lists every target; file_tool_paths feeds them to paths_rule_gate (any deny → refuse, any ask → ask, allow only if all are allowed), to the outside-workspace check, and to the headless subagent gate.

Tests

  • speculative_read_does_not_skip_read_rules (path deny, tool deny, tool ask) and speculative_read_is_dropped_after_a_write_in_the_same_batch — both fail on the old gate and pass now.
  • side_requests_never_send_the_turn_key_to_another_provider.
  • path_rules_see_apply_patch_header_targets (with and without path), paths_rule_gate_needs_every_path_allowed, patch_targets_name_every_header_over_path.
  • agent 775 + tools 541 lib tests, clippy, fmt, budgets; none of the 157 added lines is uncovered locally.

🤖 Generated with Claude Code

mrnasil and others added 4 commits November 25, 2026 16:07
… targets)

- Speculative read: a `read` started while the reply streamed ran
  before every gate, and its result stood in for the call, so
  `read: deny`, `read(secrets/**): deny`, `read: ask`, the intent check
  and restored secret placeholders were skipped. The early result is
  now used only when no gate would refuse or ask. In a sequential batch
  a call that can change files drops the pending early reads, so a
  `read` after an `edit` no longer returns the old content.
- Model race: a partner on another provider was sent the turn's API
  key. Side requests now share `side_request_key` (the title refine
  already did this): the same provider gets the turn's key, another its
  own `{PROVIDER}_API_KEY` or none.
- apply_patch: `+++` / `diff --git` header targets override `path`, but
  path rules and the outside-workspace check judged `path` only (and
  nothing when it was absent). They now judge every target
  (`patch_targets`): any deny refuses, any ask asks, allow only when
  all are allowed. The headless subagent gate does the same.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Linux CI never ran an early read under a turn intent, leaving the
intent closure of read_runs_ungated uncovered there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil enabled auto-merge (squash) November 25, 2026 13:27
mrnasil and others added 2 commits November 25, 2026 16:31
A multi-line matches! left its never-taken arm as an uncovered line on
Linux; authorize_tool == Allow is the same check without one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 8e31e49 into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/agent-permission-leaks branch November 25, 2026 14:59
@mrnasil mrnasil mentioned this pull request Nov 26, 2026
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.

1 participant