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
449 lines
18 KiB
Bash
Executable File
449 lines
18 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Tests for plugins/delivery/.apm/hooks/guard.py, the delivery guard hook
|
|
# (ADR-0032 enforcement layer 2): deny the Never tier everywhere, ask before
|
|
# Odd jobs where the runtime honours ask, allow the rest.
|
|
#
|
|
# The hook tells runtimes apart by where apm deployed it, so each runtime is
|
|
# exercised by copying the script to that runtime's deploy path.
|
|
set -euo pipefail
|
|
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
SRC="$REPO_ROOT/plugins/delivery/.apm/hooks/guard.py"
|
|
HOOKS_JSON="$REPO_ROOT/plugins/delivery/.apm/hooks/hooks.json"
|
|
PASS=0
|
|
FAIL=0
|
|
|
|
pass() { PASS=$((PASS + 1)); }
|
|
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
|
|
|
|
command -v python3 > /dev/null 2>&1 || { echo "python3 required"; exit 77; }
|
|
|
|
WORK="$(mktemp -d)"
|
|
trap 'rm -rf "$WORK"' EXIT
|
|
for d in .claude/hooks/delivery/.apm/hooks .github/hooks/scripts/delivery/.apm/hooks \
|
|
.codex/hooks/delivery/.apm/hooks elsewhere; do
|
|
mkdir -p "$WORK/$d"
|
|
cp "$SRC" "$WORK/$d/guard.py"
|
|
done
|
|
CLAUDE="$WORK/.claude/hooks/delivery/.apm/hooks/guard.py"
|
|
COPILOT="$WORK/.github/hooks/scripts/delivery/.apm/hooks/guard.py"
|
|
CODEX="$WORK/.codex/hooks/delivery/.apm/hooks/guard.py"
|
|
UNKNOWN="$WORK/elsewhere/guard.py"
|
|
|
|
# decision <script> <payload-json> -> prints allow|ask|deny
|
|
decision() {
|
|
local script="$1" payload="$2" out rc
|
|
set +e
|
|
out="$(printf '%s' "$payload" | python3 "$script" 2> /dev/null)"
|
|
rc=$?
|
|
set -e
|
|
if [[ $rc -eq 2 ]]; then
|
|
echo deny
|
|
return
|
|
fi
|
|
if [[ $rc -ne 0 ]]; then
|
|
echo "error($rc)"
|
|
return
|
|
fi
|
|
if [[ -z "$out" ]]; then
|
|
echo allow
|
|
return
|
|
fi
|
|
python3 -c '
|
|
import json, sys
|
|
d = json.loads(sys.argv[1])
|
|
print(d.get("permissionDecision") or d["hookSpecificOutput"]["permissionDecision"])
|
|
' "$out"
|
|
}
|
|
|
|
claude_bash() {
|
|
python3 -c 'import json,sys; print(json.dumps({"hook_event_name":"PreToolUse","tool_name":"Bash","tool_input":{"command":sys.argv[1]}}))' "$1"
|
|
}
|
|
|
|
expect_claude() {
|
|
local want="$1" cmd="$2" got
|
|
got="$(decision "$CLAUDE" "$(claude_bash "$cmd")")"
|
|
if [[ "$got" == "$want" ]]; then pass; else fail "claude: want $want, got $got: $cmd"; fi
|
|
}
|
|
|
|
echo "== Shell commands on Claude Code"
|
|
|
|
# Each line: expected<TAB>command
|
|
while IFS=$'\t' read -r want cmd; do
|
|
[[ -z "$want" || "$want" == \#* ]] && continue
|
|
expect_claude "$want" "$cmd"
|
|
done << 'CASES'
|
|
# --- Read tier: allowed
|
|
allow ls -la
|
|
allow git status && git diff --stat
|
|
allow kubectl get pods -A
|
|
allow kubectl -n flux-system get kustomizations
|
|
allow kubectl describe secret db-creds
|
|
allow kubectl get secrets
|
|
allow kubectl get secrets -o name
|
|
allow kubectl logs deploy/app -c main --tail=50
|
|
allow kubectl config view
|
|
allow kubectl auth can-i get pods
|
|
allow kubectl rollout status deploy/app
|
|
allow kubectl kustomize clusters/home
|
|
allow helm template charts/app
|
|
allow helm list -A
|
|
allow helm get notes app
|
|
allow flux get kustomizations -A
|
|
allow flux build kustomization apps --path ./clusters/home --dry-run
|
|
allow flux create source git x --url=https://example.com --export
|
|
allow tofu plan -out=plan.tfplan
|
|
allow tofu -chdir=infra validate
|
|
allow tofu state list
|
|
allow tofu show plan.tfplan
|
|
allow tofu output
|
|
allow ansible-playbook site.yml --check --diff
|
|
allow ansible-playbook -C site.yml
|
|
allow ansible-playbook -vC site.yml
|
|
allow ansible-playbook site.yml --syntax-check
|
|
allow ansible-playbook site.yml --list-tasks
|
|
allow ansible-lint playbooks/
|
|
allow ansible-inventory --graph
|
|
allow ansible-vault encrypt_string --stdin-name x
|
|
allow kubeseal --cert pub-cert.pem -o yaml < secret.yaml
|
|
allow git commit -m "fix: rename id_rsa handling"
|
|
allow git commit -am "msg"
|
|
allow git commit -m "-n is fine inside a message"
|
|
allow git push -n origin feat/x
|
|
allow git push origin feat/x
|
|
allow grep -rn vault_password_file ansible.cfg
|
|
allow echo "do not run kubectl apply here"
|
|
allow ssh-keygen -y -f /dev/null; cat id_rsa.pub
|
|
allow molecule test
|
|
allow command -v kubectl
|
|
allow pre-commit run --all-files
|
|
allow "$PYTHON" script.py && git log
|
|
# --- Never tier: direct cluster mutation
|
|
deny kubectl apply -f deploy.yaml
|
|
deny kubectl -n prod delete pod x
|
|
deny kubectl edit deploy/app
|
|
deny kubectl patch deploy app -p '{}'
|
|
deny kubectl scale deploy/app --replicas=0
|
|
deny kubectl rollout restart deploy/app
|
|
deny kubectl label node n1 a=b
|
|
deny kubectl exec -it pod -- sh
|
|
deny kubectl create secret generic x --from-literal=a=b
|
|
deny kubectl diff -f deploy.yaml
|
|
deny helm install app charts/app
|
|
deny helm upgrade --install app charts/app
|
|
deny helm uninstall app
|
|
deny helm rollback app 1
|
|
deny helm get values app
|
|
deny flux suspend kustomization apps
|
|
deny flux delete source git x
|
|
deny flux export source git --all --with-credentials
|
|
# --- Never tier: tofu
|
|
deny tofu destroy
|
|
deny terraform destroy -auto-approve
|
|
deny tofu apply -destroy
|
|
deny tofu state rm aws_instance.x
|
|
deny tofu state mv a b
|
|
deny tofu state pull
|
|
deny tofu state show aws_instance.x
|
|
deny tofu show
|
|
deny tofu output -json
|
|
deny tofu taint aws_instance.x
|
|
# --- Never tier: hook bypass
|
|
deny git commit --no-verify -m x
|
|
deny git commit -n -m x
|
|
deny git commit -nm x
|
|
deny git push --no-verify
|
|
deny SKIP=ansible-lint git commit -m x
|
|
deny export SKIP=all
|
|
deny git -c core.hooksPath=/dev/null commit -m x
|
|
deny git config core.hooksPath /tmp/none
|
|
# --- Never tier: secrets
|
|
deny ansible-vault view group_vars/all/vault.yml
|
|
deny ansible-vault decrypt group_vars/all/vault.yml
|
|
deny ansible-vault edit group_vars/all/vault.yml
|
|
deny ansible-vault rekey vault.yml
|
|
deny kubeseal --recovery-unseal --recovery-private-key key.pem < s.yaml
|
|
deny sops -d secrets.enc.yaml
|
|
deny kubectl get secret db-creds -o yaml
|
|
deny kubectl get secrets -n kube-system -ojsonpath='{.items}'
|
|
deny kubectl get secret/db-creds --output=json
|
|
deny kubectl config view --raw
|
|
deny cat .vault_pass
|
|
deny cat ~/.ansible/vault_password.txt
|
|
deny head -1 /root/.ssh/id_ed25519
|
|
deny less terraform.tfstate
|
|
deny cp terraform.tfstate.backup /tmp/x
|
|
deny cat ~/.kube/config
|
|
deny base64 < .vault_pass
|
|
deny git add .vault_pass
|
|
deny cat tls.key
|
|
# --- Odd jobs: ask
|
|
ask ansible-playbook site.yml
|
|
ask ansible-playbook -i inventory/prod site.yml --limit web
|
|
ask ansible-playbook site.yml --vault-password-file .vault_pass
|
|
ask ansible-playbook site.yml --private-key ~/.ssh/id_ed25519
|
|
ask ansible all -m shell -a uptime
|
|
ask tofu apply plan.tfplan
|
|
ask terraform apply -auto-approve
|
|
ask flux reconcile kustomization apps --with-source
|
|
# --- Bypass attempts
|
|
deny bash -c "kubectl apply -f x.yaml"
|
|
deny sh -lc 'kubectl delete ns prod'
|
|
deny bash -c "bash -c 'helm uninstall app'"
|
|
deny env kubectl apply -f x
|
|
deny env -i PATH=/usr/bin kubectl apply -f x
|
|
deny env -S "kubectl apply -f x"
|
|
deny FOO=1 kubectl apply -f x
|
|
deny /usr/local/bin/kubectl apply -f x
|
|
deny \kubectl apply -f x
|
|
deny "kubectl" apply -f x
|
|
deny k'ube'ctl apply -f x
|
|
deny ku\bectl apply -f x
|
|
deny sudo -u root kubectl apply -f x
|
|
deny sudo -E helm upgrade app charts/app
|
|
deny nohup kubectl delete pod x &
|
|
deny timeout 30 kubectl apply -f x
|
|
deny nice -n 10 tofu destroy
|
|
deny command kubectl apply -f x
|
|
deny exec kubectl apply -f x
|
|
deny echo x | xargs kubectl delete pod
|
|
deny find . -name '*.yaml' -exec kubectl apply -f {} \;
|
|
deny true && kubectl apply -f x
|
|
deny false || kubectl apply -f x
|
|
deny ls; kubectl apply -f x
|
|
deny ls | kubectl apply -f -
|
|
deny (kubectl apply -f x)
|
|
deny { kubectl apply -f x; }
|
|
deny if true; then kubectl apply -f x; fi
|
|
deny echo $(kubectl delete pod x)
|
|
deny echo "$(ansible-vault view v.yml)"
|
|
deny echo `kubectl apply -f x`
|
|
deny diff <(kubectl get secret x -o yaml) a.yaml
|
|
deny eval "kubectl apply -f x"
|
|
deny eval kubectl delete pod x
|
|
deny k=kubectl; $k apply -f x
|
|
deny "$(which kubectl)" apply -f x
|
|
deny watch -n 5 kubectl delete pod x
|
|
deny kubectl --context prod apply -f x
|
|
deny kubectl --kubeconfig=/tmp/k apply -f x
|
|
deny helm -n prod upgrade app charts/app
|
|
deny git -C /repo commit --no-verify -m x
|
|
deny env SKIP=ansible-lint git commit -m x
|
|
deny cat .vault_pass | base64
|
|
deny cat .vault""_pass
|
|
deny $'kubectl' apply -f x
|
|
ask bash -c 'ansible-playbook site.yml'
|
|
ask sudo ansible-playbook site.yml
|
|
# --- Runners: rtk (mandated in some repos) and project-venv runners
|
|
allow rtk git status
|
|
allow rtk kubectl get pods
|
|
deny rtk kubectl apply -f x
|
|
deny rtk git commit --no-verify -m x
|
|
deny rtk run "kubectl delete pod x"
|
|
deny rtk proxy helm upgrade app charts/app
|
|
deny rtk read .vault_pass
|
|
allow uv run ansible-playbook site.yml --check
|
|
ask uv run ansible-playbook site.yml
|
|
ask poetry run ansible-playbook site.yml
|
|
deny uv run --with x ansible-vault view v.yml
|
|
ask direnv exec . ansible-playbook site.yml
|
|
allow uv sync
|
|
# --- Regressions from the factory-audit review: bypasses that got allow
|
|
deny git commit --no-verif -m x
|
|
deny git commit --no-ve -m x
|
|
deny git --config-env=core.hooksPath=HP commit -m x
|
|
deny GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.hooksPath GIT_CONFIG_VALUE_0=/dev/null git commit -m x
|
|
deny git -c alias.c='commit --no-verify' c -m x
|
|
deny git -c alias.k='!kubectl delete pod x' k
|
|
deny SKIP+=lint git commit -m x
|
|
deny grep .vault_pass -e ''
|
|
deny rg .vault_pass -e ''
|
|
deny grep -f .vault_pass ansible.cfg
|
|
deny cat .vault_pa*
|
|
deny cat ~/.kube/conf*
|
|
deny cat ~/.kube/config.bak
|
|
deny cp -r ~/.kube /tmp/k
|
|
deny kubectl get --raw /api/v1/namespaces/default/secrets/db
|
|
deny kubectl get secret db --template={{.data}}
|
|
deny kubectl get secrets.v1. db -o yaml
|
|
deny kubectl --username get delete pod x
|
|
deny kubectl --password p delete pod x
|
|
deny bash -o pipefail -c 'kubectl delete pod x'
|
|
deny bash -O extglob -c 'kubectl delete pod x'
|
|
deny echo 'kubectl delete pod x' | bash
|
|
deny bash <<< 'kubectl delete pod x'
|
|
deny script -qc 'kubectl delete pod x' /dev/null
|
|
deny su -lc 'kubectl delete pod x'
|
|
deny /usr/bin/time kubectl delete pod x
|
|
deny /usr/bin/time -f %e kubectl delete pod x
|
|
deny /usr/bin/nohup kubectl delete pod x
|
|
deny setsid -f kubectl delete pod x
|
|
deny nohup -- kubectl delete pod x
|
|
deny {kubectl,delete,pod,x}
|
|
deny git commit {--no-verify,-m,x}
|
|
deny /usr/bin/kube?tl delete pod x
|
|
deny coproc X { kubectl delete pod y; }
|
|
deny kubecolor delete pod x
|
|
deny helm3 uninstall rel
|
|
deny terragrunt destroy
|
|
deny terragrunt run-all destroy
|
|
deny parallel kubectl delete pod ::: x
|
|
deny ssh host kubectl delete pod x
|
|
deny git rebase --exec 'kubectl delete pod x' HEAD~1
|
|
deny git rebase -x 'kubectl delete pod x' HEAD~1
|
|
deny git bisect run kubectl delete pod x
|
|
deny git submodule foreach 'kubectl delete pod x'
|
|
ask uvx --from ansible-core ansible-playbook site.yml
|
|
ask uv tool run ansible-playbook site.yml
|
|
deny EDITOR=cat sops secrets.enc.yaml
|
|
deny flux debug hr app --show-values
|
|
deny tofu show -json plan.out
|
|
deny pre-commit uninstall
|
|
deny xargs -a .vault_pass echo
|
|
# --- Regressions from the review: read work that was wrongly denied
|
|
allow rtk grep vault_pass ansible.cfg
|
|
allow git grep vault_pass
|
|
allow git grep -n -- --no-verify
|
|
allow echo '*.tfstate' >> .gitignore
|
|
allow git rm --cached .vault_pass
|
|
allow ls -la .vault_pass
|
|
allow git config core.hooksPath
|
|
allow git config --get core.hooksPath
|
|
allow kubectl apply --help
|
|
allow cat monkey.pem
|
|
allow kubectl create configmap x --from-file=a.txt --dry-run=client -o yaml
|
|
allow kubectl create secret generic x --from-literal=a=b --dry-run=client -o yaml
|
|
allow ssh -i ~/.ssh/id_ed25519 host uptime
|
|
allow grep -A 3 vault_pass ansible.cfg
|
|
allow sops --encrypt secrets.yaml
|
|
allow sops updatekeys secrets.enc.yaml
|
|
allow wc -l *
|
|
allow flux debug hr app
|
|
# --- Kept on purpose: the admin kubeconfig is not the stage session's
|
|
deny export KUBECONFIG=~/.kube/config
|
|
deny kubectl --kubeconfig ~/.kube/config get pods
|
|
CASES
|
|
|
|
# Multi-line commands do not survive the line-per-case table above; test them
|
|
# directly.
|
|
expect_claude deny $'ls\nkubectl apply -f x'
|
|
expect_claude deny $'cat <<EOF | sh\nkubectl apply -f x\nEOF'
|
|
expect_claude deny $'ls \\\n && kubectl apply -f x'
|
|
expect_claude deny $'bash <<EOF\nkubectl delete pod x\nEOF'
|
|
expect_claude deny $'kube\\\nctl delete pod x'
|
|
# Here-doc bodies are data: commit messages and PR bodies may name commands.
|
|
expect_claude allow $'git commit -F- <<\'EOF\'\nkubectl apply is denied by the guard\nEOF'
|
|
expect_claude allow $'gh pr create --title t --body "$(cat <<\'EOF\'\nhelm uninstall foo is blocked; don\'t worry\nEOF\n)"'
|
|
expect_claude allow $'cat > notes.md <<\'EOF\'\nhelm uninstall foo\nEOF'
|
|
expect_claude allow $'cat <<-EOF > x\n\tkubectl delete pod x\n\tEOF\nls'
|
|
# ...but the command after the here-doc is still checked.
|
|
expect_claude deny $'cat > notes.md <<EOF\nhello\nEOF\nkubectl apply -f x'
|
|
|
|
echo "== File tools"
|
|
file_case() {
|
|
local want="$1" script="$2" payload="$3" got
|
|
got="$(decision "$script" "$payload")"
|
|
if [[ "$got" == "$want" ]]; then pass; else fail "file tool: want $want, got $got: $payload"; fi
|
|
}
|
|
file_case deny "$CLAUDE" '{"tool_name":"Read","tool_input":{"file_path":"/repo/.vault_pass"}}'
|
|
file_case deny "$CLAUDE" '{"tool_name":"Read","tool_input":{"file_path":"/root/.ssh/id_ed25519"}}'
|
|
file_case deny "$CLAUDE" '{"tool_name":"Read","tool_input":{"file_path":"/repo/infra/terraform.tfstate"}}'
|
|
file_case deny "$CLAUDE" '{"tool_name":"Read","tool_input":{"file_path":"/root/.kube/config"}}'
|
|
file_case deny "$CLAUDE" '{"tool_name":"Grep","tool_input":{"pattern":".","path":"/repo/.vault_pass"}}'
|
|
file_case deny "$CLAUDE" '{"tool_name":"Grep","tool_input":{"pattern":".","path":"/repo","glob":"*.tfstate"}}'
|
|
file_case deny "$CLAUDE" '{"tool_name":"Edit","tool_input":{"file_path":"/repo/terraform.tfstate","old_string":"a","new_string":"b"}}'
|
|
file_case allow "$CLAUDE" '{"tool_name":"Read","tool_input":{"file_path":"/repo/ansible.cfg"}}'
|
|
file_case allow "$CLAUDE" '{"tool_name":"Read","tool_input":{"file_path":"/root/.ssh/id_ed25519.pub"}}'
|
|
file_case allow "$CLAUDE" '{"tool_name":"Glob","tool_input":{"pattern":"**/*.yml"}}'
|
|
file_case allow "$CLAUDE" '{"tool_name":"mcp__gitea__issue_read","tool_input":{"method":"get"}}'
|
|
file_case deny "$COPILOT" '{"toolName":"view","toolArgs":"{\"path\":\"/repo/.vault_pass\"}"}'
|
|
file_case allow "$COPILOT" '{"toolName":"view","toolArgs":"{\"path\":\"/repo/README.md\"}"}'
|
|
file_case deny "$CODEX" '{"tool_name":"apply_patch","tool_input":{"command":"*** Begin Patch\n*** Update File: infra/terraform.tfstate\n*** End Patch"}}'
|
|
file_case allow "$CODEX" '{"tool_name":"apply_patch","tool_input":{"command":"*** Begin Patch\n*** Update File: README.md\n*** End Patch"}}'
|
|
|
|
echo "== Per-runtime output"
|
|
runtime_case() {
|
|
local label="$1" want="$2" script="$3" payload="$4" got
|
|
got="$(decision "$script" "$payload")"
|
|
if [[ "$got" == "$want" ]]; then pass; else fail "$label: want $want, got $got"; fi
|
|
}
|
|
ODD_CLAUDE="$(claude_bash 'ansible-playbook site.yml')"
|
|
NEVER_CLAUDE="$(claude_bash 'kubectl apply -f x')"
|
|
ODD_COPILOT='{"toolName":"bash","toolArgs":"{\"command\":\"ansible-playbook site.yml\"}"}'
|
|
NEVER_COPILOT='{"toolName":"bash","toolArgs":"{\"command\":\"kubectl apply -f x\"}"}'
|
|
OK_COPILOT='{"toolName":"bash","toolArgs":"{\"command\":\"kubectl get pods\"}"}'
|
|
runtime_case "claude odd job asks" ask "$CLAUDE" "$ODD_CLAUDE"
|
|
runtime_case "copilot odd job asks" ask "$COPILOT" "$ODD_COPILOT"
|
|
runtime_case "copilot never denies" deny "$COPILOT" "$NEVER_COPILOT"
|
|
runtime_case "copilot read allows" allow "$COPILOT" "$OK_COPILOT"
|
|
runtime_case "codex odd job denies (no ask)" deny "$CODEX" "$ODD_CLAUDE"
|
|
runtime_case "codex never denies" deny "$CODEX" "$NEVER_CLAUDE"
|
|
runtime_case "unknown runtime odd job denies" deny "$UNKNOWN" "$ODD_CLAUDE"
|
|
runtime_case "camelCase payload outside a deploy path is Copilot" ask "$UNKNOWN" "$ODD_COPILOT"
|
|
|
|
# Shape checks: Claude needs hookSpecificOutput with hookEventName; Copilot
|
|
# needs a reason with every deny; Codex needs exit 2 with the reason on stderr.
|
|
out="$(claude_bash 'kubectl apply -f x' | python3 "$CLAUDE")"
|
|
if python3 -c '
|
|
import json, sys
|
|
h = json.loads(sys.argv[1])["hookSpecificOutput"]
|
|
assert h["hookEventName"] == "PreToolUse" and h["permissionDecision"] == "deny"
|
|
assert "ADR-0032" in h["permissionDecisionReason"]
|
|
' "$out"; then pass; else fail "claude deny output shape: $out"; fi
|
|
out="$(printf '%s' "$NEVER_COPILOT" | python3 "$COPILOT")"
|
|
if python3 -c '
|
|
import json, sys
|
|
d = json.loads(sys.argv[1])
|
|
assert d["permissionDecision"] == "deny" and d["permissionDecisionReason"]
|
|
' "$out"; then pass; else fail "copilot deny output shape: $out"; fi
|
|
set +e
|
|
err="$(claude_bash 'kubectl apply -f x' | python3 "$CODEX" 2>&1 > /dev/null)"
|
|
rc=$?
|
|
set -e
|
|
if [[ $rc -eq 2 && "$err" == *ADR-0032* ]]; then pass; else fail "codex deny: rc=$rc stderr=$err"; fi
|
|
|
|
echo "== Fail closed"
|
|
runtime_case "malformed payload denies on claude" deny "$CLAUDE" '{not json'
|
|
runtime_case "malformed payload denies on copilot" deny "$COPILOT" '{not json'
|
|
runtime_case "malformed payload denies on codex" deny "$CODEX" '{not json'
|
|
|
|
echo "== Hook definition"
|
|
if python3 - "$HOOKS_JSON" << 'PY'
|
|
import json, sys
|
|
doc = json.load(open(sys.argv[1]))
|
|
groups = doc["hooks"]["PreToolUse"]
|
|
assert len(groups) == 1, groups
|
|
# No matcher: the script dispatches on tool name itself, because matchers are
|
|
# written verbatim to every target and the shell tool is named differently on
|
|
# each runtime.
|
|
assert "matcher" not in groups[0], groups[0]
|
|
(h,) = groups[0]["hooks"]
|
|
# The .apm/ path is the one that resolves after install (kyberforge docs/hooks.md).
|
|
assert h["command"] == "${CLAUDE_PLUGIN_ROOT}/.apm/hooks/guard.py", h
|
|
# Claude Code fails open on a hook error unless told otherwise.
|
|
assert h["onFailure"] == "block", h
|
|
# Copilot lets a timed-out hook's call through; keep the budget short but sane.
|
|
assert 1 <= h["timeout"] <= 30, h
|
|
PY
|
|
then pass; else fail "hooks.json shape"; fi
|
|
if [[ -x "$SRC" ]]; then pass; else fail "guard.py is not executable"; fi
|
|
if head -1 "$SRC" | grep -qx '#!/usr/bin/env python3'; then pass; else fail "guard.py shebang"; fi
|
|
# Standard library only: a missing dependency would stop every tool call.
|
|
if python3 - "$SRC" << 'PY'
|
|
import ast, sys
|
|
tree = ast.parse(open(sys.argv[1]).read())
|
|
mods = set()
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Import):
|
|
mods.update(a.name.split(".")[0] for a in node.names)
|
|
elif isinstance(node, ast.ImportFrom):
|
|
mods.add(node.module.split(".")[0])
|
|
extra = mods - {"fnmatch", "json", "os", "re", "sys"}
|
|
assert not extra, extra
|
|
PY
|
|
then pass; else fail "guard.py imports outside the allowed stdlib set"; fi
|
|
|
|
echo
|
|
echo "delivery guard hook: $PASS passed, $FAIL failed"
|
|
[[ $FAIL -eq 0 ]]
|