summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/lib/live/scan.py
blob: fc997e2419c82ed63886cecf28fab58ae995fdb2 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
"""The state of a running scan."""

from __future__ import annotations

import enum
import threading
import time
from collections import deque
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, NamedTuple, TypedDict

from lib.finding import PANEL_KEEP_QUORUM, Severity

from . import words

if TYPE_CHECKING:
    from collections.abc import Callable, Iterable, Sequence

    from .atlas import Atlas

from dataclasses import asdict

LENSES = ("reachability", "impact", "defenses")
SEVERITIES = tuple(Severity.__members__)
_RECENT_TOUCHES = 600
_RECENT_NOTES = 400


class Stage(str, enum.Enum):
    """The stages of a scan, in the order in which they begin."""

    SETUP = "Setup"
    INVENTORY = "Inventory"
    SWEEP = "Sweep"
    RESEARCH = "Research"
    PANEL = "Panel"
    REPORT = "Report"


_OPENINGS = {
    Stage.SETUP: "Starting Claude Code",
    Stage.INVENTORY: "Dividing the repository into components",
    Stage.SWEEP: "Sweeping the code that the components do not cover",
    Stage.RESEARCH: "Researchers are hunting through the code",
    Stage.PANEL: "Verifiers try to disprove each candidate",
    Stage.REPORT: "Writing the report",
}


class State(str, enum.Enum):
    """Where a stage, an agent or the scan itself stands."""

    WAITING = "waiting"
    RUNNING = "running"
    DONE = "done"
    FAILED = "failed"
    SKIPPED = "skipped"


class Vote(str, enum.Enum):
    """A verifier's answer on a candidate."""

    PENDING = "pending"
    CONFIRM = "confirm"
    REFUTE = "refute"
    ABSTAIN = "abstain"


class Look(str, enum.Enum):
    """How an agent looked at the code."""

    SEARCH = "search"
    READ = "read"


class Outcome(str, enum.Enum):
    """What the panel made of a candidate."""

    TESTING = "testing"
    CONFIRMED = "confirmed"
    REFUTED = "refuted"
    UNSETTLED = "unsettled"


@dataclass
class Target:
    """What is scanned, how, and inside which limits."""

    name: str
    mode: str
    effort: str
    scope: list[str] = field(default_factory=list)
    branch: str = ""
    commit: str = ""
    dirty: bool = False
    model: str = ""
    version: str = ""
    sandboxed: bool = False
    hosts: list[str] = field(default_factory=list)
    writable: list[str] = field(default_factory=list)


@dataclass
class Result:
    """How the scan ended."""

    state: State = State.RUNNING
    message: str = ""
    report_dir: str = ""
    files: list[str] = field(default_factory=list)
    verification: str = ""


@dataclass
class Phase:
    stage: Stage
    state: State = State.WAITING
    started: float | None = None
    ended: float | None = None


@dataclass
class Component:
    name: str
    language: str
    plots: Sequence[int]


@dataclass
class Agent:
    """One agent of the scan. `plot` is where it last read."""

    id: int
    label: str
    stage: Stage
    role: str
    component: str = ""
    lens: str = ""
    state: State = State.WAITING
    started: float | None = None
    ended: float | None = None
    tools: int = 0
    tokens: int = 0
    plot: int | None = None
    activity: str = ""
    rev: int = 0
    name: str = ""


@dataclass
class Lead:
    """A place that a researcher reported, before duplicates are joined."""

    plot: int
    plot_line: int
    agent: int
    at: float
    rev: int = 0


@dataclass
class Candidate:
    """A possible flaw before the panel. `path` is its file's path as a view may show it, and no
    key for a look-up. `line` is a line of that file."""

    id: str
    path: str = ""
    line: int = 0
    plot: int | None = None
    plot_line: int = 0
    votes: dict[str, Vote] = field(default_factory=lambda: dict.fromkeys(LENSES, Vote.PENDING))
    outcome: Outcome = Outcome.TESTING
    settled: float | None = None
    rev: int = 0


@dataclass
class Finding:
    """One record of the finished report."""

    id: str
    title: str
    severity: str
    confidence: str
    file: str
    line: int
    plot: int | None
    plot_line: int
    category: str
    cwe: str
    symbol: str
    snippet: str
    description: str
    impact: str
    exploit_scenario: str
    preconditions: list[str]
    recommendation: str


@dataclass
class Visit:
    """What the agents read of one plot: merged spans of lines, first and last included."""

    plot: int
    reads: int = 0
    last: float = 0.0
    spans: list[tuple[int, int]] = field(default_factory=list)
    rev: int = 0

    def cover(self, first: int, last: int) -> int:
        """Add the span from `first` to `last`, and return how many new lines it adds."""
        merged: list[tuple[int, int]] = []
        for low, high in sorted([*self.spans, (first, last)]):
            if merged and low <= merged[-1][1] + 1:
                merged[-1] = (merged[-1][0], max(merged[-1][1], high))
            else:
                merged.append((low, high))
        added = _lines_in(merged) - _lines_in(self.spans)
        self.spans = merged
        return added


def _lines_in(spans: Iterable[tuple[int, int]]) -> int:
    return sum(high - low + 1 for low, high in spans)


class Touch(NamedTuple):
    """One look at the code: kind is "read" for lines of one plot, "search" for a run of plots.
    `plots` holds the first plot of the run and the plot after its last."""

    rev: int
    at: float
    agent: int
    kind: Look
    plots: tuple[int, int]
    first: int
    last: int


class Note(NamedTuple):
    """A line of the scan's own narration."""

    rev: int
    at: float
    text: str


class Totals(TypedDict):
    """What a scan has counted so far. Each attempt of a retried agent counts as an agent."""

    agents: int
    running: int
    done: int
    failed: int
    tools: int
    tokens: int
    files: int
    files_read: int
    lines: int
    lines_read: int
    leads: int
    candidates: int
    confirmed: int
    refuted: int
    findings: int


def _outcome(votes: list[Vote]) -> Outcome:
    if votes.count(Vote.CONFIRM) >= PANEL_KEEP_QUORUM:
        return Outcome.CONFIRMED
    if votes.count(Vote.REFUTE) >= PANEL_KEEP_QUORUM:
        return Outcome.REFUTED
    return Outcome.UNSETTLED


class Scan:
    """Everything that is known of a running scan. Hold `changed` to read one state of it."""

    def __init__(
        self, target: Target, atlas: Atlas, clock: Callable[[], float] = time.monotonic
    ) -> None:
        self.target: Target = target
        self.atlas: Atlas = atlas
        self.changed: threading.Condition = threading.Condition()
        self.rev: int = 1
        self.result: Result = Result()
        self.phases: dict[Stage, Phase] = {stage: Phase(stage) for stage in Stage}
        self.components: list[Component] = []
        self.agents: dict[int, Agent] = {}
        self.leads: list[Lead] = []
        self.candidates: dict[str, Candidate] = {}
        self.findings: list[Finding] = []
        self.visits: dict[int, Visit] = {}
        self._read: set[str] = set()
        self._lines_read: int = 0
        self.touches: deque[Touch] = deque(maxlen=_RECENT_TOUCHES)
        self.notes: deque[Note] = deque(maxlen=_RECENT_NOTES)
        self._clock: Callable[[], float] = clock
        self._began: float = clock()
        self._ended: float | None = None
        self._components_rev: int = 0
        self._findings_rev: int = 0
        self._asked_at_once: set[str] = set()

    def now(self) -> float:
        """Seconds since the scan began. The clock stops when the scan ends."""
        return self._clock() - self._began if self._ended is None else self._ended

    @property
    def running(self) -> bool:
        return self.result.state is State.RUNNING

    def totals(self) -> Totals:
        agents = self.agents.values()
        outcomes = [candidate.outcome for candidate in self.candidates.values()]
        return {
            "agents": len(agents),
            "running": sum(agent.state is State.RUNNING for agent in agents),
            "done": sum(agent.state is State.DONE for agent in agents),
            "failed": sum(agent.state is State.FAILED for agent in agents),
            "tools": sum(agent.tools for agent in agents),
            "tokens": sum(agent.tokens for agent in agents),
            "files": len(self.atlas.files),
            "files_read": len(self._read),
            "lines": self.atlas.lines,
            "lines_read": self._lines_read,
            "leads": len(self.leads),
            "candidates": len(outcomes),
            "confirmed": outcomes.count(Outcome.CONFIRMED),
            "refuted": outcomes.count(Outcome.REFUTED),
            "findings": len(self.findings),
        }

    def delta(self, since: int = 0) -> dict[str, object]:
        """What changed after revision `since`, as JSON values. Revision 0 gives everything."""

        def newer(items: Iterable[Agent | Lead | Candidate | Visit]) -> list[dict[str, object]]:
            return [asdict(item) for item in items if item.rev > since]

        with self.changed:
            out: dict[str, object] = {
                "rev": self.rev,
                "now": round(self.now(), 2),
                "target": asdict(self.target),
                "result": asdict(self.result),
                "phases": [asdict(phase) for phase in self.phases.values()],
                "totals": self.totals(),
                "agents": newer(self.agents.values()),
                "leads": newer(self.leads),
                "candidates": newer(self.candidates.values()),
                "visits": newer(self.visits.values()),
                "touches": [touch._asdict() for touch in self.touches if touch.rev > since],
                "notes": [note._asdict() for note in self.notes if note.rev > since],
            }
            if self._components_rev > since:
                out["components"] = [asdict(component) for component in self.components]
            if self._findings_rev > since:
                out["findings"] = [asdict(finding) for finding in self.findings]
            return out

    def use_model(self, model: str) -> None:
        with self.changed:
            self.target.model = model
            self._next()

    def describe(self, *, commit: str, dirty: bool) -> None:
        with self.changed:
            self.target.commit, self.target.dirty = commit, dirty
            self._next()

    def enter(self, stage: Stage) -> None:
        """Start a stage. Once no agent of an earlier one runs or waits, that one ends, or is
        skipped if it never began."""
        with self.changed:
            phase = self.phases[stage]
            if phase.started is None:
                phase.started = self.now()
                self.note(_OPENINGS[stage])
            phase.state, phase.ended = State.RUNNING, None
            self._settle_phases()
            self._next()

    def note(self, text: str) -> None:
        with self.changed:
            self.notes.append(Note(self._next(), self.now(), text))

    def map_components(self, components: Sequence[Component]) -> None:
        with self.changed:
            self.components = list(components)
            self._components_rev = self._next()

    def update_agent(
        self,
        agent: Agent,
        *,
        state: State | None = None,
        tools: int | None = None,
        tokens: int | None = None,
        activity: str | None = None,
    ) -> None:
        """Add the agent if it is new, set the fields given, and time its start and its end."""
        with self.changed:
            self.agents[agent.id] = agent
            agent.state = agent.state if state is None else state
            agent.tools = agent.tools if tools is None else tools
            agent.tokens = agent.tokens if tokens is None else tokens
            agent.activity = agent.activity if activity is None else activity
            if agent.state in {State.RUNNING, State.DONE} and agent.started is None:
                agent.started = self.now()
                self.enter(agent.stage)
            if agent.state in {State.DONE, State.FAILED} and agent.ended is None:
                agent.ended, agent.activity = self.now(), ""
                self._settle_phases()
            agent.name = words.agent(agent)
            agent.rev = self._next()

    def read(self, agent: Agent, path: str, first: int = 1, count: int | None = None) -> None:
        """Record that the agent read `count` lines of a file from line `first`, or the rest."""
        plot, lines = self.atlas.find(path), self.atlas.files.get(path)
        if plot is None or lines is None:
            return
        with self.changed:
            start = min(max(first, 1), len(lines)) - 1
            seen = lines[start : None if count is None else start + max(count, 1)]
            first, last = seen.start + 1, seen.stop
            self._read.add(path)
            visit = self.visits.setdefault(plot, Visit(plot))
            if seen:
                self._lines_read += visit.cover(first, last)
            visit.reads, visit.last = visit.reads + 1, self.now()
            visit.rev = agent.rev = self._next()
            agent.plot = plot
            run = (plot, plot + 1)
            self.touches.append(Touch(self.rev, visit.last, agent.id, Look.READ, run, first, last))

    def search(self, agent: Agent, path: str) -> None:
        """Record that the agent searched a file or a folder."""
        plots = self.atlas.under(path)
        if not plots:
            return
        # The plots under a path are adjacent, since the atlas is in path order.
        run = (plots[0], plots[-1] + 1)
        with self.changed:
            self.touches.append(Touch(self._next(), self.now(), agent.id, Look.SEARCH, run, 0, 0))

    def lead(self, agent: Agent, path: str, line: int) -> None:
        plot = self.atlas.find(path)
        if plot is None:
            return
        with self.changed:
            on_plot = self.atlas.plot_line(path, line)
            self.leads.append(Lead(plot, on_plot, agent.id, self.now(), self._next()))

    def admit(self, cid: str, path: str = "", line: int = 0, shown: str | None = None) -> Candidate:
        """The candidate of that id, added if new and placed if a path is given. A view shows
        `shown`, if given, in place of the path."""
        with self.changed:
            candidate = self.candidates.setdefault(cid, Candidate(cid))
            if path:
                candidate.path, candidate.line = path if shown is None else shown, line
                candidate.plot = self.atlas.find(path)
                candidate.plot_line = self.atlas.plot_line(path, line)
            candidate.rev = self._next()
            return candidate

    def vote(self, cid: str, lens: str, vote: Vote) -> None:
        """Record a vote. Two votes settle the candidate if both confirm or both refute it, and
        else the third does. The two are the first verifiers', or any where all were asked
        at once. A third verifier can be asked after two that settle it: see ask()."""
        with self.changed:
            candidate = self.admit(cid)
            candidate.votes[lens] = vote
            if cid in self._asked_at_once and candidate.outcome is not Outcome.TESTING:
                return
            votes = list(candidate.votes.values())
            deciding = votes if cid in self._asked_at_once else votes[:-1]
            if Vote.PENDING not in votes or _outcome(deciding) is not Outcome.UNSETTLED:
                candidate.settled, candidate.outcome = self.now(), _outcome(votes)
                cast = len(votes) - votes.count(Vote.PENDING)
                agree = f"{votes.count(Vote.CONFIRM)} of {cast} verifiers confirm it"
                self.note(f"{cid} is {candidate.outcome.value}: {agree}")

    def ask(self, cid: str, lens: str) -> None:
        """Put the candidate back under test, with this lens's vote pending, and the last lens's
        where that is asked after the others."""
        with self.changed:
            candidate = self.admit(cid)
            if lens == LENSES[-1] and set(candidate.votes.values()) == {Vote.PENDING}:
                self._asked_at_once.add(cid)
            elif cid not in self._asked_at_once:
                candidate.votes[LENSES[-1]] = Vote.PENDING
            candidate.votes[lens] = Vote.PENDING
            candidate.settled, candidate.outcome = None, Outcome.TESTING

    def reopen(self) -> None:
        """Clear the votes of each candidate that no whole panel settled: a further run has it.
        The report waits for that run."""
        with self.changed:
            if self.phases[Stage.REPORT].started is not None:
                self.phases[Stage.REPORT] = Phase(Stage.REPORT)
                self.note("Verification goes on before the report")
            for candidate in self.candidates.values():
                if candidate.outcome is Outcome.TESTING:
                    candidate.votes = dict.fromkeys(LENSES, Vote.PENDING)
                    candidate.rev = self._next()
            self._asked_at_once.clear()

    def finish(self, result: Result, findings: Sequence[Finding] = ()) -> None:
        with self.changed:
            self._ended = self.now()
            self.result, self.findings = result, list(findings)
            # A verification comes from a report that was read, so its stage did not fail.
            cut = result.state is State.FAILED and not result.verification
            # The stages end first: an agent that fails would end its stage as done.
            for phase in self.phases.values():
                if phase.state is State.RUNNING:
                    phase.state, phase.ended = State.FAILED if cut else State.DONE, self._ended
                elif phase.state is State.WAITING:
                    phase.state = State.SKIPPED
            for agent in self.agents.values():
                if agent.state in {State.WAITING, State.RUNNING}:
                    self.update_agent(agent, state=State.FAILED)
            for candidate in self.candidates.values():
                if candidate.outcome is Outcome.TESTING:
                    candidate.outcome, candidate.settled = Outcome.UNSETTLED, self._ended
                    candidate.rev = self._next()
            self._findings_rev = self._next()

    def _settle_phases(self) -> None:
        begun = [phase.stage for phase in self.phases.values() if phase.started is not None]
        busy = {a.stage for a in self.agents.values() if a.state in {State.WAITING, State.RUNNING}}
        for phase in self.phases.values():
            if phase.stage is begun[-1]:
                break
            if phase.stage in busy:
                continue
            if phase.started is None:
                phase.state = State.SKIPPED
            elif phase.state is State.RUNNING:
                phase.state, phase.ended = State.DONE, self.now()

    def _next(self) -> int:
        self.rev += 1
        self.changed.notify_all()
        return self.rev