summaryrefslogtreecommitdiff
path: root/plugins/claude-security/hooks
diff options
context:
space:
mode:
authorTobin South <tobin.south@gmail.com>2026-09-01 11:36:49 -0700
committerGitHub <noreply@github.com>2026-09-01 11:36:49 -0700
commite18ff5086423bd9c21902ce3100dc4ecc84a668a (patch)
tree7ed935773a561a74448b1fa42d6bcb135be7fb11 /plugins/claude-security/hooks
parent4b909c3492b350cdc42160ffe4b135e6f5c1db12 (diff)
parentf85f7cfcbbe9c949ba63304add8be7daf5c13e78 (diff)
Merge pull request #5728 from anthropics/claude-security-v0.11.0
Diffstat (limited to 'plugins/claude-security/hooks')
-rwxr-xr-xplugins/claude-security/hooks/banner_hook.sh7
-rwxr-xr-xplugins/claude-security/hooks/banner_notice.py99
-rw-r--r--plugins/claude-security/hooks/hooks.json32
-rwxr-xr-xplugins/claude-security/hooks/hooks.py257
-rwxr-xr-xplugins/claude-security/hooks/hooks.sh10
5 files changed, 297 insertions, 108 deletions
diff --git a/plugins/claude-security/hooks/banner_hook.sh b/plugins/claude-security/hooks/banner_hook.sh
deleted file mode 100755
index da82a120..00000000
--- a/plugins/claude-security/hooks/banner_hook.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/sh
-if python3 -c 'import sys' >/dev/null 2>&1; then
- python3 "$(dirname -- "$0")/banner_notice.py"
-else
- printf '%s\n' '{"systemMessage":"\n⚠️ Claude Security needs a working python3 (3.9 or newer) on PATH and could not run one. Install Python 3, then start a new session.\n"}'
-fi
-exit 0
diff --git a/plugins/claude-security/hooks/banner_notice.py b/plugins/claude-security/hooks/banner_notice.py
deleted file mode 100755
index 030b626f..00000000
--- a/plugins/claude-security/hooks/banner_notice.py
+++ /dev/null
@@ -1,99 +0,0 @@
-#!/usr/bin/env python3
-"""Show the Claude Security banner as a display-only systemMessage.
-
-Always exits 0 with either the banner or no output.
-"""
-
-import contextlib
-import json
-import os
-import sys
-from typing import cast
-
-PLUGIN_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
-LAUNCH_NOTICE = "Launching Claude Security..."
-
-BOX_INNER = 53
-
-MIN_PYTHON = (3, 9)
-
-
-def plugin_version() -> str:
- """The plugin's version from plugin.json, or "unknown". Never raises."""
- try:
- path = os.path.join(PLUGIN_ROOT, ".claude-plugin", "plugin.json")
- with open(path, encoding="utf-8") as handle:
- loaded = cast("object", json.load(handle))
- except Exception:
- return "unknown"
- if not isinstance(loaded, dict):
- return "unknown"
- version = cast("dict[str, object]", loaded).get("version")
- return version if isinstance(version, str) and version else "unknown"
-
-
-def box_line(text: str) -> str:
- """One boxed body line, centered so the right border always aligns."""
- if len(text) > BOX_INNER:
- text = text[:BOX_INNER]
- return " │" + text.center(BOX_INNER) + "│"
-
-
-def bottom_border(version: str) -> str:
- """The box's bottom edge with the version set into it, right-aligned."""
- tag = f" v{version} "
- fill = BOX_INNER - len(tag) - 3
- if fill < 1:
- return " └" + "─" * BOX_INNER + "┘"
- return " └" + "─" * fill + tag + "─" * 3 + "┘"
-
-
-def banner() -> str:
- lines = [
- "",
- " ██████╗██╗ █████╗ ██╗ ██╗██████╗ ███████╗",
- " ██╔════╝██║ ██╔══██╗██║ ██║██╔══██╗██╔════╝",
- " ██║ ██║ ███████║██║ ██║██║ ██║█████╗",
- " ██║ ██║ ██╔══██║██║ ██║██║ ██║██╔══╝",
- " ╚██████╗███████╗██║ ██║╚██████╔╝██████╔╝███████╗",
- " ╚═════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝",
- " ──────── S · E · C · U · R · I · T · Y ────────",
- " ┌" + "─" * BOX_INNER + "┐",
- box_line("Find and fix vulnerabilities in source code"),
- bottom_border(plugin_version()),
- "",
- ]
- return "\n".join(lines)
-
-
-def emit(message: str) -> None:
- """Write one systemMessage. Never raises; a failed write is just no banner."""
- try:
- sys.stdout.write(json.dumps({"systemMessage": message}))
- sys.stdout.flush()
- except Exception:
- # Also silence the interpreter's exit-time flush of the buffered message.
- with contextlib.suppress(Exception):
- os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())
-
-
-def main() -> int:
- if sys.version_info < MIN_PYTHON:
- need = f"{MIN_PYTHON[0]}.{MIN_PYTHON[1]}"
- have = ".".join(str(part) for part in sys.version_info[:3])
- emit(
- f"\n\u26a0\ufe0f Claude Security needs python3 {need} or newer, but this "
- f"python3 is {have}. Scanning and fixing will fail until a newer "
- "python3 is first on PATH.\n"
- )
- return 0
- try:
- message = "\n" + LAUNCH_NOTICE + "\n\n" + banner()
- except Exception:
- message = "\n" + LAUNCH_NOTICE + "\n"
- emit(message)
- return 0
-
-
-if __name__ == "__main__":
- sys.exit(main())
diff --git a/plugins/claude-security/hooks/hooks.json b/plugins/claude-security/hooks/hooks.json
index 9608c949..41931264 100644
--- a/plugins/claude-security/hooks/hooks.json
+++ b/plugins/claude-security/hooks/hooks.json
@@ -1,5 +1,5 @@
{
- "description": "A display-only banner: on the /claude-security menu it prints the Claude Security banner as a systemMessage. It fires only on UserPromptExpansion for that slash command. It is a sensor: it emits a message and never returns a permission decision.",
+ "description": "Shows the Claude Security banner when the /claude-security menu opens and reports usage counts after the plugin's helper scripts run.",
"hooks": {
"UserPromptExpansion": [
{
@@ -7,7 +7,35 @@
"hooks": [
{
"type": "command",
- "command": "sh \"${CLAUDE_PLUGIN_ROOT}/hooks/banner_hook.sh\""
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/hooks/hooks.sh\" banner"
+ }
+ ]
+ }
+ ],
+ "PostToolUse": [
+ {
+ "matcher": "Bash",
+ "hooks": [
+ {
+ "type": "command",
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/hooks/hooks.sh\" metrics",
+ "if": "Bash(python3 *claude-security*scripts/*.py *)",
+ "asyncRewake": true,
+ "timeout": 10
+ }
+ ]
+ }
+ ],
+ "PostToolUseFailure": [
+ {
+ "matcher": "Bash",
+ "hooks": [
+ {
+ "type": "command",
+ "command": "sh \"${CLAUDE_PLUGIN_ROOT}/hooks/hooks.sh\" metrics",
+ "if": "Bash(python3 *claude-security*scripts/*.py *)",
+ "asyncRewake": true,
+ "timeout": 10
}
]
}
diff --git a/plugins/claude-security/hooks/hooks.py b/plugins/claude-security/hooks/hooks.py
new file mode 100755
index 00000000..693c4602
--- /dev/null
+++ b/plugins/claude-security/hooks/hooks.py
@@ -0,0 +1,257 @@
+#!/usr/bin/env python3
+"""The Claude Security plugin's hooks.
+
+A usage error exits 2. Python 3.9-compatible, stdlib only.
+"""
+
+from __future__ import annotations
+
+import itertools
+import json
+import os
+import re
+import shlex
+import sys
+from pathlib import Path
+from typing import cast
+
+PLUGIN_ROOT = Path(os.path.abspath(__file__)).parents[1]
+SCRIPTS = PLUGIN_ROOT / "scripts"
+
+# Telemetry codes are append-only: a reader keys on them, so none is ever renumbered.
+EVENTS = {"scan_started": 1, "scan_finished": 2, "patches_written": 3, "step_failed": 4}
+STEPS = {
+ "write_scan_meta.py": 1,
+ "save_result.py": 2,
+ "render_report.py": 3,
+ "patch_artifacts.py": 4,
+}
+MODES = {"scan": 1, "changes": 2, "commit": 3}
+EFFORTS = {"low": 1, "medium": 2, "high": 3, "max": 4}
+REASONS = {
+ "no-vote-record": 1,
+ "no-candidate-count": 2,
+ "nothing-examined": 3,
+ "finding-panel-incomplete": 4,
+ "finding-below-quorum": 5,
+ "candidates-not-paneled": 6,
+ "no-panel-completed": 7,
+ "candidate-panel-incomplete": 8,
+ "continuation-incomplete": 9,
+ "findings-refused": 10,
+}
+UNKNOWN_REASON = 99
+COLLAPSED = ("small-diff", "small-scope")
+STAMP_PREFIX = "CLAUDE-SECURITY-REVISION-"
+OPERATORS = frozenset("();<>|&")
+
+
+def obj(value: object) -> dict[str, object]:
+ """value when it is a JSON object, else an empty one."""
+ return cast("dict[str, object]", value) if isinstance(value, dict) else {}
+
+
+def parse(text: str | bytes) -> dict[str, object]:
+ """The JSON object in text; an empty dict when text holds anything else."""
+ try:
+ return obj(cast("object", json.loads(text)))
+ except (ValueError, RecursionError):
+ return {}
+
+
+def count(value: object) -> int:
+ """value when it is a non-negative int (a bool is not one), else 0."""
+ return value if type(value) is int and value >= 0 else 0
+
+
+def code(table: dict[str, int], value: object) -> int:
+ """The table's code for a word; 0 for anything it does not name."""
+ return table.get(value, 0) if isinstance(value, str) else 0
+
+
+def read(path: Path) -> bytes | None:
+ """The file's bytes; None when it cannot be read."""
+ try:
+ return path.read_bytes()
+ except (OSError, ValueError):
+ return None
+
+
+def manifest_version() -> str:
+ """The version in the plugin's manifest; "" when there is not one."""
+ manifest = parse(read(PLUGIN_ROOT / ".claude-plugin" / "plugin.json") or b"")
+ version = manifest.get("version")
+ return version if isinstance(version, str) else ""
+
+
+def banner() -> None:
+ """Print the menu banner as a systemMessage."""
+ width = 53
+ version = f" v{manifest_version() or 'unknown'} "
+ box = [
+ " ██████╗██╗ █████╗ ██╗ ██╗██████╗ ███████╗",
+ " ██╔════╝██║ ██╔══██╗██║ ██║██╔══██╗██╔════╝",
+ " ██║ ██║ ███████║██║ ██║██║ ██║█████╗",
+ " ██║ ██║ ██╔══██║██║ ██║██║ ██║██╔══╝",
+ " ╚██████╗███████╗██║ ██║╚██████╔╝██████╔╝███████╗",
+ " ╚═════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝",
+ " ──────── S · E · C · U · R · I · T · Y ────────",
+ " ┌" + "─" * width + "┐",
+ " │" + "Find and fix vulnerabilities in source code".center(width) + "│",
+ " └" + version.rjust(width - 3, "─") + "───┘",
+ ]
+ message = "\nLaunching Claude Security...\n\n\n" + "\n".join(box) + "\n"
+ sys.stdout.write(json.dumps({"systemMessage": message}))
+
+
+def helper_words(command: str) -> list[str] | None:
+ """The words of a command that runs one of the plugin's helper scripts on its own; else None."""
+ if any(mark in command for mark in ("\n", "\0", "`", "$(")):
+ return None
+ lexer = shlex.shlex(command, posix=True, punctuation_chars=True)
+ lexer.whitespace_split = True
+ # A "#" begins a comment only at the start of a word, as in sh; shlex would break a word on one.
+ lexer.commenters = ""
+ try:
+ lexed = list(lexer)
+ except ValueError:
+ return None
+ if any(word and set(word) <= OPERATORS for word in lexed):
+ return None
+ words = list(itertools.takewhile(lambda word: not word.startswith("#"), lexed))
+ if len(words) < 2 or words[0] != "python3":
+ return None
+ name = os.path.basename(words[1])
+ own = os.path.realpath(SCRIPTS / name)
+ return words if name in STEPS and os.path.realpath(words[1]) == own else None
+
+
+def arguments(args: list[str]) -> tuple[list[str], dict[str, str | None]]:
+ """A helper's positional arguments and its --options, each of which takes a value."""
+ positionals: list[str] = []
+ options: dict[str, str | None] = {}
+ rest = iter(args)
+ for arg in rest:
+ if arg.startswith("--"):
+ name, equals, value = arg.partition("=")
+ options[name] = value if equals else next(rest, None)
+ else:
+ positionals.append(arg)
+ return positionals, options
+
+
+def scan_started(scan_root: str, options: dict[str, str | None]) -> dict[str, int | bool] | None:
+ """The event for a write_scan_meta.py run; None unless it names a mode and an effort."""
+ mode, effort = code(MODES, options.get("--mode")), code(EFFORTS, options.get("--effort"))
+ root = os.path.normpath(scan_root)
+ scope = (options.get("--scope") or "").split(",")
+ scoped = any(os.path.normpath(os.path.join(root, entry.strip())) != root for entry in scope)
+ return {"mode": mode, "effort": effort, "scoped": scoped} if mode and effort else None
+
+
+def scan_finished(products: Path) -> dict[str, int | bool] | None:
+ """The event for a render_report.py run, from the one revision stamp it wrote; else None."""
+ try:
+ (path,) = (
+ p for p in products.iterdir() if p.name.startswith(STAMP_PREFIX) and p.suffix == ".json"
+ )
+ except (OSError, ValueError):
+ return None
+ stamp = parse(read(path) or b"")
+ if not stamp:
+ return None
+ findings = obj(stamp.get("findings"))
+ verification = obj(stamp.get("verification"))
+ shape = obj(stamp.get("run_shape"))
+ reason = code(REASONS, verification.get("reason_kind")) or UNKNOWN_REASON
+ dispatched = count(verification.get("researchers_dispatched"))
+ refused = verification.get("refused_findings")
+ refusals = len(cast("list[object]", refused)) if isinstance(refused, list) else 0
+ return {
+ "mode": code(MODES, stamp.get("mode")),
+ "effort": code(EFFORTS, stamp.get("effort")),
+ "sev_critical": count(findings.get("critical")),
+ "sev_high": count(findings.get("high")),
+ "sev_medium": count(findings.get("medium")),
+ "sev_low": count(findings.get("low")),
+ "candidates": count(verification.get("candidates")),
+ "candidates_deduped": count(verification.get("candidates_deduped")),
+ "unverified_reason": 0 if verification.get("status") == "verified" else reason,
+ "researchers_dispatched": dispatched,
+ "researchers_lost": count(dispatched - count(verification.get("researchers_returned"))),
+ "panels_short": count(verification.get("incomplete_panel_candidates")),
+ "findings_refused": refusals,
+ "verify_runs": count(shape.get("verification_runs")),
+ "collapsed": shape.get("collapsed") in COLLAPSED,
+ "duration_s": count(stamp.get("duration_s")),
+ }
+
+
+def patches_written(patches_dir: Path) -> dict[str, int | bool] | None:
+ """The event for a patch_artifacts.py run, from the patches.jsonl it wrote; else None."""
+ data = read(patches_dir / "patches.jsonl")
+ if data is None:
+ return None
+ rows = [row for row in map(parse, data.splitlines()) if row]
+ statuses = [row.get("status") for row in rows]
+ checks = [str(row.get("apply_check")) for row in rows]
+ return {
+ "units": len(rows),
+ "patches_written": statuses.count("patch_written"),
+ "declined": statuses.count("declined"),
+ "skipped_stale": statuses.count("skipped_stale"),
+ "untested": sum(row.get("untested") is True for row in rows),
+ "apply_clean": checks.count("clean"),
+ "apply_conflicts": sum(check.startswith("conflicts") for check in checks),
+ }
+
+
+def step_failed(script: str, data: dict[str, object]) -> dict[str, int | bool]:
+ """The event for a helper run that failed, from Claude Code's error text."""
+ status = re.match(r"Exit code (\d+)", str(data.get("error", "")))
+ return {
+ "step": STEPS[script],
+ "exit_code": min(int(status[1]), 255) if status else -1,
+ "interrupted": data.get("is_interrupt") is True,
+ }
+
+
+def metrics() -> None:
+ """Print the metrics object for the hook input on stdin, when it is a helper run."""
+ data = parse(sys.stdin.buffer.read())
+ cwd, event = data.get("cwd"), data.get("hook_event_name")
+ words = helper_words(str(obj(data.get("tool_input")).get("command", "")))
+ if words is None or not isinstance(cwd, str):
+ return
+ script = os.path.basename(words[1])
+ positionals, options = arguments(words[2:])
+ if "--remove-scratch" in options:
+ return
+ if event == "PostToolUseFailure":
+ name, body = "step_failed", step_failed(script, data)
+ elif event != "PostToolUse":
+ return
+ elif script == "write_scan_meta.py" and len(positionals) >= 2:
+ name, body = "scan_started", scan_started(os.path.join(cwd, positionals[1]), options)
+ elif script == "render_report.py" and positionals:
+ products = Path(cwd, options.get("--products-dir") or positionals[0])
+ name, body = "scan_finished", scan_finished(products)
+ elif script == "patch_artifacts.py" and len(positionals) >= 2:
+ name, body = "patches_written", patches_written(Path(cwd, positionals[1]))
+ else:
+ return
+ if body is not None:
+ sys.stdout.write(json.dumps({"metrics": {"ev": EVENTS[name], **body}}))
+
+
+def main(argv: list[str]) -> int:
+ hooks = {"banner": banner, "metrics": metrics}
+ if len(argv) != 1 or argv[0] not in hooks:
+ sys.stderr.write("usage: hooks.py banner|metrics\n")
+ return 2
+ hooks[argv[0]]()
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))
diff --git a/plugins/claude-security/hooks/hooks.sh b/plugins/claude-security/hooks/hooks.sh
new file mode 100755
index 00000000..b03b3668
--- /dev/null
+++ b/plugins/claude-security/hooks/hooks.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+# Usage: hooks.sh banner|metrics -- runs hooks.py when python3 is 3.9 or newer.
+have=$(python3 -c 'import sys; sys.stdout.write("%d.%d.%d" % sys.version_info[:3]); sys.exit(3 * (sys.version_info < (3, 9)))' 2>/dev/null)
+case "$?:$1" in
+ 0:metrics) python3 "$(dirname -- "$0")/hooks.py" metrics 2>/dev/null ;;
+ 0:banner) python3 "$(dirname -- "$0")/hooks.py" banner ;;
+ 3:banner) printf '{"systemMessage": "\\n\\u26a0\\ufe0f Claude Security needs python3 3.9 or newer, but this python3 is %s. Scanning and fixing will fail until a newer python3 is first on PATH.\\n"}' "$have" ;;
+ *:banner) printf '%s\n' '{"systemMessage":"\n⚠️ Claude Security needs a working python3 (3.9 or newer) on PATH and could not run one. Install Python 3, then start a new session.\n"}' ;;
+esac
+exit 0