aboutsummaryrefslogtreecommitdiff
path: root/hooks/git-commit-confirm.py
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-22 15:38:58 -0500
committerCraig Jennings <c@cjennings.net>2026-05-22 15:38:58 -0500
commit53d33d9cdd5c6d7fd7c4dc7315b04d225add94d6 (patch)
treea2cfe5331eed9e22cc9880c3b75e246d74e2239e /hooks/git-commit-confirm.py
parentefcc8e5ffdd09f538fd2d83824f4c632264ad96c (diff)
downloadrulesets-53d33d9cdd5c6d7fd7c4dc7315b04d225add94d6.tar.gz
rulesets-53d33d9cdd5c6d7fd7c4dc7315b04d225add94d6.zip
feat(hooks): scan file-backed messages and harden rm parsing
Two audit gaps in the confirmation hooks, plus the test harness they were missing. The git-commit and gh-pr-create hooks scanned for AI attribution but only saw inline messages. A commit made with -F/--file or a PR made with --body-file slipped through, since the hook stored a placeholder instead of the file's text, and the publish flow uses -F constantly. A new read_referenced_file helper in _common.py reads the referenced local file (missing, oversized, or non-UTF-8 returns None, which means "couldn't inspect" and never "clean"), so attribution scanning now sees the real committed and posted text. An unreadable file falls through to the existing ask-anyway path. destructive-bash-confirm.py parsed rm flags by splitting on whitespace, which mangled quoted paths and missed flag variants. detect_rm_rf now tokenizes with shlex, so quoted or spaced paths and combined, separate, or reordered flags all parse. It fails toward asking (a sentinel that still fires the modal) on unbalanced quotes, or when a forced recursive rm sits alongside a pipeline, compound command, substitution, or redirect, since target attribution isn't trustworthy there. The supported and unsupported shell constructs are documented in the docstrings. These hooks had no tests and weren't in make test. Added a pytest harness under hooks/tests (an importlib-by-path loader, since the hook filenames are hyphenated) with 54 tests across the three hooks and the shared helper, and wired hooks/tests into make test. Full suite green.
Diffstat (limited to 'hooks/git-commit-confirm.py')
-rwxr-xr-xhooks/git-commit-confirm.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/hooks/git-commit-confirm.py b/hooks/git-commit-confirm.py
index 2441d23..618ac20 100755
--- a/hooks/git-commit-confirm.py
+++ b/hooks/git-commit-confirm.py
@@ -42,7 +42,12 @@ import re
import subprocess
import sys
-from _common import read_payload, respond_ask, scan_attribution
+from _common import (
+ read_payload,
+ read_referenced_file,
+ respond_ask,
+ scan_attribution,
+)
MAX_FILES_SHOWN = 25
@@ -140,6 +145,18 @@ def extract_commit_message(cmd: str) -> str:
if long_form:
return "\n\n".join(msg for _, msg in long_form).strip()
+ # File-backed message: -F <file> / --file <file> / --file=<file>.
+ # Read the file so its text is attribution-scanned. If it can't be read
+ # safely, fall through to UNPARSEABLE_MESSAGE so the hook asks (fail-safe).
+ file_flag = re.search(
+ r"(?:^|\s)(?:-F|--file)[=\s]+(\"[^\"]+\"|'[^']+'|\S+)", cmd
+ )
+ if file_flag:
+ path = file_flag.group(1).strip("\"'")
+ text = read_referenced_file(path)
+ if text is not None:
+ return text.strip()
+
return UNPARSEABLE_MESSAGE