Skip to content

fix(hitl): make session and always approval grants actually stick - #464

Merged
mohitranka merged 1 commit into
mainfrom
fix/hitl-approval-grants
Aug 30, 2026
Merged

mohitranka merged 1 commit into
mainfrom
fix/hitl-approval-grants

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

Approving a command or a network host "for this session" or "always" did not stop the next matching call from prompting again. This fixes five distinct defects on the HITL approval path, plus two more found while verifying the fixes in a real PTY.

Why the grants did nothing

Shell commands are not governance-gated — default_hitl_tools() is just ["mcp:*"]. Their prompts come from ToolError::SandboxDenied in finish_tool_application, which never consulted the operator's allow rules at all. "Always allow" wrote a line to permissions.toml that nothing on the re-prompt path ever read. The grant and the gate were different mechanisms.

Fixes

1. Sandbox denials honour existing grants (session/tools.rs)
finish_tool_application now asks Governance::grant_covers and re-runs unconfined when a rule already covers the call — the same consent the prompt would collect. Denied hosts are excluded: a command-shaped rule must not silently open a network destination.

2. Suggested patterns match the calls they came from (governance/pattern.rs)
suggest_pattern built its rule from the raw command while matches tested the normalized permission subject. 5 of 8 realistic commands produced a grant that could not match its own call:

cargo test --workspace 2>&1 | tail -20 -> bash(cargo test *)            matches_itself=false
git status && git diff                 -> bash(git status *)            matches_itself=false
RUST_LOG=debug cargo run               -> bash(RUST_LOG=debug cargo *)  matches_itself=false

It now derives the prefix from the same normalized subject, and returns None for a command with unquoted control syntax — which has no matchable rule by design, since matches refuses to let cargo test; rm -rf / inherit a cargo test * grant. pattern_allow_eligible requires a suggestion, so those calls no longer offer a "remember this" row that writes a grant which can never fire. The rustdoc invariant on suggest_pattern claimed this property and was false; it is now true and asserted.

3. Denying a network prompt is a denial (app/approvals.rs) — security
apply_hitl_decision branched on denied_host before it read decision, so "Don't run" and Esc granted the host for the session and ran the command, while the status line said "Action denied". Refusal is now settled first, failing closed on any non-approval.

4. A git approval no longer grants every git call (governance/pattern.rs) — security
suggest_pattern had no case for the structured git tool, so it fell back to the bare tool name. Approving git reset --hard HEAD~1 for the session also permitted git clean -fd. Now git(reset --hard *), with the row's help line narrowed to match. This also makes hand-written git(push *) rules work, which previously matched nothing.

5. retain_session_patterns_from is wired (session/create.rs)
It was dead code. set_governance now calls it, so a policy reload cannot silently revoke grants made at a prompt.

Found by PTY verification, not by tests

6. deny beats allow in the new gate. The auto-approve check asked only whether an allow rule matched, letting a personal allow = ["bash(cargo test *)"] override a workspace deny = ["bash"]. Driving the real TUI showed "Action approved" on a command the deny rule existed to gate. grant_covers now checks deny first, mirroring authorize's own ordering. Every unit test had used allow-only or deny-only policies, so none caught it.

7. The unconfined retry was unbounded. The rule authorising the first retry authorises the next, so a tool reporting a denial even unconfined would re-run forever. TurnState::claim_auto_unconfined_retry caps it at one retry per call per turn.

Verification

18 new tests. cargo fmt --check, cargo clippy --all-targets, cargo test --workspace (46/46 binaries) all green.

Driven in tmux (120x40) against the release binary, real model and egress proxy:

Test Result
cargo test --workspace under deny=["bash"] + personal allow card appears (not auto-approved); offers bash(cargo test *) for session and always
cargo test --workspace | head -5 only Run once / Don't run / Don't run-and-say-why — no unusable grant offered
git status --short git(status --short), not bare git
Deny a real curl https://example.com prompt skipped · denied; command did not run; permissions file untouched; re-asking prompts again
Allow **.example.com for the session, then hit www.example.com runs with no second prompt

https://claude.ai/code/session_01QxjMZfzyjBuq3m25CWkzXu

Approving a command or a network host "for this session" or "always" did
not prevent the next matching call from prompting again. Five distinct
defects, all on the human-in-the-loop approval path.

Shell commands are not governance-gated (`default_hitl_tools()` is
`["mcp:*"]`), so their prompts come from `ToolError::SandboxDenied` in
`finish_tool_application`, which never consulted the operator's allow
rules at all. An "always allow" grant was written to permissions.toml and
read by nothing on the path that re-prompts. Sandbox denials now ask
`Governance::grant_covers` and re-run unconfined when a rule already
covers the call, which is the same consent the prompt would collect.
Denied hosts are excluded: a command-shaped rule must not silently open a
network destination.

`suggest_pattern` built its rule from the raw command while
`PatternRule::matches` tested the normalized permission subject, so most
real commands produced a grant that could not match the call it came
from -- `cargo test --workspace 2>&1 | tail` suggested `bash(cargo test *)`
and then never matched anything with a pipe, and `RUST_LOG=debug cargo
test` suggested a rule with the env prefix baked in that matched nothing
at all. It now derives the prefix from the same normalized subject, and
returns None for a command carrying unquoted control syntax, which has no
matchable rule by design. `pattern_allow_eligible` requires a suggestion,
so those calls no longer offer a "remember this" row that writes a grant
which can never fire. The rustdoc invariant is now true and asserted.

`apply_hitl_decision` branched on `denied_host` before it read `decision`,
so "Don't run" and Esc on a network prompt granted the host for the
session and ran the command while the status line said "Action denied".
Refusal is now settled first, failing closed on any non-approval.

`suggest_pattern` had no case for the structured `git` tool, so it fell
back to the bare tool name: approving `git reset --hard HEAD~1` for the
session also permitted `git clean -fd`. A git branch keeps the subcommand
and its flags, and the row's help line narrows with it.

`retain_session_patterns_from` was dead code; `set_governance` now calls
it, so a policy reload cannot silently revoke grants made at a prompt.

Two further defects were found while verifying the above in a real PTY
rather than in tests, and are fixed here too. The new auto-approve gate
asked only whether an allow rule matched, letting a personal
`allow = ["bash(cargo test *)"]` override a workspace `deny = ["bash"]`;
`grant_covers` checks deny first, mirroring `authorize`'s own ordering.
And the automatic unconfined retry was unbounded -- the rule that
authorises the first retry authorises the next, so a tool reporting a
denial even unconfined would re-run forever. `TurnState` caps it at one
retry per call per turn.

Verified in tmux against the release binary: a piped command offers no
remember rows, a simple one offers a pattern that matches itself, a git
prompt names its subcommand, refusing a network prompt neither runs the
command nor grants the host, and allowing `**.example.com` for the
session covers `www.example.com` with no second prompt.

Claude-Session: https://claude.ai/code/session_01QxjMZfzyjBuq3m25CWkzXu
@mohitranka
mohitranka merged commit f9065eb into main Aug 30, 2026
7 checks passed
@mohitranka
mohitranka deleted the fix/hitl-approval-grants branch August 30, 2026 23:33
mohitranka added a commit that referenced this pull request Sep 11, 2026
fix(hitl): make session and always approval grants actually stick
mohitranka added a commit that referenced this pull request Sep 11, 2026
fix(hitl): make session and always approval grants actually stick
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