Skip to content

harden(hooks): wire tirith + supply-chain gates into the live hook path (#100 G1) - #106

Merged
thehoff merged 2 commits into
developfrom
harden/gate-wiring-100
May 20, 2026
Merged

thehoff merged 2 commits into
developfrom
harden/gate-wiring-100

Conversation

@thehoff

@thehoff thehoff commented May 20, 2026

Copy link
Copy Markdown
Owner

Summary

User-directed fix for G1 audit finding #2 (issue #100): the defence-in-depth gates were not on the live hook path. Now wired in. Codex-reviewed; follow-up applied.

The problem

contextcrawler hook claude (the installed PreToolUse hook) → process_claude_payload ran permissions + rewrite but never invoked tirith_gate or supply_chain_gate. Those gates fired only on the contextcrawler rewrite subcommand — a non-default entrypoint. Defence-in-depth did not run for the actual agent hook.

What changed (src/hooks/hook_cmd.rs)

  • New pure gate_decision(tirith, supply_chain) -> GateDecision { Proceed, Ask, Deny }. supply-chain Block → Deny; tirith should_downgrade → Ask; supply-chain Unavailable → Ask (fail-closed); else Proceed. Block outranks Ask.
  • Gate calls inserted in process_claude_payload after the PermissionVerdict::Deny block, before rewrite. A gate Ask/Deny overrides a permissions Allow (the allow emit now requires verdict == Allow && !gate_ask).
  • Reuses harden(hooks): fix G2 audit findings — payload+deny fail-closed, proxy path-traversal, +8 more #102's existing PayloadAction::Deny / emit_claude_deny — no duplicate machinery.
  • A gate-flagged command with no contextcrawler rewrite fails closed to Deny (the PreToolUse protocol can't express "ask without rewrite") — and now logs why.
  • Opt-in respected: tirith (CONTEXTCRAWLER_TIRITH_REQUIRED) and supply-chain (supply_chain.enabled) stay opt-in — when disabled the gates are no-ops, exactly as before. This change makes them actually run on the hook path when enabled.

Codex review follow-up (8b53388)

  • supply_chain_gate::load_config() now caches via OnceLock — disk read at most once per process (the gates run on every hook call; a per-call disk read was hot-path overhead).
  • The no-rewrite Deny now emits a stderr line explaining the protocol-limitation reason.

Test plan

  • cargo test --workspace — 2332 pass, 0 fail
  • test_disabled_gates_no_regression — drives the real gate fns; disabled gates → git status still rewrites normally, no deny
  • cargo build --release clean
  • Codex review — CRITICAL (no gate-skip path) and disabled-gates no-regression both verified PASS; 2 non-blocker follow-ups applied

Part of the #100 audit.

🤖 Generated with Claude Code

thehoff and others added 2 commits May 20, 2026 18:45
…path

G1 audit finding #2 of #100: the Tirith pre-execution gate and the
supply-chain gate only ran on the legacy `contextcrawler rewrite`
subcommand (hooks/rewrite_cmd.rs). The live Claude Code PreToolUse path
(`contextcrawler hook claude` -> process_claude_payload) did permissions
+ rewrite but never invoked either gate, so the defence-in-depth layer
was effectively dark for every real agent session.

Wire both gates into process_claude_payload_with, after the existing
PermissionVerdict::Deny handling and before get_rewritten:

  * New pure helper gate_decision(tirith, supply_chain) -> GateDecision
    { Proceed, Ask, Deny } maps the two gate verdicts. supply-chain
    Block -> Deny; supply-chain Unavailable -> Ask (fail closed);
    tirith should_downgrade -> Ask; both clean -> Proceed. Block
    outranks Ask.
  * supply-chain Block reuses the #102 deny machinery -- emits a
    PayloadAction::Deny (audit_tag deny:supply_chain_block) routed
    through the existing emit_claude_deny. No new PayloadAction variant.
  * A gate Ask sets gate_ask, which suppresses the permissionDecision
    "allow" so Claude Code prompts -- a gate Ask wins over a
    permissions Allow.
  * A gate-flagged command with no rewrite (e.g. `pip install foo`)
    cannot express "ask" in the PreToolUse protocol, so it fails CLOSED
    to a deny rather than a silent Skip.

Both gates stay opt-in: Tirith via CONTEXTCRAWLER_TIRITH_REQUIRED,
supply-chain via supply_chain.enabled. When disabled the gates are
no-ops and the hook behaves byte-for-byte as before -- covered by a
no-regression test asserting `git status` still rewrites with no deny.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…100 — config cache + visible no-rewrite deny

The defence-in-depth gates now run on every hook-routed Bash command, so
two non-blocker issues from the Codex review are tightened here.

Finding 1 — supply_chain_gate::load_config() read the config file from
disk on every call, before the early-return-when-disabled. Back it with a
process-lifetime std::sync::OnceLock cache (read_config_from_disk does the
actual read once) so the hottest path hits disk at most once per process.
tirith_gate has no equivalent per-call config read — it spawns a
subprocess and only the dashboard touches files — so no change needed
there.

Finding 2 — when a gate returns Ask on a command with no contextcrawler
rewrite, the code hard-Denies (the PreToolUse protocol can't express "ask
without rewrite"). The Deny is correct (fail closed) but was silent. Emit
a clear stderr line — "[contextcrawler] gate flagged '<cmd>' (ask) but it
has no rewrite; denying (protocol limitation)" — via the factored-out
gate_no_rewrite_deny_log() helper so an operator can see why a command
was blocked. Deny behaviour itself is unchanged.

Tests: load_config returns a stable &'static (pointer-identity proves the
cache); gate_no_rewrite_deny_log emits the explanatory line.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thehoff
thehoff merged commit fd5cff7 into develop May 20, 2026
4 checks passed
@thehoff
thehoff deleted the harden/gate-wiring-100 branch May 20, 2026 08:58
noogalabs pushed a commit to noogalabs/contextcrawler that referenced this pull request Jun 4, 2026
…hard deny (thehoff#111)

The thehoff#106 gate wiring put the Tirith defence-in-depth gate on the live
`hook claude` path. When a gate returned `GateDecision::Ask` for a command
with no contextcrawler rewrite (e.g. a credential-bearing `curl` loop), the
no-rewrite branch fell through to a hard `PayloadAction::Deny` — an "ask"
the user could never answer. Real users hit this in REST-verification
workflows.

The Claude Code PreToolUse protocol supports `permissionDecision` values of
`allow`, `deny`, and `ask` (the existing `handle_vscode` path already emits
`ask`). This change routes a gate `Ask` + non-rewritable command to a real
`ask` decision so the user gets an approve/deny prompt.

- Add `PayloadAction::Ask { reason, audit_tag, cmd }` mirroring `Deny`.
- Add `emit_claude_ask` alongside `emit_claude_deny`; same JSON schema,
  `permissionDecision: "ask"`.
- `run_claude` gains an `Ask` arm; `run_claude_inner` mirrors it.
- The `gate_no_rewrite` branch now returns `Ask` (audit tag
  `ask:gate_no_rewrite`) instead of `Deny`; stderr line reworded.
- A supply-chain hard `Block` (`GateDecision::Deny`) still routes through
  `Deny` — a real hard-block stays a deny.
- Gate decision is now injectable via `process_claude_payload_with_gate`
  so the no-rewrite Ask path is deterministically testable.

Tests: gate `Ask` + non-rewritable command yields the `Ask` action / `ask`
JSON; gate `Ask` + rewritable command still rewrites; supply-chain `Block`
still denies. cargo test --workspace and clippy --workspace --release pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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