patchsight 0.2.0__py3-none-any.whl → 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- patchsight/cli.py +13 -1
- patchsight/db.py +62 -6
- patchsight/eval/failures.py +118 -0
- patchsight/pipeline/__main__.py +419 -6
- patchsight/pipeline/benchmarks/extraction.py +1 -0
- patchsight/pipeline/build_db.py +18 -5
- patchsight/pipeline/extract/extractor.py +2 -1
- patchsight/pipeline/extract/prompt.py +16 -5
- patchsight/pipeline/extract/validate.py +24 -4
- patchsight/pipeline/gate.py +173 -0
- patchsight/pipeline/llm/client.py +18 -0
- patchsight/pipeline/llm/config.py +9 -0
- patchsight/pipeline/llm_records.py +76 -0
- patchsight/pipeline/pypi_extract.py +9 -6
- patchsight/pipeline/update_db.py +124 -0
- patchsight/scan/dbcache.py +19 -9
- patchsight/scan/sarif.py +177 -0
- {patchsight-0.2.0.dist-info → patchsight-0.4.0.dist-info}/METADATA +12 -3
- {patchsight-0.2.0.dist-info → patchsight-0.4.0.dist-info}/RECORD +22 -18
- {patchsight-0.2.0.dist-info → patchsight-0.4.0.dist-info}/WHEEL +1 -1
- {patchsight-0.2.0.dist-info → patchsight-0.4.0.dist-info}/entry_points.txt +0 -0
- {patchsight-0.2.0.dist-info → patchsight-0.4.0.dist-info}/licenses/LICENSE +0 -0
patchsight/cli.py
CHANGED
|
@@ -102,7 +102,9 @@ def scan(
|
|
|
102
102
|
Path | None,
|
|
103
103
|
typer.Option("--db", help="Symbol database (default: the one 'db update' downloaded)."),
|
|
104
104
|
] = None,
|
|
105
|
-
output: Annotated[
|
|
105
|
+
output: Annotated[
|
|
106
|
+
str, typer.Option("--format", help="text, json, or sarif (for code scanning).")
|
|
107
|
+
] = "text",
|
|
106
108
|
fail_on: Annotated[
|
|
107
109
|
str, typer.Option(help="Exit 1 when an alert is: called, unknown (or called), or never.")
|
|
108
110
|
] = "never",
|
|
@@ -131,6 +133,16 @@ def scan(
|
|
|
131
133
|
shown = lockfile.as_posix()
|
|
132
134
|
if output == "json":
|
|
133
135
|
typer.echo(render_json(alerts, dependencies, shown, meta))
|
|
136
|
+
elif output == "sarif":
|
|
137
|
+
import os
|
|
138
|
+
|
|
139
|
+
from patchsight.scan.sarif import render_sarif
|
|
140
|
+
|
|
141
|
+
# Code locations are relative to the scanned directory; SARIF wants them relative to
|
|
142
|
+
# where the scan runs (the repository root, in CI).
|
|
143
|
+
root = path if path.is_dir() else path.parent
|
|
144
|
+
base = os.path.relpath(root.resolve(), Path.cwd().resolve())
|
|
145
|
+
typer.echo(render_sarif(alerts, dependencies, shown, meta, base))
|
|
134
146
|
else:
|
|
135
147
|
typer.echo(render_text(alerts, dependencies, shown, meta))
|
|
136
148
|
failing = {"called": {"CALLED"}, "unknown": {"CALLED", "UNKNOWN"}}.get(fail_on, set())
|
patchsight/db.py
CHANGED
|
@@ -10,11 +10,12 @@ its vulnerability, so the scanner can look up whatever ID OSV returns.
|
|
|
10
10
|
|
|
11
11
|
Stdlib ``sqlite3`` only, so the scan path can read it without any pipeline dependency.
|
|
12
12
|
|
|
13
|
-
Compatibility:
|
|
14
|
-
and the ``evidence``
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
Compatibility: three tables were added within schema version 1. ``extraction`` holds the
|
|
14
|
+
second-opinion model and the reason for the tier; ``evidence`` holds each flagged symbol's
|
|
15
|
+
rationale and cited hunk locations; ``builds`` is the ledger of every vulnerability
|
|
16
|
+
considered, with its input hash, for incremental updates. Readers that predate them never
|
|
17
|
+
query them, so they keep working; this reader treats a database without them as having no
|
|
18
|
+
such data. Evidence holds commit SHAs, paths and line ranges only, never code (rule 13).
|
|
18
19
|
"""
|
|
19
20
|
|
|
20
21
|
import json
|
|
@@ -68,6 +69,11 @@ CREATE TABLE extraction (
|
|
|
68
69
|
tier_reason TEXT,
|
|
69
70
|
PRIMARY KEY (vulnerability, package)
|
|
70
71
|
);
|
|
72
|
+
CREATE TABLE builds (
|
|
73
|
+
vulnerability TEXT PRIMARY KEY,
|
|
74
|
+
input_hash TEXT NOT NULL,
|
|
75
|
+
status TEXT NOT NULL
|
|
76
|
+
);
|
|
71
77
|
CREATE TABLE evidence (
|
|
72
78
|
vulnerability TEXT NOT NULL,
|
|
73
79
|
package TEXT NOT NULL,
|
|
@@ -90,6 +96,17 @@ class FixCommitRef(BaseModel):
|
|
|
90
96
|
sha: str
|
|
91
97
|
|
|
92
98
|
|
|
99
|
+
class BuildEntry(BaseModel):
|
|
100
|
+
"""One vulnerability the builder considered, with or without a record: the ledger an
|
|
101
|
+
incremental update compares against (ADR 0001: re-extract only when inputs change)."""
|
|
102
|
+
|
|
103
|
+
model_config = ConfigDict(frozen=True)
|
|
104
|
+
|
|
105
|
+
vulnerability: str
|
|
106
|
+
input_hash: str
|
|
107
|
+
status: str
|
|
108
|
+
|
|
109
|
+
|
|
93
110
|
class HunkLocation(BaseModel):
|
|
94
111
|
"""Where a cited diff hunk sits: the fix commit, the file and both line ranges."""
|
|
95
112
|
|
|
@@ -152,7 +169,11 @@ class Record(BaseModel):
|
|
|
152
169
|
|
|
153
170
|
|
|
154
171
|
def write(
|
|
155
|
-
path: Path,
|
|
172
|
+
path: Path,
|
|
173
|
+
records: Iterable[Record],
|
|
174
|
+
aliases: dict[str, str],
|
|
175
|
+
meta: dict[str, str],
|
|
176
|
+
builds: Iterable[BuildEntry] = (),
|
|
156
177
|
) -> int:
|
|
157
178
|
"""Write a fresh database file. Returns the number of records written."""
|
|
158
179
|
if path.exists():
|
|
@@ -167,6 +188,10 @@ def write(
|
|
|
167
188
|
sorted({**meta, "schema_version": str(SCHEMA_VERSION)}.items()),
|
|
168
189
|
)
|
|
169
190
|
connection.executemany("INSERT INTO aliases VALUES (?, ?)", sorted(aliases.items()))
|
|
191
|
+
connection.executemany(
|
|
192
|
+
"INSERT INTO builds VALUES (?, ?, ?)",
|
|
193
|
+
sorted((b.vulnerability, b.input_hash, b.status) for b in builds),
|
|
194
|
+
)
|
|
170
195
|
for record in records:
|
|
171
196
|
count += 1
|
|
172
197
|
connection.execute(
|
|
@@ -256,6 +281,7 @@ class SymbolDatabase:
|
|
|
256
281
|
}
|
|
257
282
|
self._has_extraction = "extraction" in tables
|
|
258
283
|
self._has_evidence = "evidence" in tables
|
|
284
|
+
self._has_builds = "builds" in tables
|
|
259
285
|
|
|
260
286
|
def close(self) -> None:
|
|
261
287
|
self._connection.close()
|
|
@@ -269,6 +295,36 @@ class SymbolDatabase:
|
|
|
269
295
|
).fetchone()
|
|
270
296
|
return str(row[0]) if row else None
|
|
271
297
|
|
|
298
|
+
def all_records(self) -> list[Record]:
|
|
299
|
+
"""Every record, ordered by vulnerability and package."""
|
|
300
|
+
return [r for v in self.vulnerabilities() for r in self.records_for(v)]
|
|
301
|
+
|
|
302
|
+
def confidence_counts(self) -> dict[str, int]:
|
|
303
|
+
"""Records by confidence tier."""
|
|
304
|
+
rows = self._connection.execute(
|
|
305
|
+
"SELECT confidence, COUNT(*) FROM records GROUP BY confidence ORDER BY confidence"
|
|
306
|
+
)
|
|
307
|
+
return {str(r[0]): int(r[1]) for r in rows}
|
|
308
|
+
|
|
309
|
+
def vulnerabilities(self) -> list[str]:
|
|
310
|
+
"""Every vulnerability that has at least one record."""
|
|
311
|
+
rows = self._connection.execute(
|
|
312
|
+
"SELECT DISTINCT vulnerability FROM records ORDER BY vulnerability"
|
|
313
|
+
)
|
|
314
|
+
return [str(r[0]) for r in rows]
|
|
315
|
+
|
|
316
|
+
def alias_map(self) -> dict[str, str]:
|
|
317
|
+
"""Every advisory ID and alias, mapped to its vulnerability."""
|
|
318
|
+
rows = self._connection.execute("SELECT identifier, vulnerability FROM aliases")
|
|
319
|
+
return {str(r[0]): str(r[1]) for r in rows}
|
|
320
|
+
|
|
321
|
+
def build_ledger(self) -> dict[str, BuildEntry]:
|
|
322
|
+
"""Every vulnerability the builder considered; empty for an older database."""
|
|
323
|
+
if not self._has_builds:
|
|
324
|
+
return {}
|
|
325
|
+
rows = self._connection.execute("SELECT vulnerability, input_hash, status FROM builds")
|
|
326
|
+
return {r[0]: BuildEntry(vulnerability=r[0], input_hash=r[1], status=r[2]) for r in rows}
|
|
327
|
+
|
|
272
328
|
def identifiers_for(self, vulnerability: str) -> list[str]:
|
|
273
329
|
"""Every advisory ID and alias that points at this vulnerability."""
|
|
274
330
|
rows = self._connection.execute(
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Failure causes for extraction errors (proposal §10, "failure analysis").
|
|
2
|
+
|
|
3
|
+
Every missed true symbol (a false negative) gets exactly one cause, checked in this order:
|
|
4
|
+
|
|
5
|
+
* ``not_changed``: the fix did not change the symbol, so no patch-based method can find it
|
|
6
|
+
(the ceiling ChangedFuncs also hits: "vulnerable function unchanged, callee changed");
|
|
7
|
+
* ``name_format``: the fix changed a definition that matches the symbol only on a dotted
|
|
8
|
+
boundary (GoVulnDB's ``decryptTicket`` for the method ``Conn.decryptTicket``): a naming
|
|
9
|
+
difference in the ground truth, not a missed function;
|
|
10
|
+
* ``dead_letter``: the symbol was changed, but the extraction failed;
|
|
11
|
+
* ``truncated``: changed, but dropped from the prompt to fit the token budget;
|
|
12
|
+
* ``model_missed``: shown to the model and judged not vulnerable.
|
|
13
|
+
|
|
14
|
+
Every false positive is either ``name_format`` (it matches a true symbol on a dotted
|
|
15
|
+
boundary) or ``model_false_positive``.
|
|
16
|
+
|
|
17
|
+
A report-level miss (no true symbol found: the safety metric) is attributed to its most
|
|
18
|
+
actionable cause: a shown-but-missed symbol first, then truncation, then the rest.
|
|
19
|
+
|
|
20
|
+
Not separated: "the fix adds a new helper". The replay data does not record which side of
|
|
21
|
+
the diff a candidate comes from, so such cases fall under ``model_false_positive``.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from collections import Counter
|
|
25
|
+
from collections.abc import Mapping, Set
|
|
26
|
+
|
|
27
|
+
from pydantic import BaseModel, ConfigDict
|
|
28
|
+
|
|
29
|
+
from patchsight.eval.safety import lenient_match
|
|
30
|
+
|
|
31
|
+
MISS_CAUSES = ("not_changed", "name_format", "dead_letter", "truncated", "model_missed")
|
|
32
|
+
REPORT_MISS_ORDER = ("model_missed", "truncated", "dead_letter", "name_format", "not_changed")
|
|
33
|
+
VALID = frozenset({"ok", "repaired"})
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ReportInput(BaseModel):
|
|
37
|
+
model_config = ConfigDict(frozen=True)
|
|
38
|
+
|
|
39
|
+
truth: frozenset[str]
|
|
40
|
+
changed: frozenset[str] # every changed function (ChangedFuncs)
|
|
41
|
+
shown: frozenset[str] # the candidates the prompt kept
|
|
42
|
+
status: str
|
|
43
|
+
predicted: frozenset[str]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class FailureBreakdown(BaseModel):
|
|
47
|
+
model_config = ConfigDict(frozen=True)
|
|
48
|
+
|
|
49
|
+
reports: int
|
|
50
|
+
truth_symbols: int
|
|
51
|
+
false_negatives: dict[str, int]
|
|
52
|
+
false_positives: dict[str, int]
|
|
53
|
+
report_misses: dict[str, int]
|
|
54
|
+
examples: dict[str, tuple[str, ...]] # cause -> report IDs (sorted)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def miss_cause(symbol: str, report: ReportInput) -> str:
|
|
58
|
+
if symbol not in report.changed:
|
|
59
|
+
if any(lenient_match(c, symbol) for c in report.changed):
|
|
60
|
+
return "name_format"
|
|
61
|
+
return "not_changed"
|
|
62
|
+
if report.status not in VALID:
|
|
63
|
+
return "dead_letter"
|
|
64
|
+
if symbol not in report.shown:
|
|
65
|
+
return "truncated"
|
|
66
|
+
return "model_missed"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def breakdown(reports: Mapping[str, ReportInput]) -> FailureBreakdown:
|
|
70
|
+
negatives: Counter[str] = Counter()
|
|
71
|
+
positives: Counter[str] = Counter()
|
|
72
|
+
misses: Counter[str] = Counter()
|
|
73
|
+
examples: dict[str, set[str]] = {}
|
|
74
|
+
total = 0
|
|
75
|
+
for rid, report in sorted(reports.items()):
|
|
76
|
+
total += len(report.truth)
|
|
77
|
+
causes = {miss_cause(s, report) for s in report.truth - report.predicted}
|
|
78
|
+
for symbol in report.truth - report.predicted:
|
|
79
|
+
cause = miss_cause(symbol, report)
|
|
80
|
+
negatives[cause] += 1
|
|
81
|
+
examples.setdefault(cause, set()).add(rid)
|
|
82
|
+
for symbol in report.predicted - report.truth:
|
|
83
|
+
cause = (
|
|
84
|
+
"name_format"
|
|
85
|
+
if any(lenient_match(symbol, t) for t in report.truth)
|
|
86
|
+
else "model_false_positive"
|
|
87
|
+
)
|
|
88
|
+
positives[cause] += 1
|
|
89
|
+
examples.setdefault(f"fp:{cause}", set()).add(rid)
|
|
90
|
+
if report.truth and not report.truth & report.predicted:
|
|
91
|
+
misses[next(c for c in REPORT_MISS_ORDER if c in causes)] += 1
|
|
92
|
+
return FailureBreakdown(
|
|
93
|
+
reports=len(reports),
|
|
94
|
+
truth_symbols=total,
|
|
95
|
+
false_negatives={c: negatives[c] for c in MISS_CAUSES},
|
|
96
|
+
false_positives={c: positives[c] for c in ("name_format", "model_false_positive")},
|
|
97
|
+
report_misses={c: misses[c] for c in REPORT_MISS_ORDER},
|
|
98
|
+
examples={k: tuple(sorted(v)) for k, v in sorted(examples.items())},
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def as_inputs(
|
|
103
|
+
truth: Mapping[str, Set[str]],
|
|
104
|
+
changed: Mapping[str, Set[str]],
|
|
105
|
+
shown: Mapping[str, Set[str]],
|
|
106
|
+
status: Mapping[str, str],
|
|
107
|
+
predicted: Mapping[str, Set[str]],
|
|
108
|
+
) -> dict[str, ReportInput]:
|
|
109
|
+
return {
|
|
110
|
+
r: ReportInput(
|
|
111
|
+
truth=frozenset(truth[r]),
|
|
112
|
+
changed=frozenset(changed.get(r, set())),
|
|
113
|
+
shown=frozenset(shown.get(r, set())),
|
|
114
|
+
status=status.get(r, "dead_letter"),
|
|
115
|
+
predicted=frozenset(predicted.get(r, set())),
|
|
116
|
+
)
|
|
117
|
+
for r in truth
|
|
118
|
+
}
|