security_reminder_hook.py 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. #!/usr/bin/env python3
  2. """
  3. Security Guidance Plugin for Claude Code
  4. A hooks-based plugin that guides Claude toward writing more secure code. It runs as
  5. UserPromptSubmit, PostToolUse, and Stop hooks via the Claude Code plugin system.
  6. ## Architecture
  7. The plugin has two layers:
  8. 1. **Pattern-based rules (PostToolUse, every edit)**: Fast regex checks that run on
  9. every file write. Detects common vulnerabilities like hardcoded secrets, SQL injection,
  10. command injection, path traversal, and insecure session configs. Injects brief warnings
  11. via additionalContext.
  12. 2. **Stop hook (final review)**: When Claude finishes, uses `git diff` against a
  13. baseline SHA (captured at UserPromptSubmit) to get only the code changed during the
  14. session. Runs two Haiku analyses on the diff:
  15. a) Concrete vulnerability scan with severity ratings
  16. b) Areas-of-concern analysis identifying categories to investigate
  17. Exits with code 2 to force Claude to continue and address findings.
  18. ## How the git baseline works
  19. On each UserPromptSubmit, the plugin runs `git stash create` to get a SHA representing
  20. the current working tree state (HEAD + any uncommitted changes). This SHA is saved to
  21. the session state file. When the Stop hook fires, it runs `git diff <baseline_sha>` to
  22. get only the changes made since that snapshot. After analysis, the baseline is updated
  23. so the next Stop hook iteration only sees new changes.
  24. This means:
  25. - Only code Claude actually changed is reviewed (not pre-existing code)
  26. - Mid-session commits are handled correctly (diff is against the snapshot, not HEAD)
  27. - Each turn only reviews new changes (baseline updates after each stop hook)
  28. ## Configuration
  29. Kill switches:
  30. - SECURITY_GUIDANCE_DISABLE: "1" to fully disable the plugin (alias for ENABLE_SECURITY_REMINDER=0)
  31. - ENABLE_SECURITY_REMINDER: "0" to fully disable the plugin (legacy name)
  32. Per-feature toggles (all default enabled; set to "0" to disable):
  33. - ENABLE_PATTERN_RULES: PostToolUse regex warnings on Edit/Write
  34. - ENABLE_CODE_SECURITY_REVIEW: Stop-hook git-diff LLM review
  35. - ENABLE_COMMIT_REVIEW: PostToolUse[Bash] commit security review
  36. Other:
  37. - SECURITY_REVIEW_MODEL: Model for LLM review (default: claude-opus-4-7)
  38. - ANTHROPIC_API_KEY: Required for LLM-based reviews
  39. - ANTHROPIC_AUTH_TOKEN: Alternative to API key — OAuth access token sent as Bearer auth.
  40. Claude Code passes this automatically for OAuth-authenticated users.
  41. """
  42. try:
  43. import fcntl
  44. except ImportError:
  45. fcntl = None
  46. import contextlib
  47. import glob
  48. import json
  49. import os
  50. import random
  51. import re
  52. import subprocess
  53. import sys
  54. import threading
  55. import urllib.request
  56. from datetime import datetime
  57. from enum import IntEnum
  58. from typing import Optional, Tuple, Dict, Any, List
  59. # review_api is the importable surface for the agentic-review prompts,
  60. # schemas, and pure filters. External callers (e.g. agentic review harnesses)
  61. # import review_api directly so they run the same eval-covered prompts
  62. # without going through the CC hook protocol. The underscored names below
  63. # alias into it so this script stays the single CC-hook entrypoint.
  64. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  65. import review_api # noqa: E402
  66. from _base import ( # noqa: E402,F401
  67. DEBUG_LOG_FILE, DEBUG_LOG_MAX_BYTES, debug_log,
  68. PROVENANCE_TAG, PROVENANCE_BANNER,
  69. _read_plugin_version_int, _PV, _USAGE, _USAGE_LOCK,
  70. _PRICE_PER_MTOK, _PRICE_DEFAULT, _record_usage, _usage_metrics,
  71. state_dir as _resolve_state_dir,
  72. )
  73. import extensibility # noqa: E402
  74. from patterns import ( # noqa: E402,F401
  75. _JS_EXTS, _PY_EXTS, _DOC_EXTS,
  76. _UNSAFE_DESERIALIZATION_REMINDER, _UNSAFE_YAML_LOAD_REMINDER,
  77. _UNSAFE_TORCH_LOAD_REMINDER, SECURITY_PATTERNS, RuleId,
  78. _RULE_NAME_TO_ID, rule_names_to_mask,
  79. )
  80. from session_state import ( # noqa: E402,F401
  81. _state_key, get_state_file, get_lock_file, cleanup_old_state_files,
  82. load_state, save_state, with_locked_state,
  83. )
  84. from gitutil import ( # noqa: E402,F401
  85. GIT_CMD,
  86. _git_rev_parse_head, _find_git_index, _diff_pathspec, _temp_index,
  87. _git_toplevel, _git_dir, _git_rev_list_range, _git_diff_range,
  88. _detect_main_branch, _git_reflog_recent_commits, _git_name_only,
  89. _git_status_porcelain, _is_ancestor, get_git_diff,
  90. SOURCE_CODE_EXTENSIONS, SOURCE_CODE_BASENAMES,
  91. NON_SOURCE_EXTENSIONLESS_BASENAMES, SKIP_PATH_PATTERNS,
  92. SKIP_FILE_SUFFIXES, _SECURITY_RISK_PATH_TOKENS,
  93. _LOW_PRIORITY_SUFFIXES, _LOW_PRIORITY_PATH_TOKENS,
  94. _prioritize_diff_files, _is_reviewable_source,
  95. extract_file_paths_from_diff, parse_diff_into_files,
  96. filter_preexisting_from_diff,
  97. )
  98. from diffstate import ( # noqa: E402,F401
  99. STOP_LOOP_STATE_TTL_SEC, PREVIOUS_FINDINGS_TTL_SEC,
  100. save_baseline_sha, load_baseline_sha, record_touched_path,
  101. consume_stop_state, restore_unreviewed_stop_state,
  102. get_baseline_file_content, capture_git_baseline,
  103. _REVIEWED_SHAS_BASENAME, _REVIEWED_SHAS_CAP,
  104. _reviewed_shas_path, _load_reviewed_shas, _append_reviewed_shas,
  105. UNTRACKED_BASELINE_CAP, _list_untracked, compute_v2_review_set,
  106. )
  107. import llm # noqa: E402 module ref for reassignable globals (_last_call_claude_http_error etc.)
  108. from llm import ( # noqa: E402,F401
  109. ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, HAS_API_CREDENTIALS,
  110. SECURITY_REVIEW_MODEL, CLAUDE_CODE_SYSTEM_PROMPT,
  111. _last_call_claude_http_error,
  112. ensure_anthropic_reachable,
  113. _last_review_truncated_bytes, _auth_prefer_token,
  114. DIFF_PER_FILE_BYTES, DIFF_TOTAL_BYTES, _AGENTIC_INVESTIGATE_SYSTEM,
  115. _FINDINGS_SCHEMA, _SURVIVED_SCHEMA, _REWAKE_SUMMARY_BUDGET,
  116. _cap_files_for_prompt, _build_auth_headers, _call_claude, _call_claude_dual_or,
  117. _format_vulns_guidance, _format_vulns_summary, _finding_keys, _dedup_against_state,
  118. analyze_code_security, _agentic_commit_review_enabled, agentic_review,
  119. analyze_security_concerns,
  120. )
  121. # LLM-based code security review (enabled by default when API key is available)
  122. # Empty string or unset = enabled (default); "0" = disabled
  123. _enable_code_review_str = os.environ.get("ENABLE_CODE_SECURITY_REVIEW", "1")
  124. ENABLE_CODE_SECURITY_REVIEW = _enable_code_review_str != "0"
  125. # Pattern-based rules (enabled by default; set to "0" to use only LLM review)
  126. # Empty string or unset = enabled (default); "0" = disabled
  127. _enable_pattern_str = os.environ.get("ENABLE_PATTERN_RULES", "1")
  128. ENABLE_PATTERN_RULES = _enable_pattern_str != "0"
  129. # Per-feature kill switches. Each defaults to enabled. Set to "0" to disable
  130. # just that one feature without touching the rest. Motivated by feedback that
  131. # autonomous-agent setups sometimes need to disable specific injection points
  132. # (e.g. the PreToolUse[Task] prompt append, which can read as prompt injection
  133. # to hardened subagents) while keeping the rest of the plugin active. See
  134. # README for a full description of each feature.
  135. # Commit review also honors legacy SECURITY_GUIDANCE_COMMIT_REVIEW=off; see
  136. # is_commit_review_enabled().
  137. ENABLE_COMMIT_REVIEW = os.environ.get("ENABLE_COMMIT_REVIEW", "1") != "0"
  138. # Stop-hook git-diff review only — does NOT gate the commit/push reviews.
  139. # Lets multi-agent / shared-worktree deployments keep the commit reviewer
  140. # (anchored to a fixed SHA from the worker's own `git commit` stdout) while
  141. # turning off the Stop-hook diff (anchored on baseline_sha…HEAD, which a
  142. # sibling agent in the same worktree can move under us). The pre-existing
  143. # ENABLE_CODE_SECURITY_REVIEW gate is shared between Stop and commit/push
  144. # and stays for backwards compat as the all-LLM-review master switch.
  145. ENABLE_STOP_REVIEW = os.environ.get("ENABLE_STOP_REVIEW", "1") != "0"
  146. # Master kill switch. Either SECURITY_GUIDANCE_DISABLE=1 or
  147. # ENABLE_SECURITY_REMINDER=0 disables the plugin entirely. Kept as two names
  148. # because ENABLE_SECURITY_REMINDER predates the rename and some users already
  149. # have it baked into shell rc files; SECURITY_GUIDANCE_DISABLE reads correctly
  150. # as a kill switch (no double-negative).
  151. _disable_str = os.environ.get("SECURITY_GUIDANCE_DISABLE", "").strip().lower()
  152. SECURITY_GUIDANCE_DISABLED = (
  153. _disable_str in ("1", "true", "yes", "on")
  154. or os.environ.get("ENABLE_SECURITY_REMINDER", "1") == "0"
  155. )
  156. # Maximum number of times the stop hook can fire per user turn.
  157. # Allows iterative fixing: Claude stops → review → fix → stop → review again.
  158. # Set to 0 for unlimited (like the old plugin). Default 3 for iterative fixing.
  159. MAX_STOP_HOOK_FIRINGS = int(os.environ.get("MAX_STOP_HOOK_FIRINGS", "3"))
  160. # Cap on source files sent to the LLM reviewer per Stop fire. A stale baseline
  161. # meeting an ungitignored build directory can produce an enormous spurious
  162. # diff; unbounded diffs burn tokens and risk 400 on context length.
  163. MAX_DIFF_FILES = int(os.environ.get("MAX_DIFF_FILES", "30"))
  164. # Appended to all exit(2) guidance so the asyncRewake auto-turn doesn't
  165. # cause the model to abandon the user's original request.
  166. CONTINUATION_SUFFIX = (
  167. "\n\nAfter addressing or acknowledging this finding, continue with the "
  168. "user's original request or continue waiting for their reply — this "
  169. "review is supplementary feedback, not a replacement for your previous "
  170. "response."
  171. )
  172. def emit_metrics(metrics, rewake_summary=None):
  173. """
  174. Write a SyncHookJSONOutput line to stdout for Claude Code to pick up.
  175. For asyncRewake (Stop) hooks, CC scans stdout for the first {-prefixed line
  176. that validates as SyncHookJSONOutput and emits the hook metrics event.
  177. For sync (PostToolUse) hooks, the metrics key in the normal JSON response
  178. is picked up directly.
  179. Constraints: keys ^[a-z][a-z0-9_]{0,39}$, values bool|finite-number,
  180. 20-key cap (was 10 in older CC versions).
  181. `pv` and the tok_*/cost_usd usage block are PREPENDED so they survive any
  182. future overflow — CC keeps only the first 20 keys, so insertion order
  183. decides what drops. The old `len(metrics) < 10` guard was load-bearing for
  184. the same reason but stale: once `rate_count` was added to every
  185. commit-review emit, the with-vulns dict hit 10 keys, `pv` was skipped, and
  186. findings metrics landed without a plugin version attached, breaking
  187. per-version breakdowns.
  188. `rewake_summary` (asyncRewake only): per-run override of the static
  189. rewakeSummary in hooks.json, shown to the user in the terminal as the
  190. task-notification one-liner. Must be in the same JSON line as the metrics
  191. because CC stops scanning stdout after the first {-prefixed line.
  192. """
  193. head = {}
  194. if _PV and "pv" not in metrics:
  195. head["pv"] = _PV
  196. head.update(_usage_metrics())
  197. if head:
  198. metrics = {**head, **metrics}
  199. out = {"metrics": metrics}
  200. if rewake_summary:
  201. out["rewakeSummary"] = rewake_summary
  202. print(json.dumps(out), flush=True)
  203. # =====================================================================
  204. # State management
  205. # =====================================================================
  206. #
  207. # Low-level state-file plumbing (_state_key, get_state_file,
  208. # get_lock_file, cleanup_old_state_files, load_state, save_state,
  209. # with_locked_state) moved to session_state.py and re-exported above.
  210. def atomic_check_and_mark_warning(session_id, warning_key):
  211. """
  212. Atomically check if a warning has been shown and mark it as shown if not.
  213. Returns True if this is the first time seeing this warning (should show it),
  214. False if it was already shown (should skip it).
  215. """
  216. def _check(state):
  217. warnings = state["shown_warnings"]
  218. if warning_key in warnings:
  219. return False
  220. warnings.append(warning_key)
  221. return True
  222. result = with_locked_state(session_id, _check)
  223. return result if result is not None else True
  224. def atomic_check_counter(session_id, counter_key, max_count):
  225. """
  226. Atomically check if a counter has reached its limit and increment if not.
  227. Returns True if the counter is below max_count (should proceed),
  228. False if it has reached or exceeded max_count (should skip).
  229. """
  230. def _check(state):
  231. counters = state.get("counters", {})
  232. current = counters.get(counter_key, 0)
  233. if current >= max_count:
  234. return False
  235. counters[counter_key] = current + 1
  236. state["counters"] = counters
  237. return True
  238. result = with_locked_state(session_id, _check)
  239. return result if result is not None else True
  240. def atomic_check_rate_limit(session_id, key, max_per_window, window_s):
  241. """Rolling-window rate limit: allow at most `max_per_window` calls per
  242. `window_s` seconds, per (session_id, key).
  243. Returns (allowed: bool, count_in_window: int). count_in_window is the
  244. post-decision count (i.e., includes this call if allowed) so callers can
  245. emit it directly as a telemetry gauge.
  246. Replaces session-lifetime `atomic_check_counter` for commit-review and
  247. push-sweep. Telemetry showed a small but persistent share of sessions hit
  248. the lifetime cap, and those were multi-day persistent sessions that then
  249. lost coverage for many subsequent commits — not burst abusers. A rolling
  250. hour keeps the same cost ceiling for any 1h window while letting long
  251. sessions regain coverage.
  252. State key: rate_limits: {"<key>": [ts, ts, ...]}. Timestamps are pruned
  253. on every call so the list is bounded by max_per_window; no migration
  254. needed from the old `counters` dict — different key.
  255. """
  256. import time as _time
  257. now = _time.time()
  258. cutoff = now - window_s
  259. def _check(state):
  260. buckets = state.setdefault("rate_limits", {})
  261. ts_list = buckets.get(key, [])
  262. # Prune; tolerate non-numeric junk from a corrupted state file.
  263. ts_list = [t for t in ts_list if isinstance(t, (int, float)) and t > cutoff]
  264. if len(ts_list) >= max_per_window:
  265. buckets[key] = ts_list
  266. return False, len(ts_list)
  267. ts_list.append(now)
  268. buckets[key] = ts_list
  269. return True, len(ts_list)
  270. result = with_locked_state(session_id, _check)
  271. # State unavailable → fail-open (same posture as atomic_check_counter).
  272. return result if result is not None else (True, 0)
  273. # =====================================================================
  274. # Warning outcome tracking
  275. #
  276. # Records each pattern warning as pending when it fires. At Stop, sweep
  277. # all pending entries: re-read each file, re-check patterns, and emit a
  278. # fixed-vs-unresolved tally. No per-edit work — pending is recorded only
  279. # when a pattern matches (rare), and the sweep runs once at session end.
  280. #
  281. # State key: pending_warnings: {"<file>:<rule>": true}
  282. # =====================================================================
  283. def record_pending_warnings(session_id, file_path, rule_names):
  284. """Mark file:rule pairs as pending for the Stop-hook outcome sweep."""
  285. def _record(state):
  286. pending = state.get("pending_warnings")
  287. if not isinstance(pending, dict):
  288. pending = {}
  289. state["pending_warnings"] = pending
  290. for rule in rule_names:
  291. pending[f"{file_path}:{rule}"] = True
  292. with_locked_state(session_id, _record)
  293. def sweep_pending_warnings(session_id):
  294. """
  295. Stop-hook final sweep. Re-read every file in pending_warnings, re-check
  296. patterns, and return (fixed, unresolved, unresolved_mask). Clears state.
  297. A file that's been deleted counts as fixed — the dangerous code is gone.
  298. Never raises — this is telemetry and must not break the Stop hook.
  299. """
  300. def _sweep(state):
  301. try:
  302. pending = state.get("pending_warnings")
  303. if not isinstance(pending, dict) or not pending:
  304. return 0, 0, 0
  305. by_file = {}
  306. for key in pending:
  307. if not isinstance(key, str) or ":" not in key:
  308. continue
  309. fp, _, rule = key.rpartition(":")
  310. by_file.setdefault(fp, set()).add(rule)
  311. unresolved = []
  312. fixed = 0
  313. for fp, rules in by_file.items():
  314. try:
  315. with open(fp, "r", errors="replace") as f:
  316. still_matching = {r for r, _ in check_patterns(fp, f.read())}
  317. except (OSError, IOError):
  318. still_matching = set()
  319. for rule in rules:
  320. if rule in still_matching:
  321. unresolved.append(rule)
  322. else:
  323. fixed += 1
  324. state["pending_warnings"] = {}
  325. # Filter to known rules so a renamed/removed rule in old state
  326. # doesn't KeyError rule_names_to_mask.
  327. known = [r for r in unresolved if r in _RULE_NAME_TO_ID]
  328. return fixed, len(unresolved), rule_names_to_mask(known)
  329. except Exception as e:
  330. debug_log(f"sweep_pending_warnings failed: {e}")
  331. return 0, 0, 0
  332. result = with_locked_state(session_id, _sweep)
  333. return result if result is not None else (0, 0, 0)
  334. # =====================================================================
  335. # Git baseline management
  336. # =====================================================================
  337. # =====================================================================
  338. # Pattern matching
  339. # =====================================================================
  340. def check_patterns(file_path, content):
  341. """Check if file path or content matches any security patterns. Returns ALL matches."""
  342. normalized_path = file_path.lstrip("/")
  343. matches = []
  344. for pattern in list(SECURITY_PATTERNS) + extensibility.user_patterns():
  345. # path_filter is a gate: when present, the rule only applies to
  346. # matching paths. Distinct from path_check, which is itself a
  347. # positive match condition (e.g. .github/workflows/).
  348. if "path_filter" in pattern:
  349. try:
  350. if not pattern["path_filter"](normalized_path):
  351. continue
  352. except Exception:
  353. continue
  354. matched = False
  355. if "path_check" in pattern:
  356. try:
  357. if pattern["path_check"](normalized_path):
  358. matched = True
  359. except Exception:
  360. pass
  361. if not matched and "substrings" in pattern and content:
  362. for substring in pattern["substrings"]:
  363. if substring in content:
  364. matched = True
  365. break
  366. if not matched and "regex" in pattern and content:
  367. try:
  368. if re.search(pattern["regex"], content):
  369. matched = True
  370. except Exception:
  371. pass
  372. if matched:
  373. matches.append((pattern["ruleName"], pattern["reminder"]))
  374. return matches
  375. def extract_content_from_input(tool_name, tool_input):
  376. """Extract content to check from tool input based on tool type."""
  377. if tool_name == "Write":
  378. return tool_input.get("content", "")
  379. elif tool_name == "Edit":
  380. return tool_input.get("new_string", "")
  381. elif tool_name == "MultiEdit":
  382. edits = tool_input.get("edits", [])
  383. if edits:
  384. return " ".join(edit.get("new_string", "") for edit in edits)
  385. return ""
  386. return ""
  387. # =====================================================================
  388. # Hook handlers
  389. # =====================================================================
  390. def handle_user_prompt_submit(input_data):
  391. """
  392. Handle UserPromptSubmit — capture git baseline SHA.
  393. Called on every user prompt. Updates the baseline so the stop hook
  394. only reviews changes made since the last prompt.
  395. Does NOT reset touched_paths/fire_count/previous_findings — those are
  396. consumed by Stop (consume_stop_state) and time-expired respectively.
  397. UPS racing the asyncRewake Stop hook caused a meaningful share of reviews
  398. to be lost when the wipe landed before Stop's state read.
  399. """
  400. cwd = input_data.get("cwd", "")
  401. if not cwd:
  402. debug_log("UPS: no cwd, skipping baseline capture")
  403. sys.exit(0)
  404. session_id = input_data.get("session_id", "default")
  405. # stash-create and ls-files both walk the worktree (~2-5s each in a very
  406. # large repo). Run them concurrently so UPS latency stays ≈ max(both).
  407. import concurrent.futures as _cf
  408. with _cf.ThreadPoolExecutor(max_workers=2) as _ex:
  409. _f_sha = _ex.submit(capture_git_baseline, cwd)
  410. _f_ut = _ex.submit(_list_untracked, cwd)
  411. sha = _f_sha.result()
  412. # Always capture the untracked snapshot. `git stash create` returns
  413. # empty when there are no TRACKED changes, but pre-existing untracked
  414. # files still need to be excluded from the next Stop's review_set —
  415. # otherwise an untracked-only working tree gets every untracked file
  416. # reviewed on every turn until something tracked is dirtied.
  417. untracked_now = _f_ut.result() or {}
  418. head = _git_rev_parse_head(cwd)
  419. # If the previous turn's Stop hook never ran (user interrupt, follow-up
  420. # during work, tool-reject, model crash, maxTurns, PostToolUse block…),
  421. # touched_paths is still populated because consume_stop_state is the only
  422. # consumer and it runs under the state lock. Overwriting baseline_sha now
  423. # would re-baseline *past* those unreviewed edits, making them permanently
  424. # invisible to the next Stop. Preserve the old baseline so the next Stop
  425. # diffs the aborted turn's edits plus the new turn's edits together.
  426. preserved = {"value": False}
  427. def _save(state):
  428. # Only preserve if there's actually an old baseline to preserve.
  429. # First UPS of a session can have touched_paths if PostToolUse
  430. # somehow ran first (print mode, odd harnesses) — in that case
  431. # we still need to capture a baseline.
  432. if state.get("touched_paths") and state.get("baseline_sha"):
  433. preserved["value"] = True
  434. return
  435. if sha:
  436. state["baseline_sha"] = sha
  437. state["head_at_capture"] = head
  438. # untracked_at_baseline is independent of whether the stash produced
  439. # a SHA — write it unconditionally so compute_v2_review_set's
  440. # preexisting-untracked exclusion works in untracked-only trees.
  441. state["untracked_at_baseline"] = untracked_now
  442. with_locked_state(session_id, _save)
  443. if preserved["value"]:
  444. debug_log(
  445. "UPS: preserving prior baseline — previous Stop hook never "
  446. "consumed touched_paths (likely user interrupt / aborted turn)"
  447. )
  448. elif sha:
  449. debug_log(f"Captured git baseline: {sha[:12]}")
  450. else:
  451. debug_log("Failed to capture git baseline (not a git repo?)")
  452. sys.exit(0)
  453. def _resolve_amend_pre_sha(repo_root, expected_post_sha=None):
  454. """For a `git commit --amend` we just ran, return the pre-amend SHA via
  455. reflog, or None if it can't be safely determined.
  456. expected_post_sha: the post-amend SHA the caller parsed from bash stdout
  457. (or reflog). If provided, HEAD@{0} of `repo_root` must match it (prefix
  458. compare — bash stdout SHAs are abbreviated, reflog %H is 40 chars) before
  459. we trust the reflog-derived pre-amend SHA. This guards against the
  460. cross-repo case (`cd ../other && git commit --amend && cd -`) where
  461. `repo_root` happens to have its own recent amend that's unrelated to
  462. the bash command we're reviewing.
  463. We require HEAD@{0}'s reflog subject to start with `commit (amend)` —
  464. otherwise our `--amend` regex matched something that didn't actually
  465. perform an amend (e.g., `git commit --amend --dry-run`, aliased commands,
  466. aborted amends), and HEAD@{1} would be the wrong commit. Also requires
  467. HEAD@{1} to NOT itself be an amend, since back-to-back amends would have
  468. HEAD@{1} as the previous-amend's post state — the original commit we
  469. want to compare against is then HEAD@{2}, but at that point we're
  470. reaching and fall back to a full review.
  471. Bytes + decode('utf-8', errors='replace'): reflog subjects embed commit
  472. subjects, which git stores as raw bytes (commit messages may be latin-1
  473. / cp1252 / etc.). text=True would raise UnicodeDecodeError (a
  474. ValueError, not OSError) on non-UTF8 bytes and crash the hook.
  475. """
  476. if not repo_root:
  477. return None
  478. try:
  479. r = subprocess.run(
  480. [*GIT_CMD, "log", "-g", "-2", "--format=%H|%gs", "HEAD"],
  481. cwd=repo_root, capture_output=True, timeout=5,
  482. )
  483. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  484. return None
  485. if r.returncode != 0:
  486. return None
  487. stdout_text = r.stdout.decode("utf-8", errors="replace")
  488. lines = [ln for ln in stdout_text.splitlines() if "|" in ln]
  489. if len(lines) < 2:
  490. return None
  491. head0_sha, _, head0_subj = lines[0].partition("|")
  492. head1_sha, _, head1_subj = lines[1].partition("|")
  493. if not head0_subj.startswith("commit (amend)"):
  494. return None
  495. if head1_subj.startswith("commit (amend)"):
  496. return None
  497. # Cross-repo guard: the post-amend SHA the caller is about to review must
  498. # match HEAD@{0} of repo_root. Otherwise the bash command was likely run
  499. # in a different repo than repo_root, and the reflog we just read is
  500. # unrelated. Prefix-compare: expected_post_sha is typically the 7-char
  501. # abbreviated SHA captured from bash stdout by _COMMIT_SHA_RE (git's
  502. # default core.abbrev floor), while head0_sha is the full 40-char %H —
  503. # strict equality would always fail and silently disable the delta path.
  504. if expected_post_sha and not head0_sha.startswith(expected_post_sha):
  505. return None
  506. return head1_sha or None
  507. # git-only signals that corroborate a real commit object — NOT emitted by
  508. # pre-commit / lint-staged / husky hook output, which can contain bracketed
  509. # labels like `[pre-commit abc1234]` that otherwise look like a commit line.
  510. _COMMIT_DIFFSTAT_PATTERNS = [
  511. re.compile(r'\b\d+ files? changed'),
  512. re.compile(r'^ create mode ', re.MULTILINE),
  513. re.compile(r'^ delete mode ', re.MULTILINE),
  514. re.compile(r'^ rename ', re.MULTILINE),
  515. ]
  516. # Capture-group form of the [branch sha] pattern. Mirrors Claude Code's own
  517. # commit-id parsing, but tolerates spaces before the
  518. # sha (covers `[detached HEAD abc1234]`). 7–40 hex chars: git's abbrev floor
  519. # through full sha; the abbrev resolves fine with `git show`. Anchored to
  520. # line-start so a `[hex]` in the commit subject (`[main abc] Revert [e38]`)
  521. # or trailing hook output isn't picked up and fed to `git show`.
  522. _COMMIT_SHA_RE = re.compile(r'^\[[^\]]*?\b([0-9a-f]{7,40})\]', re.MULTILINE)
  523. # Regex matching `git commit` commands. Mirrors Claude Code's own commit
  524. # detection — it does NOT tolerate `git -c k=v commit` global options, which
  525. # keeps this hook aligned with CC's commit attribution on what counts as a
  526. # commit.
  527. _GIT_COMMIT_RE = re.compile(r'\bgit\s+commit(?:\s|$)')
  528. _GIT_AMEND_RE = re.compile(r'\s--amend\b')
  529. # Rolling-window cap on LLM commit-review calls. See atomic_check_rate_limit
  530. # docstring for the rationale that motivated the switch from a lifetime cap.
  531. # `MAX_COMMIT_REVIEWS_PER_SESSION` is read for backward-compat with users who
  532. # tuned it; the value is reinterpreted as per-hour.
  533. MAX_COMMIT_REVIEWS_PER_HOUR = int(
  534. os.environ.get("MAX_COMMIT_REVIEWS_PER_HOUR")
  535. or os.environ.get("MAX_COMMIT_REVIEWS_PER_SESSION", "20")
  536. )
  537. COMMIT_REVIEW_RATE_WINDOW_S = int(
  538. os.environ.get("COMMIT_REVIEW_RATE_WINDOW_S", "3600")
  539. )
  540. # ─── push-sweep ─────────────────────────────────────────────────────────────
  541. #
  542. # Mirrors Claude Code's own push-command matching — tolerates `git -C <p>` /
  543. # `git -c k=v` global options. The hooks.json `Bash(git push:*)` matcher
  544. # (subcommand prefix) doesn't, but those forms are rare in practice
  545. # and the python only ever runs after CC's matcher fired, so this regex is a
  546. # defensive re-gate, not a widening — `git -C path push` won't reach python
  547. # unless chained with a plain `git push` in the same compound command.
  548. #
  549. # `gh pr create` is intentionally NOT a separate hooks.json matcher: gh runs
  550. # `git push` as a child process, which CC's matcher doesn't observe (it sees
  551. # only the top-level `gh pr create` argv). A separate `Bash(gh pr create:*)`
  552. # entry would buy minimal extra coverage (sessions that push only via gh) at
  553. # the cost of an extra python spawn on every `... && gh pr create` compound
  554. # (the common case). Those sessions are caught on their next standalone `git push`.
  555. _GIT_PUSH_RE = re.compile(
  556. r'\bgit(?:\s+-[cC]\s+\S+|\s+--\S+=\S+)*\s+push\b'
  557. )
  558. # `git push` stdout: "abc1234..def5678 branch -> branch" (or `+abc..def` on
  559. # force, `* [new branch]` on first push). The left sha is where the remote
  560. # was BEFORE this push — exactly the base we need. Captures (old, new,
  561. # local-ref) so the handler can verify the pushed ref == HEAD before
  562. # diffing — `git push origin other` while on a different branch would
  563. # otherwise diff the wrong range.
  564. _PUSH_RANGE_RE = re.compile(
  565. r'^\s*\+?\s*([0-9a-f]{7,40})\.\.\.?([0-9a-f]{7,40})\s+(\S+)\s+->\s+\S+',
  566. re.MULTILINE,
  567. )
  568. MAX_PUSH_SWEEP_FILES = int(os.environ.get("SG_PUSH_SWEEP_MAX_FILES", "30"))
  569. MAX_PUSH_SWEEP_RANGE = int(os.environ.get("SG_PUSH_SWEEP_MAX_RANGE", "50"))
  570. PUSH_SWEEP_REPORT_CAP = int(os.environ.get("SG_PUSH_SWEEP_REPORT_CAP", "3"))
  571. def _claim_bash_hook_once(input_data):
  572. """De-dupe across hooks.json `if` matchers firing for the same Bash call.
  573. `git commit -m x && git push` matches both `Bash(git commit:*)` and
  574. `Bash(git push:*)` `if` configs → CC spawns this script twice with the
  575. SAME `tool_use_id`. The first spawn atomically creates a
  576. sentinel under `.git/`; subsequent spawns see it and exit early. Avoids
  577. redundant LLM calls (and the redundant asyncRewake) on compound commands.
  578. Returns True if this spawn won the claim (or no de-dupe is possible),
  579. False if another spawn already claimed it.
  580. Sentinel is per-clone (`.git/sg-hook-once-<tool_use_id>`), not /tmp,
  581. so concurrent CC sessions in *different* repos don't collide. Stale
  582. sentinels (>5min) are GC'd opportunistically.
  583. """
  584. tuid = input_data.get("tool_use_id")
  585. cwd = input_data.get("cwd")
  586. if not tuid or not cwd:
  587. return True
  588. gd = _git_dir(_git_toplevel(cwd) or cwd)
  589. if not gd:
  590. return True
  591. # GC: best-effort sweep of stale sentinels so they don't accumulate.
  592. import time as _time
  593. now = _time.time()
  594. try:
  595. for name in os.listdir(gd):
  596. if name.startswith("sg-hook-once-"):
  597. p = os.path.join(gd, name)
  598. try:
  599. if now - os.path.getmtime(p) > 300:
  600. os.unlink(p)
  601. except OSError:
  602. pass
  603. except OSError:
  604. pass
  605. # Sanitize tuid into a filesystem-safe basename — defensive, the value is
  606. # CC-generated (toolu_<b64ish>), but it ends up in a path.
  607. safe = re.sub(r"[^A-Za-z0-9_-]", "_", tuid)[:80]
  608. sentinel = os.path.join(gd, f"sg-hook-once-{safe}")
  609. try:
  610. fd = os.open(sentinel, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
  611. os.close(fd)
  612. return True
  613. except FileExistsError:
  614. return False
  615. except OSError:
  616. # Can't write sentinel (read-only fs, perms) — proceed rather than
  617. # silently dropping the review.
  618. return True
  619. def is_push_sweep_enabled():
  620. """Gate for the push-sweep PostToolUse[Bash] hook.
  621. Enabled by default. ENABLE_COMMIT_REVIEW=0 remains the unconditional
  622. kill switch (push-sweep reuses the same review pipeline and budget).
  623. SG_PUSH_SWEEP is the per-user override (=1/on or =0/off) checked
  624. next so users can opt out.
  625. """
  626. if not ENABLE_COMMIT_REVIEW:
  627. return False
  628. v = os.environ.get("SG_PUSH_SWEEP", "").strip().lower()
  629. if v in ("1", "on"):
  630. return True
  631. if v in ("0", "off"):
  632. return False
  633. return True
  634. PUSH_SWEEP_ENABLED = is_push_sweep_enabled()
  635. def _compute_push_sweep_base(prev_upstream, push_range, reviewed):
  636. """Advance the diff base past the contiguous reviewed prefix.
  637. Spec: review `git diff B..HEAD` where `B` is the newest commit such that
  638. `prev_upstream..B` is entirely in `reviewed`. Returns (B, unreviewed_tail).
  639. `B == None` means the whole range is reviewed (caller should skip).
  640. `push_range` must be oldest→newest.
  641. Examples (✓=reviewed, ✗=not):
  642. [✓1, ✗2, ✓3] → B=1, tail=[2,3] (cannot trim suffix; Read is at HEAD)
  643. [✓1, ✓2, ✓3] → B=None (all reviewed → skip)
  644. [✗1, ✓2, ✗3] → B=prev_upstream, tail=[1,2,3]
  645. [] → B=None
  646. """
  647. i = 0
  648. while i < len(push_range) and push_range[i] in reviewed:
  649. i += 1
  650. if i == len(push_range):
  651. return None, []
  652. base = push_range[i - 1] if i > 0 else prev_upstream
  653. return base, push_range[i:]
  654. def _push_section(bash_output):
  655. """Return the slice of `bash_output` that contains the push's range lines.
  656. `_PUSH_RANGE_RE` is not push-specific — `git fetch` and `git pull` print
  657. range lines (`abc..def branch -> origin/branch`) in the same format. On
  658. chained calls the Bash tool returns combined stdout+stderr, so a naive
  659. `_PUSH_RANGE_RE.finditer(bash_output)` matches both sections and a
  660. fetch+push compound trips the multi-ref skip.
  661. `git push` prints `To <remote>` immediately before its range lines;
  662. `git fetch`/`git pull` prints `From <remote>` before theirs. The slice
  663. is symmetric: start at the LAST `To <remote>` header (strips fetch output
  664. that ran *before* the push, e.g. `git fetch && git push`), and end at
  665. the next `From <remote>` after that (strips fetch output that ran
  666. *after* the push, e.g. `git push && git fetch`).
  667. If no `To ` header is present (push failed before connecting, output
  668. suppressed by `-q`) the full buffer is returned and the caller's
  669. other guards handle it.
  670. """
  671. if not bash_output:
  672. return ""
  673. # Match line-anchored "To " — look for "\nTo " or "To " at start-of-string.
  674. idx = bash_output.rfind("\nTo ")
  675. if idx >= 0:
  676. section = bash_output[idx:]
  677. elif bash_output.startswith("To "):
  678. section = bash_output
  679. else:
  680. return bash_output
  681. # Strip a trailing fetch/pull `From <remote>` block (push && fetch /
  682. # push && pull, or any wrapper that re-syncs after the push).
  683. end = section.find("\nFrom ")
  684. if end >= 0:
  685. section = section[:end]
  686. return section
  687. def _detect_prev_upstream(repo_root, bash_output):
  688. """Where the remote was BEFORE this push.
  689. Preference order:
  690. 1. Parse `abc..def` from push stdout — authoritative, exact.
  691. 2. `<branch>@{u}@{1}` — the remote-tracking ref's reflog position before
  692. this push moved it. PostToolUse runs after `git push` completes, so
  693. `@{u}` is already updated and `@{u}@{1}` is the prior value.
  694. 3. merge-base with the detected main branch — first push of a new
  695. branch (`* [new branch]` in output, no upstream reflog yet).
  696. Returns a resolvable ref/sha or None.
  697. """
  698. m = _PUSH_RANGE_RE.search(_push_section(bash_output or ""))
  699. if m:
  700. return m.group(1)
  701. # @{u}@{1} — only meaningful if an upstream is configured.
  702. for ref in ("@{u}@{1}", "@{push}@{1}"):
  703. try:
  704. r = subprocess.run(
  705. [*GIT_CMD, "rev-parse", "--verify", "-q", ref],
  706. cwd=repo_root, capture_output=True, text=True, timeout=5,
  707. )
  708. if r.returncode == 0 and r.stdout.strip():
  709. return r.stdout.strip()
  710. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  711. pass
  712. main = _detect_main_branch(repo_root)
  713. if main:
  714. try:
  715. r = subprocess.run(
  716. [*GIT_CMD, "merge-base", "HEAD", main],
  717. cwd=repo_root, capture_output=True, text=True, timeout=5,
  718. )
  719. if r.returncode == 0 and r.stdout.strip():
  720. return r.stdout.strip()
  721. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  722. pass
  723. return None
  724. def is_commit_review_enabled():
  725. """Gate for the commit-review PostToolUse[Bash] hook.
  726. Commit review is enabled by default; ENABLE_COMMIT_REVIEW=0 remains the
  727. unconditional kill switch and SECURITY_GUIDANCE_COMMIT_REVIEW (on/off)
  728. remains a legacy per-user override; everything else defaults on.
  729. commit_review_on is still emitted in metrics for continuity.
  730. """
  731. if not ENABLE_COMMIT_REVIEW:
  732. return False
  733. override = os.environ.get("SECURITY_GUIDANCE_COMMIT_REVIEW", "").strip().lower()
  734. if override in ("on", "off"):
  735. return override == "on"
  736. return True
  737. COMMIT_REVIEW_ENABLED = is_commit_review_enabled()
  738. def _agentic_review_with_race(
  739. repo_root: str,
  740. diff_files: List[Tuple[str, str]],
  741. rel_touched: List[str],
  742. previous_findings: List[Dict[str, Any]],
  743. ) -> Tuple[Optional[str], List[Dict[str, Any]], Dict[str, Any]]:
  744. """Race the agentic reviewer against a delayed single-shot fallback.
  745. Agentic starts at t=0. After SG_AGENTIC_RACE_DELAY_S (default 180s), the
  746. single-shot diff reviewer also starts. Whichever finishes first wins. If
  747. agentic finishes before the delay elapses, the fallback never runs.
  748. Metrics added:
  749. race_winner : 1 = agentic won, 2 = fallback won (CC accepts only
  750. bool/finite-number metric values — strings would discard the dict)
  751. race_delay_s : the configured delay
  752. race_started : 1 if the fallback was actually launched, else 0
  753. Only the commit-review handler calls this — external harnesses invoke
  754. agentic_review() directly and are unaffected. SG_AGENTIC_NO_RACE=1
  755. disables the race for any other caller that wants pure agentic.
  756. """
  757. import queue as _queue
  758. import threading as _th
  759. import time as _t
  760. if os.environ.get("SG_AGENTIC_NO_RACE") == "1":
  761. return agentic_review(repo_root, diff_files, rel_touched)
  762. delay_s = int(os.environ.get("SG_AGENTIC_RACE_DELAY_S", "180"))
  763. q: "_queue.Queue[Tuple[str, Any]]" = _queue.Queue(maxsize=1)
  764. fallback_started = _th.Event()
  765. def _agentic() -> None:
  766. try:
  767. r = agentic_review(repo_root, diff_files, rel_touched)
  768. except Exception as e: # pragma: no cover — crash → let fallback win
  769. r = (None, [], {"agentic_fallback": f"race_crash:{type(e).__name__}"})
  770. try:
  771. q.put_nowait(("agentic", r))
  772. except _queue.Full:
  773. pass
  774. def _fallback() -> None:
  775. _t.sleep(delay_s)
  776. if not q.empty():
  777. return # agentic finished within the delay — never start fallback
  778. fallback_started.set()
  779. try:
  780. g, v = analyze_code_security(
  781. diff_files, is_diff=True, previous_findings=previous_findings
  782. )
  783. except Exception as e: # pragma: no cover
  784. g, v = None, []
  785. try:
  786. q.put_nowait(("fallback", (g, v, {"agentic": False})))
  787. except _queue.Full:
  788. pass
  789. _th.Thread(target=_agentic, daemon=True).start()
  790. _th.Thread(target=_fallback, daemon=True).start()
  791. winner, (g, v, m) = q.get()
  792. m = dict(m) # don't mutate the callee's metrics dict
  793. m["race_winner"] = 1 if winner == "agentic" else 2
  794. m["race_delay_s"] = delay_s
  795. m["race_started"] = 1 if fallback_started.is_set() else 0
  796. return g, v, m
  797. def handle_commit_review_posttooluse(input_data):
  798. """PostToolUse handler for Bash — reviews git commits for security issues.
  799. Runs as asyncRewake: detects `git commit` in the Bash command, parses
  800. the resulting SHA(s) from the Bash stdout `[branch sha] msg` line, runs
  801. `git show -p <sha>` per SHA, sends the combined diff through
  802. analyze_code_security, and exits with code 2 (stderr findings) to wake
  803. the model. Deduplicates against the shared previous_findings state so
  804. the Stop hook won't re-flag the same (filePath, vulnerableCode) pair.
  805. """
  806. session_id = input_data.get("session_id", "default")
  807. tool_input = input_data.get("tool_input", {})
  808. tool_response = input_data.get("tool_response", {})
  809. cwd = input_data.get("cwd", "")
  810. command = tool_input.get("command", "")
  811. if not isinstance(command, str) or not _GIT_COMMIT_RE.search(command):
  812. # Defensive only — hooks.json's `"if": "Bash(git commit:*)"` is the
  813. # real gate so CC never spawns python3 for ls/grep/etc. This catches
  814. # cases where CC's command matching fails open and spawns the hook anyway.
  815. sys.exit(0)
  816. debug_log(f"Commit review: detected git commit in command")
  817. # Bash tool_response has no exit_code field (only stdout, stderr,
  818. # interrupted), so success is inferred from the output text — the same
  819. # heuristic Claude Code itself uses.
  820. if not isinstance(tool_response, dict):
  821. tool_response = {}
  822. stdout = tool_response.get("stdout", "") or ""
  823. stderr = tool_response.get("stderr", "") or ""
  824. bash_output = stdout + "\n" + stderr
  825. interrupted = bool(tool_response.get("interrupted"))
  826. # Require BOTH a line-anchored `[branch sha]` AND a git-only diffstat
  827. # signal before treating the tool call as a successful commit. The old
  828. # `any()` check false-positived on (a) pre-commit/husky/lint-staged hooks
  829. # emitting labels like `[pre-commit abc1234]`, and on (b) chained
  830. # `git commit || git log --stat` where `N files changed` appears in output
  831. # even though the commit itself failed.
  832. commit_succeeded = (
  833. not interrupted
  834. and _COMMIT_SHA_RE.search(bash_output) is not None
  835. and any(p.search(bash_output) for p in _COMMIT_DIFFSTAT_PATTERNS)
  836. )
  837. # commit_review_on emitted on every path so telemetry can filter on
  838. # commit_review and group by commit_review_on.
  839. _base = {"commit_review": True, "commit_review_on": COMMIT_REVIEW_ENABLED}
  840. # Reflog fallback for hidden stdout. Analysis of skip_reason=21 emissions
  841. # showed a large share were commits that DID succeed
  842. # but whose `[branch sha]` line was hidden by piping/redirection/-q
  843. # (e.g., `git commit -m ... 2>&1 | tail -3`). A HEAD@{0}
  844. # reflog check substantially reduced this skip; follow-up analysis found
  845. # the residual is dominated by (a) chained commands moving HEAD@{0} past
  846. # `commit:` (`git commit && git push`), and (b) the `_obvious_noop` guard
  847. # false-positiving on chained `git status` output after a successful -q
  848. # commit. Widening to the last-5-entries × 120s scan and dropping the noop
  849. # guard fixes both. The reviewed-shas dedup below prevents the wider window
  850. # from re-reviewing a prior Bash call's commit, and is the same file
  851. # push-sweep reads — so a SHA is reviewed at most once across both
  852. # surfaces. See _git_reflog_recent_commits docstring for cross-repo /
  853. # race safety.
  854. _reflog_shas: List[str] = []
  855. _skip_21_sub = 0
  856. if not commit_succeeded and not interrupted and cwd:
  857. _root = _git_toplevel(cwd)
  858. _fresh, _stale = _git_reflog_recent_commits(_root)
  859. if _fresh:
  860. _already = _load_reviewed_shas(_root)
  861. _reflog_shas = [s for s in _fresh if s not in _already]
  862. if _reflog_shas:
  863. commit_succeeded = True
  864. debug_log(
  865. f"Commit review: stdout had no `[branch sha]`; reflog "
  866. f"shows {len(_reflog_shas)} fresh unreviewed commit(s) "
  867. f"({_reflog_shas[0][:12]}...)"
  868. )
  869. else:
  870. # Fresh commit(s) in reflog but all already in
  871. # sg-reviewed-shas — likely a Bash retry or the commit was
  872. # reviewed via a prior fire. Correct to skip; sub=2 lets telemetry
  873. # split this from genuine fails.
  874. _skip_21_sub = 2
  875. elif _stale:
  876. _skip_21_sub = 3 # commit entries exist but all >120s old
  877. else:
  878. _skip_21_sub = 4 # no commit-action entries — genuine fail
  879. if not commit_succeeded:
  880. debug_log("Commit review: commit did not succeed, skipping")
  881. emit_metrics({"skipped": True, "skip_reason": 21, **_base,
  882. **({"skip_21_sub": 1} if interrupted
  883. else {"skip_21_sub": _skip_21_sub} if _skip_21_sub
  884. else {})})
  885. sys.exit(0)
  886. if not COMMIT_REVIEW_ENABLED:
  887. debug_log("Commit review: disabled, skipping")
  888. emit_metrics({"skipped": True, "skip_reason": 32, **_base})
  889. sys.exit(0)
  890. if not ENABLE_CODE_SECURITY_REVIEW or not HAS_API_CREDENTIALS:
  891. debug_log("Commit review: LLM review disabled or no API credentials")
  892. emit_metrics({"skipped": True, "skip_reason": 22, **_base})
  893. sys.exit(0)
  894. if not ensure_anthropic_reachable():
  895. debug_log("Commit review: api.anthropic.com unreachable")
  896. emit_metrics({"skipped": True, "skip_reason": 24, **_base})
  897. sys.exit(0)
  898. if not cwd:
  899. debug_log("Commit review: no cwd")
  900. emit_metrics({"skipped": True, "skip_reason": 25, **_base})
  901. sys.exit(0)
  902. repo_root = _git_toplevel(cwd)
  903. if not repo_root:
  904. debug_log("Commit review: not in a git repo")
  905. emit_metrics({"skipped": True, "skip_reason": 26, **_base})
  906. sys.exit(0)
  907. # Pin the review to the exact SHA the Bash command produced, parsed from
  908. # its stdout. Reviewing HEAD instead is wrong when the commit was made in
  909. # a different repo than the hook's cwd (`cd ../other && git commit && cd -`,
  910. # subshells), or when a second commit lands before this async hook reaches
  911. # `git show` — both would review an unrelated commit. The reflog-action
  912. # fallback above is the narrow exception: it only fires when output gave
  913. # us nothing AND the cwd repo's own reflog confirms a `commit:` just
  914. # happened there, which rules out the cross-repo case.
  915. #
  916. # Take only the LAST match: pre-commit/husky hooks can print bracketed
  917. # labels like `[pre-commit abc1234]` that precede the real `[branch sha]`
  918. # line; chained commands like `git commit && git commit` produce multiple
  919. # real SHAs and we want the most recent. The real commit line is always
  920. # last in git's own output — the earlier matches are either decoys or
  921. # superseded commits.
  922. if _reflog_shas:
  923. # Output-based detection already failed above; the reflog SHAs are the
  924. # authoritative ones. Don't re-parse bash_output here — any bracketed
  925. # token it contains is by construction NOT the `[branch sha]` line
  926. # (or commit_succeeded would have been True via the fast path). The
  927. # list is newest-first and may contain >1 entry when a single Bash
  928. # call made multiple commits (`git commit -m a && git commit -m b`);
  929. # all are reviewed.
  930. shas = _reflog_shas
  931. else:
  932. all_shas = _COMMIT_SHA_RE.findall(bash_output)
  933. shas = [all_shas[-1]] if all_shas else []
  934. if not shas:
  935. debug_log("Commit review: no SHA in commit output")
  936. emit_metrics({"skipped": True, "skip_reason": 33, **_base})
  937. sys.exit(0)
  938. if _reflog_shas:
  939. # Observability: track how often the fallback path is hit so
  940. # future analysis can split on it.
  941. # `reflog_shas_n` lets telemetry measure how often the widened scan picked
  942. # up >1 commit (i.e., chained `git commit && git commit`).
  943. _base = {**_base, "sha_via_reflog": True,
  944. "reflog_shas_n": len(_reflog_shas)}
  945. # `git commit --amend`: review only the delta added by the amend
  946. # (pre-amend..post-amend) instead of the full amended commit. Without this,
  947. # the amend re-reviews the entire commit including code already reviewed
  948. # on the original commit, costing 30-60s of LLM time and re-flagging
  949. # findings the user may have just amended IN ORDER TO fix. Pre-amend
  950. # SHA comes from the reflog and is validated to be an amend (see
  951. # _resolve_amend_pre_sha) — otherwise we fall back to full-commit review.
  952. #
  953. # Three guards skip the delta path and fall back to full `git show`
  954. # review. All three close variants of "chained `git commit && git commit
  955. # --amend` in one Bash call", which would otherwise enter the delta path,
  956. # see an empty `git diff sha_wip sha_amend`, emit skip_reason=35, and
  957. # silently drop the first commit's content from review (no prior
  958. # PostToolUse fired for it — same Bash call):
  959. #
  960. # 1. `not _reflog_shas`: reflog fallback path was taken (both commits'
  961. # bash output suppressed via -q / pipe / redirect). The multi-SHA scan
  962. # already populates `shas` with every fresh commit (amend + any
  963. # pre-amend WIP) and the loop below `git show`s each, so coverage is
  964. # correct without delta — and the delta path doesn't compose with a
  965. # multi-SHA `shas` list (it would diff every entry against the same
  966. # pre-amend SHA). Losing the 30-60s saving on the reflog-fallback
  967. # fraction is an acceptable trade.
  968. #
  969. # 2. `len(all_shas) <= 1`: both commits visible (no -q). Two `[branch
  970. # sha]` lines in bash_output → all_shas len 2. Only defined on the
  971. # bash-output path; short-circuit ordering keeps it unevaluated when
  972. # `_reflog_shas` is non-empty.
  973. #
  974. # 3. `commit_invocations <= 1`: asymmetric — first commit -q, amend
  975. # visible. Fast-path fires on the amend's `[branch sha]` line (so
  976. # `_reflog_shas` stays empty), all_shas = [sha_amend] (len 1) — guards
  977. # 1 and 2 both pass. The command string itself is the only remaining
  978. # signal that two commits happened. False-positives (e.g.
  979. # `git commit --amend -m "fix git commit bug"`) are safe — they fall
  980. # back to full review.
  981. is_amend = bool(_GIT_AMEND_RE.search(command))
  982. commit_invocations = len(_GIT_COMMIT_RE.findall(command))
  983. pre_amend_sha = None
  984. if (is_amend and not _reflog_shas and len(all_shas) <= 1
  985. and commit_invocations <= 1):
  986. pre_amend_sha = _resolve_amend_pre_sha(repo_root, expected_post_sha=shas[0])
  987. if is_amend and pre_amend_sha:
  988. _base = {**_base, "amend_delta_review": True}
  989. debug_log(
  990. f"Commit review: --amend detected; reviewing delta "
  991. f"{pre_amend_sha[:12]}..{shas[-1][:12]}"
  992. )
  993. # --no-color: `color.ui=always` would emit ANSI escapes that corrupt
  994. # parse_diff_into_files' header match. Bytes + errors='replace': commits
  995. # can contain non-UTF8 source (latin-1, cp1252) and text=True would raise
  996. # UnicodeDecodeError outside the except clause.
  997. diff_files = []
  998. resolved = 0
  999. for sha in shas:
  1000. try:
  1001. if pre_amend_sha:
  1002. # Delta review: pre-amend → post-amend. `git diff` (not show)
  1003. # so the output is a pure unified diff with no commit header.
  1004. result = subprocess.run(
  1005. [*GIT_CMD, "diff", "--no-color", "--no-ext-diff", pre_amend_sha, sha, "--"],
  1006. cwd=repo_root, capture_output=True, timeout=15
  1007. )
  1008. else:
  1009. result = subprocess.run(
  1010. [*GIT_CMD, "show", "-p", "--no-color", "--no-ext-diff", sha, "--"],
  1011. cwd=repo_root, capture_output=True, timeout=15
  1012. )
  1013. except (subprocess.TimeoutExpired, FileNotFoundError, OSError) as e:
  1014. _cmd = "git diff" if pre_amend_sha else "git show"
  1015. debug_log(f"Commit review: {_cmd} {sha} error: {e}")
  1016. continue
  1017. if result.returncode != 0:
  1018. # SHA not in this repo (cross-repo commit) or already gc'd. Better
  1019. # to skip than to fall back to HEAD and review the wrong commit.
  1020. _cmd = "git diff" if pre_amend_sha else "git show"
  1021. debug_log(f"Commit review: {_cmd} {sha} rc={result.returncode}")
  1022. continue
  1023. resolved += 1
  1024. diff_files.extend(parse_diff_into_files(
  1025. result.stdout.decode("utf-8", errors="replace")))
  1026. # Dedup by path. The widened reflog scan can return >1 SHA (e.g.
  1027. # `git commit && git commit --amend` within 120s); a path that appears in
  1028. # both diffs would consume two MAX_DIFF_FILES slots and be re-analyzed.
  1029. # `shas` is newest-first so the first occurrence is the most recent
  1030. # version of the file — keep it.
  1031. if len(shas) > 1:
  1032. _seen = set()
  1033. diff_files = [
  1034. (fp, c) for fp, c in diff_files
  1035. if not (fp in _seen or _seen.add(fp))
  1036. ]
  1037. if resolved == 0:
  1038. debug_log("Commit review: no parsed SHA resolved in cwd repo")
  1039. emit_metrics({"skipped": True, "skip_reason": 28, **_base,
  1040. "shas_found": len(shas)})
  1041. sys.exit(0)
  1042. # Empty amend delta = message-only amend (or whitespace-only that the
  1043. # diff already collapses). No code to review; skip cleanly. skip_reason=35.
  1044. # Gated on resolved > 0 so subprocess failures (caught with `continue`
  1045. # above) don't get mislabeled as message-only — they fall through to
  1046. # skip_reason=28 correctly.
  1047. if pre_amend_sha and not diff_files:
  1048. debug_log("Commit review: --amend produced empty delta (message-only?), skipping")
  1049. emit_metrics({"skipped": True, "skip_reason": 35, **_base,
  1050. "files_reviewed": 0})
  1051. sys.exit(0)
  1052. debug_log(f"Commit review: {resolved}/{len(shas)} sha(s) resolved, "
  1053. f"{len(diff_files)} files")
  1054. if not diff_files:
  1055. debug_log("Commit review: no reviewable source files in commit")
  1056. emit_metrics({"skipped": True, "skip_reason": 30, **_base})
  1057. sys.exit(0)
  1058. # Large commits (initial scaffolds, big refactors) used to bail here with
  1059. # skip_reason=31. Large multi-file changes are exactly where
  1060. # cross-file source→sink vulns hide. Reviewing nothing is
  1061. # worse than reviewing the riskiest 30 — _cap_files_for_prompt already
  1062. # bounds total bytes downstream so this can't blow context.
  1063. # `diff_files_dropped` lets telemetry measure how often the prioritizer engages
  1064. # and how much it drops; skip_reason=31 is now reserved for the truly
  1065. # pathological case (e.g. >300 source files — almost certainly a bad
  1066. # baseline, not a real commit).
  1067. if len(diff_files) > 10 * MAX_DIFF_FILES:
  1068. debug_log(f"Commit review: pathological diff ({len(diff_files)} files), skipping")
  1069. emit_metrics({"skipped": True, "skip_reason": 31, **_base,
  1070. "diff_files_count": len(diff_files)})
  1071. sys.exit(0)
  1072. diff_files, _dropped = _prioritize_diff_files(diff_files, MAX_DIFF_FILES)
  1073. if _dropped:
  1074. debug_log(f"Commit review: prioritized to {len(diff_files)} files "
  1075. f"(dropped {_dropped} lower-risk)")
  1076. _base = {**_base, "diff_files_dropped": _dropped}
  1077. # Rolling-hour rate limit on LLM spend, so only burn a slot once we know
  1078. # we'll actually call analyze_code_security — skip 28/30/31/33 above are
  1079. # free. `rate_count` is emitted on every fire (not just rejections) so
  1080. # telemetry can show how close to the cap sessions run.
  1081. _allowed, _rate_n = atomic_check_rate_limit(
  1082. session_id, "CommitReview",
  1083. MAX_COMMIT_REVIEWS_PER_HOUR, COMMIT_REVIEW_RATE_WINDOW_S)
  1084. _base = {**_base, "rate_count": _rate_n}
  1085. if not _allowed:
  1086. debug_log("Commit review: hourly rate limit reached, skipping")
  1087. emit_metrics({"skipped": True, "skip_reason": 23, **_base})
  1088. sys.exit(0)
  1089. # Read previous_findings for dedup (shared with Stop hook)
  1090. import time as _time
  1091. now = _time.time()
  1092. def _read_previous(state):
  1093. findings_ts = state.get("previous_findings_ts", 0)
  1094. if (now - findings_ts) > PREVIOUS_FINDINGS_TTL_SEC:
  1095. return []
  1096. return list(state.get("previous_findings", []))
  1097. previous_findings = with_locked_state(session_id, _read_previous) or []
  1098. review_start = _time.time()
  1099. agentic_metrics: Dict[str, Any] = {}
  1100. if _agentic_commit_review_enabled():
  1101. rel_touched = [fp for fp, _ in diff_files]
  1102. concrete_guidance, vulns, _am = _agentic_review_with_race(
  1103. repo_root, diff_files, rel_touched, previous_findings
  1104. )
  1105. agentic_metrics.update(_am)
  1106. # Fall back to single-shot only on agentic FAILURE (SDK/investigate
  1107. # crash). If agentic completed and returned 0 findings, trust that.
  1108. if agentic_metrics.get("agentic_fallback"):
  1109. concrete_guidance, vulns = analyze_code_security(
  1110. diff_files, is_diff=True, previous_findings=previous_findings
  1111. )
  1112. else:
  1113. concrete_guidance, vulns = analyze_code_security(
  1114. diff_files, is_diff=True, previous_findings=previous_findings
  1115. )
  1116. # push-sweep state: record this commit as reviewed (full 40-hex sha) so a
  1117. # later `git push` can advance its diff base past it. Recorded here — after
  1118. # the review ran but before any exit path — so it's marked regardless of
  1119. # whether findings were emitted. `shas` holds abbreviated refs from
  1120. # `[branch sha]`; resolve to full so set-membership in the push-sweep is
  1121. # exact. Best-effort; failures here never block the review result.
  1122. try:
  1123. full_shas = []
  1124. for s in shas:
  1125. r = subprocess.run(
  1126. [*GIT_CMD, "rev-parse", "--verify", "-q", s],
  1127. cwd=repo_root, capture_output=True, text=True, timeout=5,
  1128. )
  1129. if r.returncode == 0:
  1130. full_shas.append(r.stdout.strip())
  1131. _append_reviewed_shas(repo_root, full_shas, vulns_found=len(vulns or []))
  1132. except Exception:
  1133. pass
  1134. review_ms = int((_time.time() - review_start) * 1000)
  1135. # `survived` is the raw self-refute count BEFORE the high/critical-only
  1136. # severity filter; `survived_after_sev` is the count the user actually
  1137. # sees. Include `survived_after_sev` ONLY when the filter actually
  1138. # dropped candidates — otherwise it's redundant with `survived` and eats
  1139. # into CC's 10-key emit cap, pushing files_reviewed/review_ms out of the
  1140. # emitted metrics.
  1141. #
  1142. # CC accepts only booleans and finite numbers as metric values.
  1143. # A null or string value makes CC discard the ENTIRE dict, so:
  1144. # - candidates/survived are omitted when None (early-return at
  1145. # candidates==0, or any fallback path)
  1146. # - agentic_fallback is mapped to an int reason code; the string detail
  1147. # stays in debug_log for diagnosis
  1148. _sev_raw = agentic_metrics.get("survived")
  1149. _sev_post = agentic_metrics.get("survived_after_sev")
  1150. _cand = agentic_metrics.get("candidates")
  1151. _fb = agentic_metrics.get("agentic_fallback")
  1152. # 1 = SDK import failed (claude_agent_sdk not installed)
  1153. # 2 = investigate stage failed (CLI/network/model error or schema-retry exhausted)
  1154. _fb_code = (1 if _fb and _fb.startswith("import:") else 2) if _fb else None
  1155. _race = agentic_metrics.get("race_winner")
  1156. _agentic_m = (
  1157. # `agentic` = which path produced the result, not which was attempted.
  1158. # On race-loss the _fallback() metrics dict has agentic=False — emitting
  1159. # True there blends the high-find-rate single-shot race-loss bucket into
  1160. # `agentic=true` queries and overstates agentic yield.
  1161. {"agentic": bool(agentic_metrics.get("agentic")),
  1162. **({"candidates": _cand} if _cand is not None else {}),
  1163. **({"survived": _sev_raw} if _sev_raw is not None else {}),
  1164. **({"survived_after_sev": _sev_post}
  1165. if _sev_post is not None and _sev_post != _sev_raw else {}),
  1166. **({"agentic_fallback": _fb_code} if _fb_code is not None else {}),
  1167. # 1 = agentic won, 2 = single-shot fallback won. review_ms already
  1168. # captures timing; race_winner lets telemetry segment recall by which path
  1169. # actually produced the result.
  1170. **({"race_winner": _race} if _race is not None else {})}
  1171. if agentic_metrics.get("agentic") or _fb or _race is not None
  1172. else {}
  1173. )
  1174. if not concrete_guidance:
  1175. debug_log("Commit review: no security issues found")
  1176. emit_metrics({
  1177. "vulns_found": 0, **_base, **_agentic_m,
  1178. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1179. **({
  1180. "api_error": llm._last_call_claude_http_error
  1181. } if llm._last_call_claude_http_error is not None else {}),
  1182. })
  1183. sys.exit(0)
  1184. # Late dedup: drop only what a concurrent Stop hook wrote while our LLM
  1185. # ran. Anything in `previous_findings` (the pre-LLM snapshot) that the
  1186. # LLM chose to re-flag is an intentional "fix incomplete" verdict.
  1187. new_vulns, n_deduped = _dedup_against_state(
  1188. session_id, vulns, prompted=_finding_keys(previous_findings)
  1189. )
  1190. if not new_vulns:
  1191. debug_log("Commit review: all findings already known, skipping")
  1192. emit_metrics({
  1193. "vulns_found": 0, **_base, **_agentic_m, "deduped": n_deduped,
  1194. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1195. })
  1196. sys.exit(0)
  1197. # Record new findings into shared state. Key on (filePath, category) —
  1198. # vulnerableCode bytes drift between fires (diff context lines shift) so
  1199. # matching on it under-dedupes; this aligns with Stop's _record_fire.
  1200. finding_snapshots = [
  1201. {
  1202. "filePath": v.get("filePath", ""),
  1203. "category": v.get("category", "Unknown"),
  1204. "vulnerableCode": v.get("vulnerableCode", ""),
  1205. }
  1206. for v in new_vulns
  1207. ]
  1208. def _record_findings(state):
  1209. existing = [f for f in state.get("previous_findings", []) if isinstance(f, dict)]
  1210. seen = {(f.get("filePath", ""), f.get("category", "")) for f in existing}
  1211. for f in finding_snapshots:
  1212. key = (f["filePath"], f["category"])
  1213. if key not in seen:
  1214. seen.add(key)
  1215. existing.append(f)
  1216. state["previous_findings"] = existing
  1217. state["previous_findings_ts"] = _time.time()
  1218. with_locked_state(session_id, _record_findings)
  1219. sev = {"critical": 0, "high": 0, "medium": 0}
  1220. for v in new_vulns:
  1221. s = v.get("severity", "medium")
  1222. if s in sev:
  1223. sev[s] += 1
  1224. emit_metrics({
  1225. "vulns_found": len(new_vulns), **_base, **_agentic_m,
  1226. "critical_count": sev["critical"], "high_count": sev["high"],
  1227. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1228. **({"deduped": n_deduped} if n_deduped else {}),
  1229. }, rewake_summary=_format_vulns_summary(new_vulns, prefix="Commit security review found"))
  1230. # Rebuild guidance from new_vulns only — concrete_guidance from the LLM
  1231. # still lists deduped entries.
  1232. sys.stderr.write(PROVENANCE_BANNER + "\n\n"
  1233. + _format_vulns_guidance(new_vulns)
  1234. + CONTINUATION_SUFFIX + "\n")
  1235. sys.exit(2)
  1236. def handle_push_sweep_posttooluse(input_data):
  1237. """Review the just-pushed range as one diff, advancing the base past the
  1238. contiguous prefix of already-per-commit-reviewed shas.
  1239. Spec: review `git diff B..HEAD` where `B` is the newest commit such that
  1240. `prev_upstream..B` is entirely in `.git/sg-reviewed-shas`. Skip if
  1241. `B == HEAD`. Mark `B..HEAD` reviewed afterward.
  1242. Diff and Read are both at HEAD (push doesn't move the working tree), so the
  1243. agentic reviewer sees a consistent view — a vuln introduced in commit A and
  1244. removed in commit B is absent from the net diff by construction. Any
  1245. reviewed commits in the tail (after the first unreviewed one) are included
  1246. in the diff; their findings are dropped by `_dedup_against_state` against
  1247. `previous_findings` the per-commit hook already recorded.
  1248. Metrics: `push_sweep: True` is the telemetry splitter; `pushed`/`unreviewed`/
  1249. `prefix_advanced` give the funnel; skip_reasons 40-49 are reserved for
  1250. this surface.
  1251. """
  1252. tool_input = input_data.get("tool_input", {}) or {}
  1253. tool_response = input_data.get("tool_response", {}) or {}
  1254. command = tool_input.get("command", "") or ""
  1255. cwd = input_data.get("cwd")
  1256. session_id = input_data.get("session_id", "")
  1257. bash_output = (
  1258. (tool_response.get("stdout", "") or "")
  1259. + "\n"
  1260. + (tool_response.get("stderr", "") or "")
  1261. )
  1262. interrupted = tool_response.get("interrupted", False)
  1263. # Re-gate: hooks.json `if` matched, but confirm with the broader regex
  1264. # (defensive — `git -C`/`-c` forms won't reach here via the hooks.json
  1265. # prefix matcher alone, but a compound with a plain `git push` would).
  1266. if not _GIT_PUSH_RE.search(command):
  1267. sys.exit(0)
  1268. _base = {"push_sweep": True, "push_sweep_on": PUSH_SWEEP_ENABLED}
  1269. if not PUSH_SWEEP_ENABLED:
  1270. emit_metrics({"skipped": True, "skip_reason": 40, **_base})
  1271. sys.exit(0)
  1272. if interrupted:
  1273. emit_metrics({"skipped": True, "skip_reason": 21, **_base})
  1274. sys.exit(0)
  1275. if not ENABLE_CODE_SECURITY_REVIEW or not HAS_API_CREDENTIALS:
  1276. emit_metrics({"skipped": True, "skip_reason": 22, **_base})
  1277. sys.exit(0)
  1278. if not cwd:
  1279. emit_metrics({"skipped": True, "skip_reason": 25, **_base})
  1280. sys.exit(0)
  1281. repo_root = _git_toplevel(cwd)
  1282. if not repo_root:
  1283. emit_metrics({"skipped": True, "skip_reason": 26, **_base})
  1284. sys.exit(0)
  1285. # Guard: the sweep diffs `base..HEAD` and the agent Reads the working
  1286. # tree, so the pushed ref MUST be HEAD or the review is of the wrong
  1287. # range. `git push origin other` while checked out elsewhere, or a
  1288. # multi-ref push, are skipped (skip_reason 44). Check the new-tip from
  1289. # the `abc..def local -> remote` line against HEAD.
  1290. #
  1291. # Scope range-line detection to the push section of bash_output: a chained
  1292. # `git fetch && git push` produces fetch range lines that the regex would
  1293. # otherwise match too, false-tripping multi-ref. `_push_section` slices
  1294. # forward from the last `To <remote>` header.
  1295. #
  1296. # If there are no range lines, we MUST also see a positive push-success
  1297. # signal (`* [new branch]` or `Everything up-to-date`) AND verify the
  1298. # pushed local ref resolves to HEAD before falling through to the
  1299. # @{u}@{1}/merge-base detection. Without this, two real cases misdirect
  1300. # the sweep: `git push origin feature2` while on `feature1` (no range
  1301. # line, no HEAD check → reviews wrong branch and poisons reviewed-shas),
  1302. # and rejected pushes (no range line, no `interrupted` signal → reviews
  1303. # unpushed local commits and marks them reviewed). skip_reason=46 covers
  1304. # both.
  1305. head = None
  1306. try:
  1307. r = subprocess.run([*GIT_CMD, "rev-parse", "HEAD"], cwd=repo_root,
  1308. capture_output=True, text=True, timeout=5)
  1309. head = r.stdout.strip() if r.returncode == 0 else None
  1310. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  1311. pass
  1312. push_section = _push_section(bash_output or "")
  1313. range_matches = list(_PUSH_RANGE_RE.finditer(push_section))
  1314. if range_matches and head:
  1315. # Multi-ref push (multiple range lines) or pushed-tip ≠ HEAD → skip.
  1316. if len(range_matches) > 1:
  1317. emit_metrics({"skipped": True, "skip_reason": 44, **_base})
  1318. sys.exit(0)
  1319. new_tip = range_matches[0].group(2)
  1320. if not head.startswith(new_tip):
  1321. debug_log(f"Push sweep: pushed tip {new_tip} != HEAD {head[:12]}")
  1322. emit_metrics({"skipped": True, "skip_reason": 44, **_base})
  1323. sys.exit(0)
  1324. elif head:
  1325. # No range lines. Need a positive push-success signal — otherwise
  1326. # the push may have failed and we'd review unpushed local commits.
  1327. new_branch_matches = re.findall(
  1328. r"^\s*\*\s+\[new branch\]\s+(\S+)\s+->\s+\S+",
  1329. push_section, re.M)
  1330. up_to_date = "Everything up-to-date" in push_section
  1331. # `git push -q` suppresses all output on success. Distinguish quiet-
  1332. # success from a failed push (which has error text) by checking the
  1333. # upstream's reflog: a successful push leaves @{u}@{1} (the prior
  1334. # value) different from @{u} (now equal to HEAD). A rejected push
  1335. # would not advance @{u}, so this signal is push-specific.
  1336. quiet_success = False
  1337. if not (bash_output or "").strip() and not interrupted:
  1338. try:
  1339. r_cur = subprocess.run(
  1340. [*GIT_CMD, "rev-parse", "--verify", "-q", "@{u}"],
  1341. cwd=repo_root, capture_output=True, text=True, timeout=5)
  1342. r_prev = subprocess.run(
  1343. [*GIT_CMD, "rev-parse", "--verify", "-q", "@{u}@{1}"],
  1344. cwd=repo_root, capture_output=True, text=True, timeout=5)
  1345. cur = r_cur.stdout.strip() if r_cur.returncode == 0 else ""
  1346. prev_u = r_prev.stdout.strip() if r_prev.returncode == 0 else ""
  1347. quiet_success = bool(cur and prev_u and cur == head and prev_u != cur)
  1348. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  1349. pass
  1350. if not (new_branch_matches or up_to_date or quiet_success):
  1351. debug_log("Push sweep: no push-success signal in bash output")
  1352. emit_metrics({"skipped": True, "skip_reason": 46, **_base})
  1353. sys.exit(0)
  1354. # `* [new branch] local -> remote`: verify the pushed local ref
  1355. # resolves to HEAD. `git push origin feature2` while on feature1
  1356. # would otherwise review feature1's commits and poison its
  1357. # reviewed-shas state.
  1358. for local_ref in new_branch_matches:
  1359. try:
  1360. r = subprocess.run(
  1361. [*GIT_CMD, "rev-parse", "--verify", "-q", local_ref],
  1362. cwd=repo_root, capture_output=True, text=True, timeout=5,
  1363. )
  1364. local_sha = r.stdout.strip() if r.returncode == 0 else ""
  1365. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  1366. local_sha = ""
  1367. if local_sha and local_sha != head:
  1368. debug_log(f"Push sweep: new-branch {local_ref} ({local_sha[:12]}) != HEAD {head[:12]}")
  1369. emit_metrics({"skipped": True, "skip_reason": 44, **_base})
  1370. sys.exit(0)
  1371. prev_upstream = _detect_prev_upstream(repo_root, bash_output)
  1372. if not prev_upstream:
  1373. debug_log("Push sweep: could not determine prev_upstream")
  1374. emit_metrics({"skipped": True, "skip_reason": 41, **_base})
  1375. sys.exit(0)
  1376. push_range = _git_rev_list_range(repo_root, prev_upstream, "HEAD")
  1377. if not push_range:
  1378. emit_metrics({"skipped": True, "skip_reason": 42, **_base, "pushed": 0})
  1379. sys.exit(0)
  1380. if len(push_range) > MAX_PUSH_SWEEP_RANGE:
  1381. # Huge first-push of a long-lived branch — Stop hook is the backstop.
  1382. emit_metrics({"skipped": True, "skip_reason": 43, **_base,
  1383. "pushed": len(push_range)})
  1384. sys.exit(0)
  1385. reviewed = _load_reviewed_shas(repo_root)
  1386. base, tail = _compute_push_sweep_base(prev_upstream, push_range, reviewed)
  1387. prefix_advanced = len(push_range) - len(tail)
  1388. if base is None:
  1389. debug_log("Push sweep: every pushed commit already reviewed")
  1390. emit_metrics({**_base, "pushed": len(push_range), "unreviewed": 0,
  1391. "prefix_advanced": prefix_advanced})
  1392. sys.exit(0)
  1393. debug_log(f"Push sweep: range={len(push_range)} prefix_advanced="
  1394. f"{prefix_advanced} base={base[:12]} tail={len(tail)}")
  1395. diff_text = _git_diff_range(repo_root, base, "HEAD")
  1396. if diff_text is None:
  1397. # Diff failed (non-zero exit / 30s timeout / git missing). Do NOT
  1398. # mark `tail` reviewed — we did not actually review it. Marking
  1399. # them would silently advance the prefix past unreviewed commits
  1400. # forever (the whole point of push-sweep is to catch outside-CC
  1401. # commits, and a 50-commit range over large files can hit the
  1402. # 30s timeout). skip_reason=45 lets a retry / smaller subsequent
  1403. # push still cover them, mirroring how skip_reason=31 handles
  1404. # too-many-files without recording the tail.
  1405. emit_metrics({**_base, "pushed": len(push_range),
  1406. "unreviewed": len(tail), "skip_reason": 45})
  1407. sys.exit(0)
  1408. diff_files = parse_diff_into_files(diff_text)
  1409. if not diff_files:
  1410. emit_metrics({**_base, "pushed": len(push_range),
  1411. "unreviewed": len(tail), "skip_reason": 30})
  1412. # Still mark tail reviewed — there's nothing to review.
  1413. _append_reviewed_shas(repo_root, tail, vulns_found=0)
  1414. sys.exit(0)
  1415. # Same prioritize-don't-bail logic as commit-review (see comment there).
  1416. # push-sweep ranges are net diffs over many commits so they hit the cap
  1417. # more often; reviewing the riskiest MAX_PUSH_SWEEP_FILES is strictly
  1418. # better than reviewing none. We still mark `tail` reviewed afterward —
  1419. # the dropped files are by construction the low-risk ones (config, .gen,
  1420. # tests, migrations), and NOT advancing the base would make the next
  1421. # push re-hit the same overflow with an even larger range. Per-commit
  1422. # review remains the primary surface for those files. The 10×
  1423. # pathological guard stays so a 500-file vendored-dir push doesn't burn
  1424. # a counter slot.
  1425. if len(diff_files) > 10 * MAX_PUSH_SWEEP_FILES:
  1426. emit_metrics({**_base, "pushed": len(push_range),
  1427. "unreviewed": len(tail), "skip_reason": 31,
  1428. "diff_files_count": len(diff_files)})
  1429. sys.exit(0)
  1430. diff_files, _dropped = _prioritize_diff_files(diff_files, MAX_PUSH_SWEEP_FILES)
  1431. if _dropped:
  1432. _base = {**_base, "diff_files_dropped": _dropped}
  1433. _allowed, _rate_n = atomic_check_rate_limit(
  1434. session_id, "PushSweep",
  1435. MAX_COMMIT_REVIEWS_PER_HOUR, COMMIT_REVIEW_RATE_WINDOW_S)
  1436. _base = {**_base, "rate_count": _rate_n}
  1437. if not _allowed:
  1438. emit_metrics({"skipped": True, "skip_reason": 23, **_base})
  1439. sys.exit(0)
  1440. import time as _time
  1441. now = _time.time()
  1442. previous_findings = with_locked_state(
  1443. session_id,
  1444. lambda s: list(s.get("previous_findings", []))
  1445. if (now - s.get("previous_findings_ts", 0)) <= PREVIOUS_FINDINGS_TTL_SEC
  1446. else []
  1447. ) or []
  1448. review_start = _time.time()
  1449. rel_touched = [fp for fp, _ in diff_files]
  1450. if _agentic_commit_review_enabled():
  1451. concrete_guidance, vulns, agentic_metrics = _agentic_review_with_race(
  1452. repo_root, diff_files, rel_touched, previous_findings
  1453. )
  1454. if agentic_metrics.get("agentic_fallback"):
  1455. concrete_guidance, vulns = analyze_code_security(
  1456. diff_files, is_diff=True, previous_findings=previous_findings
  1457. )
  1458. else:
  1459. concrete_guidance, vulns = analyze_code_security(
  1460. diff_files, is_diff=True, previous_findings=previous_findings
  1461. )
  1462. agentic_metrics = {}
  1463. review_ms = int((_time.time() - review_start) * 1000)
  1464. # The tail is now covered by this net-diff review.
  1465. _append_reviewed_shas(repo_root, tail, vulns_found=len(vulns or []))
  1466. new_vulns, n_deduped = _dedup_against_state(
  1467. session_id, vulns or [], prompted=_finding_keys(previous_findings)
  1468. )
  1469. # Metrics — keep within the 10-key cap; agentic sub-metrics are dropped
  1470. # here in favour of the push-sweep funnel keys (telemetry can join on session_id
  1471. # to the per-commit fires for agentic detail). rewake_summary must ride
  1472. # this line (CC reads only the first {-prefixed stdout line); it's a
  1473. # no-op when new_vulns is empty since we exit 0 below.
  1474. emit_metrics({
  1475. **_base, "pushed": len(push_range), "unreviewed": len(tail),
  1476. "prefix_advanced": prefix_advanced, "vulns_found": len(new_vulns),
  1477. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1478. **({"deduped": n_deduped} if n_deduped else {}),
  1479. }, rewake_summary=_format_vulns_summary(new_vulns, prefix="Push security review found"))
  1480. if not new_vulns:
  1481. debug_log("Push sweep: no new findings")
  1482. sys.exit(0)
  1483. # First-push of a big branch can surface many findings at once across
  1484. # week-old code. Report only the top-N by severity so the asyncRewake
  1485. # isn't a wall of text; the rest go to telemetry (vulns_found is the
  1486. # full count) and into previous_findings so Stop / next commit-review
  1487. # don't re-flag them. Stable sort: severity, then category for
  1488. # determinism in tests.
  1489. _sev_rank = {"critical": 0, "high": 1, "medium": 2, "low": 3}
  1490. new_vulns.sort(key=lambda v: (_sev_rank.get(v.get("severity", "medium"), 2),
  1491. v.get("category", "")))
  1492. reported = new_vulns[:PUSH_SWEEP_REPORT_CAP]
  1493. n_suppressed = len(new_vulns) - len(reported)
  1494. # Record only the REPORTED findings into shared state. previous_findings
  1495. # means "the user was told about this — don't repeat it"; suppressed
  1496. # findings were NOT told, so recording them would silently bury them
  1497. # against any future commit-review/Stop that touches the same code. The
  1498. # range is marked reviewed in `.git/sg-reviewed-shas` regardless, so the
  1499. # push-sweep itself won't re-find them; leaving them out of
  1500. # previous_findings keeps the door open for the per-commit hook to
  1501. # surface them later if the code is touched again.
  1502. snapshots = [
  1503. {"filePath": v.get("filePath", ""),
  1504. "category": v.get("category", "Unknown"),
  1505. "vulnerableCode": v.get("vulnerableCode", "")}
  1506. for v in reported
  1507. ]
  1508. def _record(state):
  1509. existing = [f for f in state.get("previous_findings", [])
  1510. if isinstance(f, dict)]
  1511. seen = {(f.get("filePath", ""), f.get("category", "")) for f in existing}
  1512. for f in snapshots:
  1513. k = (f["filePath"], f["category"])
  1514. if k not in seen:
  1515. seen.add(k); existing.append(f)
  1516. state["previous_findings"] = existing
  1517. state["previous_findings_ts"] = _time.time()
  1518. with_locked_state(session_id, _record)
  1519. # Prefer the LLM's formatted guidance (richer context, fix suggestions)
  1520. # when NOTHING was dropped from the LLM's full vuln list; fall back to
  1521. # re-formatting from `reported` whenever either the cap suppressed
  1522. # findings OR `_dedup_against_state` dropped findings the user has
  1523. # already been shown. concrete_guidance is built against the LLM's
  1524. # full pre-dedup list, so leaking it past dedup re-surfaces findings
  1525. # the per-commit hook already reported (the [✓1, ✗2, ✓3] case where
  1526. # the tail reviewed commits' findings are in previous_findings).
  1527. if n_suppressed or n_deduped:
  1528. guidance = _format_vulns_guidance(reported) or ""
  1529. else:
  1530. guidance = concrete_guidance or _format_vulns_guidance(reported) or ""
  1531. sys.stderr.write(
  1532. PROVENANCE_BANNER + "\n\n" + guidance + CONTINUATION_SUFFIX + "\n"
  1533. )
  1534. sys.exit(2)
  1535. def handle_stop_hook(input_data):
  1536. """
  1537. Handle the Stop hook — final security check using git diff.
  1538. Diffs against the baseline SHA captured at UserPromptSubmit to review
  1539. only code changed during this turn. Runs two Haiku analyses and
  1540. exits with code 2 to force Claude to continue and fix issues.
  1541. Also sweeps pending pattern warnings to emit a session-level
  1542. fixed/unresolved tally; the sweep needs no LLM and measures
  1543. pattern-rule efficacy.
  1544. """
  1545. session_id = input_data.get("session_id", "default")
  1546. stop_hook_active = input_data.get("stop_hook_active", False)
  1547. cwd = input_data.get("cwd", "")
  1548. # Recursion guard FIRST — consume_stop_state clears touched_paths, and CC
  1549. # sets stop_hook_active session-wide while any asyncRewake Stop is in
  1550. # flight, so a concurrent active=True fire winning the lock would discard
  1551. # paths the concurrent active=False fire needs.
  1552. if stop_hook_active:
  1553. debug_log("Stop hook: stop_hook_active=True, skipping to avoid recursion")
  1554. emit_metrics({"skipped": True, "skip_reason": 1, "diff_strategy_v2": True})
  1555. sys.exit(0)
  1556. # Snapshot all state under one lock BEFORE any slow work (sweep file I/O,
  1557. # git, network). asyncRewake Stop runs in the background; the next turn's
  1558. # UPS/PostToolUse can fire while we're still here. The snapshot is immune
  1559. # to those writes — they affect the NEXT Stop fire's snapshot.
  1560. snap = consume_stop_state(session_id)
  1561. fire_count = snap["fire_count"]
  1562. touched_paths = snap["touched_paths"]
  1563. baseline_sha = snap["baseline_sha"]
  1564. snap_baseline = baseline_sha # pre-reassignment value for restore-on-transient-skip
  1565. head_at_capture = snap["head_at_capture"]
  1566. untracked_at_baseline = snap.get("untracked_at_baseline") or {}
  1567. previous_findings = snap["previous_findings"]
  1568. # Sweep pattern-warning outcomes (pure local work; stop_hook_active is
  1569. # already guaranteed False here so no double-count guard needed).
  1570. sweep = {}
  1571. warn_fixed, warn_unresolved, warn_unresolved_mask = sweep_pending_warnings(session_id)
  1572. if warn_fixed or warn_unresolved:
  1573. sweep = {
  1574. "warn_fixed": warn_fixed,
  1575. "warn_unresolved": warn_unresolved,
  1576. "warn_unresolved_mask": warn_unresolved_mask,
  1577. }
  1578. v2_metrics = {}
  1579. def _skip(reason, restore=False, **extra):
  1580. if restore:
  1581. restore_unreviewed_stop_state(session_id, touched_paths, snap_baseline)
  1582. # CC truncates metrics to 10 keys by
  1583. # insertion order. v2_metrics (3) must precede sweep (3) so the v2
  1584. # diagnostics survive when extra adds touched_paths_count + ip_* keys.
  1585. emit_metrics({
  1586. "skipped": True, "skip_reason": reason, "fire_index": fire_count + 1,
  1587. "diff_strategy_v2": True,
  1588. **v2_metrics, **extra, **sweep,
  1589. })
  1590. sys.exit(0)
  1591. # Limit stop hook firings per asyncRewake loop to prevent infinite loops.
  1592. # fire_count auto-expires after STOP_LOOP_STATE_TTL_SEC so a stale count
  1593. # from a prior turn doesn't block this one.
  1594. if MAX_STOP_HOOK_FIRINGS > 0 and fire_count >= MAX_STOP_HOOK_FIRINGS:
  1595. debug_log(f"Stop hook: already fired {fire_count} times (max {MAX_STOP_HOOK_FIRINGS}), skipping")
  1596. _skip(2)
  1597. if not ENABLE_CODE_SECURITY_REVIEW or not HAS_API_CREDENTIALS:
  1598. debug_log("Stop hook: LLM review disabled or no API credentials")
  1599. _skip(3)
  1600. # Stop-hook-only kill switch — placed after consume_stop_state so
  1601. # touched_paths is still cleared each turn (a disabled Stop hook that
  1602. # never consumed state would accumulate stale paths) and after the sweep
  1603. # so pattern-warning efficacy metrics still emit. The commit/push reviews
  1604. # have their own gates (ENABLE_COMMIT_REVIEW / ENABLE_CODE_SECURITY_REVIEW).
  1605. if not ENABLE_STOP_REVIEW:
  1606. debug_log("Stop hook: ENABLE_STOP_REVIEW=0")
  1607. # 50+ for opt-out skips that aren't push-sweep (which owns 40-49).
  1608. _skip(50)
  1609. if not ensure_anthropic_reachable():
  1610. debug_log("Stop hook: api.anthropic.com unreachable")
  1611. _skip(10, restore=True)
  1612. if not cwd:
  1613. debug_log("Stop hook: no cwd")
  1614. _skip(4)
  1615. review_paths, diff_base, repo_root, untracked, v2_metrics = compute_v2_review_set(
  1616. cwd, baseline_sha, head_at_capture, untracked_at_baseline
  1617. )
  1618. if not review_paths:
  1619. debug_log("Stop hook: empty review set")
  1620. _skip(9, touched_paths_count=len(touched_paths))
  1621. debug_log(f"Stop hook: review_set={len(review_paths)} base={diff_base[:12]} dirty_now={v2_metrics['dirty_now_count']} changed_since={v2_metrics['changed_since_count']}")
  1622. # Run from repo_root so the toplevel-relative review_paths resolve.
  1623. # Diff CONTENT against the turn-start stash (baseline_sha) so the LLM
  1624. # sees only this-turn edits — diffing against HEAD includes the user's
  1625. # pre-turn uncommitted WIP, which inflates review_ms and can re-flag
  1626. # the same pre-existing pattern every turn. The file LIST still comes
  1627. # from git state (compute_v2_review_set), so Bash/subagent edits are
  1628. # caught either way. Fall back to diff_base (HEAD/head_at_capture)
  1629. # when the stash is missing or pruned.
  1630. content_base = baseline_sha or diff_base
  1631. diff_output = get_git_diff(repo_root, content_base, full_context=False,
  1632. paths=review_paths, untracked_paths=untracked)
  1633. if diff_output is None and content_base != diff_base:
  1634. debug_log(f"Stop hook: diff against {content_base[:12]} failed — falling back to {diff_base}")
  1635. diff_output = get_git_diff(repo_root, diff_base, full_context=False,
  1636. paths=review_paths, untracked_paths=untracked)
  1637. # filter_preexisting_from_diff needs a resolvable pre-turn ref; fall
  1638. # back to HEAD when UPS never captured a baseline (print mode).
  1639. if not baseline_sha:
  1640. baseline_sha = "HEAD"
  1641. if not diff_output or not diff_output.strip():
  1642. debug_log("Stop hook: no changes since baseline")
  1643. _skip(6)
  1644. # Parse diff into per-file content
  1645. diff_files = parse_diff_into_files(diff_output)
  1646. if not diff_files:
  1647. debug_log("Stop hook: no source code files in diff")
  1648. _skip(7)
  1649. # Mirror commit-review: hard-bail only on pathological diffs (>300 files,
  1650. # usually a bad baseline), otherwise prioritize by security-risk path
  1651. # tokens and review the top MAX_DIFF_FILES. Stop is the only surface for
  1652. # uncommitted edits; the old hard-skip at >30 files dropped the 31-300
  1653. # bucket entirely, which is where cross-file source→sink vulns hide.
  1654. # _cap_files_for_prompt already bounds bytes downstream.
  1655. _stop_dropped = 0
  1656. if len(diff_files) > 10 * MAX_DIFF_FILES:
  1657. debug_log(f"Stop hook: pathological diff ({len(diff_files)} files > "
  1658. f"{10 * MAX_DIFF_FILES}), skipping")
  1659. _skip(8, diff_files_count=len(diff_files))
  1660. if len(diff_files) > MAX_DIFF_FILES:
  1661. diff_files, _stop_dropped = _prioritize_diff_files(
  1662. diff_files, MAX_DIFF_FILES)
  1663. debug_log(f"Stop hook: prioritized to {len(diff_files)} files "
  1664. f"(dropped {_stop_dropped} lower-risk)")
  1665. # Filter out pre-existing content from file rewrites
  1666. diff_files = filter_preexisting_from_diff(diff_files, cwd, baseline_sha)
  1667. debug_log(f"Stop hook: reviewing {len(diff_files)} changed files (standard diff)")
  1668. import time as _time
  1669. stop_review_start = _time.time()
  1670. # Stop hook is single-shot only. Agentic review is wired into
  1671. # handle_commit_review_posttooluse (PostToolUse on `git commit`) — commits
  1672. # are slower-OK and benefit from the deeper context-reading loop.
  1673. concrete_guidance, vulns = analyze_code_security(
  1674. diff_files, is_diff=True, previous_findings=previous_findings
  1675. )
  1676. # NOTE: analyze_security_concerns disabled — it produces too many false positives
  1677. # on pre-existing patterns in starter code. The concrete vulnerability analysis
  1678. # is more precise and has severity filtering (high/critical only).
  1679. stop_review_elapsed = _time.time() - stop_review_start
  1680. debug_log(f"Stop hook: LLM reviews took {stop_review_elapsed:.1f}s total")
  1681. review_ms = int(stop_review_elapsed * 1000)
  1682. fire_index = fire_count + 1
  1683. # Late dedup: drop only what a concurrent commit-review wrote while our
  1684. # LLM ran. Anything already in `previous_findings` (the consume_stop_state
  1685. # snapshot) that the LLM re-flagged is an intentional "fix incomplete"
  1686. # verdict and passes through.
  1687. if vulns:
  1688. vulns, n_deduped = _dedup_against_state(
  1689. session_id, vulns, prompted=_finding_keys(previous_findings)
  1690. )
  1691. if n_deduped and not vulns:
  1692. debug_log("Stop hook: all findings already delivered by commit-review")
  1693. _skip(35, deduped=n_deduped, review_ms=review_ms)
  1694. concrete_guidance = _format_vulns_guidance(vulns)
  1695. if concrete_guidance:
  1696. finding_snapshots = [
  1697. {
  1698. "filePath": v.get("filePath", ""),
  1699. "category": v.get("category", "Unknown"),
  1700. "vulnerableCode": v.get("vulnerableCode", ""),
  1701. }
  1702. for v in vulns
  1703. ]
  1704. # Update baseline so next stop hook iteration only sees new changes
  1705. new_sha = capture_git_baseline(cwd)
  1706. new_untracked_baseline = _list_untracked(cwd) if new_sha else None
  1707. def _record_fire(state):
  1708. state["stop_hook_fire_count"] = fire_index
  1709. state["stop_hook_fire_count_ts"] = _time.time()
  1710. # Re-read under lock — the commit-review PostToolUse hook may have
  1711. # appended findings since consume_stop_state snapshotted.
  1712. # Dedupe on (filePath, category) — vulnerableCode includes diff
  1713. # context lines that drift between fires, so byte-identical
  1714. # matching let the same finding accumulate as "new" each fire.
  1715. existing = [f for f in state.get("previous_findings", []) if isinstance(f, dict)]
  1716. seen = {(f.get("filePath", ""), f.get("category", "")) for f in existing}
  1717. for f in finding_snapshots:
  1718. key = (f["filePath"], f["category"])
  1719. if key not in seen:
  1720. seen.add(key)
  1721. existing.append(f)
  1722. state["previous_findings"] = existing
  1723. state["previous_findings_ts"] = _time.time()
  1724. if new_sha:
  1725. state["baseline_sha"] = new_sha
  1726. state["untracked_at_baseline"] = new_untracked_baseline
  1727. with_locked_state(session_id, _record_fire)
  1728. if new_sha:
  1729. debug_log(f"Updated git baseline after stop hook: {new_sha[:12]}")
  1730. sev = {"critical": 0, "high": 0, "medium": 0}
  1731. for v in vulns:
  1732. s = v.get("severity", "medium")
  1733. if s in sev:
  1734. sev[s] += 1
  1735. # 8 base keys + at most 2 sweep keys = 10 (cap). Drop the mask here.
  1736. # untracked_baseline_n is the signal for whether the UPS-time
  1737. # untracked-snapshot capture actually ran.
  1738. sweep_trimmed = {k: v for k, v in sweep.items() if k != "warn_unresolved_mask"}
  1739. emit_metrics({
  1740. "vulns_found": len(vulns),
  1741. "untracked_baseline_n": len(untracked_at_baseline),
  1742. "diff_strategy_v2": True,
  1743. "critical_count": sev["critical"],
  1744. "high_count": sev["high"],
  1745. "files_reviewed": len(diff_files),
  1746. "touched_paths_count": len(touched_paths),
  1747. "review_ms": review_ms,
  1748. "fire_index": fire_index,
  1749. **({"diff_truncated": llm._last_review_truncated_bytes}
  1750. if llm._last_review_truncated_bytes else {}),
  1751. **sweep_trimmed,
  1752. }, rewake_summary=_format_vulns_summary(vulns))
  1753. # Exit code 2 with stderr forces Claude to continue and fix
  1754. sys.stderr.write(PROVENANCE_BANNER + "\n\n" + concrete_guidance + CONTINUATION_SUFFIX + "\n")
  1755. sys.exit(2)
  1756. if llm._last_call_claude_http_error is not None:
  1757. debug_log(f"Stop hook: API call failed with status {llm._last_call_claude_http_error}")
  1758. restore_unreviewed_stop_state(session_id, touched_paths, snap_baseline)
  1759. else:
  1760. debug_log("Stop hook: no security issues found")
  1761. # CC truncates metrics to 10 keys by
  1762. # insertion order. The previous **sweep,**v2_metrics tail meant the 3
  1763. # v2_metrics keys were always sliced off this most-common path, so the
  1764. # diff-strategy diagnostics never reached telemetry. Drop sweep here (it's
  1765. # PostToolUse-warning state, orthogonal to diff-strategy comparison).
  1766. # 6 base + optional api_error + 3 v2_metrics = ≤10.
  1767. emit_metrics({
  1768. "vulns_found": 0,
  1769. "diff_strategy_v2": True,
  1770. "files_reviewed": len(diff_files),
  1771. "touched_paths_count": len(touched_paths),
  1772. "review_ms": review_ms,
  1773. "fire_index": fire_index,
  1774. **({"api_error": llm._last_call_claude_http_error} if llm._last_call_claude_http_error is not None else {}),
  1775. **({"diff_truncated": llm._last_review_truncated_bytes}
  1776. if llm._last_review_truncated_bytes else {}),
  1777. **v2_metrics,
  1778. })
  1779. sys.exit(0)
  1780. _SDK_BOOTSTRAP_THROTTLE = os.path.join(_resolve_state_dir(), ".sdk_bootstrap_spawned")
  1781. def _maybe_bootstrap_agent_sdk_async():
  1782. """Fire-and-forget SDK bootstrap, for remote-pod environments.
  1783. Under CLAUDE_CODE_SYNC_PLUGIN_INSTALL=true (CCR-style remote pods),
  1784. plugins are synced *after* SessionStart fires, so the SessionStart
  1785. `ensure_agent_sdk.py` hook never runs and the agentic commit reviewer
  1786. falls back 100% of the time. A PostToolUse hook firing is itself proof
  1787. the plugin is now registered, so re-trigger the bootstrap here.
  1788. Detached, so the ~17s venv build never blocks the hook — the first
  1789. 1-2 commits of a remote session still fall back while it builds, then
  1790. every subsequent commit gets the agentic path. ensure_agent_sdk.py
  1791. is idempotent and O_EXCL-locked, so concurrent/repeat spawns are safe;
  1792. the throttle file only avoids spawning dozens of subprocesses during
  1793. the build window. No-ops in ~10ms on local installs (SDK already
  1794. importable).
  1795. """
  1796. try:
  1797. import importlib.util
  1798. if importlib.util.find_spec("claude_agent_sdk") is not None:
  1799. return
  1800. import time as _t
  1801. try:
  1802. if _t.time() - os.path.getmtime(_SDK_BOOTSTRAP_THROTTLE) < 300:
  1803. return
  1804. except OSError:
  1805. pass
  1806. os.makedirs(os.path.dirname(_SDK_BOOTSTRAP_THROTTLE), exist_ok=True)
  1807. # Touch the throttle BEFORE spawning so a burst of PostToolUse
  1808. # fires in the same second don't each spawn a subprocess.
  1809. open(_SDK_BOOTSTRAP_THROTTLE, "w").close()
  1810. script = os.path.join(
  1811. os.path.dirname(os.path.abspath(__file__)), "ensure_agent_sdk.py")
  1812. subprocess.Popen(
  1813. [sys.executable, script],
  1814. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
  1815. stdin=subprocess.DEVNULL, start_new_session=True,
  1816. )
  1817. except Exception:
  1818. pass # best-effort; never break the hook over a bootstrap attempt
  1819. def main():
  1820. """Main hook function."""
  1821. debug_log(f"Hook called with args: {sys.argv}")
  1822. # Master kill switch — honors ENABLE_SECURITY_REMINDER=0 (legacy) and
  1823. # SECURITY_GUIDANCE_DISABLE=1 (clearer name, no double negative). Emit
  1824. # empty metrics so asyncRewake hooks (Stop) don't hang waiting for stdout
  1825. # output that never comes.
  1826. if SECURITY_GUIDANCE_DISABLED:
  1827. emit_metrics({"skipped": True, "skip_reason": -1})
  1828. sys.exit(0)
  1829. # Periodically clean up old state files (10% chance per run)
  1830. if random.random() < 0.1:
  1831. cleanup_old_state_files()
  1832. # Read input from stdin
  1833. try:
  1834. raw_input = sys.stdin.read()
  1835. input_data = json.loads(raw_input)
  1836. except json.JSONDecodeError as e:
  1837. debug_log(f"JSON decode error: {e}")
  1838. emit_metrics({"skipped": True, "skip_reason": -2})
  1839. sys.exit(0)
  1840. session_id = input_data.get("session_id", "default")
  1841. tool_name = input_data.get("tool_name", "")
  1842. tool_input = input_data.get("tool_input", {})
  1843. hook_event_name = input_data.get("hook_event_name", "")
  1844. debug_log(f"Processing: hook_event={hook_event_name}, tool={tool_name}")
  1845. # Load project-specific security guidance and custom patterns once
  1846. # per invocation. Failures are non-fatal (debug-logged) so a malformed
  1847. # config never prevents the built-in checks from running.
  1848. extensibility.load_for_session(input_data.get("cwd"))
  1849. # Remote-pod SDK-bootstrap rescue: PostToolUse is the earliest hook event
  1850. # that is guaranteed to fire *after* async plugin sync (its firing proves
  1851. # the plugin is registered), so it's where we recover the SessionStart
  1852. # bootstrap that remote pods miss under CLAUDE_CODE_SYNC_PLUGIN_INSTALL.
  1853. # Fires on Edit/Write too (not just Bash), so the venv is usually built
  1854. # before the first `git commit`.
  1855. if hook_event_name == "PostToolUse":
  1856. _maybe_bootstrap_agent_sdk_async()
  1857. # Handle UserPromptSubmit — capture git baseline
  1858. if hook_event_name == "UserPromptSubmit":
  1859. handle_user_prompt_submit(input_data)
  1860. return
  1861. # Handle Stop hook — final security check
  1862. if hook_event_name == "Stop":
  1863. handle_stop_hook(input_data)
  1864. return
  1865. # Handle PostToolUse[Bash] — commit review or push sweep (asyncRewake).
  1866. #
  1867. # hooks.json has two `if` configs under the Bash matcher (`git commit:*`
  1868. # and `git push:*`). CC evaluates each `if` independently and spawns this
  1869. # script ONCE PER MATCH — so `git commit -m x && git push` spawns python
  1870. # twice with the same command string and the same tool_use_id. The python
  1871. # cannot tell which `if` fired it.
  1872. #
  1873. # Routing therefore MUST check commit FIRST so that compound commit+push
  1874. # commands continue to hit commit-review (the pre-existing behaviour) on
  1875. # the commit-matcher invocation. The push-matcher invocation of the SAME
  1876. # compound command is deduped by `_claim_bash_hook_once` below: the second
  1877. # spawn loses the tool_use_id sentinel race and exits early with
  1878. # `bash_hook_dedup`, so commit-review runs exactly once. The alternative —
  1879. # checking push first — would silently DROP commit-review
  1880. # on `git commit && git push`, which is a regression.
  1881. #
  1882. # The push-sweep does NOT run on the compound call. That's acceptable: the
  1883. # just-made commit is recorded by commit-review, so the next standalone
  1884. # push sees it as reviewed and the sweep base advances past it. Older
  1885. # unreviewed commits in the range are caught on that next push.
  1886. if tool_name == "Bash" and hook_event_name == "PostToolUse":
  1887. cmd = (input_data.get("tool_input") or {}).get("command", "") or ""
  1888. if not (_GIT_COMMIT_RE.search(cmd) or _GIT_PUSH_RE.search(cmd)):
  1889. return
  1890. if not _claim_bash_hook_once(input_data):
  1891. # Another spawn for this same tool_use_id already claimed the
  1892. # work (compound matched multiple `if` configs). Emit a single
  1893. # metric so telemetry can count how often the de-dupe kicks in.
  1894. print(json.dumps({"metrics": {"bash_hook_dedup": True}}), flush=True)
  1895. sys.exit(0)
  1896. if _GIT_COMMIT_RE.search(cmd):
  1897. handle_commit_review_posttooluse(input_data)
  1898. elif _GIT_PUSH_RE.search(cmd):
  1899. handle_push_sweep_posttooluse(input_data)
  1900. return
  1901. # Handle PostToolUse — pattern-based checks only (no LLM review per-edit)
  1902. if tool_name in ["Edit", "Write", "MultiEdit", "NotebookEdit"]:
  1903. file_path = tool_input.get("file_path") or tool_input.get("notebook_path") or ""
  1904. if not file_path:
  1905. sys.exit(0)
  1906. # Skip plan files
  1907. plans_dir = os.path.expanduser("~/.claude/plans")
  1908. if file_path.startswith(plans_dir):
  1909. sys.exit(0)
  1910. record_touched_path(session_id, file_path)
  1911. content = extract_content_from_input(tool_name, tool_input)
  1912. all_guidance = []
  1913. raw_pattern_matches = []
  1914. if ENABLE_PATTERN_RULES:
  1915. pattern_matches = check_patterns(file_path, content)
  1916. raw_pattern_matches = pattern_matches
  1917. if pattern_matches:
  1918. debug_log(f"Pattern matches for {file_path}: {[r for r, _ in pattern_matches]}")
  1919. # For Write tool, filter out patterns that existed in the baseline version
  1920. # This prevents flagging pre-existing insecure patterns when Claude rewrites a file
  1921. if tool_name == "Write" and pattern_matches:
  1922. cwd = os.environ.get("CLAUDE_PROJECT_DIR", os.getcwd())
  1923. baseline_content = get_baseline_file_content(session_id, file_path, cwd)
  1924. if baseline_content is not None:
  1925. baseline_matches = set(r for r, _ in check_patterns(file_path, baseline_content))
  1926. pattern_matches = [(r, msg) for r, msg in pattern_matches if r not in baseline_matches]
  1927. if pattern_matches:
  1928. debug_log(f"New patterns (not in baseline): {[r for r, _ in pattern_matches]}")
  1929. else:
  1930. debug_log("All patterns existed in baseline, skipping")
  1931. for rule_name, reminder in pattern_matches:
  1932. warning_key = f"{file_path}-{rule_name}"
  1933. if atomic_check_and_mark_warning(session_id, warning_key):
  1934. all_guidance.append(reminder)
  1935. # Record matched rules as pending so the Stop-hook sweep can
  1936. # later tally fixed vs unresolved. Only runs when patterns match.
  1937. if pattern_matches:
  1938. record_pending_warnings(session_id, file_path,
  1939. [r for r, _ in pattern_matches])
  1940. # Emit metrics when raw patterns matched (even if all were baseline-suppressed
  1941. # or dedup'd — pattern_hits reflects warnings actually shown, may be 0).
  1942. # Gate on raw matches so clean edits don't flood the metrics event.
  1943. # rule_id: RuleId of the first raw match (values stay small/enumerable in telemetry)
  1944. # rule_mask: bitmask of ALL raw matches — POPCOUNT gives raw hit count,
  1945. # (mask >> N) & 1 tests for a specific rule
  1946. if raw_pattern_matches:
  1947. raw_names = [r for r, _ in raw_pattern_matches]
  1948. output = {"metrics": {
  1949. "pattern_hits": len(all_guidance),
  1950. # User-defined patterns (rule_name="user:*") have no static
  1951. # RuleId; emit -1 so the metrics pipeline can distinguish.
  1952. "rule_id": int(_RULE_NAME_TO_ID.get(raw_names[0], -1)),
  1953. "rule_mask": rule_names_to_mask(raw_names),
  1954. **({"pv": _PV} if _PV else {}),
  1955. }}
  1956. if all_guidance:
  1957. output["hookSpecificOutput"] = {
  1958. "hookEventName": "PostToolUse",
  1959. "additionalContext": PROVENANCE_TAG + "\n\n" + "\n\n".join(all_guidance),
  1960. }
  1961. print(json.dumps(output))
  1962. elif all_guidance:
  1963. # Defensive: pattern rules disabled but guidance somehow set (shouldn't happen)
  1964. print(json.dumps({
  1965. "hookSpecificOutput": {
  1966. "hookEventName": "PostToolUse",
  1967. "additionalContext": PROVENANCE_TAG + "\n\n" + "\n\n".join(all_guidance),
  1968. }
  1969. }))
  1970. sys.exit(0)
  1971. if __name__ == "__main__":
  1972. main()