summaryrefslogtreecommitdiff
path: root/plugins/claude-security/hooks/hooks.py
blob: 693c4602d2d0333b0ca19e719d624ccbff1a14ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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:]))