# 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` subcommands outside the allowlist, `helm`/`helm3` install/upgrade/uninstall/rollback, `helm get values/manifest/hooks/all`, `helm diff --show-secrets`, `helm secrets` and `helm-secrets` except `encrypt`, `helmfile` subcommands outside the allowlist, `flux suspend/delete/…`, `flux debug --show-values/--show-vars`, `tofu`/`terraform`/`terragrunt destroy`, `tofu apply -destroy`, `tofu init -migrate-state/-force-copy`, `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+=`, `HUSKY=0`, `LEFTHOOK=0`, `core.hooksPath` overrides (`-c`, `--config-env`, `GIT_CONFIG_*`, `git config`), git aliases carrying `--no-verify` or a guarded `!` command (`-c alias.…` or `git config alias.…`), `pre-commit uninstall`, writing, moving, removing or `chmod`-ing anything under `.git/hooks/`, `ansible-vault view/decrypt/edit/rekey`, `kubeseal --recovery-*`, `sops` in any decrypting mode (including bare `sops `), `gpg --export-secret-keys/-subkeys`, `kubectl get secret -o …`/`--template`, `kubectl get -k … -o …`, `kubectl get -f -o …`, `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`, `helmfile`, `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`, `helm diff upgrade/install/revision/rollback` (the read-only helm-diff plugin), `kubectl wait`, and `kubectl create … --dry-run=client` (which renders a Secret to pipe into `kubeseal`) all write local files or caches. When `--dry-run` is given twice, the last one counts. `helmfile` allows `template`, `lint`, `build`, `list`, `deps`, `repos`, `version`, `help`, `write-values` and `show-dag`. `terragrunt` follows the `tofu` rule and also allows its own HCL commands: `hclfmt`, `hcl fmt`, `hcl validate`, `render-json` and `render`. `--help` is always allowed. Secret paths are basename globs: `.vault_pass*`, `vault_pass*`, `*vault_password*` (and the `-` spellings), `*deploy_key*`, `*.tfstate`, `*.tfstate.*`, `id_rsa*`/`id_ed25519*`/`id_ecdsa*`/`id_dsa*`, `*.key`, `key.pem`, `*-key.pem`, `*_key.pem`, `privkey*.pem`, `*private*.pem`, `*.p12`, `*.pfx` and `.git-credentials*`. The undotted vault-password names and `*deploy_key*` also fit source and docs about them, so a doc or data suffix (`.yml`, `.yaml`, `.md`, `.rst`, `.j2`, `.adoc`, `.html`) exempts them. A code suffix (`.py`, `.sh`, …) exempts only a test file: `test_vault_password.py` is fine, but `vault_pass.py` and `vault-pass.sh` are the usual names of an Ansible vault-password script, which prints the password when run. `vault_password` and `.vault_pass.yml` are not exempt. git's `REV:path` form is checked by its path (`git show HEAD:.vault_pass`). Also covered: - the admin kubeconfig `.kube/config` and its backups (`config.*`, `config~`). Other kubeconfigs in `.kube`, such as `.kube/configs/stage-ro.yaml`, are fine. - the `.kube` and `.ssh` directories as a whole, and any glob inside them (`~/.ssh/id*`, `~/.kube/*`) - `sops/age/keys.txt`, `/etc/rancher/k3s/k3s.yaml` and `/etc/kubernetes/admin.conf` Any other shell glob with at least three literal characters (`.vault_pa*`) counts if it could expand to a secret name. Redirect targets are checked the same way for every program, so `echo x > terraform.tfstate` and `grep < ~/.ssh/id_rsa .` are denied. An option name is never a path: only its `=value` is checked. So `--vault-password-file` itself does not count, and `ansible-vault encrypt --vault-password-file ~/placeholder vars.yml` (the placeholder-password stage workflow) is allowed, while `--vault-password-file .vault_pass` is not. Exemptions: - `.pub` files. - The values of `--vault-password-file`, `--vault-id`, `--private-key` and `--key-file`, for `ansible-playbook`, `ansible` and `ansible-pull` only. Ansible reads those itself, and that run is already an Odd job. Elsewhere, `ansible-inventory` and `ansible-vault` included, the value is checked like any path. - SSH keys handed to ssh: `ssh -i`, `scp -i`, `sftp -i`, `ssh-add`, and the command in `GIT_SSH_COMMAND` or `core.sshCommand`, which the guard reads as a command. - 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. - The filter of `jq`, `gojq` and `yq` (`jq -r '.tls.key' x.json`), unless `-f` reads it from a file. The files they read are still checked. - The `-k`/`-m` test selector of `pytest` and `python -m pytest`. - File-name patterns: the values of `--include`, `--exclude`, `--exclude-dir`, `--glob`/`-g` and `--iglob` for the search tools, and of `find -name`/`-iname`/`-path`/`-ipath`/`-regex`. A directory `find` searches is still checked, so `find ~/.ssh -name id_rsa` is denied. A `find` that deletes or runs something (`-delete`, `-exec`, `-execdir`, `-ok`) loses this exemption, so `find . -name '*.tfstate' -delete` is denied, and so is any such `find` naming `.git/hooks`. - Prose: the `-m`/`--message` value of `git commit`, `tag`, `merge`, `notes`, `stash` and `revert`, and the title, body and notes values of `gh` and `tea`. `git commit -m x --no-verify` is still denied: only the secret-path check skips the message. The search values of `git log -S`/`-G`/`--grep` are skipped by the `--no-verify` check. - Programs that only print or stat their arguments: `echo`, `printf`, `ls`, `stat`, `test`. Their redirect targets are still checked. - `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, including `Move to`. Write and Edit under `.git/hooks/` are denied; reading there is fine. ## How it reads a command The script tokenises the shell line itself. It handles: - quoting and escapes, including backslash-newline and `$'…'` with escaped quotes - `;`, `&&`, `||`, `|`, newlines, subshells, braces, `function NAME { … }` and `((…))` - redirections, which never become words of the command. The fd number in `2>&1` is dropped, and the target is checked for secret paths and `.git/hooks/`. - here-documents. A body is data, so commit messages and PR bodies may name any command. Two exceptions: an unquoted delimiter's body (`<(…)` with the same rules as the rest of the line, so a quote or bracket in a here-document inside `$(…)` cannot hide what follows. Backtick bodies are parsed too. It also unwraps: - shells: `bash`/`sh`/… `-c` (including clusters such as `-lc`, `-o opt -c` and `-euo pipefail -c`), here-strings, here-documents, and a shell reading a pipe, which is denied when the line mentions a guarded tool. `bash <(…)`, `source <(…)`, `. <(…)` and a `-c` string holding a `$(…)` are built at run time, so they are denied when the line mentions a guarded tool. - `eval` (the same run-time rule), `trap 'cmd' SIG`, and leading `NAME=value` / `NAME+=value` assignments - `env` (including `-S`), `sudo`, `doas`, `su -c`, `script -c`, `flock`, `strace`, `systemd-run`, `chroot`, `busybox`, `k3s`, `microk8s` - `nohup`, `setsid`, `time`, `command` and `exec` (by basename, with their options) - `timeout`, `nice`, `xargs`, `parallel`, `watch`, `find -exec`, `ssh host ` - `nix-shell --run`, `nix shell/develop -c`, `tmux new-session/new-window/split-window` - git's own shell-outs: `rebase --exec`, `bisect run`, `submodule foreach` and `!` aliases - `rtk` (`rtk `, `rtk run`, `rtk proxy`) - project runners: `uv`/`poetry`/`pipenv`/`pdm`/`hatch`/`pipx run`, `uv tool run`, `uvx`, `direnv exec`, `mise exec`, `devbox run`, and secret injectors `aws-vault exec`, `op run` and `doppler run` In an option cluster, a last letter that takes a value consumes the next word, for shells and for every wrapper: `sudo -iu root kubectl …` runs `kubectl`, not `root`. It takes the program's basename and drops a `.exe` suffix, so `/usr/bin/kubectl`, `\kubectl` and `kubectl.exe` are all `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 an unquoted brace expansion hides a guarded command. A guarded tool counts only as a whole name, so `.pre-commit-config.yaml` is not `pre-commit`. The PowerShell tool gets a PowerShell mode of the same tokenizer, not a full PowerShell parser. The backtick is the escape and line-continuation character, backslash is a plain path separator, `''` escapes a single quote, and `@'…'@` here-strings are read. Program names are matched without case. It unwraps `iex`/`Invoke-Expression`, `cmd /c` and `pwsh`/`powershell -Command`, and denies `-EncodedCommand`. Copilot lets a timed-out hook's call through, so the guard refuses input it cannot read well inside the 10-second budget. It denies outright a command longer than 256 KB, a simple command of more than 2,000 words, a line of more than 5,000 commands, substitutions nested more than 40 deep, and more than 32 wrappers inside one another (`sudo sudo …`, `find -exec find -exec …`). Each of these finishes well under a second. **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 - file names produced at run time, such as `ls ~/.ssh/id_* | xargs cat`: `xargs` reads its arguments from the pipe, which the guard cannot see - a shell reading a pipe when the line mentions no guarded tool, such as `curl … | bash` - 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: odd jobs are denied, with exit 2. The payload's shape (`toolName` or `tool_name`) picks only how the payload is read, never the runtime. | ## 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), PowerShell, the per-runtime output shape, and fail-closed handling of malformed payloads. A malformed payload is denied: not JSON or not an object, no tool name, a shell tool whose input is not an object or carries no command string (read from `command`, `cmd`, `input` or `chars`), and Copilot `toolArgs` that is not an object. It also covers the time the guard takes on pathological input and the `hooks.json` invariants.