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

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
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-11-23 08:15:14 +00:00
1 parent 6edd66432c
commit a6ff1c14d4
11 files changed
+2244 -110

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
# Guard hook
`delivery` ships one pre-tool-use hook, `.apm/hooks/guard.py`. It is ADR-0032's enforcement layer
2, a guardrail above the credential boundary and not the boundary itself. The decisions behind it are
in the `delivery guard hook` ticket (holocron#190) and ADR-0032's 2026-11-23 amendment.
## What it decides
| Tier | Examples | Claude Code | Copilot CLI | Codex / unknown |
|---|---|---|---|---|
| Never | `kubectl`/`kubecolor` verbs outside the read allowlist, `helm`/`helm3` install/upgrade/uninstall/rollback, `helm get values/manifest/all`, `flux suspend/delete/…`, `flux debug --show-values`, `tofu`/`terraform`/`terragrunt destroy`, `tofu apply -destroy`, `tofu state` (except `list`), `tofu show` without a plan file or with `-json`, `tofu output -json/-raw`, `git … --no-verify` (and its abbreviations), `git commit -n`, `SKIP=`/`SKIP+=`, `core.hooksPath` overrides (`-c`, `--config-env`, `GIT_CONFIG_*`, `git config`), git aliases carrying `--no-verify`, `pre-commit uninstall`, `ansible-vault view/decrypt/edit/rekey`, `kubeseal --recovery-*`, `sops` in any decrypting mode (including bare `sops <file>`), `kubectl get secret -o …`/`--template`, `kubectl get --raw …secrets…`, `kubectl config view --raw`, any mention of a secret path | deny | deny | deny |
| Odd job | `ansible-playbook` / ad-hoc `ansible` / `ansible-pull` without `--check`, `ansible-console`, `tofu apply/import/refresh/test`, `flux reconcile` | ask | ask | deny |
| Read / Propose | everything else | allow | allow | allow |
`kubectl`, `helm`, `flux` and `tofu` are **allowlists**: a subcommand the guard does not list as
allowed is denied. Widen the allowlist in `guard.py` and its test table together. The allowed sets
are "no cluster or state change and no secret printed", not strictly read-only. `helm pull`,
`package`, `create` and `repo add/update`, `tofu init`, `plan` and `get`, `kubectl wait`, and
`kubectl create … --dry-run=client` (which renders a Secret to pipe into `kubeseal`) all write local
files or caches. `--help` is always allowed.
Secret paths are basename globs: `.vault_pass*`, `vault_pass*`, `*vault_password*` (and the `-`
spellings), `*.tfstate`, `*.tfstate.*`, `id_rsa`/`id_ed25519`/`id_ecdsa`/`id_dsa`, `*.key`,
`key.pem`, `*-key.pem`, `*_key.pem`, `privkey*.pem`, `*private*.pem`, `*.p12` and `*.pfx`. Also
covered: anything under `.kube/config` (backups included) and the `.kube` directory itself. A shell
glob with at least three literal characters (`.vault_pa*`) counts if it could expand to one of
these.
Exemptions:
- `.pub` files.
- The values of `--vault-password-file`, `--vault-id`, `--private-key` and `--key-file`. Ansible
reads those itself, and that run is already an Odd job.
- `ssh -i`, for the same reason.
- The search pattern of `grep`/`rg`/`ag`/`ack`/`git grep`, wherever the program sits after
unwrapping (so `rtk grep vault_pass ansible.cfg` is allowed). The exemption doesn't apply once
`-e` or `-f` makes the pattern explicit.
- Programs that only print or stat their arguments: `echo`, `printf`, `ls`, `stat`, `test`.
- `git rm`, `check-ignore`, `ls-files` and `status`.
Pointing at the admin `~/.kube/config` is denied on purpose: stage sessions use the read-only
kubeconfig. File tools are checked too: Read, Grep, Edit and Write paths, Copilot's `view`, and
Codex's `apply_patch` targets.
## How it reads a command
The script tokenises the shell line itself. It handles:
- quoting and escapes, including backslash-newline
- `;`, `&&`, `||`, `|`, newlines, subshells and braces
- here-documents, whose bodies are treated as data, not commands, so commit messages and PR bodies
may name any command
It analyses the inner text of `$(…)`, backticks, `<(…)` and `>(…)` as further commands. It also
unwraps:
- shells: `bash`/`sh`/… `-c` (including clusters such as `-lc` and `-o opt -c`), here-strings, and
a shell reading stdin, which is denied when the line mentions a guarded tool
- `eval`, and leading `NAME=value` / `NAME+=value` assignments
- `env` (including `-S`), `sudo`, `doas`, `su -c`, `script -c`, `flock`, `strace`, `systemd-run`,
`busybox`
- `nohup`, `setsid`, `time`, `command` and `exec` (by basename, with their options)
- `timeout`, `nice`, `xargs`, `parallel`, `watch`, `find -exec`, `ssh host <cmd>`
- git's own shell-outs: `rebase --exec`, `bisect run`, `submodule foreach` and `!` aliases
- `rtk` (`rtk <tool>`, `rtk run`, `rtk proxy`)
- project runners: `uv`/`poetry`/`pipenv`/`pdm`/`hatch`/`pipx run`, `uv tool run`, `uvx`,
`direnv exec`, `mise exec`, `devbox run`
It takes the program's basename, so `/usr/bin/kubectl` and `\kubectl` are both `kubectl`. It
denies, rather than guesses, when a program name is built at run time (`$k apply`) and the line
mentions a guarded tool, when the program name is a glob, or when brace expansion hides a guarded
command.
**Known gaps, by construction:**
- a script the agent writes and then runs
- `python -c` or `node -e` subprocesses
- MCP tools, which carry no shell command
- secret files at an unusual path
- `cd ~/.kube && cat config`, because the guard does not track the working directory
- what an allowed command prints at run time, such as `ansible -C -m debug` of a vaulted variable
The credential boundary covers all of these: the read-only kubeconfig, no vault password and no
apply credentials in stage sessions.
## Runtimes
The script tells runtimes apart by where apm deployed it:
| Deployed under | Runtime | Notes |
|---|---|---|
| `.claude/hooks/` | Claude Code | Output is `hookSpecificOutput.permissionDecision`. `onFailure: block` makes a crash or timeout block the call (Claude Code ≥ 2.1.295). Verified live on 2.1.326. |
| `.github/hooks/scripts/` | Copilot CLI | Output is top-level `permissionDecision` plus a reason. A non-zero exit denies, but a **timeout lets the call through**. The TUI auto-approves `ask` (ADR-0032 accepted risk). apm writes the entry in Claude's nested shape with `command`/`timeout`, and Copilot's docs accept both keys but don't document the nested group, so it is **unverified** until a Copilot account exists. |
| `.codex/hooks/` | Codex | `ask` is unsupported, so the guard denies with exit 2 and the reason on stderr. apm deploys only if `.codex/` already exists, and Codex skips project hooks until a human trusts them. Codex resolves the relative command from the session cwd, so a session started in a subdirectory may not find the script. |
| anywhere else | unknown | Treated like Codex. |
## What a consuming repo needs
- `python3` on PATH. The script uses the standard library only. Without python3 the hook cannot
start, which blocks every tool call on Claude Code and Copilot. That's deliberate.
- An `executables.allow` grant for the package in the repo's `apm.yml`, when that repo turns on apm's
executable-trust gate:
```yaml
executables:
allow:
delivery:
hooks: true
```
apm 0.28.0 prints "parked pending approval" even when the grant matches (holocron#208). Check that
the hook actually deployed by looking for `guard.py` under `.claude/hooks/delivery/` and the
`PreToolUse` entry in `.claude/settings.json`. Don't rely on the install exit code.
- Host hardening is documented, not shipped (ADR-0032 layer 3). Examples are Claude permission
deny rules, sandbox `denyRead` on the vault password file, and managed settings with
`allowManagedHooksOnly` so the guard cannot be switched off.
## Tests
`tests/test-delivery-guard-hook.sh` copies the script to each runtime's deploy path and drives it
with Claude, Copilot and Codex payloads. It covers the deny, ask and allow tables, bypass attempts
(quoting, `bash -c`, `env`, `sudo`, `xargs`, `find -exec`, substitution, `eval`, dynamic program
names), the per-runtime output shape, fail-closed handling of malformed payloads, and the
`hooks.json` invariants.