Files
Holocron/tests/test-check-executables-allow-sync.sh
Defame1297andClaude Opus 5.5 a6ff1c14d4 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
2026-11-23 08:15:14 +00:00

358 lines
15 KiB
Bash

#!/usr/bin/env bash
# Tests for scripts/check-executables-allow-sync.sh — the pre-push gate that
# keeps root apm.yml's executables.allow keys level with the version of every
# plugin that ships executables (plugins/<name>/.apm/hooks/ or bin/).
#
# Fixtures are skeletons (root apm.yml + plugins/<name>/apm.yml + an empty
# hooks/ or bin/ dir) rather than copies of the real repo: the gate reads
# exactly those files, and a hand-built fixture is the only way to construct
# the drift it exists to catch without editing the real manifests.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SCRIPT="$REPO_ROOT/scripts/check-executables-allow-sync.sh"
PASS=0
FAIL=0
pass() { echo " PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo " FAIL: $1"; FAIL=$((FAIL + 1)); }
# Same `exit 77` (automake convention; run-tests.sh renders it as SKIPPED) guard
# the vale suites use. The script itself runs fine without python3 — it falls
# back to a shape scan — but this suite asserts BOTH readers agree, and the
# PyYAML path cannot be exercised at all on a machine without it. Reporting
# those cases as failures would say "a regression landed" when the truth is
# "this machine is missing a dev dependency".
command -v python3 > /dev/null 2>&1 || { echo "SKIP: python3 is required to exercise the PyYAML reader"; exit 77; }
python3 -c 'import yaml' > /dev/null 2>&1 || { echo "SKIP: PyYAML is required to exercise the PyYAML reader"; exit 77; }
FIXTURES=()
cleanup() { [[ ${#FIXTURES[@]} -eq 0 ]] || rm -rf "${FIXTURES[@]}"; }
trap cleanup EXIT
# make_fixture <plugin-version-line> <root-executables-block>
# The executables block is passed verbatim (may be empty) so a fixture can omit
# it entirely, which is one of the failure modes under test.
make_fixture() {
local version_line="$1" executables_block="$2" dir
dir="$(mktemp -d)"
FIXTURES+=("$dir")
# kyberforge ships both hooks and bin, which is what makes the gate check it.
mkdir -p "$dir/plugins/kyberforge/.apm/hooks" "$dir/plugins/kyberforge/bin"
{
echo "name: kyberforge"
echo "$version_line"
echo "description: fixture"
} > "$dir/plugins/kyberforge/apm.yml"
{
echo "name: ai-development"
echo "version: 0.0.1"
echo "dependencies:"
echo " apm:"
echo " - name: kyberforge"
[[ -n "$executables_block" ]] && printf '%s\n' "$executables_block"
# A top-level key after the block: the fallback reader must stop collecting
# allow keys here rather than reading on into the next section.
echo "marketplace:"
echo " owner:"
echo " name: fixture"
} > "$dir/apm.yml"
printf '%s\n' "$dir"
}
# add_plugin <fixture> <dir> <version-line> [<name>] — a second plugin that
# ships hooks only (like delivery). <name> defaults to <dir>.
add_plugin() {
local dir="$1/plugins/$2"
mkdir -p "$dir/.apm/hooks"
{
echo "name: ${4:-$2}"
echo "$3"
} > "$dir/apm.yml"
}
MATCHING_BLOCK='executables:
allow:
kyberforge#1.5.0:
hooks: true
bin: true'
STALE_BLOCK='executables:
allow:
kyberforge#1.4.0:
hooks: true
bin: true'
TWO_MATCHING_BLOCK='executables:
allow:
kyberforge#1.5.0:
hooks: true
bin: true
# delivery ships a guard hook; an indented comment must not read as a key.
delivery#0.1.0:
hooks: true'
TWO_STALE_BLOCK='executables:
allow:
kyberforge#1.4.0:
hooks: true
bin: true
delivery#0.0.9:
hooks: true'
OTHER_PACKAGE_BLOCK='executables:
allow:
git#1.0.0:
hooks: true'
# run_gate <fixture> — echoes combined output, sets GATE_RC.
GATE_RC=0
run_gate() {
GATE_RC=0
bash "$SCRIPT" "$1" > /dev/null 2>&1 || GATE_RC=$?
}
# ---------------------------------------------------------------------------
echo "--- the real repo passes ---"
# ---------------------------------------------------------------------------
# The gate's whole value is that it is green on a correct tree and red on drift;
# a version bump landing in only one of the two real manifests must show up here.
run_gate "$REPO_ROOT"
[[ $GATE_RC -eq 0 ]] && pass "current repo state passes" \
|| fail "current repo state should pass — the gate said: $(bash "$SCRIPT" "$REPO_ROOT" 2>&1 | head -3)"
# ---------------------------------------------------------------------------
echo ""
echo "--- matching version ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "exits 0 when the allow key names the plugin's version" \
|| fail "should pass when key and version agree, got rc=$GATE_RC"
F="$(make_fixture 'version: "1.5.0"' "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "exits 0 when the version is quoted" \
|| fail "a quoted version must compare the same as an unquoted one, got rc=$GATE_RC"
# ---------------------------------------------------------------------------
echo ""
echo "--- mismatched version ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$STALE_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when the allow key names a different version" \
|| fail "a stale allow key must fail the push"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && pass "names the key that should be there" \
|| fail "the failure must state the expected key"
grep -q "kyberforge#1.4.0" <<< "$OUT" && pass "names the stale key it found instead" \
|| fail "the failure must quote back the stale key"
grep -q "Why:" <<< "$OUT" && grep -q "Fix:" <<< "$OUT" && pass "uses the FAIL/Why/Fix message block" \
|| fail "message must carry Why: and Fix: lines"
# ---------------------------------------------------------------------------
echo ""
echo "--- missing executables.allow ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when there is no executables block at all" \
|| fail "a missing executables.allow must fail — apm deploys no hooks without it"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "executables:" <<< "$OUT" && grep -q "kyberforge#1.5.0" <<< "$OUT" \
&& pass "shows the block to add" || fail "the failure must show the block to add"
# An `executables:` key that is not a mapping is the same hole as no key at all.
F="$(make_fixture "version: 1.5.0" "executables:")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when executables: exists but allow: does not" \
|| fail "an empty executables: block grants nothing and must fail"
# ---------------------------------------------------------------------------
echo ""
echo "--- allow present, kyberforge key missing ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$OTHER_PACKAGE_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when allow: exists but names no kyberforge key" \
|| fail "an allow block covering only other packages must still fail"
# ---------------------------------------------------------------------------
echo ""
echo "--- degenerate inputs fail loudly rather than passing silently ---"
# ---------------------------------------------------------------------------
run_gate "$REPO_ROOT/definitely-not-a-directory"
[[ $GATE_RC -ne 0 ]] && pass "fails on a nonexistent REPO_ROOT" \
|| fail "a bad path must not exit 0 — that reads as 'checked, in sync'"
# No executable-shipping plugin at all is the one legitimate no-op: nothing to pin.
NO_PLUGIN="$(mktemp -d)"; FIXTURES+=("$NO_PLUGIN")
echo "name: someone-else" > "$NO_PLUGIN/apm.yml"
run_gate "$NO_PLUGIN"
[[ $GATE_RC -eq 0 ]] && pass "no-ops in a repo with no executable-shipping plugin" \
|| fail "a repo without any plugins/*/.apm/hooks or bin has nothing to check"
# A plugin dir that ships nothing executable is not this gate's business.
mkdir -p "$NO_PLUGIN/plugins/kyberforge"
run_gate "$NO_PLUGIN"
[[ $GATE_RC -eq 0 ]] && pass "no-ops when plugins/kyberforge/ ships no hooks/ or bin/" \
|| fail "a plugin dir without hooks/ or bin/ has nothing to check"
# ...but one that ships executables with no manifest is drift, not a no-op.
mkdir -p "$NO_PLUGIN/plugins/kyberforge/.apm/hooks"
run_gate "$NO_PLUGIN"
[[ $GATE_RC -ne 0 ]] && pass "fails when plugins/kyberforge/ ships hooks but has no apm.yml" \
|| fail "a plugin dir with no manifest must not silently pass"
F="$(make_fixture "description: no version here" "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when the plugin manifest declares no version" \
|| fail "no version means nothing to compare — must fail, not pass"
# ---------------------------------------------------------------------------
echo ""
echo "--- several executable-shipping plugins ---"
# ---------------------------------------------------------------------------
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "exits 0 when both shipping plugins have matching keys" \
|| fail "two in-sync packages should pass, got rc=$GATE_RC"
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when the second package's key is missing" \
|| fail "a shipping plugin with no allow key must fail"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "delivery#0.1.0" <<< "$OUT" && pass "names the second package's expected key" \
|| fail "the failure must state delivery#0.1.0"
grep -q "kyberforge#" <<< "$OUT" && fail "an in-sync kyberforge must not be reported" \
|| pass "does not report the package that is in sync"
F="$(make_fixture "version: 1.5.0" "$TWO_STALE_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && grep -q "delivery#0.1.0" <<< "$OUT" \
&& pass "reports every failing package in one run" \
|| fail "both stale packages must be named, got: $OUT"
grep -q "kyberforge#1.4.0" <<< "$OUT" && grep -q "delivery#0.0.9" <<< "$OUT" \
&& pass "quotes back each package's stale key" || fail "each stale key must be quoted back"
F="$(make_fixture "version: 1.5.0" "")"
add_plugin "$F" delivery "version: 0.1.0"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && grep -q "delivery#0.1.0" <<< "$OUT" \
&& pass "missing executables.allow lists every package's key to add" \
|| fail "the add-this block must name every shipping package"
# A plugin with no hooks/ or bin/ needs no key, whatever its version.
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
mkdir -p "$F/plugins/docs-only/.apm/skills"
printf 'name: docs-only\nversion: 9.9.9\n' > "$F/plugins/docs-only/apm.yml"
run_gate "$F"
[[ $GATE_RC -eq 0 ]] && pass "ignores a plugin that ships no hooks/ or bin/" \
|| fail "a plugin without executables must not need an allow key"
# Keys use the package name, so the dir name and name: must agree.
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0" "delivery-renamed"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when a plugin's name: differs from its directory" \
|| fail "a dir/name mismatch must fail"
OUT="$(bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "delivery-renamed" <<< "$OUT" && pass "names the mismatched name:" \
|| fail "the failure must quote the mismatched name:"
# A 2-component version is a YAML float; the PyYAML reader refuses to guess.
F="$(make_fixture "version: 1.5" "$MATCHING_BLOCK")"
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "refuses an unquoted 2-component version" \
|| fail "a float version must fail rather than be compared in reader-invented form"
# ---------------------------------------------------------------------------
echo ""
echo "--- the fallback reader agrees with the PyYAML reader ---"
# ---------------------------------------------------------------------------
# PyYAML is deliberately not a hard dependency of this gate (no other pre-push
# hook needs it), so the shape-scan fallback carries the same verdicts. Masking
# is done with a python3 stub whose `import yaml` fails, which is the exact
# condition on a machine that has python3 without PyYAML.
NO_YAML_BIN="$(mktemp -d)"; FIXTURES+=("$NO_YAML_BIN")
printf '#!/usr/bin/env bash\nexit 1\n' > "$NO_YAML_BIN/python3"
chmod +x "$NO_YAML_BIN/python3"
run_fallback() {
GATE_RC=0
PATH="$NO_YAML_BIN:$PATH" bash "$SCRIPT" "$1" > /dev/null 2>&1 || GATE_RC=$?
}
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -eq 0 ]] && pass "fallback passes a matching fixture" || fail "fallback should pass when in sync"
F="$(make_fixture 'version: "1.5.0"' "$MATCHING_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -eq 0 ]] && pass "fallback strips quotes from the version" || fail "fallback mishandled a quoted version"
F="$(make_fixture "version: 1.5.0" "$STALE_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a stale key" || fail "fallback missed a stale key"
F="$(make_fixture "version: 1.5.0" "")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a missing executables block" || fail "fallback missed a missing block"
F="$(make_fixture "version: 1.5.0" "$OTHER_PACKAGE_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails when no kyberforge key is present" || fail "fallback missed an absent key"
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
run_fallback "$F"
[[ $GATE_RC -eq 0 ]] && pass "fallback passes two in-sync packages" || fail "fallback should pass two in-sync packages"
F="$(make_fixture "version: 1.5.0" "$TWO_STALE_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0"
OUT="$(PATH="$NO_YAML_BIN:$PATH" bash "$SCRIPT" "$F" 2>&1 || true)"
grep -q "kyberforge#1.5.0" <<< "$OUT" && grep -q "delivery#0.1.0" <<< "$OUT" \
&& pass "fallback reports both stale packages" || fail "fallback must name both stale packages"
F="$(make_fixture "version: 1.5.0" "$TWO_MATCHING_BLOCK")"
add_plugin "$F" delivery "version: 0.1.0" "delivery-renamed"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a dir/name mismatch" || fail "fallback missed a dir/name mismatch"
run_fallback "$REPO_ROOT"
[[ $GATE_RC -eq 0 ]] && pass "fallback passes the real repo" || fail "fallback disagrees with PyYAML on the real repo"
# ---------------------------------------------------------------------------
echo ""
echo "--- wired into the pre-push gate ---"
# ---------------------------------------------------------------------------
# A gate nobody runs is not a gate; this is the only assertion that the script
# is actually reachable from `git push`.
CONFIG="$REPO_ROOT/.pre-commit-config.yaml"
grep -q "id: check-executables-allow-sync" "$CONFIG" \
&& pass ".pre-commit-config.yaml declares the hook" || fail "hook is not declared in .pre-commit-config.yaml"
grep -q "scripts/check-executables-allow-sync.sh" "$CONFIG" \
&& pass ".pre-commit-config.yaml points at the script" || fail "hook does not reference the script path"
# ---------------------------------------------------------------------------
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ $FAIL -eq 0 ]]