security_reminder_hook.py 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192
  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. _GIT_COMMIT_RE = re.compile(r'\bgit\s+commit(?:\s|$)')
  527. _GIT_AMEND_RE = re.compile(r'\s--amend\b')
  528. # Rolling-window cap on LLM commit-review calls. See atomic_check_rate_limit
  529. # docstring for the rationale that motivated the switch from a lifetime cap.
  530. # `MAX_COMMIT_REVIEWS_PER_SESSION` is read for backward-compat with users who
  531. # tuned it; the value is reinterpreted as per-hour.
  532. MAX_COMMIT_REVIEWS_PER_HOUR = int(
  533. os.environ.get("MAX_COMMIT_REVIEWS_PER_HOUR")
  534. or os.environ.get("MAX_COMMIT_REVIEWS_PER_SESSION", "20")
  535. )
  536. COMMIT_REVIEW_RATE_WINDOW_S = int(
  537. os.environ.get("COMMIT_REVIEW_RATE_WINDOW_S", "3600")
  538. )
  539. # ─── push-sweep ─────────────────────────────────────────────────────────────
  540. #
  541. # Mirrors Claude Code's own push-command matching — tolerates `git -C <p>` /
  542. # `git -c k=v` global options. The hooks.json `Bash(git push:*)` matcher
  543. # (subcommand prefix) doesn't, but those forms are rare in practice
  544. # and the python only ever runs after CC's matcher fired, so this regex is a
  545. # defensive re-gate, not a widening — `git -C path push` won't reach python
  546. # unless chained with a plain `git push` in the same compound command.
  547. #
  548. # `gh pr create` is intentionally NOT a separate hooks.json matcher: gh runs
  549. # `git push` as a child process, which CC's matcher doesn't observe (it sees
  550. # only the top-level `gh pr create` argv). A separate `Bash(gh pr create:*)`
  551. # entry would buy minimal extra coverage (sessions that push only via gh) at
  552. # the cost of an extra python spawn on every `... && gh pr create` compound
  553. # (the common case). Those sessions are caught on their next standalone `git push`.
  554. _GIT_PUSH_RE = re.compile(
  555. r'\bgit(?:\s+-[cC]\s+\S+|\s+--\S+=\S+)*\s+push\b'
  556. )
  557. # `git push` stdout: "abc1234..def5678 branch -> branch" (or `+abc..def` on
  558. # force, `* [new branch]` on first push). The left sha is where the remote
  559. # was BEFORE this push — exactly the base we need. Captures (old, new,
  560. # local-ref) so the handler can verify the pushed ref == HEAD before
  561. # diffing — `git push origin other` while on a different branch would
  562. # otherwise diff the wrong range.
  563. _PUSH_RANGE_RE = re.compile(
  564. r'^\s*\+?\s*([0-9a-f]{7,40})\.\.\.?([0-9a-f]{7,40})\s+(\S+)\s+->\s+\S+',
  565. re.MULTILINE,
  566. )
  567. MAX_PUSH_SWEEP_FILES = int(os.environ.get("SG_PUSH_SWEEP_MAX_FILES", "30"))
  568. MAX_PUSH_SWEEP_RANGE = int(os.environ.get("SG_PUSH_SWEEP_MAX_RANGE", "50"))
  569. PUSH_SWEEP_REPORT_CAP = int(os.environ.get("SG_PUSH_SWEEP_REPORT_CAP", "3"))
  570. def _claim_bash_hook_once(input_data):
  571. """De-dupe across hooks.json `if` matchers firing for the same Bash call.
  572. `git commit -m x && git push` matches both `Bash(git commit:*)` and
  573. `Bash(git push:*)` `if` configs → CC spawns this script twice with the
  574. SAME `tool_use_id`. The first spawn atomically creates a
  575. sentinel under `.git/`; subsequent spawns see it and exit early. Avoids
  576. redundant LLM calls (and the redundant asyncRewake) on compound commands.
  577. Returns True if this spawn won the claim (or no de-dupe is possible),
  578. False if another spawn already claimed it.
  579. Sentinel is per-clone (`.git/sg-hook-once-<tool_use_id>`), not /tmp,
  580. so concurrent CC sessions in *different* repos don't collide. Stale
  581. sentinels (>5min) are GC'd opportunistically.
  582. """
  583. tuid = input_data.get("tool_use_id")
  584. cwd = input_data.get("cwd")
  585. if not tuid or not cwd:
  586. return True
  587. gd = _git_dir(_git_toplevel(cwd) or cwd)
  588. if not gd:
  589. return True
  590. # GC: best-effort sweep of stale sentinels so they don't accumulate.
  591. import time as _time
  592. now = _time.time()
  593. try:
  594. for name in os.listdir(gd):
  595. if name.startswith("sg-hook-once-"):
  596. p = os.path.join(gd, name)
  597. try:
  598. if now - os.path.getmtime(p) > 300:
  599. os.unlink(p)
  600. except OSError:
  601. pass
  602. except OSError:
  603. pass
  604. # Sanitize tuid into a filesystem-safe basename — defensive, the value is
  605. # CC-generated (toolu_<b64ish>), but it ends up in a path.
  606. safe = re.sub(r"[^A-Za-z0-9_-]", "_", tuid)[:80]
  607. sentinel = os.path.join(gd, f"sg-hook-once-{safe}")
  608. try:
  609. fd = os.open(sentinel, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
  610. os.close(fd)
  611. return True
  612. except FileExistsError:
  613. return False
  614. except OSError:
  615. # Can't write sentinel (read-only fs, perms) — proceed rather than
  616. # silently dropping the review.
  617. return True
  618. def is_push_sweep_enabled():
  619. """Gate for the push-sweep PostToolUse[Bash] hook.
  620. Enabled by default. ENABLE_COMMIT_REVIEW=0 remains the unconditional
  621. kill switch (push-sweep reuses the same review pipeline and budget).
  622. SG_PUSH_SWEEP is the per-user override (=1/on or =0/off) checked
  623. next so users can opt out.
  624. """
  625. if not ENABLE_COMMIT_REVIEW:
  626. return False
  627. v = os.environ.get("SG_PUSH_SWEEP", "").strip().lower()
  628. if v in ("1", "on"):
  629. return True
  630. if v in ("0", "off"):
  631. return False
  632. return True
  633. PUSH_SWEEP_ENABLED = is_push_sweep_enabled()
  634. def _compute_push_sweep_base(prev_upstream, push_range, reviewed):
  635. """Advance the diff base past the contiguous reviewed prefix.
  636. Spec: review `git diff B..HEAD` where `B` is the newest commit such that
  637. `prev_upstream..B` is entirely in `reviewed`. Returns (B, unreviewed_tail).
  638. `B == None` means the whole range is reviewed (caller should skip).
  639. `push_range` must be oldest→newest.
  640. Examples (✓=reviewed, ✗=not):
  641. [✓1, ✗2, ✓3] → B=1, tail=[2,3] (cannot trim suffix; Read is at HEAD)
  642. [✓1, ✓2, ✓3] → B=None (all reviewed → skip)
  643. [✗1, ✓2, ✗3] → B=prev_upstream, tail=[1,2,3]
  644. [] → B=None
  645. """
  646. i = 0
  647. while i < len(push_range) and push_range[i] in reviewed:
  648. i += 1
  649. if i == len(push_range):
  650. return None, []
  651. base = push_range[i - 1] if i > 0 else prev_upstream
  652. return base, push_range[i:]
  653. def _push_section(bash_output):
  654. """Return the slice of `bash_output` that contains the push's range lines.
  655. `_PUSH_RANGE_RE` is not push-specific — `git fetch` and `git pull` print
  656. range lines (`abc..def branch -> origin/branch`) in the same format. On
  657. chained calls the Bash tool returns combined stdout+stderr, so a naive
  658. `_PUSH_RANGE_RE.finditer(bash_output)` matches both sections and a
  659. fetch+push compound trips the multi-ref skip.
  660. `git push` prints `To <remote>` immediately before its range lines;
  661. `git fetch`/`git pull` prints `From <remote>` before theirs. The slice
  662. is symmetric: start at the LAST `To <remote>` header (strips fetch output
  663. that ran *before* the push, e.g. `git fetch && git push`), and end at
  664. the next `From <remote>` after that (strips fetch output that ran
  665. *after* the push, e.g. `git push && git fetch`).
  666. If no `To ` header is present (push failed before connecting, output
  667. suppressed by `-q`) the full buffer is returned and the caller's
  668. other guards handle it.
  669. """
  670. if not bash_output:
  671. return ""
  672. # Match line-anchored "To " — look for "\nTo " or "To " at start-of-string.
  673. idx = bash_output.rfind("\nTo ")
  674. if idx >= 0:
  675. section = bash_output[idx:]
  676. elif bash_output.startswith("To "):
  677. section = bash_output
  678. else:
  679. return bash_output
  680. # Strip a trailing fetch/pull `From <remote>` block (push && fetch /
  681. # push && pull, or any wrapper that re-syncs after the push).
  682. end = section.find("\nFrom ")
  683. if end >= 0:
  684. section = section[:end]
  685. return section
  686. def _detect_prev_upstream(repo_root, bash_output):
  687. """Where the remote was BEFORE this push.
  688. Preference order:
  689. 1. Parse `abc..def` from push stdout — authoritative, exact.
  690. 2. `<branch>@{u}@{1}` — the remote-tracking ref's reflog position before
  691. this push moved it. PostToolUse runs after `git push` completes, so
  692. `@{u}` is already updated and `@{u}@{1}` is the prior value.
  693. 3. merge-base with the detected main branch — first push of a new
  694. branch (`* [new branch]` in output, no upstream reflog yet).
  695. Returns a resolvable ref/sha or None.
  696. """
  697. m = _PUSH_RANGE_RE.search(_push_section(bash_output or ""))
  698. if m:
  699. return m.group(1)
  700. # @{u}@{1} — only meaningful if an upstream is configured.
  701. for ref in ("@{u}@{1}", "@{push}@{1}"):
  702. try:
  703. r = subprocess.run(
  704. [*GIT_CMD, "rev-parse", "--verify", "-q", ref],
  705. cwd=repo_root, capture_output=True, text=True, timeout=5,
  706. )
  707. if r.returncode == 0 and r.stdout.strip():
  708. return r.stdout.strip()
  709. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  710. pass
  711. main = _detect_main_branch(repo_root)
  712. if main:
  713. try:
  714. r = subprocess.run(
  715. [*GIT_CMD, "merge-base", "HEAD", main],
  716. cwd=repo_root, capture_output=True, text=True, timeout=5,
  717. )
  718. if r.returncode == 0 and r.stdout.strip():
  719. return r.stdout.strip()
  720. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  721. pass
  722. return None
  723. def is_commit_review_enabled():
  724. """Gate for the commit-review PostToolUse[Bash] hook.
  725. Commit review is enabled by default; ENABLE_COMMIT_REVIEW=0 remains the
  726. unconditional kill switch and SECURITY_GUIDANCE_COMMIT_REVIEW (on/off)
  727. remains a legacy per-user override; everything else defaults on.
  728. commit_review_on is still emitted in metrics for continuity.
  729. """
  730. if not ENABLE_COMMIT_REVIEW:
  731. return False
  732. override = os.environ.get("SECURITY_GUIDANCE_COMMIT_REVIEW", "").strip().lower()
  733. if override in ("on", "off"):
  734. return override == "on"
  735. return True
  736. COMMIT_REVIEW_ENABLED = is_commit_review_enabled()
  737. def _agentic_review_with_race(
  738. repo_root: str,
  739. diff_files: List[Tuple[str, str]],
  740. rel_touched: List[str],
  741. previous_findings: List[Dict[str, Any]],
  742. ) -> Tuple[Optional[str], List[Dict[str, Any]], Dict[str, Any]]:
  743. """Race the agentic reviewer against a delayed single-shot fallback.
  744. Agentic starts at t=0. After SG_AGENTIC_RACE_DELAY_S (default 180s), the
  745. single-shot diff reviewer also starts. Whichever finishes first wins. If
  746. agentic finishes before the delay elapses, the fallback never runs.
  747. Metrics added:
  748. race_winner : 1 = agentic won, 2 = fallback won (CC accepts only
  749. bool/finite-number metric values — strings would discard the dict)
  750. race_delay_s : the configured delay
  751. race_started : 1 if the fallback was actually launched, else 0
  752. Only the commit-review handler calls this — external harnesses invoke
  753. agentic_review() directly and are unaffected. SG_AGENTIC_NO_RACE=1
  754. disables the race for any other caller that wants pure agentic.
  755. """
  756. import queue as _queue
  757. import threading as _th
  758. import time as _t
  759. if os.environ.get("SG_AGENTIC_NO_RACE") == "1":
  760. return agentic_review(repo_root, diff_files, rel_touched)
  761. delay_s = int(os.environ.get("SG_AGENTIC_RACE_DELAY_S", "180"))
  762. q: "_queue.Queue[Tuple[str, Any]]" = _queue.Queue(maxsize=1)
  763. fallback_started = _th.Event()
  764. def _agentic() -> None:
  765. try:
  766. r = agentic_review(repo_root, diff_files, rel_touched)
  767. except Exception as e: # pragma: no cover — crash → let fallback win
  768. r = (None, [], {"agentic_fallback": f"race_crash:{type(e).__name__}"})
  769. try:
  770. q.put_nowait(("agentic", r))
  771. except _queue.Full:
  772. pass
  773. def _fallback() -> None:
  774. _t.sleep(delay_s)
  775. if not q.empty():
  776. return # agentic finished within the delay — never start fallback
  777. fallback_started.set()
  778. try:
  779. g, v = analyze_code_security(
  780. diff_files, is_diff=True, previous_findings=previous_findings
  781. )
  782. except Exception as e: # pragma: no cover
  783. g, v = None, []
  784. try:
  785. q.put_nowait(("fallback", (g, v, {"agentic": False})))
  786. except _queue.Full:
  787. pass
  788. _th.Thread(target=_agentic, daemon=True).start()
  789. _th.Thread(target=_fallback, daemon=True).start()
  790. winner, (g, v, m) = q.get()
  791. m = dict(m) # don't mutate the callee's metrics dict
  792. m["race_winner"] = 1 if winner == "agentic" else 2
  793. m["race_delay_s"] = delay_s
  794. m["race_started"] = 1 if fallback_started.is_set() else 0
  795. return g, v, m
  796. def handle_commit_review_posttooluse(input_data):
  797. """PostToolUse handler for Bash — reviews git commits for security issues.
  798. Runs as asyncRewake: detects `git commit` in the Bash command, parses
  799. the resulting SHA(s) from the Bash stdout `[branch sha] msg` line, runs
  800. `git show -p <sha>` per SHA, sends the combined diff through
  801. analyze_code_security, and exits with code 2 (stderr findings) to wake
  802. the model. Deduplicates against the shared previous_findings state so
  803. the Stop hook won't re-flag the same (filePath, vulnerableCode) pair.
  804. """
  805. session_id = input_data.get("session_id", "default")
  806. tool_input = input_data.get("tool_input", {})
  807. tool_response = input_data.get("tool_response", {})
  808. cwd = input_data.get("cwd", "")
  809. command = tool_input.get("command", "")
  810. if not isinstance(command, str) or not _GIT_COMMIT_RE.search(command):
  811. # Defensive only — hooks.json's `"if": "Bash(git commit:*)"` is the
  812. # real gate so CC never spawns python3 for ls/grep/etc. This catches
  813. # cases where CC's command matching fails open and spawns the hook anyway.
  814. sys.exit(0)
  815. debug_log(f"Commit review: detected git commit in command")
  816. # Bash tool_response has no exit_code field (only stdout, stderr,
  817. # interrupted), so success is inferred from the output text — the same
  818. # heuristic Claude Code itself uses.
  819. if not isinstance(tool_response, dict):
  820. tool_response = {}
  821. stdout = tool_response.get("stdout", "") or ""
  822. stderr = tool_response.get("stderr", "") or ""
  823. bash_output = stdout + "\n" + stderr
  824. interrupted = bool(tool_response.get("interrupted"))
  825. # Require BOTH a line-anchored `[branch sha]` AND a git-only diffstat
  826. # signal before treating the tool call as a successful commit. The old
  827. # `any()` check false-positived on (a) pre-commit/husky/lint-staged hooks
  828. # emitting labels like `[pre-commit abc1234]`, and on (b) chained
  829. # `git commit || git log --stat` where `N files changed` appears in output
  830. # even though the commit itself failed.
  831. commit_succeeded = (
  832. not interrupted
  833. and _COMMIT_SHA_RE.search(bash_output) is not None
  834. and any(p.search(bash_output) for p in _COMMIT_DIFFSTAT_PATTERNS)
  835. )
  836. # commit_review_on emitted on every path so telemetry can filter on
  837. # commit_review and group by commit_review_on.
  838. _base = {"commit_review": True, "commit_review_on": COMMIT_REVIEW_ENABLED}
  839. # Reflog fallback for hidden stdout. Analysis of skip_reason=21 emissions
  840. # showed a large share were commits that DID succeed
  841. # but whose `[branch sha]` line was hidden by piping/redirection/-q
  842. # (e.g., `git commit -m ... 2>&1 | tail -3`). A HEAD@{0}
  843. # reflog check substantially reduced this skip; follow-up analysis found
  844. # the residual is dominated by (a) chained commands moving HEAD@{0} past
  845. # `commit:` (`git commit && git push`), and (b) the `_obvious_noop` guard
  846. # false-positiving on chained `git status` output after a successful -q
  847. # commit. Widening to the last-5-entries × 120s scan and dropping the noop
  848. # guard fixes both. The reviewed-shas dedup below prevents the wider window
  849. # from re-reviewing a prior Bash call's commit, and is the same file
  850. # push-sweep reads — so a SHA is reviewed at most once across both
  851. # surfaces. See _git_reflog_recent_commits docstring for cross-repo /
  852. # race safety.
  853. _reflog_shas: List[str] = []
  854. _skip_21_sub = 0
  855. if not commit_succeeded and not interrupted and cwd:
  856. _root = _git_toplevel(cwd)
  857. _fresh, _stale = _git_reflog_recent_commits(_root)
  858. if _fresh:
  859. _already = _load_reviewed_shas(_root)
  860. _reflog_shas = [s for s in _fresh if s not in _already]
  861. if _reflog_shas:
  862. commit_succeeded = True
  863. debug_log(
  864. f"Commit review: stdout had no `[branch sha]`; reflog "
  865. f"shows {len(_reflog_shas)} fresh unreviewed commit(s) "
  866. f"({_reflog_shas[0][:12]}...)"
  867. )
  868. else:
  869. # Fresh commit(s) in reflog but all already in
  870. # sg-reviewed-shas — likely a Bash retry or the commit was
  871. # reviewed via a prior fire. Correct to skip; sub=2 lets telemetry
  872. # split this from genuine fails.
  873. _skip_21_sub = 2
  874. elif _stale:
  875. _skip_21_sub = 3 # commit entries exist but all >120s old
  876. else:
  877. _skip_21_sub = 4 # no commit-action entries — genuine fail
  878. if not commit_succeeded:
  879. debug_log("Commit review: commit did not succeed, skipping")
  880. emit_metrics({"skipped": True, "skip_reason": 21, **_base,
  881. **({"skip_21_sub": 1} if interrupted
  882. else {"skip_21_sub": _skip_21_sub} if _skip_21_sub
  883. else {})})
  884. sys.exit(0)
  885. if not COMMIT_REVIEW_ENABLED:
  886. debug_log("Commit review: disabled, skipping")
  887. emit_metrics({"skipped": True, "skip_reason": 32, **_base})
  888. sys.exit(0)
  889. if not ENABLE_CODE_SECURITY_REVIEW or not HAS_API_CREDENTIALS:
  890. debug_log("Commit review: LLM review disabled or no API credentials")
  891. emit_metrics({"skipped": True, "skip_reason": 22, **_base})
  892. sys.exit(0)
  893. if not ensure_anthropic_reachable():
  894. debug_log("Commit review: api.anthropic.com unreachable")
  895. emit_metrics({"skipped": True, "skip_reason": 24, **_base})
  896. sys.exit(0)
  897. if not cwd:
  898. debug_log("Commit review: no cwd")
  899. emit_metrics({"skipped": True, "skip_reason": 25, **_base})
  900. sys.exit(0)
  901. repo_root = _git_toplevel(cwd)
  902. if not repo_root:
  903. debug_log("Commit review: not in a git repo")
  904. emit_metrics({"skipped": True, "skip_reason": 26, **_base})
  905. sys.exit(0)
  906. # Pin the review to the exact SHA the Bash command produced, parsed from
  907. # its stdout. Reviewing HEAD instead is wrong when the commit was made in
  908. # a different repo than the hook's cwd (`cd ../other && git commit && cd -`,
  909. # subshells), or when a second commit lands before this async hook reaches
  910. # `git show` — both would review an unrelated commit. The reflog-action
  911. # fallback above is the narrow exception: it only fires when output gave
  912. # us nothing AND the cwd repo's own reflog confirms a `commit:` just
  913. # happened there, which rules out the cross-repo case.
  914. #
  915. # Take only the LAST match: pre-commit/husky hooks can print bracketed
  916. # labels like `[pre-commit abc1234]` that precede the real `[branch sha]`
  917. # line; chained commands like `git commit && git commit` produce multiple
  918. # real SHAs and we want the most recent. The real commit line is always
  919. # last in git's own output — the earlier matches are either decoys or
  920. # superseded commits.
  921. if _reflog_shas:
  922. # Output-based detection already failed above; the reflog SHAs are the
  923. # authoritative ones. Don't re-parse bash_output here — any bracketed
  924. # token it contains is by construction NOT the `[branch sha]` line
  925. # (or commit_succeeded would have been True via the fast path). The
  926. # list is newest-first and may contain >1 entry when a single Bash
  927. # call made multiple commits (`git commit -m a && git commit -m b`);
  928. # all are reviewed.
  929. shas = _reflog_shas
  930. else:
  931. all_shas = _COMMIT_SHA_RE.findall(bash_output)
  932. shas = [all_shas[-1]] if all_shas else []
  933. if not shas:
  934. debug_log("Commit review: no SHA in commit output")
  935. emit_metrics({"skipped": True, "skip_reason": 33, **_base})
  936. sys.exit(0)
  937. if _reflog_shas:
  938. # Observability: track how often the fallback path is hit so
  939. # future analysis can split on it.
  940. # `reflog_shas_n` lets telemetry measure how often the widened scan picked
  941. # up >1 commit (i.e., chained `git commit && git commit`).
  942. _base = {**_base, "sha_via_reflog": True,
  943. "reflog_shas_n": len(_reflog_shas)}
  944. # `git commit --amend`: review only the delta added by the amend
  945. # (pre-amend..post-amend) instead of the full amended commit. Without this,
  946. # the amend re-reviews the entire commit including code already reviewed
  947. # on the original commit, costing 30-60s of LLM time and re-flagging
  948. # findings the user may have just amended IN ORDER TO fix. Pre-amend
  949. # SHA comes from the reflog and is validated to be an amend (see
  950. # _resolve_amend_pre_sha) — otherwise we fall back to full-commit review.
  951. #
  952. # Three guards skip the delta path and fall back to full `git show`
  953. # review. All three close variants of "chained `git commit && git commit
  954. # --amend` in one Bash call", which would otherwise enter the delta path,
  955. # see an empty `git diff sha_wip sha_amend`, emit skip_reason=35, and
  956. # silently drop the first commit's content from review (no prior
  957. # PostToolUse fired for it — same Bash call):
  958. #
  959. # 1. `not _reflog_shas`: reflog fallback path was taken (both commits'
  960. # bash output suppressed via -q / pipe / redirect). The multi-SHA scan
  961. # already populates `shas` with every fresh commit (amend + any
  962. # pre-amend WIP) and the loop below `git show`s each, so coverage is
  963. # correct without delta — and the delta path doesn't compose with a
  964. # multi-SHA `shas` list (it would diff every entry against the same
  965. # pre-amend SHA). Losing the 30-60s saving on the reflog-fallback
  966. # fraction is an acceptable trade.
  967. #
  968. # 2. `len(all_shas) <= 1`: both commits visible (no -q). Two `[branch
  969. # sha]` lines in bash_output → all_shas len 2. Only defined on the
  970. # bash-output path; short-circuit ordering keeps it unevaluated when
  971. # `_reflog_shas` is non-empty.
  972. #
  973. # 3. `commit_invocations <= 1`: asymmetric — first commit -q, amend
  974. # visible. Fast-path fires on the amend's `[branch sha]` line (so
  975. # `_reflog_shas` stays empty), all_shas = [sha_amend] (len 1) — guards
  976. # 1 and 2 both pass. The command string itself is the only remaining
  977. # signal that two commits happened. False-positives (e.g.
  978. # `git commit --amend -m "fix git commit bug"`) are safe — they fall
  979. # back to full review.
  980. is_amend = bool(_GIT_AMEND_RE.search(command))
  981. commit_invocations = len(_GIT_COMMIT_RE.findall(command))
  982. pre_amend_sha = None
  983. if (is_amend and not _reflog_shas and len(all_shas) <= 1
  984. and commit_invocations <= 1):
  985. pre_amend_sha = _resolve_amend_pre_sha(repo_root, expected_post_sha=shas[0])
  986. if is_amend and pre_amend_sha:
  987. _base = {**_base, "amend_delta_review": True}
  988. debug_log(
  989. f"Commit review: --amend detected; reviewing delta "
  990. f"{pre_amend_sha[:12]}..{shas[-1][:12]}"
  991. )
  992. # --no-color: `color.ui=always` would emit ANSI escapes that corrupt
  993. # parse_diff_into_files' header match. Bytes + errors='replace': commits
  994. # can contain non-UTF8 source (latin-1, cp1252) and text=True would raise
  995. # UnicodeDecodeError outside the except clause.
  996. diff_files = []
  997. resolved = 0
  998. for sha in shas:
  999. try:
  1000. if pre_amend_sha:
  1001. # Delta review: pre-amend → post-amend. `git diff` (not show)
  1002. # so the output is a pure unified diff with no commit header.
  1003. result = subprocess.run(
  1004. [*GIT_CMD, "diff", "--no-color", "--no-ext-diff", pre_amend_sha, sha, "--"],
  1005. cwd=repo_root, capture_output=True, timeout=15
  1006. )
  1007. else:
  1008. result = subprocess.run(
  1009. [*GIT_CMD, "show", "-p", "--no-color", "--no-ext-diff", sha, "--"],
  1010. cwd=repo_root, capture_output=True, timeout=15
  1011. )
  1012. except (subprocess.TimeoutExpired, FileNotFoundError, OSError) as e:
  1013. _cmd = "git diff" if pre_amend_sha else "git show"
  1014. debug_log(f"Commit review: {_cmd} {sha} error: {e}")
  1015. continue
  1016. if result.returncode != 0:
  1017. # SHA not in this repo (cross-repo commit) or already gc'd. Better
  1018. # to skip than to fall back to HEAD and review the wrong commit.
  1019. _cmd = "git diff" if pre_amend_sha else "git show"
  1020. debug_log(f"Commit review: {_cmd} {sha} rc={result.returncode}")
  1021. continue
  1022. resolved += 1
  1023. diff_files.extend(parse_diff_into_files(
  1024. result.stdout.decode("utf-8", errors="replace")))
  1025. # Dedup by path. The widened reflog scan can return >1 SHA (e.g.
  1026. # `git commit && git commit --amend` within 120s); a path that appears in
  1027. # both diffs would consume two MAX_DIFF_FILES slots and be re-analyzed.
  1028. # `shas` is newest-first so the first occurrence is the most recent
  1029. # version of the file — keep it.
  1030. if len(shas) > 1:
  1031. _seen = set()
  1032. diff_files = [
  1033. (fp, c) for fp, c in diff_files
  1034. if not (fp in _seen or _seen.add(fp))
  1035. ]
  1036. if resolved == 0:
  1037. debug_log("Commit review: no parsed SHA resolved in cwd repo")
  1038. emit_metrics({"skipped": True, "skip_reason": 28, **_base,
  1039. "shas_found": len(shas)})
  1040. sys.exit(0)
  1041. # Empty amend delta = message-only amend (or whitespace-only that the
  1042. # diff already collapses). No code to review; skip cleanly. skip_reason=35.
  1043. # Gated on resolved > 0 so subprocess failures (caught with `continue`
  1044. # above) don't get mislabeled as message-only — they fall through to
  1045. # skip_reason=28 correctly.
  1046. if pre_amend_sha and not diff_files:
  1047. debug_log("Commit review: --amend produced empty delta (message-only?), skipping")
  1048. emit_metrics({"skipped": True, "skip_reason": 35, **_base,
  1049. "files_reviewed": 0})
  1050. sys.exit(0)
  1051. debug_log(f"Commit review: {resolved}/{len(shas)} sha(s) resolved, "
  1052. f"{len(diff_files)} files")
  1053. if not diff_files:
  1054. debug_log("Commit review: no reviewable source files in commit")
  1055. emit_metrics({"skipped": True, "skip_reason": 30, **_base})
  1056. sys.exit(0)
  1057. # Large commits (initial scaffolds, big refactors) used to bail here with
  1058. # skip_reason=31. Large multi-file changes are exactly where
  1059. # cross-file source→sink vulns hide. Reviewing nothing is
  1060. # worse than reviewing the riskiest 30 — _cap_files_for_prompt already
  1061. # bounds total bytes downstream so this can't blow context.
  1062. # `diff_files_dropped` lets telemetry measure how often the prioritizer engages
  1063. # and how much it drops; skip_reason=31 is now reserved for the truly
  1064. # pathological case (e.g. >300 source files — almost certainly a bad
  1065. # baseline, not a real commit).
  1066. if len(diff_files) > 10 * MAX_DIFF_FILES:
  1067. debug_log(f"Commit review: pathological diff ({len(diff_files)} files), skipping")
  1068. emit_metrics({"skipped": True, "skip_reason": 31, **_base,
  1069. "diff_files_count": len(diff_files)})
  1070. sys.exit(0)
  1071. diff_files, _dropped = _prioritize_diff_files(diff_files, MAX_DIFF_FILES)
  1072. if _dropped:
  1073. debug_log(f"Commit review: prioritized to {len(diff_files)} files "
  1074. f"(dropped {_dropped} lower-risk)")
  1075. _base = {**_base, "diff_files_dropped": _dropped}
  1076. # Rolling-hour rate limit on LLM spend, so only burn a slot once we know
  1077. # we'll actually call analyze_code_security — skip 28/30/31/33 above are
  1078. # free. `rate_count` is emitted on every fire (not just rejections) so
  1079. # telemetry can show how close to the cap sessions run.
  1080. _allowed, _rate_n = atomic_check_rate_limit(
  1081. session_id, "CommitReview",
  1082. MAX_COMMIT_REVIEWS_PER_HOUR, COMMIT_REVIEW_RATE_WINDOW_S)
  1083. _base = {**_base, "rate_count": _rate_n}
  1084. if not _allowed:
  1085. debug_log("Commit review: hourly rate limit reached, skipping")
  1086. emit_metrics({"skipped": True, "skip_reason": 23, **_base})
  1087. sys.exit(0)
  1088. # Read previous_findings for dedup (shared with Stop hook)
  1089. import time as _time
  1090. now = _time.time()
  1091. def _read_previous(state):
  1092. findings_ts = state.get("previous_findings_ts", 0)
  1093. if (now - findings_ts) > PREVIOUS_FINDINGS_TTL_SEC:
  1094. return []
  1095. return list(state.get("previous_findings", []))
  1096. previous_findings = with_locked_state(session_id, _read_previous) or []
  1097. review_start = _time.time()
  1098. agentic_metrics: Dict[str, Any] = {}
  1099. if _agentic_commit_review_enabled():
  1100. rel_touched = [fp for fp, _ in diff_files]
  1101. concrete_guidance, vulns, _am = _agentic_review_with_race(
  1102. repo_root, diff_files, rel_touched, previous_findings
  1103. )
  1104. agentic_metrics.update(_am)
  1105. # Fall back to single-shot only on agentic FAILURE (SDK/investigate
  1106. # crash). If agentic completed and returned 0 findings, trust that.
  1107. if agentic_metrics.get("agentic_fallback"):
  1108. concrete_guidance, vulns = analyze_code_security(
  1109. diff_files, is_diff=True, previous_findings=previous_findings
  1110. )
  1111. else:
  1112. concrete_guidance, vulns = analyze_code_security(
  1113. diff_files, is_diff=True, previous_findings=previous_findings
  1114. )
  1115. # push-sweep state: record this commit as reviewed (full 40-hex sha) so a
  1116. # later `git push` can advance its diff base past it. Recorded here — after
  1117. # the review ran but before any exit path — so it's marked regardless of
  1118. # whether findings were emitted. `shas` holds abbreviated refs from
  1119. # `[branch sha]`; resolve to full so set-membership in the push-sweep is
  1120. # exact. Best-effort; failures here never block the review result.
  1121. try:
  1122. full_shas = []
  1123. for s in shas:
  1124. r = subprocess.run(
  1125. [*GIT_CMD, "rev-parse", "--verify", "-q", s],
  1126. cwd=repo_root, capture_output=True, text=True, timeout=5,
  1127. )
  1128. if r.returncode == 0:
  1129. full_shas.append(r.stdout.strip())
  1130. _append_reviewed_shas(repo_root, full_shas, vulns_found=len(vulns or []))
  1131. except Exception:
  1132. pass
  1133. review_ms = int((_time.time() - review_start) * 1000)
  1134. # `survived` is the raw self-refute count BEFORE the high/critical-only
  1135. # severity filter; `survived_after_sev` is the count the user actually
  1136. # sees. Include `survived_after_sev` ONLY when the filter actually
  1137. # dropped candidates — otherwise it's redundant with `survived` and eats
  1138. # into CC's 10-key emit cap, pushing files_reviewed/review_ms out of the
  1139. # emitted metrics.
  1140. #
  1141. # CC accepts only booleans and finite numbers as metric values.
  1142. # A null or string value makes CC discard the ENTIRE dict, so:
  1143. # - candidates/survived are omitted when None (early-return at
  1144. # candidates==0, or any fallback path)
  1145. # - agentic_fallback is mapped to an int reason code; the string detail
  1146. # stays in debug_log for diagnosis
  1147. _sev_raw = agentic_metrics.get("survived")
  1148. _sev_post = agentic_metrics.get("survived_after_sev")
  1149. _cand = agentic_metrics.get("candidates")
  1150. _fb = agentic_metrics.get("agentic_fallback")
  1151. # 1 = SDK import failed (claude_agent_sdk not installed)
  1152. # 2 = investigate stage failed (CLI/network/model error or schema-retry exhausted)
  1153. _fb_code = (1 if _fb and _fb.startswith("import:") else 2) if _fb else None
  1154. _race = agentic_metrics.get("race_winner")
  1155. _agentic_m = (
  1156. # `agentic` = which path produced the result, not which was attempted.
  1157. # On race-loss the _fallback() metrics dict has agentic=False — emitting
  1158. # True there blends the high-find-rate single-shot race-loss bucket into
  1159. # `agentic=true` queries and overstates agentic yield.
  1160. {"agentic": bool(agentic_metrics.get("agentic")),
  1161. **({"candidates": _cand} if _cand is not None else {}),
  1162. **({"survived": _sev_raw} if _sev_raw is not None else {}),
  1163. **({"survived_after_sev": _sev_post}
  1164. if _sev_post is not None and _sev_post != _sev_raw else {}),
  1165. **({"agentic_fallback": _fb_code} if _fb_code is not None else {}),
  1166. # 1 = agentic won, 2 = single-shot fallback won. review_ms already
  1167. # captures timing; race_winner lets telemetry segment recall by which path
  1168. # actually produced the result.
  1169. **({"race_winner": _race} if _race is not None else {})}
  1170. if agentic_metrics.get("agentic") or _fb or _race is not None
  1171. else {}
  1172. )
  1173. if not concrete_guidance:
  1174. debug_log("Commit review: no security issues found")
  1175. emit_metrics({
  1176. "vulns_found": 0, **_base, **_agentic_m,
  1177. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1178. **({
  1179. "api_error": llm._last_call_claude_http_error
  1180. } if llm._last_call_claude_http_error is not None else {}),
  1181. })
  1182. sys.exit(0)
  1183. # Late dedup: drop only what a concurrent Stop hook wrote while our LLM
  1184. # ran. Anything in `previous_findings` (the pre-LLM snapshot) that the
  1185. # LLM chose to re-flag is an intentional "fix incomplete" verdict.
  1186. new_vulns, n_deduped = _dedup_against_state(
  1187. session_id, vulns, prompted=_finding_keys(previous_findings)
  1188. )
  1189. if not new_vulns:
  1190. debug_log("Commit review: all findings already known, skipping")
  1191. emit_metrics({
  1192. "vulns_found": 0, **_base, **_agentic_m, "deduped": n_deduped,
  1193. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1194. })
  1195. sys.exit(0)
  1196. # Record new findings into shared state. Key on (filePath, category) —
  1197. # vulnerableCode bytes drift between fires (diff context lines shift) so
  1198. # matching on it under-dedupes; this aligns with Stop's _record_fire.
  1199. finding_snapshots = [
  1200. {
  1201. "filePath": v.get("filePath", ""),
  1202. "category": v.get("category", "Unknown"),
  1203. "vulnerableCode": v.get("vulnerableCode", ""),
  1204. }
  1205. for v in new_vulns
  1206. ]
  1207. def _record_findings(state):
  1208. existing = [f for f in state.get("previous_findings", []) if isinstance(f, dict)]
  1209. seen = {(f.get("filePath", ""), f.get("category", "")) for f in existing}
  1210. for f in finding_snapshots:
  1211. key = (f["filePath"], f["category"])
  1212. if key not in seen:
  1213. seen.add(key)
  1214. existing.append(f)
  1215. state["previous_findings"] = existing
  1216. state["previous_findings_ts"] = _time.time()
  1217. with_locked_state(session_id, _record_findings)
  1218. sev = {"critical": 0, "high": 0, "medium": 0}
  1219. for v in new_vulns:
  1220. s = v.get("severity", "medium")
  1221. if s in sev:
  1222. sev[s] += 1
  1223. emit_metrics({
  1224. "vulns_found": len(new_vulns), **_base, **_agentic_m,
  1225. "critical_count": sev["critical"], "high_count": sev["high"],
  1226. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1227. **({"deduped": n_deduped} if n_deduped else {}),
  1228. }, rewake_summary=_format_vulns_summary(new_vulns, prefix="Commit security review found"))
  1229. # Rebuild guidance from new_vulns only — concrete_guidance from the LLM
  1230. # still lists deduped entries.
  1231. sys.stderr.write(PROVENANCE_BANNER + "\n\n"
  1232. + _format_vulns_guidance(new_vulns)
  1233. + CONTINUATION_SUFFIX + "\n")
  1234. sys.exit(2)
  1235. def handle_push_sweep_posttooluse(input_data):
  1236. """Review the just-pushed range as one diff, advancing the base past the
  1237. contiguous prefix of already-per-commit-reviewed shas.
  1238. Spec: review `git diff B..HEAD` where `B` is the newest commit such that
  1239. `prev_upstream..B` is entirely in `.git/sg-reviewed-shas`. Skip if
  1240. `B == HEAD`. Mark `B..HEAD` reviewed afterward.
  1241. Diff and Read are both at HEAD (push doesn't move the working tree), so the
  1242. agentic reviewer sees a consistent view — a vuln introduced in commit A and
  1243. removed in commit B is absent from the net diff by construction. Any
  1244. reviewed commits in the tail (after the first unreviewed one) are included
  1245. in the diff; their findings are dropped by `_dedup_against_state` against
  1246. `previous_findings` the per-commit hook already recorded.
  1247. Metrics: `push_sweep: True` is the telemetry splitter; `pushed`/`unreviewed`/
  1248. `prefix_advanced` give the funnel; skip_reasons 40-49 are reserved for
  1249. this surface.
  1250. """
  1251. tool_input = input_data.get("tool_input", {}) or {}
  1252. tool_response = input_data.get("tool_response", {}) or {}
  1253. command = tool_input.get("command", "") or ""
  1254. cwd = input_data.get("cwd")
  1255. session_id = input_data.get("session_id", "")
  1256. bash_output = (
  1257. (tool_response.get("stdout", "") or "")
  1258. + "\n"
  1259. + (tool_response.get("stderr", "") or "")
  1260. )
  1261. interrupted = tool_response.get("interrupted", False)
  1262. # Re-gate: hooks.json `if` matched, but confirm with the broader regex
  1263. # (defensive — `git -C`/`-c` forms won't reach here via the hooks.json
  1264. # prefix matcher alone, but a compound with a plain `git push` would).
  1265. if not _GIT_PUSH_RE.search(command):
  1266. sys.exit(0)
  1267. _base = {"push_sweep": True, "push_sweep_on": PUSH_SWEEP_ENABLED}
  1268. if not PUSH_SWEEP_ENABLED:
  1269. emit_metrics({"skipped": True, "skip_reason": 40, **_base})
  1270. sys.exit(0)
  1271. if interrupted:
  1272. emit_metrics({"skipped": True, "skip_reason": 21, **_base})
  1273. sys.exit(0)
  1274. if not ENABLE_CODE_SECURITY_REVIEW or not HAS_API_CREDENTIALS:
  1275. emit_metrics({"skipped": True, "skip_reason": 22, **_base})
  1276. sys.exit(0)
  1277. if not cwd:
  1278. emit_metrics({"skipped": True, "skip_reason": 25, **_base})
  1279. sys.exit(0)
  1280. repo_root = _git_toplevel(cwd)
  1281. if not repo_root:
  1282. emit_metrics({"skipped": True, "skip_reason": 26, **_base})
  1283. sys.exit(0)
  1284. # Guard: the sweep diffs `base..HEAD` and the agent Reads the working
  1285. # tree, so the pushed ref MUST be HEAD or the review is of the wrong
  1286. # range. `git push origin other` while checked out elsewhere, or a
  1287. # multi-ref push, are skipped (skip_reason 44). Check the new-tip from
  1288. # the `abc..def local -> remote` line against HEAD.
  1289. #
  1290. # Scope range-line detection to the push section of bash_output: a chained
  1291. # `git fetch && git push` produces fetch range lines that the regex would
  1292. # otherwise match too, false-tripping multi-ref. `_push_section` slices
  1293. # forward from the last `To <remote>` header.
  1294. #
  1295. # If there are no range lines, we MUST also see a positive push-success
  1296. # signal (`* [new branch]` or `Everything up-to-date`) AND verify the
  1297. # pushed local ref resolves to HEAD before falling through to the
  1298. # @{u}@{1}/merge-base detection. Without this, two real cases misdirect
  1299. # the sweep: `git push origin feature2` while on `feature1` (no range
  1300. # line, no HEAD check → reviews wrong branch and poisons reviewed-shas),
  1301. # and rejected pushes (no range line, no `interrupted` signal → reviews
  1302. # unpushed local commits and marks them reviewed). skip_reason=46 covers
  1303. # both.
  1304. head = None
  1305. try:
  1306. r = subprocess.run([*GIT_CMD, "rev-parse", "HEAD"], cwd=repo_root,
  1307. capture_output=True, text=True, timeout=5)
  1308. head = r.stdout.strip() if r.returncode == 0 else None
  1309. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  1310. pass
  1311. push_section = _push_section(bash_output or "")
  1312. range_matches = list(_PUSH_RANGE_RE.finditer(push_section))
  1313. if range_matches and head:
  1314. # Multi-ref push (multiple range lines) or pushed-tip ≠ HEAD → skip.
  1315. if len(range_matches) > 1:
  1316. emit_metrics({"skipped": True, "skip_reason": 44, **_base})
  1317. sys.exit(0)
  1318. new_tip = range_matches[0].group(2)
  1319. if not head.startswith(new_tip):
  1320. debug_log(f"Push sweep: pushed tip {new_tip} != HEAD {head[:12]}")
  1321. emit_metrics({"skipped": True, "skip_reason": 44, **_base})
  1322. sys.exit(0)
  1323. elif head:
  1324. # No range lines. Need a positive push-success signal — otherwise
  1325. # the push may have failed and we'd review unpushed local commits.
  1326. new_branch_matches = re.findall(
  1327. r"^\s*\*\s+\[new branch\]\s+(\S+)\s+->\s+\S+",
  1328. push_section, re.M)
  1329. up_to_date = "Everything up-to-date" in push_section
  1330. # `git push -q` suppresses all output on success. Distinguish quiet-
  1331. # success from a failed push (which has error text) by checking the
  1332. # upstream's reflog: a successful push leaves @{u}@{1} (the prior
  1333. # value) different from @{u} (now equal to HEAD). A rejected push
  1334. # would not advance @{u}, so this signal is push-specific.
  1335. quiet_success = False
  1336. if not (bash_output or "").strip() and not interrupted:
  1337. try:
  1338. r_cur = subprocess.run(
  1339. [*GIT_CMD, "rev-parse", "--verify", "-q", "@{u}"],
  1340. cwd=repo_root, capture_output=True, text=True, timeout=5)
  1341. r_prev = subprocess.run(
  1342. [*GIT_CMD, "rev-parse", "--verify", "-q", "@{u}@{1}"],
  1343. cwd=repo_root, capture_output=True, text=True, timeout=5)
  1344. cur = r_cur.stdout.strip() if r_cur.returncode == 0 else ""
  1345. prev_u = r_prev.stdout.strip() if r_prev.returncode == 0 else ""
  1346. quiet_success = bool(cur and prev_u and cur == head and prev_u != cur)
  1347. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  1348. pass
  1349. if not (new_branch_matches or up_to_date or quiet_success):
  1350. debug_log("Push sweep: no push-success signal in bash output")
  1351. emit_metrics({"skipped": True, "skip_reason": 46, **_base})
  1352. sys.exit(0)
  1353. # `* [new branch] local -> remote`: verify the pushed local ref
  1354. # resolves to HEAD. `git push origin feature2` while on feature1
  1355. # would otherwise review feature1's commits and poison its
  1356. # reviewed-shas state.
  1357. for local_ref in new_branch_matches:
  1358. try:
  1359. r = subprocess.run(
  1360. [*GIT_CMD, "rev-parse", "--verify", "-q", local_ref],
  1361. cwd=repo_root, capture_output=True, text=True, timeout=5,
  1362. )
  1363. local_sha = r.stdout.strip() if r.returncode == 0 else ""
  1364. except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
  1365. local_sha = ""
  1366. if local_sha and local_sha != head:
  1367. debug_log(f"Push sweep: new-branch {local_ref} ({local_sha[:12]}) != HEAD {head[:12]}")
  1368. emit_metrics({"skipped": True, "skip_reason": 44, **_base})
  1369. sys.exit(0)
  1370. prev_upstream = _detect_prev_upstream(repo_root, bash_output)
  1371. if not prev_upstream:
  1372. debug_log("Push sweep: could not determine prev_upstream")
  1373. emit_metrics({"skipped": True, "skip_reason": 41, **_base})
  1374. sys.exit(0)
  1375. push_range = _git_rev_list_range(repo_root, prev_upstream, "HEAD")
  1376. if not push_range:
  1377. emit_metrics({"skipped": True, "skip_reason": 42, **_base, "pushed": 0})
  1378. sys.exit(0)
  1379. if len(push_range) > MAX_PUSH_SWEEP_RANGE:
  1380. # Huge first-push of a long-lived branch — Stop hook is the backstop.
  1381. emit_metrics({"skipped": True, "skip_reason": 43, **_base,
  1382. "pushed": len(push_range)})
  1383. sys.exit(0)
  1384. reviewed = _load_reviewed_shas(repo_root)
  1385. base, tail = _compute_push_sweep_base(prev_upstream, push_range, reviewed)
  1386. prefix_advanced = len(push_range) - len(tail)
  1387. if base is None:
  1388. debug_log("Push sweep: every pushed commit already reviewed")
  1389. emit_metrics({**_base, "pushed": len(push_range), "unreviewed": 0,
  1390. "prefix_advanced": prefix_advanced})
  1391. sys.exit(0)
  1392. debug_log(f"Push sweep: range={len(push_range)} prefix_advanced="
  1393. f"{prefix_advanced} base={base[:12]} tail={len(tail)}")
  1394. diff_text = _git_diff_range(repo_root, base, "HEAD")
  1395. if diff_text is None:
  1396. # Diff failed (non-zero exit / 30s timeout / git missing). Do NOT
  1397. # mark `tail` reviewed — we did not actually review it. Marking
  1398. # them would silently advance the prefix past unreviewed commits
  1399. # forever (the whole point of push-sweep is to catch outside-CC
  1400. # commits, and a 50-commit range over large files can hit the
  1401. # 30s timeout). skip_reason=45 lets a retry / smaller subsequent
  1402. # push still cover them, mirroring how skip_reason=31 handles
  1403. # too-many-files without recording the tail.
  1404. emit_metrics({**_base, "pushed": len(push_range),
  1405. "unreviewed": len(tail), "skip_reason": 45})
  1406. sys.exit(0)
  1407. diff_files = parse_diff_into_files(diff_text)
  1408. if not diff_files:
  1409. emit_metrics({**_base, "pushed": len(push_range),
  1410. "unreviewed": len(tail), "skip_reason": 30})
  1411. # Still mark tail reviewed — there's nothing to review.
  1412. _append_reviewed_shas(repo_root, tail, vulns_found=0)
  1413. sys.exit(0)
  1414. # Same prioritize-don't-bail logic as commit-review (see comment there).
  1415. # push-sweep ranges are net diffs over many commits so they hit the cap
  1416. # more often; reviewing the riskiest MAX_PUSH_SWEEP_FILES is strictly
  1417. # better than reviewing none. We still mark `tail` reviewed afterward —
  1418. # the dropped files are by construction the low-risk ones (config, .gen,
  1419. # tests, migrations), and NOT advancing the base would make the next
  1420. # push re-hit the same overflow with an even larger range. Per-commit
  1421. # review remains the primary surface for those files. The 10×
  1422. # pathological guard stays so a 500-file vendored-dir push doesn't burn
  1423. # a counter slot.
  1424. if len(diff_files) > 10 * MAX_PUSH_SWEEP_FILES:
  1425. emit_metrics({**_base, "pushed": len(push_range),
  1426. "unreviewed": len(tail), "skip_reason": 31,
  1427. "diff_files_count": len(diff_files)})
  1428. sys.exit(0)
  1429. diff_files, _dropped = _prioritize_diff_files(diff_files, MAX_PUSH_SWEEP_FILES)
  1430. if _dropped:
  1431. _base = {**_base, "diff_files_dropped": _dropped}
  1432. _allowed, _rate_n = atomic_check_rate_limit(
  1433. session_id, "PushSweep",
  1434. MAX_COMMIT_REVIEWS_PER_HOUR, COMMIT_REVIEW_RATE_WINDOW_S)
  1435. _base = {**_base, "rate_count": _rate_n}
  1436. if not _allowed:
  1437. emit_metrics({"skipped": True, "skip_reason": 23, **_base})
  1438. sys.exit(0)
  1439. import time as _time
  1440. now = _time.time()
  1441. previous_findings = with_locked_state(
  1442. session_id,
  1443. lambda s: list(s.get("previous_findings", []))
  1444. if (now - s.get("previous_findings_ts", 0)) <= PREVIOUS_FINDINGS_TTL_SEC
  1445. else []
  1446. ) or []
  1447. review_start = _time.time()
  1448. rel_touched = [fp for fp, _ in diff_files]
  1449. if _agentic_commit_review_enabled():
  1450. concrete_guidance, vulns, agentic_metrics = _agentic_review_with_race(
  1451. repo_root, diff_files, rel_touched, previous_findings
  1452. )
  1453. if agentic_metrics.get("agentic_fallback"):
  1454. concrete_guidance, vulns = analyze_code_security(
  1455. diff_files, is_diff=True, previous_findings=previous_findings
  1456. )
  1457. else:
  1458. concrete_guidance, vulns = analyze_code_security(
  1459. diff_files, is_diff=True, previous_findings=previous_findings
  1460. )
  1461. agentic_metrics = {}
  1462. review_ms = int((_time.time() - review_start) * 1000)
  1463. # The tail is now covered by this net-diff review.
  1464. _append_reviewed_shas(repo_root, tail, vulns_found=len(vulns or []))
  1465. new_vulns, n_deduped = _dedup_against_state(
  1466. session_id, vulns or [], prompted=_finding_keys(previous_findings)
  1467. )
  1468. # Metrics — keep within the 10-key cap; agentic sub-metrics are dropped
  1469. # here in favour of the push-sweep funnel keys (telemetry can join on session_id
  1470. # to the per-commit fires for agentic detail). rewake_summary must ride
  1471. # this line (CC reads only the first {-prefixed stdout line); it's a
  1472. # no-op when new_vulns is empty since we exit 0 below.
  1473. emit_metrics({
  1474. **_base, "pushed": len(push_range), "unreviewed": len(tail),
  1475. "prefix_advanced": prefix_advanced, "vulns_found": len(new_vulns),
  1476. "files_reviewed": len(diff_files), "review_ms": review_ms,
  1477. **({"deduped": n_deduped} if n_deduped else {}),
  1478. }, rewake_summary=_format_vulns_summary(new_vulns, prefix="Push security review found"))
  1479. if not new_vulns:
  1480. debug_log("Push sweep: no new findings")
  1481. sys.exit(0)
  1482. # First-push of a big branch can surface many findings at once across
  1483. # week-old code. Report only the top-N by severity so the asyncRewake
  1484. # isn't a wall of text; the rest go to telemetry (vulns_found is the
  1485. # full count) and into previous_findings so Stop / next commit-review
  1486. # don't re-flag them. Stable sort: severity, then category for
  1487. # determinism in tests.
  1488. _sev_rank = {"critical": 0, "high": 1, "medium": 2, "low": 3}
  1489. new_vulns.sort(key=lambda v: (_sev_rank.get(v.get("severity", "medium"), 2),
  1490. v.get("category", "")))
  1491. reported = new_vulns[:PUSH_SWEEP_REPORT_CAP]
  1492. n_suppressed = len(new_vulns) - len(reported)
  1493. # Record only the REPORTED findings into shared state. previous_findings
  1494. # means "the user was told about this — don't repeat it"; suppressed
  1495. # findings were NOT told, so recording them would silently bury them
  1496. # against any future commit-review/Stop that touches the same code. The
  1497. # range is marked reviewed in `.git/sg-reviewed-shas` regardless, so the
  1498. # push-sweep itself won't re-find them; leaving them out of
  1499. # previous_findings keeps the door open for the per-commit hook to
  1500. # surface them later if the code is touched again.
  1501. snapshots = [
  1502. {"filePath": v.get("filePath", ""),
  1503. "category": v.get("category", "Unknown"),
  1504. "vulnerableCode": v.get("vulnerableCode", "")}
  1505. for v in reported
  1506. ]
  1507. def _record(state):
  1508. existing = [f for f in state.get("previous_findings", [])
  1509. if isinstance(f, dict)]
  1510. seen = {(f.get("filePath", ""), f.get("category", "")) for f in existing}
  1511. for f in snapshots:
  1512. k = (f["filePath"], f["category"])
  1513. if k not in seen:
  1514. seen.add(k); existing.append(f)
  1515. state["previous_findings"] = existing
  1516. state["previous_findings_ts"] = _time.time()
  1517. with_locked_state(session_id, _record)
  1518. # Prefer the LLM's formatted guidance (richer context, fix suggestions)
  1519. # when NOTHING was dropped from the LLM's full vuln list; fall back to
  1520. # re-formatting from `reported` whenever either the cap suppressed
  1521. # findings OR `_dedup_against_state` dropped findings the user has
  1522. # already been shown. concrete_guidance is built against the LLM's
  1523. # full pre-dedup list, so leaking it past dedup re-surfaces findings
  1524. # the per-commit hook already reported (the [✓1, ✗2, ✓3] case where
  1525. # the tail reviewed commits' findings are in previous_findings).
  1526. if n_suppressed or n_deduped:
  1527. guidance = _format_vulns_guidance(reported) or ""
  1528. else:
  1529. guidance = concrete_guidance or _format_vulns_guidance(reported) or ""
  1530. sys.stderr.write(
  1531. PROVENANCE_BANNER + "\n\n" + guidance + CONTINUATION_SUFFIX + "\n"
  1532. )
  1533. sys.exit(2)
  1534. def handle_stop_hook(input_data):
  1535. """
  1536. Handle the Stop hook — final security check using git diff.
  1537. Diffs against the baseline SHA captured at UserPromptSubmit to review
  1538. only code changed during this turn. Runs two Haiku analyses and
  1539. exits with code 2 to force Claude to continue and fix issues.
  1540. Also sweeps pending pattern warnings to emit a session-level
  1541. fixed/unresolved tally; the sweep needs no LLM and measures
  1542. pattern-rule efficacy.
  1543. """
  1544. session_id = input_data.get("session_id", "default")
  1545. stop_hook_active = input_data.get("stop_hook_active", False)
  1546. cwd = input_data.get("cwd", "")
  1547. # Recursion guard FIRST — consume_stop_state clears touched_paths, and CC
  1548. # sets stop_hook_active session-wide while any asyncRewake Stop is in
  1549. # flight, so a concurrent active=True fire winning the lock would discard
  1550. # paths the concurrent active=False fire needs.
  1551. if stop_hook_active:
  1552. debug_log("Stop hook: stop_hook_active=True, skipping to avoid recursion")
  1553. emit_metrics({"skipped": True, "skip_reason": 1, "diff_strategy_v2": True})
  1554. sys.exit(0)
  1555. # Snapshot all state under one lock BEFORE any slow work (sweep file I/O,
  1556. # git, network). asyncRewake Stop runs in the background; the next turn's
  1557. # UPS/PostToolUse can fire while we're still here. The snapshot is immune
  1558. # to those writes — they affect the NEXT Stop fire's snapshot.
  1559. snap = consume_stop_state(session_id)
  1560. fire_count = snap["fire_count"]
  1561. touched_paths = snap["touched_paths"]
  1562. baseline_sha = snap["baseline_sha"]
  1563. snap_baseline = baseline_sha # pre-reassignment value for restore-on-transient-skip
  1564. head_at_capture = snap["head_at_capture"]
  1565. untracked_at_baseline = snap.get("untracked_at_baseline") or {}
  1566. previous_findings = snap["previous_findings"]
  1567. # Sweep pattern-warning outcomes (pure local work; stop_hook_active is
  1568. # already guaranteed False here so no double-count guard needed).
  1569. sweep = {}
  1570. warn_fixed, warn_unresolved, warn_unresolved_mask = sweep_pending_warnings(session_id)
  1571. if warn_fixed or warn_unresolved:
  1572. sweep = {
  1573. "warn_fixed": warn_fixed,
  1574. "warn_unresolved": warn_unresolved,
  1575. "warn_unresolved_mask": warn_unresolved_mask,
  1576. }
  1577. v2_metrics = {}
  1578. def _skip(reason, restore=False, **extra):
  1579. if restore:
  1580. restore_unreviewed_stop_state(session_id, touched_paths, snap_baseline)
  1581. # CC truncates metrics to 10 keys by
  1582. # insertion order. v2_metrics (3) must precede sweep (3) so the v2
  1583. # diagnostics survive when extra adds touched_paths_count + ip_* keys.
  1584. emit_metrics({
  1585. "skipped": True, "skip_reason": reason, "fire_index": fire_count + 1,
  1586. "diff_strategy_v2": True,
  1587. **v2_metrics, **extra, **sweep,
  1588. })
  1589. sys.exit(0)
  1590. # Limit stop hook firings per asyncRewake loop to prevent infinite loops.
  1591. # fire_count auto-expires after STOP_LOOP_STATE_TTL_SEC so a stale count
  1592. # from a prior turn doesn't block this one.
  1593. if MAX_STOP_HOOK_FIRINGS > 0 and fire_count >= MAX_STOP_HOOK_FIRINGS:
  1594. debug_log(f"Stop hook: already fired {fire_count} times (max {MAX_STOP_HOOK_FIRINGS}), skipping")
  1595. _skip(2)
  1596. if not ENABLE_CODE_SECURITY_REVIEW or not HAS_API_CREDENTIALS:
  1597. debug_log("Stop hook: LLM review disabled or no API credentials")
  1598. _skip(3)
  1599. # Stop-hook-only kill switch — placed after consume_stop_state so
  1600. # touched_paths is still cleared each turn (a disabled Stop hook that
  1601. # never consumed state would accumulate stale paths) and after the sweep
  1602. # so pattern-warning efficacy metrics still emit. The commit/push reviews
  1603. # have their own gates (ENABLE_COMMIT_REVIEW / ENABLE_CODE_SECURITY_REVIEW).
  1604. if not ENABLE_STOP_REVIEW:
  1605. debug_log("Stop hook: ENABLE_STOP_REVIEW=0")
  1606. # 50+ for opt-out skips that aren't push-sweep (which owns 40-49).
  1607. _skip(50)
  1608. if not ensure_anthropic_reachable():
  1609. debug_log("Stop hook: api.anthropic.com unreachable")
  1610. _skip(10, restore=True)
  1611. if not cwd:
  1612. debug_log("Stop hook: no cwd")
  1613. _skip(4)
  1614. review_paths, diff_base, repo_root, untracked, v2_metrics = compute_v2_review_set(
  1615. cwd, baseline_sha, head_at_capture, untracked_at_baseline
  1616. )
  1617. if not review_paths:
  1618. debug_log("Stop hook: empty review set")
  1619. _skip(9, touched_paths_count=len(touched_paths))
  1620. 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']}")
  1621. # Run from repo_root so the toplevel-relative review_paths resolve.
  1622. # Diff CONTENT against the turn-start stash (baseline_sha) so the LLM
  1623. # sees only this-turn edits — diffing against HEAD includes the user's
  1624. # pre-turn uncommitted WIP, which inflates review_ms and can re-flag
  1625. # the same pre-existing pattern every turn. The file LIST still comes
  1626. # from git state (compute_v2_review_set), so Bash/subagent edits are
  1627. # caught either way. Fall back to diff_base (HEAD/head_at_capture)
  1628. # when the stash is missing or pruned.
  1629. content_base = baseline_sha or diff_base
  1630. diff_output = get_git_diff(repo_root, content_base, full_context=False,
  1631. paths=review_paths, untracked_paths=untracked)
  1632. if diff_output is None and content_base != diff_base:
  1633. debug_log(f"Stop hook: diff against {content_base[:12]} failed — falling back to {diff_base}")
  1634. diff_output = get_git_diff(repo_root, diff_base, full_context=False,
  1635. paths=review_paths, untracked_paths=untracked)
  1636. # filter_preexisting_from_diff needs a resolvable pre-turn ref; fall
  1637. # back to HEAD when UPS never captured a baseline (print mode).
  1638. if not baseline_sha:
  1639. baseline_sha = "HEAD"
  1640. if not diff_output or not diff_output.strip():
  1641. debug_log("Stop hook: no changes since baseline")
  1642. _skip(6)
  1643. # Parse diff into per-file content
  1644. diff_files = parse_diff_into_files(diff_output)
  1645. if not diff_files:
  1646. debug_log("Stop hook: no source code files in diff")
  1647. _skip(7)
  1648. # Mirror commit-review: hard-bail only on pathological diffs (>300 files,
  1649. # usually a bad baseline), otherwise prioritize by security-risk path
  1650. # tokens and review the top MAX_DIFF_FILES. Stop is the only surface for
  1651. # uncommitted edits; the old hard-skip at >30 files dropped the 31-300
  1652. # bucket entirely, which is where cross-file source→sink vulns hide.
  1653. # _cap_files_for_prompt already bounds bytes downstream.
  1654. _stop_dropped = 0
  1655. if len(diff_files) > 10 * MAX_DIFF_FILES:
  1656. debug_log(f"Stop hook: pathological diff ({len(diff_files)} files > "
  1657. f"{10 * MAX_DIFF_FILES}), skipping")
  1658. _skip(8, diff_files_count=len(diff_files))
  1659. if len(diff_files) > MAX_DIFF_FILES:
  1660. diff_files, _stop_dropped = _prioritize_diff_files(
  1661. diff_files, MAX_DIFF_FILES)
  1662. debug_log(f"Stop hook: prioritized to {len(diff_files)} files "
  1663. f"(dropped {_stop_dropped} lower-risk)")
  1664. # Filter out pre-existing content from file rewrites
  1665. diff_files = filter_preexisting_from_diff(diff_files, cwd, baseline_sha)
  1666. debug_log(f"Stop hook: reviewing {len(diff_files)} changed files (standard diff)")
  1667. import time as _time
  1668. stop_review_start = _time.time()
  1669. # Stop hook is single-shot only. Agentic review is wired into
  1670. # handle_commit_review_posttooluse (PostToolUse on `git commit`) — commits
  1671. # are slower-OK and benefit from the deeper context-reading loop.
  1672. concrete_guidance, vulns = analyze_code_security(
  1673. diff_files, is_diff=True, previous_findings=previous_findings
  1674. )
  1675. # NOTE: analyze_security_concerns disabled — it produces too many false positives
  1676. # on pre-existing patterns in starter code. The concrete vulnerability analysis
  1677. # is more precise and has severity filtering (high/critical only).
  1678. stop_review_elapsed = _time.time() - stop_review_start
  1679. debug_log(f"Stop hook: LLM reviews took {stop_review_elapsed:.1f}s total")
  1680. review_ms = int(stop_review_elapsed * 1000)
  1681. fire_index = fire_count + 1
  1682. # Late dedup: drop only what a concurrent commit-review wrote while our
  1683. # LLM ran. Anything already in `previous_findings` (the consume_stop_state
  1684. # snapshot) that the LLM re-flagged is an intentional "fix incomplete"
  1685. # verdict and passes through.
  1686. if vulns:
  1687. vulns, n_deduped = _dedup_against_state(
  1688. session_id, vulns, prompted=_finding_keys(previous_findings)
  1689. )
  1690. if n_deduped and not vulns:
  1691. debug_log("Stop hook: all findings already delivered by commit-review")
  1692. _skip(35, deduped=n_deduped, review_ms=review_ms)
  1693. concrete_guidance = _format_vulns_guidance(vulns)
  1694. if concrete_guidance:
  1695. finding_snapshots = [
  1696. {
  1697. "filePath": v.get("filePath", ""),
  1698. "category": v.get("category", "Unknown"),
  1699. "vulnerableCode": v.get("vulnerableCode", ""),
  1700. }
  1701. for v in vulns
  1702. ]
  1703. # Update baseline so next stop hook iteration only sees new changes
  1704. new_sha = capture_git_baseline(cwd)
  1705. new_untracked_baseline = _list_untracked(cwd) if new_sha else None
  1706. def _record_fire(state):
  1707. state["stop_hook_fire_count"] = fire_index
  1708. state["stop_hook_fire_count_ts"] = _time.time()
  1709. # Re-read under lock — the commit-review PostToolUse hook may have
  1710. # appended findings since consume_stop_state snapshotted.
  1711. # Dedupe on (filePath, category) — vulnerableCode includes diff
  1712. # context lines that drift between fires, so byte-identical
  1713. # matching let the same finding accumulate as "new" each fire.
  1714. existing = [f for f in state.get("previous_findings", []) if isinstance(f, dict)]
  1715. seen = {(f.get("filePath", ""), f.get("category", "")) for f in existing}
  1716. for f in finding_snapshots:
  1717. key = (f["filePath"], f["category"])
  1718. if key not in seen:
  1719. seen.add(key)
  1720. existing.append(f)
  1721. state["previous_findings"] = existing
  1722. state["previous_findings_ts"] = _time.time()
  1723. if new_sha:
  1724. state["baseline_sha"] = new_sha
  1725. state["untracked_at_baseline"] = new_untracked_baseline
  1726. with_locked_state(session_id, _record_fire)
  1727. if new_sha:
  1728. debug_log(f"Updated git baseline after stop hook: {new_sha[:12]}")
  1729. sev = {"critical": 0, "high": 0, "medium": 0}
  1730. for v in vulns:
  1731. s = v.get("severity", "medium")
  1732. if s in sev:
  1733. sev[s] += 1
  1734. # 8 base keys + at most 2 sweep keys = 10 (cap). Drop the mask here.
  1735. # untracked_baseline_n is the signal for whether the UPS-time
  1736. # untracked-snapshot capture actually ran.
  1737. sweep_trimmed = {k: v for k, v in sweep.items() if k != "warn_unresolved_mask"}
  1738. emit_metrics({
  1739. "vulns_found": len(vulns),
  1740. "untracked_baseline_n": len(untracked_at_baseline),
  1741. "diff_strategy_v2": True,
  1742. "critical_count": sev["critical"],
  1743. "high_count": sev["high"],
  1744. "files_reviewed": len(diff_files),
  1745. "touched_paths_count": len(touched_paths),
  1746. "review_ms": review_ms,
  1747. "fire_index": fire_index,
  1748. **({"diff_truncated": llm._last_review_truncated_bytes}
  1749. if llm._last_review_truncated_bytes else {}),
  1750. **sweep_trimmed,
  1751. }, rewake_summary=_format_vulns_summary(vulns))
  1752. # Exit code 2 with stderr forces Claude to continue and fix
  1753. sys.stderr.write(PROVENANCE_BANNER + "\n\n" + concrete_guidance + CONTINUATION_SUFFIX + "\n")
  1754. sys.exit(2)
  1755. if llm._last_call_claude_http_error is not None:
  1756. debug_log(f"Stop hook: API call failed with status {llm._last_call_claude_http_error}")
  1757. restore_unreviewed_stop_state(session_id, touched_paths, snap_baseline)
  1758. else:
  1759. debug_log("Stop hook: no security issues found")
  1760. # CC truncates metrics to 10 keys by
  1761. # insertion order. The previous **sweep,**v2_metrics tail meant the 3
  1762. # v2_metrics keys were always sliced off this most-common path, so the
  1763. # diff-strategy diagnostics never reached telemetry. Drop sweep here (it's
  1764. # PostToolUse-warning state, orthogonal to diff-strategy comparison).
  1765. # 6 base + optional api_error + 3 v2_metrics = ≤10.
  1766. emit_metrics({
  1767. "vulns_found": 0,
  1768. "diff_strategy_v2": True,
  1769. "files_reviewed": len(diff_files),
  1770. "touched_paths_count": len(touched_paths),
  1771. "review_ms": review_ms,
  1772. "fire_index": fire_index,
  1773. **({"api_error": llm._last_call_claude_http_error} if llm._last_call_claude_http_error is not None else {}),
  1774. **({"diff_truncated": llm._last_review_truncated_bytes}
  1775. if llm._last_review_truncated_bytes else {}),
  1776. **v2_metrics,
  1777. })
  1778. sys.exit(0)
  1779. _SDK_BOOTSTRAP_THROTTLE = os.path.join(
  1780. os.environ.get("SECURITY_WARNINGS_STATE_DIR")
  1781. or os.path.expanduser("~/.claude/security"),
  1782. ".sdk_bootstrap_spawned")
  1783. def _maybe_bootstrap_agent_sdk_async():
  1784. """Fire-and-forget SDK bootstrap, for remote-pod environments.
  1785. Under CLAUDE_CODE_SYNC_PLUGIN_INSTALL=true (CCR-style remote pods),
  1786. plugins are synced *after* SessionStart fires, so the SessionStart
  1787. `ensure_agent_sdk.py` hook never runs and the agentic commit reviewer
  1788. falls back 100% of the time. A PostToolUse hook firing is itself proof
  1789. the plugin is now registered, so re-trigger the bootstrap here.
  1790. Detached, so the ~17s venv build never blocks the hook — the first
  1791. 1-2 commits of a remote session still fall back while it builds, then
  1792. every subsequent commit gets the agentic path. ensure_agent_sdk.py
  1793. is idempotent and O_EXCL-locked, so concurrent/repeat spawns are safe;
  1794. the throttle file only avoids spawning dozens of subprocesses during
  1795. the build window. No-ops in ~10ms on local installs (SDK already
  1796. importable).
  1797. """
  1798. try:
  1799. import importlib.util
  1800. if importlib.util.find_spec("claude_agent_sdk") is not None:
  1801. return
  1802. import time as _t
  1803. try:
  1804. if _t.time() - os.path.getmtime(_SDK_BOOTSTRAP_THROTTLE) < 300:
  1805. return
  1806. except OSError:
  1807. pass
  1808. os.makedirs(os.path.dirname(_SDK_BOOTSTRAP_THROTTLE), exist_ok=True)
  1809. # Touch the throttle BEFORE spawning so a burst of PostToolUse
  1810. # fires in the same second don't each spawn a subprocess.
  1811. open(_SDK_BOOTSTRAP_THROTTLE, "w").close()
  1812. script = os.path.join(
  1813. os.path.dirname(os.path.abspath(__file__)), "ensure_agent_sdk.py")
  1814. subprocess.Popen(
  1815. [sys.executable, script],
  1816. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
  1817. stdin=subprocess.DEVNULL, start_new_session=True,
  1818. )
  1819. except Exception:
  1820. pass # best-effort; never break the hook over a bootstrap attempt
  1821. def main():
  1822. """Main hook function."""
  1823. debug_log(f"Hook called with args: {sys.argv}")
  1824. # Master kill switch — honors ENABLE_SECURITY_REMINDER=0 (legacy) and
  1825. # SECURITY_GUIDANCE_DISABLE=1 (clearer name, no double negative). Emit
  1826. # empty metrics so asyncRewake hooks (Stop) don't hang waiting for stdout
  1827. # output that never comes.
  1828. if SECURITY_GUIDANCE_DISABLED:
  1829. emit_metrics({"skipped": True, "skip_reason": -1})
  1830. sys.exit(0)
  1831. # Periodically clean up old state files (10% chance per run)
  1832. if random.random() < 0.1:
  1833. cleanup_old_state_files()
  1834. # Read input from stdin
  1835. try:
  1836. raw_input = sys.stdin.read()
  1837. input_data = json.loads(raw_input)
  1838. except json.JSONDecodeError as e:
  1839. debug_log(f"JSON decode error: {e}")
  1840. emit_metrics({"skipped": True, "skip_reason": -2})
  1841. sys.exit(0)
  1842. session_id = input_data.get("session_id", "default")
  1843. tool_name = input_data.get("tool_name", "")
  1844. tool_input = input_data.get("tool_input", {})
  1845. hook_event_name = input_data.get("hook_event_name", "")
  1846. debug_log(f"Processing: hook_event={hook_event_name}, tool={tool_name}")
  1847. # Load project-specific security guidance and custom patterns once
  1848. # per invocation. Failures are non-fatal (debug-logged) so a malformed
  1849. # config never prevents the built-in checks from running.
  1850. extensibility.load_for_session(input_data.get("cwd"))
  1851. # Remote-pod SDK-bootstrap rescue: PostToolUse is the earliest hook event
  1852. # that is guaranteed to fire *after* async plugin sync (its firing proves
  1853. # the plugin is registered), so it's where we recover the SessionStart
  1854. # bootstrap that remote pods miss under CLAUDE_CODE_SYNC_PLUGIN_INSTALL.
  1855. # Fires on Edit/Write too (not just Bash), so the venv is usually built
  1856. # before the first `git commit`.
  1857. if hook_event_name == "PostToolUse":
  1858. _maybe_bootstrap_agent_sdk_async()
  1859. # Handle UserPromptSubmit — capture git baseline
  1860. if hook_event_name == "UserPromptSubmit":
  1861. handle_user_prompt_submit(input_data)
  1862. return
  1863. # Handle Stop hook — final security check
  1864. if hook_event_name == "Stop":
  1865. handle_stop_hook(input_data)
  1866. return
  1867. # Handle PostToolUse[Bash] — commit review or push sweep (asyncRewake).
  1868. #
  1869. # hooks.json has two `if` configs under the Bash matcher (`git commit:*`
  1870. # and `git push:*`). CC evaluates each `if` independently and spawns this
  1871. # script ONCE PER MATCH — so `git commit -m x && git push` spawns python
  1872. # twice with the same command string and the same tool_use_id. The python
  1873. # cannot tell which `if` fired it.
  1874. #
  1875. # Routing therefore MUST check commit FIRST so that compound commit+push
  1876. # commands continue to hit commit-review (the pre-existing behaviour) on
  1877. # the commit-matcher invocation. The push-matcher invocation of the SAME
  1878. # compound command is deduped by `_claim_bash_hook_once` below: the second
  1879. # spawn loses the tool_use_id sentinel race and exits early with
  1880. # `bash_hook_dedup`, so commit-review runs exactly once. The alternative —
  1881. # checking push first — would silently DROP commit-review
  1882. # on `git commit && git push`, which is a regression.
  1883. #
  1884. # The push-sweep does NOT run on the compound call. That's acceptable: the
  1885. # just-made commit is recorded by commit-review, so the next standalone
  1886. # push sees it as reviewed and the sweep base advances past it. Older
  1887. # unreviewed commits in the range are caught on that next push.
  1888. if tool_name == "Bash" and hook_event_name == "PostToolUse":
  1889. cmd = (input_data.get("tool_input") or {}).get("command", "") or ""
  1890. if not (_GIT_COMMIT_RE.search(cmd) or _GIT_PUSH_RE.search(cmd)):
  1891. return
  1892. if not _claim_bash_hook_once(input_data):
  1893. # Another spawn for this same tool_use_id already claimed the
  1894. # work (compound matched multiple `if` configs). Emit a single
  1895. # metric so telemetry can count how often the de-dupe kicks in.
  1896. print(json.dumps({"metrics": {"bash_hook_dedup": True}}), flush=True)
  1897. sys.exit(0)
  1898. if _GIT_COMMIT_RE.search(cmd):
  1899. handle_commit_review_posttooluse(input_data)
  1900. elif _GIT_PUSH_RE.search(cmd):
  1901. handle_push_sweep_posttooluse(input_data)
  1902. return
  1903. # Handle PostToolUse — pattern-based checks only (no LLM review per-edit)
  1904. if tool_name in ["Edit", "Write", "MultiEdit", "NotebookEdit"]:
  1905. file_path = tool_input.get("file_path") or tool_input.get("notebook_path") or ""
  1906. if not file_path:
  1907. sys.exit(0)
  1908. # Skip plan files
  1909. plans_dir = os.path.expanduser("~/.claude/plans")
  1910. if file_path.startswith(plans_dir):
  1911. sys.exit(0)
  1912. record_touched_path(session_id, file_path)
  1913. content = extract_content_from_input(tool_name, tool_input)
  1914. all_guidance = []
  1915. raw_pattern_matches = []
  1916. if ENABLE_PATTERN_RULES:
  1917. pattern_matches = check_patterns(file_path, content)
  1918. raw_pattern_matches = pattern_matches
  1919. if pattern_matches:
  1920. debug_log(f"Pattern matches for {file_path}: {[r for r, _ in pattern_matches]}")
  1921. # For Write tool, filter out patterns that existed in the baseline version
  1922. # This prevents flagging pre-existing insecure patterns when Claude rewrites a file
  1923. if tool_name == "Write" and pattern_matches:
  1924. cwd = os.environ.get("CLAUDE_PROJECT_DIR", os.getcwd())
  1925. baseline_content = get_baseline_file_content(session_id, file_path, cwd)
  1926. if baseline_content is not None:
  1927. baseline_matches = set(r for r, _ in check_patterns(file_path, baseline_content))
  1928. pattern_matches = [(r, msg) for r, msg in pattern_matches if r not in baseline_matches]
  1929. if pattern_matches:
  1930. debug_log(f"New patterns (not in baseline): {[r for r, _ in pattern_matches]}")
  1931. else:
  1932. debug_log("All patterns existed in baseline, skipping")
  1933. for rule_name, reminder in pattern_matches:
  1934. warning_key = f"{file_path}-{rule_name}"
  1935. if atomic_check_and_mark_warning(session_id, warning_key):
  1936. all_guidance.append(reminder)
  1937. # Record matched rules as pending so the Stop-hook sweep can
  1938. # later tally fixed vs unresolved. Only runs when patterns match.
  1939. if pattern_matches:
  1940. record_pending_warnings(session_id, file_path,
  1941. [r for r, _ in pattern_matches])
  1942. # Emit metrics when raw patterns matched (even if all were baseline-suppressed
  1943. # or dedup'd — pattern_hits reflects warnings actually shown, may be 0).
  1944. # Gate on raw matches so clean edits don't flood the metrics event.
  1945. # rule_id: RuleId of the first raw match (values stay small/enumerable in telemetry)
  1946. # rule_mask: bitmask of ALL raw matches — POPCOUNT gives raw hit count,
  1947. # (mask >> N) & 1 tests for a specific rule
  1948. if raw_pattern_matches:
  1949. raw_names = [r for r, _ in raw_pattern_matches]
  1950. output = {"metrics": {
  1951. "pattern_hits": len(all_guidance),
  1952. # User-defined patterns (rule_name="user:*") have no static
  1953. # RuleId; emit -1 so the metrics pipeline can distinguish.
  1954. "rule_id": int(_RULE_NAME_TO_ID.get(raw_names[0], -1)),
  1955. "rule_mask": rule_names_to_mask(raw_names),
  1956. **({"pv": _PV} if _PV else {}),
  1957. }}
  1958. if all_guidance:
  1959. output["hookSpecificOutput"] = {
  1960. "hookEventName": "PostToolUse",
  1961. "additionalContext": PROVENANCE_TAG + "\n\n" + "\n\n".join(all_guidance),
  1962. }
  1963. print(json.dumps(output))
  1964. elif all_guidance:
  1965. # Defensive: pattern rules disabled but guidance somehow set (shouldn't happen)
  1966. print(json.dumps({
  1967. "hookSpecificOutput": {
  1968. "hookEventName": "PostToolUse",
  1969. "additionalContext": PROVENANCE_TAG + "\n\n" + "\n\n".join(all_guidance),
  1970. }
  1971. }))
  1972. sys.exit(0)
  1973. if __name__ == "__main__":
  1974. main()