From 7ae3b8f312083c7a77f1a2d47efb3ac1874613a8 Mon Sep 17 00:00:00 2001 From: Chad Saunders Date: Thu, 12 Nov 2026 13:27:45 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20[fix]=20hooks:=20deny=20editing?= =?UTF-8?q?=20a=20posted=20=F0=9F=99=8B=20approval=20request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - New PreToolUse(Bash) guard `guard-approval-edit.py`: an edit (`--edit-last`, PATCH `issues/comments/`, `updateIssueComment`) whose body keeps `approval-request:` or a ๐Ÿ™‹ `Reason tag:` is denied; posting a new comment never is - The merge gate refuses an edited request, so the human's `done` was lost (seen in a downstream run) - Wired in settings-snippet.json (on by default); run.md, block-template and the writing standard: post the request once, as the exit's last write; a correction is a new comment - test-guard-hooks.sh gains 14 cases; install/get counts move to 7 guard hooks --- README.md | 3 +- hooks/README.md | 5 + hooks/guard-approval-edit.py | 106 ++++++++++++++++++ hooks/settings-snippet.json | 4 + .../super-board/references/block-template.md | 3 +- skills/super-board/references/run.md | 6 +- .../references/writing-standard.md | 3 + tests/test-get.sh | 2 +- tests/test-guard-hooks.sh | 27 +++++ tests/test-install.sh | 6 +- 10 files changed, 158 insertions(+), 7 deletions(-) create mode 100755 hooks/guard-approval-edit.py diff --git a/README.md b/README.md index c9650242..f8a3c6f8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@

Stop babysitting coding agents: cards you drop in Ready come back as tested, reviewed PRs.

-

9 skills (5 you type, 4 the board runs) ยท 9 commands ยท 6 guard hooks

+

9 skills (5 you type, 4 the board runs) ยท 9 commands ยท 7 guard hooks

Version @@ -154,6 +154,7 @@ Run automatically once installed. Python stdlib, JSON in, JSON out. | [guard-secrets](hooks/guard-secrets.py) | Blocks reading or piping dotenv files, SSH keys and credential files; key names via `super-board-env-check.sh` | | [guard-key-literals](hooks/guard-key-literals.py) | Blocks a live-looking API key written into a file; flags one already there | | [guard-delete-outside](hooks/guard-delete-outside.py) | Blocks `rm`, `find -delete` and `git clean` aimed outside the project, `~` or `/` | +| [guard-approval-edit](hooks/guard-approval-edit.py) | Blocks editing a posted ๐Ÿ™‹ approval request; a correction is a new comment | | [guard-protected-push](hooks/guard-protected-push.py) | Opt-in (`onboard` asks, or `install.sh --protect-main`): blocks direct and force pushes to main/master/base | | [README sync](scripts/super-board-readme-sync.py) | Regenerates the skill table; pre-commit and PostToolUse hooks keep it fresh | | [cleanup-wt](hooks/cleanup-wt.py) | Removes merged worktrees and branches after each merge and at session start, with a recovery file | diff --git a/hooks/README.md b/hooks/README.md index 08745fa7..2bd00195 100644 --- a/hooks/README.md +++ b/hooks/README.md @@ -9,6 +9,7 @@ protected-push guard (opt-in) and the dev-only skill-eval gate. - `guard-secrets.py` โ€” PreToolUse `Bash|Read|Grep`: denies reading or piping dotenv files, SSH keys, `.npmrc`, `credentials(.json)`, service-account JSON and `.pem`; `.env.example` stays readable. One plain call to `super-board-env-check.sh KEYโ€ฆ` is allowed: it prints present / empty / missing per key and never a value (no pipe, chain or redirect may ride along). - `guard-key-literals.py` โ€” PreToolUse `Edit|MultiEdit|Write|NotebookEdit|Bash`: denies writing a live-looking API key into a file; PostToolUse `Edit|MultiEdit|Write|NotebookEdit`: flags one already on disk. Reports line numbers, never the value. - `guard-delete-outside.py` โ€” PreToolUse `Bash`: denies `rm`, `rmdir`, `unlink`, `find โ€ฆ -delete` / `-exec rm` and `git clean` whose target resolves outside `$CLAUDE_PROJECT_DIR`, and always `/` or `~` itself (honours `cd X &&`, `git -C X`, `~`, `$HOME`). Inside `/tmp`, `/private/tmp`, `/var/folders` and `$TMPDIR` stays allowed. A path whose value is only known at run time (`$(โ€ฆ)`, a variable set earlier in the same command) is allowed. +- `guard-approval-edit.py` โ€” PreToolUse `Bash`: denies editing an existing comment (`gh issue|pr comment --edit-last`, a PATCH to `issues/comments/`, GraphQL `updateIssueComment`) whose new body carries an `approval-request:` line or a `Reason tag: โ€ฆ ๐Ÿ™‹` line. The merge gate refuses an edited request, so the human's `done` would be lost; post a new comment instead. Reads the body from the command (inline, heredoc) and from the files it names (`--body-file`, `-F body=@file`, `--input`, `$(cat file)`, `< file`). Posting a new comment is never blocked. `SB_ALLOW_APPROVAL_EDIT=1` turns it off for a session. - `cleanup-wt.py` โ€” SessionStart (`--auto`): when a base branch tip moved since the last session, removes worktrees and local branches already merged into it. The merge gate also runs it with `--post-merge` after every merge. Never deletes unmerged work (a dirty worktree on a merged branch is wip-committed and its branch kept), never touches bases or the worktree it runs in, and writes a recovery TSV to `/cleanup-wt/.tsv` before acting. Restore with `git branch `. A manual dry run (`python3 .claude/hooks/cleanup-wt.py`, then `--apply`) still works. ## Enable @@ -32,6 +33,10 @@ project and merges the block below (kept in `settings-snippet.json`) into { "type": "command", "command": "python3 \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard-delete-outside.py" + }, + { + "type": "command", + "command": "python3 \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard-approval-edit.py" } ] }, diff --git a/hooks/guard-approval-edit.py b/hooks/guard-approval-edit.py new file mode 100755 index 00000000..2521eb28 --- /dev/null +++ b/hooks/guard-approval-edit.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +"""PreToolUse(Bash) guard: never edit a ๐Ÿ™‹ approval request; post a new comment. + +super-board-approval.py refuses a request comment whose updated_at differs from its +created_at ("approval request was edited; post a fresh request"): an edit could change +what the human's `done` approved. A lane that edits its own request to add a link or +fix a line loses the human's `done` and costs a review round. + +Denied: an edit of an existing comment (`gh issue|pr comment --edit-last`, a PATCH to +`issues/comments/`, a GraphQL `updateIssueComment`) whose new body carries an +`approval-request:` line or a `Reason tag: โ€ฆ ๐Ÿ™‹` line. The body is read from the command +itself (inline `--body`, a heredoc) and from any file it names (`--body-file`, `-F`, +`--input`, `body=@file`, `$(cat file)`, `< file`). Posting a new comment is never blocked. + +SB_ALLOW_APPROVAL_EDIT=1 in Claude Code's environment turns it off for a session. +Its own errors never block a tool call: anything unexpected exits 0 with no output. +""" +import json +import os +import re +import shlex +import sys + +STOP = r"[^;&|\n]*" # stay inside one simple command +PATCH = r"(?:-X\s*PATCH|--method[ =]PATCH)" +EDIT = re.compile( + rf"\bgh\s+(?:issue|pr)\s+comment\b{STOP}--edit-last" + rf"|\bgh\s+api\b{STOP}{PATCH}{STOP}issues/comments/" + rf"|\bgh\s+api\b{STOP}issues/comments/\d+{STOP}{PATCH}" + r"|\bupdateIssueComment\b", re.I) +REQUEST = re.compile(r"approval-request:|Reason tag:[^\n]*๐Ÿ™‹") +FILE_FLAGS = {"--body-file", "-F", "--field", "-f", "--raw-field", "--input"} + +REASON = ( + "Blocked: this edits a ๐Ÿ™‹ approval request.\n" + "The merge gate refuses an edited request (\"approval request was edited\"), so the " + "human's `done` is lost. Never edit a request after posting: post a NEW comment instead. " + "A follow-up note carries no `Reason tag: ๐Ÿ™‹` and no `approval-request:` line; a changed " + "request is a fresh full block with the gate's `approval-request:` line copied verbatim " + "(block-template.md โ†’ \"Approval request\"). If this edit really is intended, ask the user " + "to run it, or start Claude Code with SB_ALLOW_APPROVAL_EDIT=1.") + + +def named_files(command): + """Files whose text becomes the new body without appearing in the command.""" + files = re.findall(r"\$\(\s*cat\s+['\"]?([^)'\"\s]+)['\"]?\s*\)", command) + files += re.findall(r"(?&1) || fail "grouped-output install failed: $o order=$(printf '%s\n' "$out" | grep -oE '^(๐Ÿงฉ|๐Ÿ”|๐Ÿ“ฆ|๐Ÿ”ง|๐Ÿง |๐Ÿ”’|๐ŸŽ‰|๐Ÿ‘‰)' | tr -d '\n') [ "$order" = "๐Ÿงฉ๐Ÿ”๐Ÿ“ฆ๐Ÿ”ง๐Ÿง ๐Ÿ”’๐ŸŽ‰๐Ÿ‘‰" ] || fail "groups out of order ($order): $out" printf '%s\n' "$out" | grep -q '^ โœ“ curl ยท tar ยท python3 ยท gh ยท jq ยท node$' || fail "checks line wrong: $out" -printf '%s\n' "$out" | grep -q '^ ๐Ÿ›ก๏ธ 6 guard hooks โ†’ .claude/settings.json (new file)$' || fail "guard line wrong: $out" +printf '%s\n' "$out" | grep -q '^ ๐Ÿ›ก๏ธ 7 guard hooks โ†’ .claude/settings.json (new file)$' || fail "guard line wrong: $out" printf '%s\n' "$out" | grep -q '^ ! CLAUDE.md holds its own rules โ€” onboard offers a merge$' || fail "CLAUDE.md hint missing: $out" printf '%s\n' "$out" | grep -q '^๐Ÿง  helper skills: installed$' || fail "helper line wrong: $out" [ "$(printf '%s\n' "$out" | wc -l | tr -d ' ')" -le 13 ] || fail "output should stay โ‰ค 13 lines: $out" diff --git a/tests/test-guard-hooks.sh b/tests/test-guard-hooks.sh index b96f6985..e45fac28 100755 --- a/tests/test-guard-hooks.sh +++ b/tests/test-guard-hooks.sh @@ -139,6 +139,33 @@ dd allow 'T=$(mktemp -d); rm -rf "$T/x"' "unknowable value is allowed" dd allow 'echo "rm -rf /"' "quoted text is not a command" dd allow 'ls -la ~ && git status' "no deletion" +# ---- guard-approval-edit ----------------------------------------------------- +# The merge gate refuses an edited ๐Ÿ™‹ request ("approval request was edited"), so the +# human's `done` is lost. An edit whose new body keeps a request line is denied; posting +# a new comment never is. +AP=$(mktemp -d) +printf '[reviewer] [blocker] ๐Ÿ™‹ Your turn\nReason tag: ๐Ÿ™‹ needs you\napproval-request: {"head":"%s","pr":7}\n' \ + "$(printf 'a%.0s' $(seq 1 40))" > "$AP/request.md" +printf '[reviewer] [report] โœ… link: PR #7\n' > "$AP/note.md" +ae() { expect "$1" guard-approval-edit.py "$(bash_cmd "$2" "$AP")" "$3"; } +ae deny 'gh issue comment 7 --edit-last --body-file request.md' "edit-last with a request file" +ae deny 'gh pr comment 7 --edit-last --body "approval-request: {}"' "edit-last with an inline request" +ae deny 'gh api -X PATCH repos/o/r/issues/comments/991 -F body=@request.md' "PATCH with body=@file" +ae deny 'gh api repos/o/r/issues/comments/991 --method PATCH -f body="$(cat request.md)" && echo ok' "PATCH with \$(cat file)" +ae deny "gh api repos/o/r/issues/comments/991 -X PATCH --input - <<'EOF' +{\"body\": \"approval-request: {}\"} +EOF" "PATCH with a heredoc" +ae deny 'gh api repos/o/r/issues/comments/991 -X PATCH --input - < request.md' "PATCH with < file" +ae deny "gh api graphql -f query='mutation{updateIssueComment(input:{id:\"X\",body:\"Reason tag: ๐Ÿ™‹ needs you\"}){issueComment{id}}}'" "GraphQL updateIssueComment" +ae allow 'gh issue comment 7 --body-file request.md' "posting a new request" +ae allow 'gh api repos/o/r/issues/7/comments -F body=@request.md' "POSTing a new request via api" +ae allow 'gh issue comment 7 --edit-last --body-file note.md' "editing a plain note" +ae allow 'cat request.md && git status' "unrelated command" +expect allow guard-approval-edit.py "not json" "garbage input fails open" +OUT=$(printf '%s' "$(bash_cmd 'gh issue comment 7 --edit-last --body-file request.md' "$AP")" | SB_ALLOW_APPROVAL_EDIT=1 python3 "$H/guard-approval-edit.py") +[ -z "$OUT" ] || fail "SB_ALLOW_APPROVAL_EDIT=1 should turn the guard off"; PASS=$((PASS + 1)) +rm -rf "$AP" + # ---- dev/gate-skill-evals (Stop; pack + EricOS only) ------------------------- G=$(mktemp -d); G=$(cd "$G" && pwd -P) git -C "$G" init -q -b main; git -C "$G" config user.email t@t; git -C "$G" config user.name t diff --git a/tests/test-install.sh b/tests/test-install.sh index 95e76b09..369ff698 100755 --- a/tests/test-install.sh +++ b/tests/test-install.sh @@ -186,7 +186,7 @@ cat > "$T/.claude/settings.json" <<'JSON' {"permissions":{"allow":["Bash(ls:*)"]},"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"echo mine"}]}],"Stop":[{"hooks":[{"type":"command","command":"echo stop"}]}]}} JSON "$INSTALL" "$T" >/dev/null 2>&1 -for h in guard-worktree-path.py guard-secrets.py guard-key-literals.py guard-delete-outside.py guard-protected-push.py cleanup-wt.py; do +for h in guard-worktree-path.py guard-secrets.py guard-key-literals.py guard-delete-outside.py guard-approval-edit.py guard-protected-push.py cleanup-wt.py; do [ -f "$T/.claude/hooks/$h" ] || fail "hook $h was not installed" done [ ! -e "$T/.claude/skills/cleanup-wt" ] || fail "cleanup-wt is a hook now, not a skill" @@ -200,7 +200,7 @@ jq -e '[.hooks.SessionStart[].hooks[].command | select(contains("cleanup-wt.py - || fail "the cleanup-wt SessionStart hook is not wired by default" jq -e '[.hooks[][].hooks[].command | select(contains("guard-protected-push"))] | length == 0' "$S" >/dev/null \ || fail "the protected-push guard is opt-in: it must not be wired without --protect-main" -for h in guard-worktree-path guard-secrets guard-key-literals guard-delete-outside; do +for h in guard-worktree-path guard-secrets guard-key-literals guard-delete-outside guard-approval-edit; do jq -e --arg h "$h" '[.hooks[][].hooks[].command | select(contains($h))] | length > 0' "$S" >/dev/null || fail "$h not wired in settings" done ls "$T/.claude/" | grep -q '^settings.json.bak-' || fail "no backup written before changing settings.json" @@ -269,7 +269,7 @@ OUT=$(SUPER_BOARD_QUIET_NEXT=1 "$INSTALL" "$T" 2>&1) [ "$(echo "$OUT" | head -1)" = "๐Ÿ”ง installing" ] || fail "quiet mode should start at the installing group, got: $OUT" echo "$OUT" | grep -q "next:" && fail "quiet mode must not print the next step" echo "$OUT" | grep -q "โœ“ skills, scripts and workflows โ†’ .agents/" || fail "quiet mode should print the grouped files line" -echo "$OUT" | grep -q "๐Ÿ›ก๏ธ 6 guard hooks โ†’ .claude/settings.json (new file)" || fail "guard line should count hooks, got: $OUT" +echo "$OUT" | grep -q "๐Ÿ›ก๏ธ 7 guard hooks โ†’ .claude/settings.json (new file)" || fail "guard line should count hooks, got: $OUT" [ "$(echo "$OUT" | wc -l | tr -d ' ')" -le 3 ] || fail "grouped output should be โ‰ค 3 lines here, got: $OUT" rm -rf "$T"