security_reminder_hook.py 106 KB

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