diff --git a/.claude/hooks/guard-merge.sh b/.claude/hooks/guard-merge.sh index c79a9c1..d148c4e 100755 --- a/.claude/hooks/guard-merge.sh +++ b/.claude/hooks/guard-merge.sh @@ -4,14 +4,19 @@ # # Why: the /merge skill tells Claude to review before merging. This hook makes # skipping the review, or merging after new commits made a review stale, fail -# instead of relying on the instruction alone. It can't check that the review -# behind a marker was done properly. The ask rule for merges in -# .claude/settings.json stays as a second layer, so every merge also prompts. +# instead of relying on the instruction alone. # # A merge must be exactly one command of the form -# .claude/scripts/gitea.sh POST /repos/OWNER/REPO/pulls/N/merge @FILE -# where FILE pins head_commit_id. Any other write call whose path mentions -# "merge", or a path it can't read safely, is refused. +# .claude/scripts/gitea.sh POST /repos/OWNER/REPO/pulls/N/merge @/abs/file.json +# where the file is exactly {"Do":"merge","head_commit_id":"
"}. +# Any other command that calls gitea.sh and mentions "merg" (outside @file +# names) is refused, as are gitea.sh write calls with a path that isn't plain. +# Commands that only name gitea.sh as a file (git add, git diff) pass. +# +# Limits: this catches mistakes and shortcuts, not a deliberate workaround such +# as a script that merges on its own. The ask rule for merges in +# .claude/settings.json and the required checks on the server are the other +# layers. It also can't check that the review behind a marker was done properly. # # Fails closed: exit 2 blocks the tool call, and any unexpected error ends in # exit 2 too, because Claude Code treats other exit codes as non-blocking. @@ -25,58 +30,73 @@ trap 'rc=$?; if [[ $rc -ne 0 && $rc -ne 2 ]]; then echo "Merge refused: guard-me input=$(cat) # Most commands mention neither a merge nor gitea.sh; let them through before parsing. -grep -qiE 'merge|gitea\.sh' <<<"$input" || exit 0 +grep -qiE 'merg|gitea\.sh' <<<"$input" || exit 0 cmd=$(jq -r '.tool_input.command // empty' <<<"$input") || deny "could not read the hook input" [[ -z $cmd ]] && exit 0 shopt -s nocasematch # Merges go through gitea.sh only. -if [[ $cmd =~ (curl|wget|http) && $cmd =~ /api/v1 && $cmd =~ merge ]]; then +if [[ $cmd =~ (curl|wget|http) && $cmd =~ /api/v1 && $cmd =~ merg ]]; then deny "merge PRs through .claude/scripts/gitea.sh, not curl or wget" fi -# Find gitea.sh write calls. Their path must be a plain literal so it can't -# hide a merge (variables, dot segments, encoding). Legitimate calls never need more. -write_re="gitea\.sh[\"']?[[:space:]]+[\"']?(POST|PUT|PATCH|DELETE)[\"']?[[:space:]]+[\"']?([^[:space:]\"']*)" -[[ $cmd =~ $write_re ]] || exit 0 -path="${BASH_REMATCH[2]}" -safe_path='^/[A-Za-z0-9_/?=&-]+$' -if ! [[ $path =~ $safe_path ]] || [[ $path == *//* ]]; then - deny "gitea.sh write call with a path that isn't plain ($path)" +# Only commands that call gitea.sh matter. A call is gitea.sh at the start of a +# command (after ; & | ( a backtick, quote or newline) or after a launcher such +# as xargs or bash. As a plain argument (git add -- .claude/scripts/gitea.sh) +# it is just a file name. +flat=${cmd//$'\n'/;} +launcher='(xargs|env|sh|bash|zsh|exec|command|nohup|time|timeout|sudo|nice|eval)' +call_re="(^|[;&|(\`\"'\$])[[:space:]]*[^[:space:];&|]*gitea\.sh|(^|[[:space:];&|(])${launcher}([[:space:]]+[^[:space:];&|]+)*[[:space:]]+[^[:space:];&|]*gitea\.sh" +[[ $flat =~ $call_re ]] || exit 0 + +# File names may contain "merge" (e.g. a PR body file); everything else may not, +# unless the whole command is the strict merge form checked below. +without_files=$(sed -E 's/@[^[:space:]]+//g' <<<"$cmd") +if ! [[ $without_files =~ merg ]]; then + # Not a merge. Write calls still need a plain path, so nothing can hide in it. + write_re="gitea\.sh[\"']?[[:space:]]+[\"']?(POST|PUT|PATCH|DELETE)[\"']?[[:space:]]+[\"']?([^[:space:]\"']*)" + if [[ $cmd =~ $write_re ]]; then + path="${BASH_REMATCH[2]}" + safe_path='^/[A-Za-z0-9_/?=&-]+$' + if ! [[ $path =~ $safe_path ]] || [[ $path == *//* ]]; then + deny "gitea.sh write call with a path that isn't plain ($path)" + fi + fi + exit 0 fi -[[ $path =~ merge ]] || exit 0 shopt -u nocasematch -strict_re='^(\.claude/scripts/|/[^[:space:]]*/\.claude/scripts/)gitea\.sh POST (/repos/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)/pulls/([0-9]+)/merge @([^[:space:];&|<>`$]+)$' +strict_re='^(\.claude/scripts/|/[^[:space:]]*/\.claude/scripts/)gitea\.sh POST (/repos/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)/pulls/([0-9]+)/merge @(/[^[:space:];&|<>`$]+)$' [[ $cmd =~ $strict_re ]] \ - || deny "a merge must be the single command '.claude/scripts/gitea.sh POST /repos/OWNER/REPO/pulls/N/merge @FILE'" + || deny "a merge must be the single command '.claude/scripts/gitea.sh POST /repos/OWNER/REPO/pulls/N/merge @/absolute/body.json'" repo="${BASH_REMATCH[2]}" pr="${BASH_REMATCH[3]}" body_file="${BASH_REMATCH[4]}" +jq -e 'type == "object" and keys == ["Do", "head_commit_id"] and .Do == "merge"' "$body_file" >/dev/null 2>&1 \ + || deny "the merge body must be exactly {\"Do\":\"merge\",\"head_commit_id\":\"