The hook parsed the tool input with `python3 -c` and matched plain substrings: any ">" and "releasedir" in one command was refused (a `2>&1` too), and "git checkout" anywhere blocked the command. tools/ is POSIX sh, no Python. - jq when it is there (`.tool_input.command // empty`, fail closed when the field cannot be read), sed only as a fallback; dash -n clean. - releasedir: the operation must target a path under it -- rm/mv/cp/touch/ chmod/mkdir, `sed -i`, tee, or a redirection whose target is under releasedir. A read with 2>/dev/null stays allowed. - git reset --hard, git clean -f (any flag order), `git checkout -- .` and `git checkout .` in the root; the src/ rule matches src/ as a path component, so xsrc/ is not blocked. - rm of the set lists with any flag order (rm -r -f, -fr, -Rf, long options). - The allowed entry points named in the refusal are the root Makefile's. tools/claude-build-gate-test.sh: 25 cases, each one command with its expected exit code (16 blocked, 8 allowed, one unparsable input that must fail closed). Measured: `sh tools/claude-build-gate-test.sh` -> 25 ok, 0 bad, exit 0; dash -n on both scripts clean; .claude/settings.json still calls the same path. tools/vmctl.py and tools/vmmon.py are deleted: building in a VM is gone (2026-11-13) and nothing in the tree calls them (git grep finds only the history files); tools/AGENTS.md already described them as removed. No Python in tools/ remains.
102 lines
5.1 KiB
Bash
Executable File
102 lines
5.1 KiB
Bash
Executable File
#!/bin/sh
|
|
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
# claude-build-gate.sh -- PreToolUse hook for Claude Code.
|
|
#
|
|
# Enforces the forbidden commands in AGENTS.md (root, "Forbidden") and
|
|
# tools/AGENTS.md. Claude Code passes hook JSON on stdin; exit 2 blocks the
|
|
# tool call and the message on stderr is shown to the model.
|
|
#
|
|
# Blocked: building through a transient unit, building inside build/,
|
|
# hand work on releasedir, destructive git operations in the repository root
|
|
# (git reset --hard, git checkout -- ., git clean -f[d|x|dx|...]), and
|
|
# deleting the set lists under src/distrib/sets/lists.
|
|
set -eu
|
|
|
|
INPUT=$(cat)
|
|
|
|
[ -z "$INPUT" ] && exit 0
|
|
|
|
CMD=
|
|
if command -v jq >/dev/null 2>&1; then
|
|
# jq -r prints "null" for null; // empty yields nothing.
|
|
if ! CMD=$(printf '%s' "$INPUT" | jq -er '.tool_input.command // empty' 2>/dev/null); then
|
|
# Non-empty stdin but no extractable command: fail closed unless the
|
|
# field is explicitly empty/null (jq -e exits 1 for empty/false/null).
|
|
if printf '%s' "$INPUT" | jq -e '(.tool_input.command // "") == ""' >/dev/null 2>&1; then
|
|
CMD=
|
|
else
|
|
printf '%s\n' "Forbidden: could not read tool_input.command from hook input (failing closed)." >&2
|
|
exit 2
|
|
fi
|
|
fi
|
|
else
|
|
# No jq: try to extract the command string field. Fail closed if stdin is
|
|
# non-empty but no command can be recovered safely.
|
|
CMD=$(printf '%s' "$INPUT" | sed -n 's/.*"command"[[:space:]]*:[[:space:]]*"\(.*\)".*/\1/p' | sed 's/\\"/"/g; s/\\\\/\\/g' | head -n 1)
|
|
if [ -z "$CMD" ]; then
|
|
case "$INPUT" in
|
|
*'"command"'*) printf '%s\n' "Forbidden: could not parse tool_input.command without jq (failing closed)." >&2; exit 2 ;;
|
|
esac
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
[ -z "$CMD" ] && exit 0
|
|
|
|
block() {
|
|
printf '%s\n' "Forbidden: $1" >&2
|
|
printf '%s\n' "Allowed entry points are the root Makefile targets: make, make build, make release, make install, make iso, make usb, make compress, make sourcesets, make isosrc, make userland, make test, make cdtest, make tools, make kernel, make mcde-linux, make mcde-host, make stamp, make clean-sanity." >&2
|
|
printf '%s\n' "Rules: AGENTS.md (section \"Forbidden\"), docs/BUILD-CONTRACT.md, tools/AGENTS.md." >&2
|
|
exit 2
|
|
}
|
|
|
|
# Transient build units.
|
|
case "$CMD" in
|
|
*systemd-run*) block "building through a transient unit. Use a named root Makefile target." ;;
|
|
esac
|
|
|
|
# Building directly inside build/.
|
|
case "$CMD" in
|
|
*"make -C build"*|*"make -Cbuild"*|*"cd build && make"*|*"cd build&&make"*|*"cd build; make"*|*"cd build;make"*)
|
|
block "running make directly in build/. Build through the targets in the repository root." ;;
|
|
esac
|
|
|
|
# Hand work on releasedir: writes/moves/deletes/chmod/sed -i targeting it, or
|
|
# shell redirection whose target path is under releasedir. Redirects like 2>&1
|
|
# are not targets and must not be blocked.
|
|
if printf '%s\n' "$CMD" | grep -q 'releasedir'; then
|
|
if printf '%s\n' "$CMD" | grep -Eq '(^|[[:space:];|&()])(rm|mv|cp|touch|chmod|mkdir)([[:space:]]|$).*(^|[[:space:]])[^[:space:]]*releasedir|(^|[[:space:];|&()])(sed[[:space:]].*-i|tee([[:space:]]|$)).*(^|[[:space:]])[^[:space:]]*releasedir|>>?[[:space:]]*[^[:space:]]*releasedir'; then
|
|
block "hand work under build/obj/releasedir. Release artefacts are produced only by the build."
|
|
fi
|
|
fi
|
|
|
|
# Destructive git operations in the repository root (AGENTS.md "Forbidden").
|
|
# git reset --hard (allowing git global options such as -C <path>)
|
|
if printf '%s\n' "$CMD" | grep -Eq '(^|[[:space:];|&])git([[:space:]].*)?[[:space:]]reset([[:space:]].*)?[[:space:]]--hard([[:space:]]|$)'; then
|
|
block "git reset --hard in the repository root. Use git restore <file> after inspecting the change."
|
|
fi
|
|
# git clean with -f (including -fd, -fx, -fdx, -xfd, etc.)
|
|
if printf '%s\n' "$CMD" | grep -Eq '(^|[[:space:];|&])git([[:space:]].*)?[[:space:]]clean([[:space:]].*)?[[:space:]]-[a-zA-Z]*f'; then
|
|
block "git clean -f in the repository root."
|
|
fi
|
|
# git checkout -- . / git checkout .
|
|
if printf '%s\n' "$CMD" | grep -Eq '(^|[[:space:];|&])git([[:space:]].*)?[[:space:]]checkout([[:space:]].*)?[[:space:]]--[[:space:]]+\.([[:space:]]|$)|(^|[[:space:];|&])git([[:space:]].*)?[[:space:]]checkout[[:space:]]+\.([[:space:]]|$)'; then
|
|
block "git checkout -- . in the repository root. Use git restore <file> after inspecting the change."
|
|
fi
|
|
# Existing protection: destructive git operations against the imported src/ tree.
|
|
# Match src/ as a path component (not xsrc, msrc, resources, ...).
|
|
if printf '%s\n' "$CMD" | grep -Eq '(^|[[:space:];|&])git([[:space:]].*)?[[:space:]](checkout|clean|reset)([[:space:]]|$)'; then
|
|
if printf '%s\n' "$CMD" | grep -Eq '(^|[[:space:];/=])src([[:space:];/]|$)|-C[[:space:]]*src([[:space:];/]|$)|--git-dir(=|[[:space:]]+)[^[:space:]]*src([[:space:];/]|$)'; then
|
|
block "git checkout/clean/reset against src/. src/ is the imported tree; its state is the build input."
|
|
fi
|
|
fi
|
|
|
|
# Deleting the set lists (any flag order/combination including rm -r -f).
|
|
if printf '%s\n' "$CMD" | grep -Eq '(^|[[:space:];|&])rm([[:space:]]+-[a-zA-Z]+|[[:space:]]+--[^[:space:]]+)*([[:space:]]+-[a-zA-Z]+)*([[:space:]]|$)'; then
|
|
if printf '%s\n' "$CMD" | grep -q 'sets/lists'; then
|
|
block "deleting src/distrib/sets/lists."
|
|
fi
|
|
fi
|
|
|
|
exit 0
|