feat(delivery): add the guard hook (ADR-0032 layer 2) #211

Merged
Defame1297 merged 2 commits from feat/delivery-guard-hook into main 2026-11-23 14:57:52 +00:00
Owner

Builds the delivery guard hook: the portable pre-tool-use guard from ADR-0032's enforcement layer 2. Refs #190 (left open; it closes with the wayfinder resolution after merge and a deploy check).

What it does

plugins/delivery/.apm/hooks/guard.py (stdlib-only Python) runs before every tool call in a delivery session:

Tier Claude Code Copilot CLI Codex / unknown
Never (kubectl/helm/flux changes, tofu destroy/state, --no-verify/SKIP=/hooksPath/.git/hooks writes, vault and Sealed Secrets decryption, secret-file reads) deny deny deny
Odd job (ansible-playbook outside check mode, tofu apply, flux reconcile) ask ask deny
Read / Propose allow allow allow
  • Owner decisions: ask on Copilot (accepted risk, github/copilot-cli#3590); file tools checked as well as shell; kubectl/helm/flux/tofu/helmfile are allowlists.
  • The runtime is told from apm's deploy path; output shape per runtime. Fails closed on crash, malformed payload, oversize input (onFailure: block on Claude Code).
  • Own shell tokenizer: quotes, here-docs (bodies are data unless the delimiter is unquoted), substitutions, redirects, PowerShell mode; unwraps shells, eval, env/sudo/xargs/find -exec/… , git's shell-outs, rtk and project runners.
  • Known gaps (scripts the agent writes, python -c, MCP tools, unusual paths, cwd tracking) are listed in plugins/delivery/docs/guard-hook.md; the credential boundary covers them.

Also in this PR

  • delivery 0.2.0 and the delivery#0.2.0 grant in root apm.yml executables.allow.
  • check-executables-allow-sync generalised from kyberforge to every plugin that ships hooks or bin/ (bash 3.2 safe).
  • ADR-0032 amendment: Copilot ask as an accepted risk; file-tool coverage; allowlists.
  • Consumer docs: plugins/delivery/docs/guard-hook.md.

Verification

  • tests/test-delivery-guard-hook.sh: 521 cases across Claude, Copilot and Codex payloads (deny/ask/allow tables, bypass attempts, false-deny regressions, fail-closed payloads, perf caps, hooks.json invariants). Full pre-push stage passes.
  • Live on Claude Code 2.1.326: kubectl apply, bash -c "cat .vault_pass" and a Read of .vault_pass denied; ordinary commands ran.
  • apm 0.28.0 deploys the hook to Claude, Copilot and Codex paths (exec bit and onFailure kept) despite the spurious "parked" warning (#208).
  • Three review rounds by fresh-context subagents (factory-audit, adversarial bypass hunts, spec/standards code-review); all confirmed findings fixed with regression cases. Second commit carries the hardening.
  • Unverified: whether Copilot CLI accepts apm's nested hook shape (no account yet; documented).

Decisions worth a reviewer's eye

  • Denied beyond ADR-0032's list: pre-commit uninstall, HUSKY=0/LEFTHOOK=0, sops decryption, core.hooksPath, the admin ~/.kube/config, mkdir/chmod/ssh-keygen in ~/.ssh.
  • Commands over 256 KB or 2,000 words in one simple command are denied (Copilot fails open on a timeout).
  • tofu plan -destroy -out=x && tofu apply x asks rather than denies.

🤖 Generated with Claude Code

https://claude.ai/code/session_012CqgnwEGtmfsjcoG9QwByN

Builds the [delivery guard hook](https://git.rkdr.net/Mortis/Holocron/issues/190): the portable pre-tool-use guard from ADR-0032's enforcement layer 2. Refs #190 (left open; it closes with the wayfinder resolution after merge and a deploy check). ## What it does `plugins/delivery/.apm/hooks/guard.py` (stdlib-only Python) runs before every tool call in a delivery session: | Tier | Claude Code | Copilot CLI | Codex / unknown | |---|---|---|---| | Never (kubectl/helm/flux changes, `tofu destroy`/state, `--no-verify`/`SKIP=`/hooksPath/`.git/hooks` writes, vault and Sealed Secrets decryption, secret-file reads) | deny | deny | deny | | Odd job (`ansible-playbook` outside check mode, `tofu apply`, `flux reconcile`) | ask | ask | deny | | Read / Propose | allow | allow | allow | - Owner decisions: ask on Copilot (accepted risk, github/copilot-cli#3590); file tools checked as well as shell; kubectl/helm/flux/tofu/helmfile are allowlists. - The runtime is told from apm's deploy path; output shape per runtime. Fails closed on crash, malformed payload, oversize input (`onFailure: block` on Claude Code). - Own shell tokenizer: quotes, here-docs (bodies are data unless the delimiter is unquoted), substitutions, redirects, PowerShell mode; unwraps shells, `eval`, `env`/`sudo`/`xargs`/`find -exec`/… , git's shell-outs, `rtk` and project runners. - Known gaps (scripts the agent writes, `python -c`, MCP tools, unusual paths, cwd tracking) are listed in `plugins/delivery/docs/guard-hook.md`; the credential boundary covers them. ## Also in this PR - `delivery` 0.2.0 and the `delivery#0.2.0` grant in root `apm.yml` `executables.allow`. - `check-executables-allow-sync` generalised from kyberforge to every plugin that ships hooks or `bin/` (bash 3.2 safe). - ADR-0032 amendment: Copilot ask as an accepted risk; file-tool coverage; allowlists. - Consumer docs: `plugins/delivery/docs/guard-hook.md`. ## Verification - `tests/test-delivery-guard-hook.sh`: 521 cases across Claude, Copilot and Codex payloads (deny/ask/allow tables, bypass attempts, false-deny regressions, fail-closed payloads, perf caps, `hooks.json` invariants). Full pre-push stage passes. - Live on Claude Code 2.1.326: `kubectl apply`, `bash -c "cat .vault_pass"` and a Read of `.vault_pass` denied; ordinary commands ran. - apm 0.28.0 deploys the hook to Claude, Copilot and Codex paths (exec bit and `onFailure` kept) despite the spurious "parked" warning (#208). - Three review rounds by fresh-context subagents (factory-audit, adversarial bypass hunts, spec/standards code-review); all confirmed findings fixed with regression cases. Second commit carries the hardening. - **Unverified:** whether Copilot CLI accepts apm's nested hook shape (no account yet; documented). ## Decisions worth a reviewer's eye - Denied beyond ADR-0032's list: `pre-commit uninstall`, `HUSKY=0`/`LEFTHOOK=0`, `sops` decryption, `core.hooksPath`, the admin `~/.kube/config`, `mkdir`/`chmod`/`ssh-keygen` in `~/.ssh`. - Commands over 256 KB or 2,000 words in one simple command are denied (Copilot fails open on a timeout). - `tofu plan -destroy -out=x && tofu apply x` asks rather than denies. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_012CqgnwEGtmfsjcoG9QwByN
Claude added 2 commits 2026-11-23 13:59:31 +00:00
Ship one pre-tool-use hook in the delivery package. It denies the Never
tier on every runtime and asks before Odd jobs on Claude Code and Copilot
CLI (denying them on Codex and unknown runtimes):

- guard.py: stdlib-only Python; tokenises the shell line, unwraps
  bash -c / env / sudo / eval / xargs / find -exec / rtk / uv run and
  friends, checks file-tool paths for secret material, and uses read-verb
  allowlists for kubectl, helm, flux and tofu
- hooks.json: no matcher, onFailure: block, 10s timeout
- tests/test-delivery-guard-hook.sh: 281 cases incl. bypass attempts and
  per-runtime output shape
- root apm.yml: executables.allow delivery#0.1.0
- check-executables-allow-sync now covers every plugin shipping hooks/bin
- ADR-0032 amended: Copilot ask kept as an accepted risk, file tools and
  allowlists recorded

Refs #190

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012CqgnwEGtmfsjcoG9QwByN
Two fresh-context adversarial reviews and a spec/standards review of the
guard found bypasses, false denies and fail-open paths. Fixes:

- Substitution bodies are parsed by the tokenizer itself, so an
  apostrophe in a here-doc commit/PR body no longer hides later commands.
- Redirect operands never become words; every redirect target is checked
  for secret paths and .git/hooks.
- Unquoted here-doc substitutions are analysed; stdin shells (bash -,
  ssh host, pwsh) analyse their here-doc as a script.
- Option clusters ending in a value letter (bash -euo pipefail -c,
  sudo -iu root) consume their value.
- PowerShell mode: backtick escapes, iex, cmd /c, -EncodedCommand denied.
- Malformed payloads and unknown runtimes fail closed; runtime comes from
  the deploy path only.
- Linear-time parsing with size, word, command, depth and unwrap caps, so
  Copilot cannot time out (and fail open) on long input.
- Wider secret coverage (.ssh/.kube globs and dirs, id_* suffixes, sops
  age key, k3s/admin kubeconfigs, git-credentials, deploy keys, REV:path)
  and hook-bypass routes (git config alias, .git/hooks writes, find
  -delete, HUSKY=0, LEFTHOOK=0).
- Fewer false denies: commit/PR message values, find -name, search globs,
  jq/yq filters, pytest -k, git -S/--grep, ssh -i naming, non-admin
  kubeconfigs, quoted braces, option names, doc and test files.
- New allowlists: helmfile, terragrunt hcl/render, helm diff.
- delivery 0.2.0: a new hook changes compiled output (configure.md).
- ADR-0032: allowlists described as "no cluster or state change and no
  secret printed"; Write added to the file tools.

Refs #190

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012CqgnwEGtmfsjcoG9QwByN
Defame1297 merged commit 54a2d6a677 into main 2026-11-23 14:57:52 +00:00
Defame1297 deleted branch feat/delivery-guard-hook 2026-11-23 14:57:53 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Holocron#211