security_reminder_hook.py 102 KB

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