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:
1 parent
6edd66432c
commit
a6ff1c14d4
11 files changed
+2244
-110
No files matched your search
@@ -1,23 +1,29 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Fails the push when root apm.yml's executables.allow key stops naming
|
||||
# kyberforge's actual version. The grant itself is version-blind — apm matches
|
||||
# the version-less name alongside '<package>#<version>' (exec_gate.py builds
|
||||
# the candidate list, _map_grants matches it) — so a stale key keeps granting
|
||||
# kyberforge's hooks/ and bin/ and deployment does not break. What this gate
|
||||
# buys is repo-level, not apm-level: a version bump without the matching key
|
||||
# edit fails this repo's own pre-push, so the key stays an accurate record of
|
||||
# what was approved. See ADR-0019's 2026-09-19 correction and docs/spec/gates.md,
|
||||
# Fails the push when root apm.yml's executables.allow stops naming the actual
|
||||
# version of every plugin that ships executables. A plugin ships executables
|
||||
# when plugins/<name>/.apm/hooks/ or plugins/<name>/bin/ exists; each one needs
|
||||
# a '<name>#<version>' key, where <name> and <version> come from its own
|
||||
# apm.yml (and <name> must equal the directory name, since keys use the
|
||||
# package name). Every failing package is reported in one run.
|
||||
#
|
||||
# The grant itself is version-blind — apm matches the version-less name
|
||||
# alongside '<package>#<version>' (exec_gate.py builds the candidate list,
|
||||
# _map_grants matches it) — so a stale key keeps granting the package's hooks/
|
||||
# and bin/ and deployment does not break. What this gate buys is repo-level,
|
||||
# not apm-level: a version bump without the matching key edit fails this
|
||||
# repo's own pre-push, so each key stays an accurate record of what was
|
||||
# approved. See ADR-0019's 2026-09-19 correction and docs/spec/gates.md,
|
||||
# "check-executables-allow-sync"; nothing else in the pre-push gate compares
|
||||
# these two files.
|
||||
# these files.
|
||||
#
|
||||
# Run from repo root or pass REPO_ROOT as arg.
|
||||
|
||||
REPO_ROOT="${1:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}"
|
||||
# A nonexistent REPO_ROOT fails loudly rather than falling through to the
|
||||
# "no kyberforge plugin here" no-op below: that no-op is for a repo that
|
||||
# legitimately does not ship the plugin, not for a typo'd or stale path, and
|
||||
# "no executable-shipping plugin here" no-op below: that no-op is for a repo
|
||||
# that legitimately ships no executables, not for a typo'd or stale path, and
|
||||
# exit 0 would read as "checked, in sync" when nothing was compared at all.
|
||||
if [[ ! -d "$REPO_ROOT" ]]; then
|
||||
echo "FAIL: executables-allow sync check: REPO_ROOT '$REPO_ROOT' is not a directory." >&2
|
||||
@@ -26,28 +32,69 @@ if [[ ! -d "$REPO_ROOT" ]]; then
|
||||
fi
|
||||
REPO_ROOT="$(cd "$REPO_ROOT" && pwd)"
|
||||
|
||||
PLUGIN_DIR="$REPO_ROOT/plugins/kyberforge"
|
||||
PLUGIN_MANIFEST="$PLUGIN_DIR/apm.yml"
|
||||
ROOT_MANIFEST="$REPO_ROOT/apm.yml"
|
||||
|
||||
# Only a repo with no kyberforge plugin at all is a legitimate no-op — there is
|
||||
# no version to pin and no hook to deploy.
|
||||
if [[ ! -d "$PLUGIN_DIR" ]]; then
|
||||
# ---------------------------------------------------------------------------
|
||||
# Discovery
|
||||
# ---------------------------------------------------------------------------
|
||||
# PLUGINS: directory names of every plugin that ships executables. READABLE
|
||||
# (below) is the subset whose apm.yml exists; only those reach the readers.
|
||||
PLUGINS=()
|
||||
shopt -s nullglob
|
||||
for plugin_dir in "$REPO_ROOT"/plugins/*/; do
|
||||
plugin_dir="${plugin_dir%/}"
|
||||
if [[ -d "$plugin_dir/.apm/hooks" || -d "$plugin_dir/bin" ]]; then
|
||||
PLUGINS+=("${plugin_dir##*/}")
|
||||
fi
|
||||
done
|
||||
shopt -u nullglob
|
||||
|
||||
# Only a repo with no executable-shipping plugin at all is a legitimate no-op —
|
||||
# there is no version to pin and no hook to deploy.
|
||||
if [[ ${#PLUGINS[@]} -eq 0 ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ ! -f "$PLUGIN_MANIFEST" ]]; then
|
||||
echo "FAIL: plugins/kyberforge/ exists but has no apm.yml, so its version cannot be read." >&2
|
||||
FAILED=0
|
||||
|
||||
# "hooks/ and bin/", "hooks/" or "bin/" — what the plugin actually ships, so a
|
||||
# message never claims a package has a bin/ it does not.
|
||||
ships() {
|
||||
local dir="$REPO_ROOT/plugins/$1"
|
||||
if [[ -d "$dir/.apm/hooks" && -d "$dir/bin" ]]; then
|
||||
printf 'hooks/ and bin/'
|
||||
elif [[ -d "$dir/.apm/hooks" ]]; then
|
||||
printf 'hooks/'
|
||||
else
|
||||
printf 'bin/'
|
||||
fi
|
||||
}
|
||||
|
||||
# "hooks: true, bin: true" etc. — the allow entry body the plugin needs.
|
||||
grants() {
|
||||
local dir="$REPO_ROOT/plugins/$1" out=""
|
||||
if [[ -d "$dir/.apm/hooks" ]]; then out="hooks: true"; fi
|
||||
if [[ -d "$dir/bin" ]]; then out+="${out:+, }bin: true"; fi
|
||||
printf '%s' "$out"
|
||||
}
|
||||
|
||||
READABLE=()
|
||||
for plugin in ${PLUGINS[@]+"${PLUGINS[@]}"}; do
|
||||
if [[ -f "$REPO_ROOT/plugins/$plugin/apm.yml" ]]; then
|
||||
READABLE+=("$plugin")
|
||||
continue
|
||||
fi
|
||||
echo "FAIL: plugins/$plugin/ ships $(ships "$plugin") but has no apm.yml, so its version cannot be read." >&2
|
||||
echo " Why: this gate compares that version against root apm.yml's executables.allow key;" >&2
|
||||
echo " without the manifest it would exit 0 having compared nothing." >&2
|
||||
echo " Fix: restore plugins/kyberforge/apm.yml, or remove plugins/kyberforge/ entirely." >&2
|
||||
exit 1
|
||||
fi
|
||||
echo " Fix: restore plugins/$plugin/apm.yml, or remove plugins/$plugin/ entirely." >&2
|
||||
FAILED=1
|
||||
done
|
||||
|
||||
if [[ ! -f "$ROOT_MANIFEST" ]]; then
|
||||
echo "FAIL: root apm.yml is missing, so kyberforge's executables allow key cannot be verified." >&2
|
||||
echo "FAIL: root apm.yml is missing, so no executables allow key can be verified (${PLUGINS[*]})." >&2
|
||||
echo " Why: apm reads executables.allow from the consuming package's manifest; with no root" >&2
|
||||
echo " manifest nothing grants kyberforge's hooks/ and bin/ permission to deploy." >&2
|
||||
echo " manifest nothing grants these packages' hooks/ and bin/ permission to deploy." >&2
|
||||
echo " Fix: restore apm.yml at the repo root." >&2
|
||||
exit 1
|
||||
fi
|
||||
@@ -58,16 +105,30 @@ fi
|
||||
# Both readers emit the same line protocol, so the rest of the script does not
|
||||
# care which ran:
|
||||
#
|
||||
# v<TAB><kyberforge version> exactly once
|
||||
# a<TAB>present|absent exactly once — is executables.allow a mapping?
|
||||
# k<TAB><allow key> zero or more
|
||||
# v<TAB><plugin dir><TAB><version> once per readable plugin (version may be empty)
|
||||
# n<TAB><plugin dir><TAB><name> once per readable plugin (name may be empty)
|
||||
# e<TAB><plugin dir><TAB><message> zero or more — the plugin's facts are unusable
|
||||
# a<TAB>present|absent exactly once — is executables.allow a mapping?
|
||||
# k<TAB><allow key> zero or more
|
||||
#
|
||||
# python3 + PyYAML is preferred where importable; the fallback below is not a
|
||||
# YAML parser, it recognises only the two shapes these manifests use. PyYAML
|
||||
# stays optional so a missing pip package can't block every push (ADR-0019).
|
||||
# YAML parser, it recognises only the shapes these manifests use. PyYAML stays
|
||||
# optional so a missing pip package can't block every push (ADR-0019).
|
||||
|
||||
reader_args() {
|
||||
local plugin
|
||||
printf '%s\0' "$ROOT_MANIFEST"
|
||||
for plugin in ${READABLE[@]+"${READABLE[@]}"}; do
|
||||
printf '%s\0%s\0' "$plugin" "$REPO_ROOT/plugins/$plugin/apm.yml"
|
||||
done
|
||||
}
|
||||
|
||||
read_facts_python() {
|
||||
python3 - "$ROOT_MANIFEST" "$PLUGIN_MANIFEST" << 'PY'
|
||||
local args=() arg
|
||||
while IFS= read -r -d '' arg; do
|
||||
args+=("$arg")
|
||||
done < <(reader_args)
|
||||
python3 - ${args[@]+"${args[@]}"} << 'PY'
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
@@ -78,19 +139,25 @@ def load(path):
|
||||
return data if isinstance(data, dict) else {}
|
||||
|
||||
|
||||
root, plugin = load(sys.argv[1]), load(sys.argv[2])
|
||||
root = load(sys.argv[1])
|
||||
pairs = sys.argv[2:]
|
||||
|
||||
version = plugin.get("version")
|
||||
# A 2-component version parses as a YAML float and would render back as e.g.
|
||||
# "1.5" from 1.50 — a mismatch invented by the reader rather than found in the
|
||||
# files. Refuse instead of guessing; semver keys here are always strings.
|
||||
if version is not None and not isinstance(version, str):
|
||||
sys.stderr.write(
|
||||
"kyberforge's version: is not a string (%r) — quote it so the "
|
||||
"executables.allow key can be compared verbatim.\n" % (version,)
|
||||
)
|
||||
raise SystemExit(2)
|
||||
print("v\t%s" % ("" if version is None else version))
|
||||
for plugin_dir, path in zip(pairs[::2], pairs[1::2]):
|
||||
plugin = load(path)
|
||||
version = plugin.get("version")
|
||||
# A 2-component version parses as a YAML float and would render back as
|
||||
# e.g. "1.5" from 1.50 — a mismatch invented by the reader rather than
|
||||
# found in the files. Refuse instead of guessing; semver keys here are
|
||||
# always strings.
|
||||
if version is not None and not isinstance(version, str):
|
||||
print(
|
||||
"e\t%s\tversion: is not a string (%r) — quote it so the "
|
||||
"executables.allow key can be compared verbatim." % (plugin_dir, version)
|
||||
)
|
||||
continue
|
||||
name = plugin.get("name")
|
||||
print("v\t%s\t%s" % (plugin_dir, "" if version is None else version))
|
||||
print("n\t%s\t%s" % (plugin_dir, "" if name is None else name))
|
||||
|
||||
executables = root.get("executables")
|
||||
allow = executables.get("allow") if isinstance(executables, dict) else None
|
||||
@@ -114,14 +181,24 @@ unquote() {
|
||||
printf '%s\n' "$value"
|
||||
}
|
||||
|
||||
read_facts_bash() {
|
||||
local line version="" allow_state="absent" in_executables=0 in_allow=0 key
|
||||
|
||||
# top_level_scalar <file> <key> — the first column-0 '<key>:' value, unquoted.
|
||||
top_level_scalar() {
|
||||
local line
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
[[ "$line" == version:* ]] || continue
|
||||
version="$(unquote "${line#version:}")"
|
||||
break
|
||||
done < "$PLUGIN_MANIFEST"
|
||||
[[ "$line" == "$2:"* ]] || continue
|
||||
unquote "${line#"$2":}"
|
||||
return
|
||||
done < "$1"
|
||||
}
|
||||
|
||||
read_facts_bash() {
|
||||
local line allow_state="absent" in_executables=0 in_allow=0 key plugin manifest
|
||||
|
||||
for plugin in ${READABLE[@]+"${READABLE[@]}"}; do
|
||||
manifest="$REPO_ROOT/plugins/$plugin/apm.yml"
|
||||
printf 'v\t%s\t%s\n' "$plugin" "$(top_level_scalar "$manifest" version)"
|
||||
printf 'n\t%s\t%s\n' "$plugin" "$(top_level_scalar "$manifest" name)"
|
||||
done
|
||||
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
# Blank and full-line comments carry no structure at any depth.
|
||||
@@ -160,17 +237,17 @@ read_facts_bash() {
|
||||
fi
|
||||
done < "$ROOT_MANIFEST"
|
||||
|
||||
printf 'v\t%s\n' "$version"
|
||||
printf 'a\t%s\n' "$allow_state"
|
||||
}
|
||||
|
||||
if command -v python3 > /dev/null 2>&1 && python3 -c 'import yaml' > /dev/null 2>&1; then
|
||||
READER="python3 + PyYAML"
|
||||
if ! FACTS="$(read_facts_python)"; then
|
||||
echo "FAIL: could not read apm.yml / plugins/kyberforge/apm.yml (see the parser error above)." >&2
|
||||
echo " Why: this gate compares kyberforge's version against root apm.yml's executables.allow" >&2
|
||||
echo " key; an unreadable manifest means the comparison did not happen." >&2
|
||||
echo " Fix: make both manifests valid YAML, then re-run." >&2
|
||||
echo "FAIL: could not read apm.yml or a plugin's apm.yml (see the parser error above)." >&2
|
||||
echo " Why: this gate compares each executable-shipping plugin's version against root" >&2
|
||||
echo " apm.yml's executables.allow keys; an unreadable manifest means the comparison" >&2
|
||||
echo " did not happen." >&2
|
||||
echo " Fix: make the manifests valid YAML, then re-run." >&2
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
@@ -178,49 +255,104 @@ else
|
||||
FACTS="$(read_facts_bash)"
|
||||
fi
|
||||
|
||||
VERSION="$(printf '%s\n' "$FACTS" | sed -n 's/^v\t//p')"
|
||||
# fact <tag> <plugin dir> — the third field of that plugin's <tag> line.
|
||||
fact() {
|
||||
awk -F'\t' -v t="$1" -v d="$2" '$1 == t && $2 == d { sub(/^[^\t]*\t[^\t]*\t/, ""); print; exit }' <<< "$FACTS"
|
||||
}
|
||||
|
||||
ALLOW_STATE="$(printf '%s\n' "$FACTS" | sed -n 's/^a\t//p')"
|
||||
ALLOW_KEYS="$(printf '%s\n' "$FACTS" | sed -n 's/^k\t//p')"
|
||||
|
||||
if [[ -z "$VERSION" ]]; then
|
||||
echo "FAIL: plugins/kyberforge/apm.yml declares no version:, so no allow key can be checked against it." >&2
|
||||
echo " Why: apm's executables.allow lookup is keyed on '<package>#<version>' exactly; with no" >&2
|
||||
echo " version there is nothing for root apm.yml's key to stay in sync with." >&2
|
||||
echo " Fix: give plugins/kyberforge/apm.yml a top-level version: (read by $READER)." >&2
|
||||
exit 1
|
||||
fi
|
||||
# ---------------------------------------------------------------------------
|
||||
# Per-package checks
|
||||
# ---------------------------------------------------------------------------
|
||||
# EXPECTED: plugin dir names whose manifest is usable; the key each needs is
|
||||
# '<dir>#<version>' because a usable manifest's name: equals its dir name.
|
||||
EXPECTED=()
|
||||
for plugin in ${READABLE[@]+"${READABLE[@]}"}; do
|
||||
error="$(fact e "$plugin")"
|
||||
if [[ -n "$error" ]]; then
|
||||
echo "FAIL: plugins/$plugin/apm.yml: $error" >&2
|
||||
echo " Why: a non-string version would be compared in a form the reader invented rather" >&2
|
||||
echo " than the one written in the file." >&2
|
||||
echo " Fix: quote the version, e.g. version: \"1.5\" (read by $READER)." >&2
|
||||
FAILED=1
|
||||
continue
|
||||
fi
|
||||
|
||||
EXPECTED_KEY="kyberforge#$VERSION"
|
||||
version="$(fact v "$plugin")"
|
||||
name="$(fact n "$plugin")"
|
||||
|
||||
if [[ "$ALLOW_STATE" != "present" ]]; then
|
||||
echo "FAIL: root apm.yml has no executables.allow mapping, but plugins/kyberforge is version $VERSION." >&2
|
||||
echo " Why: without an allow entry apm refuses to deploy kyberforge's hooks/ and bin/, so the" >&2
|
||||
echo " SessionStart hook that keeps this install level with the remote never runs and the" >&2
|
||||
echo " deployed skills go stale silently (ADR-0019)." >&2
|
||||
if [[ "$name" != "$plugin" ]]; then
|
||||
echo "FAIL: plugins/$plugin/apm.yml declares name: '${name}', which does not match its directory '$plugin'." >&2
|
||||
echo " Why: executables.allow keys use the package name; with the two disagreeing this gate" >&2
|
||||
echo " cannot tell which '<name>#<version>' key root apm.yml should carry." >&2
|
||||
echo " Fix: make plugins/$plugin/apm.yml's name: equal '$plugin', or rename the directory (read by $READER)." >&2
|
||||
FAILED=1
|
||||
continue
|
||||
fi
|
||||
|
||||
if [[ -z "$version" ]]; then
|
||||
echo "FAIL: plugins/$plugin/apm.yml declares no version:, so no allow key can be checked against it." >&2
|
||||
echo " Why: the executables.allow key is '<package>#<version>'; with no version there is" >&2
|
||||
echo " nothing for root apm.yml's key to stay in sync with." >&2
|
||||
echo " Fix: give plugins/$plugin/apm.yml a top-level version: (read by $READER)." >&2
|
||||
FAILED=1
|
||||
continue
|
||||
fi
|
||||
|
||||
EXPECTED+=("$plugin")
|
||||
done
|
||||
|
||||
if [[ ${#EXPECTED[@]} -gt 0 && "$ALLOW_STATE" != "present" ]]; then
|
||||
summary=""
|
||||
for plugin in ${EXPECTED[@]+"${EXPECTED[@]}"}; do
|
||||
summary+="${summary:+, }$plugin#$(fact v "$plugin")"
|
||||
done
|
||||
echo "FAIL: root apm.yml has no executables.allow mapping, but these packages ship executables: $summary." >&2
|
||||
echo " Why: without an allow entry apm refuses to deploy a package's hooks/ and bin/ — for" >&2
|
||||
echo " kyberforge that means the SessionStart hook that keeps this install level with the" >&2
|
||||
echo " remote never runs and the deployed skills go stale silently (ADR-0019)." >&2
|
||||
echo " Fix: add to root apm.yml:" >&2
|
||||
echo " executables:" >&2
|
||||
echo " allow:" >&2
|
||||
echo " $EXPECTED_KEY:" >&2
|
||||
echo " hooks: true" >&2
|
||||
echo " bin: true" >&2
|
||||
for plugin in ${EXPECTED[@]+"${EXPECTED[@]}"}; do
|
||||
echo " $plugin#$(fact v "$plugin"):" >&2
|
||||
if [[ -d "$REPO_ROOT/plugins/$plugin/.apm/hooks" ]]; then echo " hooks: true" >&2; fi
|
||||
if [[ -d "$REPO_ROOT/plugins/$plugin/bin" ]]; then echo " bin: true" >&2; fi
|
||||
done
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if grep -qxF "$EXPECTED_KEY" <<< "$ALLOW_KEYS"; then
|
||||
exit 0
|
||||
fi
|
||||
for plugin in ${EXPECTED[@]+"${EXPECTED[@]}"}; do
|
||||
expected_key="$plugin#$(fact v "$plugin")"
|
||||
if grep -qxF -- "$expected_key" <<< "$ALLOW_KEYS"; then
|
||||
continue
|
||||
fi
|
||||
|
||||
STALE_KEYS="$(printf '%s\n' "$ALLOW_KEYS" | grep '^kyberforge#' || true)"
|
||||
stale_keys=()
|
||||
while IFS= read -r key; do
|
||||
if [[ -n "$key" && "$key" == "$plugin#"* ]]; then stale_keys+=("$key"); fi
|
||||
done <<< "$ALLOW_KEYS"
|
||||
|
||||
echo "FAIL: root apm.yml's executables.allow has no '$EXPECTED_KEY' key, but that is kyberforge's version." >&2
|
||||
if [[ -n "$STALE_KEYS" ]]; then
|
||||
echo " Found instead:" >&2
|
||||
printf '%s\n' "$STALE_KEYS" | sed 's/^/ /' >&2
|
||||
fi
|
||||
echo " Why: nothing is broken right now — apm's grant is version-blind, so a key naming another" >&2
|
||||
echo " version still grants kyberforge's hooks/ and bin/. This gate is a repo-level record" >&2
|
||||
echo " check: a version bump without the matching key edit fails here, which is what keeps" >&2
|
||||
echo " the key an accurate record of what was approved (ADR-0019, 2026-09-19 correction)." >&2
|
||||
echo " Fix: bump the key in root apm.yml to '$EXPECTED_KEY:' — the version bump in" >&2
|
||||
echo " plugins/kyberforge/apm.yml is not complete without it." >&2
|
||||
exit 1
|
||||
echo "FAIL: root apm.yml's executables.allow has no '$expected_key' key, but that is $plugin's version." >&2
|
||||
if [[ ${#stale_keys[@]} -gt 0 ]]; then
|
||||
echo " Found instead:" >&2
|
||||
printf ' %s\n' ${stale_keys[@]+"${stale_keys[@]}"} >&2
|
||||
echo " Why: nothing is broken right now — apm's grant is version-blind, so a key naming another" >&2
|
||||
echo " version still grants $plugin's $(ships "$plugin"). This gate is a repo-level record" >&2
|
||||
echo " check: a version bump without the matching key edit fails here, which is what keeps" >&2
|
||||
echo " the key an accurate record of what was approved (ADR-0019, 2026-09-19 correction)." >&2
|
||||
echo " Fix: bump the key in root apm.yml to '$expected_key:' — the version bump in" >&2
|
||||
echo " plugins/$plugin/apm.yml is not complete without it." >&2
|
||||
else
|
||||
echo " Why: $plugin ships $(ships "$plugin") and no key grants them, so apm refuses to deploy" >&2
|
||||
echo " them; this gate also keeps each key an accurate record of what was approved" >&2
|
||||
echo " (ADR-0019, 2026-09-19 correction)." >&2
|
||||
echo " Fix: add '$expected_key:' under executables.allow in root apm.yml, granting" >&2
|
||||
echo " $(grants "$plugin")." >&2
|
||||
fi
|
||||
FAILED=1
|
||||
done
|
||||
|
||||
exit "$FAILED"
|
||||
Reference in new issue
Block a user