Repository navigation
Expand file tree
/
Copy pathcve_report.py
More file actions
executable file
·787 lines (703 loc) · 32.2 KB
/
Copy pathcve_report.py
File metadata and controls
executable file
·787 lines (703 loc) · 32.2 KB
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
#!/usr/bin/env python3
"""Turn scripts/scan-images.sh's reports into a job summary and issues (#852).
WHAT THIS IS FOR. scan-images.sh leaves one trivy JSON per pinned image. This
reads them and does two things with them:
* the summary: one row per pinned image, clean ones included, so a run
shows it scanned everything, not only that it found something;
* the issues: one open issue per image REPOSITORY with fixable HIGH or
CRITICAL findings, labelled `security` and its stacks' labels.
ONE ISSUE PER REPOSITORY, NOT PER TAG. The identity is `vaultwarden/server`,
whatever tag is pinned, and lives in a hidden marker in the body. The fix for a
finding is nearly always a bump, and a bump changes the tag: keyed on the
tag, every Dependabot merge would close one issue and open its twin. Keyed on
the repository, the issue survives the bump and closes when a scan finds the
new digest clean. postgres:18.6 (sensitive) and postgres:17.11 (wiki) are
therefore one issue with two sections — one upstream, one place to look.
WHAT EACH RUN DOES TO AN ISSUE
fixable, no open issue create it
fixable, open issue rewrite the body to this run's picture; comment
only when a finding appears that the last body
did not have, because an edit notifies nobody
clean, open issue comment the clean digest, close
no longer pinned, open issue comment, close — which is also how the issue a
`--extra` test image opened tidies itself away
on the next scheduled run
any of its tags failed scan leave it alone: unknown is not clean
WHAT "NEW" IS MEASURED AGAINST. Not the rendered table: an oversized body drops
rows, and trivy's IDs are not all CVEs — GHSA-, GO- and ALAS- IDs appear too.
The body carries every finding ID of the last scan in a second hidden marker, zlib-compressed and base64-encoded, and the next scan
compares against that. A body without one (edited by hand, or older) falls
back to the CVE and GHSA IDs visible in it.
WHAT IS NOT REPORTED (#985). Unfixed findings, which scan-images.sh already
leaves out, and one shape of version: a Go main module stamped from a dirty
checkout, `v0.0.0-<timestamp>-<commit>+dirty`. Trivy orders that below every
release, so velociraptor-server:0.77.3 was flagged for CVE-2023-0242, fixed in
0.6.7-5 in 2023, and every later release would be too: the issue could never
close itself. `+dirty` only ever appears on the module `go build` was run in.
Dependencies come from the module cache and are never dirty, so the rule cannot
hide a vulnerable library. It is a property of the version string, not a list
of IDs, so it cannot grow. It trusts the version's shape, not the build's
provenance, so it is never silent: the summary, the issue body and the
closing comment all count what it dropped.
LABELS. `security` plus one per stack the image is pinned in. Stack labels are
the scanner's to manage, so an edit also removes a stack label the image no
longer has — an image dropped from `sensitive` must not keep claiming it.
Every other label on the issue, priority and triage included, is left alone.
A stack label that does not exist is warned about and dropped: failing the
sync for a missing label would hide every finding behind a cosmetic problem.
Usage: scripts/cve_report.py --dir DIR the summary, to stdout
scripts/cve_report.py --dir DIR --sync-issues create/edit/close via gh
scripts/cve_report.py --dir DIR --sync-issues --dry-run
scripts/cve_report.py --self-test
Environment:
GH_TOKEN for --sync-issues; needs issues: write
GITHUB_REPOSITORY owner/name, default Gerrrt/HomeLab
GITHUB_SERVER_URL, GITHUB_RUN_ID link each issue to the run that wrote it
"""
from __future__ import annotations
import argparse
import base64
import dataclasses
import json
import os
import pathlib
import re
import subprocess
import sys
import time
import zlib
RED = "\033[0;31m"
YELLOW = "\033[0;33m"
GREEN = "\033[0;32m"
OFF = "\033[0m"
SEVERITIES = ("CRITICAL", "HIGH")
MARKER = re.compile(r"<!-- cve-scan: (\S+) -->")
IDS_MARKER = re.compile(r"<!-- cve-scan-ids: ([A-Za-z0-9+/=]*) -->")
FINDING_ID = re.compile(r"\b(?:CVE-\d{4}-\d{4,}|GHSA(?:-[0-9a-z]{4}){3})\b")
# A Go main module's VCS stamp from a dirty checkout: not comparable against a
# release version. See WHAT IS NOT REPORTED above.
UNCOMPARABLE = re.compile(r"^v0\.0\.0-\d{14}-[0-9a-f]{12}\+dirty$")
# Stacks whose label is not their name. `test` is scan-images.sh --extra.
# `tooling` is scan-images.sh's pseudo-stack for an image whose service is
# profiled `lint` or `scan` only: CI's and the Mac's linters and scanners,
# pinned in stacks/observability/compose.yaml but never run on prometheus.
LABEL_FOR = {"test": "ci", "tooling": "ci"}
# The pseudo-stacks: not in scripts/stacks.sh, but their labels are the
# scanner's to add and remove all the same.
PSEUDO_STACKS = frozenset(LABEL_FOR)
# An issue body is capped at 65,536 characters; leave room for the frame.
BODY_LIMIT = 60_000
SUMMARY_ROWS_PER_IMAGE = 50
PACE_SECONDS = 2
@dataclasses.dataclass(frozen=True)
class Finding:
id: str
severity: str
package: str
installed: str
fixed: str
title: str
url: str
@dataclasses.dataclass
class Image:
ref: str
stacks: list[str]
findings: list[Finding] | None # None: the scan failed
error: str = ""
uncompared: int = 0 # findings dropped against an UNCOMPARABLE version
@property
def repo(self) -> str:
return repo_of(self.ref)
@property
def name(self) -> str:
"""repo:tag, without the digest."""
return self.ref.split("@", 1)[0]
@property
def digest(self) -> str:
return self.ref.split("@", 1)[1] if "@" in self.ref else ""
def count(self, severity: str) -> int:
return sum(f.severity == severity for f in self.findings or [])
@dataclasses.dataclass
class Action:
kind: str # create | edit | close
repo: str
number: int | None = None
title: str = ""
body: str = ""
labels: tuple[str, ...] = ()
comment: str = ""
remove_labels: tuple[str, ...] = ()
def repo_of(ref: str) -> str:
"""`ghcr.io/a/b:1@sha256:..` -> `ghcr.io/a/b`. The tag is after the last
colon only if that colon is after the last slash: `host:5000/x` has none."""
name = ref.split("@", 1)[0]
head, _, last = name.rpartition("/")
if ":" in last:
last = last.split(":", 1)[0]
return f"{head}/{last}" if head else last
def reportable(doc: dict) -> list[dict]:
"""Fixable HIGH and CRITICAL vulnerabilities, UNCOMPARABLE ones included."""
return [
v
for result in doc.get("Results") or []
for v in result.get("Vulnerabilities") or []
if v.get("Severity", "") in SEVERITIES and v.get("FixedVersion", "")
]
def uncompared(doc: dict) -> int:
return len(
{
(v.get("VulnerabilityID"), v.get("PkgName"), v.get("InstalledVersion"))
for v in reportable(doc)
if UNCOMPARABLE.match(v.get("InstalledVersion", ""))
}
)
def parse_trivy(doc: dict) -> list[Finding]:
seen: dict[tuple[str, str, str], Finding] = {}
for v in reportable(doc):
if UNCOMPARABLE.match(v.get("InstalledVersion", "")):
continue
f = Finding(
id=v.get("VulnerabilityID", "?"),
severity=v["Severity"],
package=v.get("PkgName", "?"),
installed=v.get("InstalledVersion", ""),
fixed=v["FixedVersion"],
title=" ".join((v.get("Title") or "").split()),
url=v.get("PrimaryURL", ""),
)
seen.setdefault((f.id, f.package, f.installed), f)
return sorted(seen.values(), key=lambda f: (SEVERITIES.index(f.severity), f.package, f.id))
def load(directory: pathlib.Path) -> list[Image]:
index = directory / "images.tsv"
if not index.is_file():
sys.exit(f"{RED}error:{OFF} no {index}; run scripts/scan-images.sh first")
images = []
for line in index.read_text(encoding="utf-8").splitlines():
if not line.strip():
continue
n, ref, stacks = line.split("\t")
report, err = directory / f"{n}.json", directory / f"{n}.err"
image = Image(ref=ref, stacks=stacks.split(","), findings=None)
if report.is_file():
doc = json.loads(report.read_text(encoding="utf-8"))
image.findings, image.uncompared = parse_trivy(doc), uncompared(doc)
else:
lines = err.read_text(encoding="utf-8").strip().splitlines() if err.is_file() else []
image.error = lines[-1] if lines else "no report written"
images.append(image)
return images
def status(image: Image) -> str:
if image.findings is None:
return "scan error"
return "fixable" if image.findings else "clean"
def cell(text: str) -> str:
return text.replace("|", "\\|").replace("\n", " ")
def findings_table(findings: list[Finding], limit: int | None = None) -> list[str]:
rows = ["| Severity | ID | Package | Installed | Fixed in |", "|---|---|---|---|---|"]
shown = findings if limit is None else findings[:limit]
for f in shown:
ident = f"[{f.id}]({f.url})" if f.url else f.id
rows.append(f"| {f.severity} | {ident} | {cell(f.package)} | {cell(f.installed)} | {cell(f.fixed)} |")
if limit is not None and len(findings) > limit:
rows.append(f"\n…and {len(findings) - limit} more.")
return rows
def summary(images: list[Image]) -> str:
ordered = sorted(images, key=lambda i: (i.repo, i.name))
fixable = [i for i in ordered if status(i) == "fixable"]
errors = [i for i in ordered if status(i) == "scan error"]
out = [
"## Fixable HIGH and CRITICAL CVEs in pinned images",
"",
(
f"{len(images)} image(s) scanned: {len(fixable)} with fixable findings, "
f"{len(errors)} scan error(s), {len(images) - len(fixable) - len(errors)} clean."
),
"",
"| Image | Stacks | Critical | High | Status |",
"|---|---|---:|---:|---|",
]
for i in ordered:
st = status(i)
crit = "–" if i.findings is None else str(i.count("CRITICAL"))
high = "–" if i.findings is None else str(i.count("HIGH"))
mark = {"fixable": "⚠️ fixable", "scan error": "❌ scan error", "clean": "✅ clean"}[st]
out.append(f"| `{i.name}` | {', '.join(i.stacks)} | {crit} | {high} | {mark} |")
dropped = [i for i in ordered if i.uncompared]
if dropped:
out += ["", "Not compared, because the installed version is a Go main module's `+dirty` VCS stamp (#985):", ""]
out += [f"- `{i.name}`: {i.uncompared} finding(s)" for i in dropped]
for i in errors:
out += ["", f"### ❌ `{i.name}`", "", "```", i.error, "```"]
for i in fixable:
out += [
"",
"<details>",
(f"<summary><code>{i.name}</code>: {i.count('CRITICAL')} critical, {i.count('HIGH')} high</summary>"),
"",
]
out += findings_table(i.findings or [], SUMMARY_ROWS_PER_IMAGE)
out += ["", "</details>"]
return "\n".join(out) + "\n"
def run_url() -> str:
server = os.environ.get("GITHUB_SERVER_URL", "https://github.com")
run = os.environ.get("GITHUB_RUN_ID")
return f"{server}/{repo_name()}/actions/runs/{run}" if run else ""
def repo_name() -> str:
return os.environ.get("GITHUB_REPOSITORY", "Gerrrt/HomeLab")
def issue_title(repo: str, images: list[Image]) -> str:
crit = sum(i.count("CRITICAL") for i in images)
high = sum(i.count("HIGH") for i in images)
return f"Fixable CVEs in {repo} ({crit} critical, {high} high)"
def encode_ids(ids: set[str]) -> str:
raw = "\n".join(sorted(ids)).encode("utf-8")
return f"<!-- cve-scan-ids: {base64.b64encode(zlib.compress(raw, 9)).decode('ascii')} -->"
def previous_ids(body: str) -> set[str]:
"""The finding IDs the last scan recorded in this body. See the docstring."""
m = IDS_MARKER.search(body)
if m:
try:
raw = zlib.decompress(base64.b64decode(m.group(1), validate=True)).decode("utf-8")
return set(filter(None, raw.split("\n")))
except (ValueError, zlib.error, UnicodeDecodeError):
pass
return set(FINDING_ID.findall(body))
def uncompared_note(images: list[Image]) -> str:
"""Said wherever the scan's verdict reaches a person, so a dropped finding
is never silent: the rule trusts a version shape, not provenance."""
n = sum(i.uncompared for i in images)
return (
f" {n} finding(s) against a Go main module's `+dirty` VCS version were not compared;"
" see WHAT IS NOT REPORTED in `scripts/cve_report.py`."
if n
else ""
)
def issue_body(repo: str, images: list[Image], url: str) -> str:
ids = {f.id for i in images for f in i.findings or []}
head = [
f"<!-- cve-scan: {repo} -->",
encode_ids(ids),
(
"The weekly CVE scan found fixable HIGH or CRITICAL vulnerabilities in "
f"`{repo}`. This body is rewritten by every scan, and the issue closes "
"itself when a scan finds the pinned digest clean."
),
"",
(
"The fix is normally the Dependabot bump for this image. If upstream has "
"not released one, the fixed versions below say what to wait for."
),
]
note = uncompared_note(images).strip()
foot = (["", note] if note else []) + [
"",
"---",
"Written by `.github/workflows/cve-scan.yml`" + (f" in [this run]({url})." if url else "."),
]
sections = []
for i in sorted(images, key=lambda i: i.name):
if not i.findings:
continue
sections.append(
["", f"### `{i.name}`", "", f"Stacks: {', '.join(i.stacks)}. Digest: `{i.digest}`.", ""]
+ findings_table(i.findings)
)
body = "\n".join(head + [line for s in sections for line in s] + foot)
if len(body) <= BODY_LIMIT:
return body
# Too long for an issue: keep the counts, drop rows from the bottom up.
limit = 200
while limit > 5:
limit //= 2
sections = [
["", f"### `{i.name}`", "", f"Stacks: {', '.join(i.stacks)}. Digest: `{i.digest}`.", ""]
+ findings_table(i.findings, limit)
for i in images
if i.findings
]
body = "\n".join(head + [line for s in sections for line in s] + foot)
if len(body) <= BODY_LIMIT:
break
return body
def plan(
images: list[Image], existing: list[dict], known_labels: set[str], stack_names: set[str] | None = None
) -> tuple[list[Action], list[str]]:
"""What to do to the issues, and the warnings to print. Pure, so the
self-test can hold it to the table in the module docstring.
`stack_names` is every stack scripts/stacks.sh lists; their labels are the
ones an edit may remove. It defaults to the stacks the images name."""
if stack_names is None:
stack_names = {s for i in images for s in i.stacks}
owned = {LABEL_FOR.get(s, s) for s in stack_names | PSEUDO_STACKS}
by_repo: dict[str, list[Image]] = {}
for i in images:
by_repo.setdefault(i.repo, []).append(i)
open_issues: dict[str, dict] = {}
for issue in existing:
m = MARKER.search(issue.get("body") or "")
if m:
open_issues.setdefault(m.group(1), issue)
actions: list[Action] = []
warnings: list[str] = []
url = run_url()
for repo in sorted(by_repo):
imgs = by_repo[repo]
issue = open_issues.get(repo)
if any(i.findings is None for i in imgs):
warnings.append(f"{repo}: a tag failed to scan; its issue is left as it is")
continue
if not any(i.findings for i in imgs):
if issue:
clean = ", ".join(f"`{i.ref}`" for i in imgs)
actions.append(
Action(
"close",
repo,
issue["number"],
comment=f"Clean at {clean}: no fixable HIGH or CRITICAL findings."
+ uncompared_note(imgs)
+ (f" ([scan]({url}))" if url else ""),
)
)
continue
labels = ["security"]
for stack in sorted({s for i in imgs for s in i.stacks}):
label = LABEL_FOR.get(stack, stack)
if label in known_labels:
labels.append(label)
else:
warnings.append(f"{repo}: no label `{label}` for stack {stack}; skipped")
title, body = issue_title(repo, imgs), issue_body(repo, imgs, url)
if issue is None:
actions.append(Action("create", repo, None, title, body, tuple(labels)))
continue
before = previous_ids(issue.get("body") or "")
new = sorted({f.id for i in imgs for f in i.findings or []} - before)
comment = ""
if new:
shown = ", ".join(new[:20]) + (f" and {len(new) - 20} more" if len(new) > 20 else "")
comment = f"New since the last scan: {shown}."
current = {label["name"] for label in issue.get("labels") or []}
stale = tuple(sorted((current & owned) - set(labels)))
actions.append(Action("edit", repo, issue["number"], title, body, tuple(labels), comment, stale))
for repo, issue in sorted(open_issues.items()):
if repo not in by_repo:
actions.append(
Action(
"close", repo, issue["number"], comment=f"`{repo}` is no longer pinned in any stack's compose.yaml."
)
)
return actions, warnings
def gh(*args: str, stdin: str | None = None) -> str:
proc = subprocess.run(["gh", *args], input=stdin, text=True, capture_output=True, check=False)
if proc.returncode != 0:
sys.exit(f"{RED}error:{OFF} gh {' '.join(args[:2])} failed: {proc.stderr.strip()}")
return proc.stdout
def sync(images: list[Image], dry_run: bool) -> int:
repo = repo_name()
# Every open `security` issue, matched on the marker here rather than by
# GitHub search: the marker is an HTML comment, and whether search indexes
# one is not something to depend on.
existing = json.loads(
gh(
"issue",
"list",
"-R",
repo,
"--state",
"open",
"--label",
"security",
"--limit",
"1000",
"--json",
"number,title,body,labels",
)
)
known = {label["name"] for label in json.loads(gh("label", "list", "-R", repo, "--limit", "500", "--json", "name"))}
stacks = subprocess.run(
[str(pathlib.Path(__file__).with_name("stacks.sh"))], capture_output=True, text=True, check=True
).stdout.split()
actions, warnings = plan(images, existing, known, set(stacks))
for w in warnings:
print(f"{YELLOW}warning:{OFF} {w}", file=sys.stderr)
for a in actions:
where = f"#{a.number}" if a.number else "new"
print(
f"{a.kind:6} {where:6} {a.repo}"
+ (f" [{', '.join(a.labels)}]" if a.labels else "")
+ (f" -[{', '.join(a.remove_labels)}]" if a.remove_labels else "")
)
if dry_run:
continue
# The first run opens dozens at once, and GitHub's secondary rate limit
# is on content creation per minute, not on the token's hourly budget.
time.sleep(PACE_SECONDS)
if a.kind == "create":
label_args = [arg for label in a.labels for arg in ("--label", label)]
gh("issue", "create", "-R", repo, "--title", a.title, "--body-file", "-", *label_args, stdin=a.body)
elif a.kind == "edit":
remove = ["--remove-label", ",".join(a.remove_labels)] if a.remove_labels else []
gh(
"issue",
"edit",
str(a.number),
"-R",
repo,
"--title",
a.title,
"--body-file",
"-",
"--add-label",
",".join(a.labels),
*remove,
stdin=a.body,
)
if a.comment:
gh("issue", "comment", str(a.number), "-R", repo, "--body-file", "-", stdin=a.comment)
elif a.kind == "close":
gh("issue", "close", str(a.number), "-R", repo, "--comment", a.comment)
if not actions:
print(f"{GREEN}no issue changes{OFF}")
return 0
def self_test() -> int:
failures = []
def check(name: str, cond: bool) -> None:
print(f"{GREEN}PASS{OFF} {name}" if cond else f"{RED}FAIL{OFF} {name}")
if not cond:
failures.append(name)
for ref, want in [
("caddy:2.11.4@sha256:ab", "caddy"),
("ghcr.io/immich-app/immich-server:v3.2.4@sha256:ab", "ghcr.io/immich-app/immich-server"),
("registry:5000/foo", "registry:5000/foo"),
("registry:5000/foo:1@sha256:ab", "registry:5000/foo"),
("lscr.io/linuxserver/speedtest-tracker:1.15.0", "lscr.io/linuxserver/speedtest-tracker"),
]:
check(f"repo_of({ref})", repo_of(ref) == want)
doc = {
"Results": [
{
"Vulnerabilities": [
{
"VulnerabilityID": "CVE-2026-0001",
"Severity": "CRITICAL",
"PkgName": "openssl",
"InstalledVersion": "3.0.1",
"FixedVersion": "3.0.2",
},
{
"VulnerabilityID": "CVE-2026-0002",
"Severity": "HIGH",
"PkgName": "zlib",
"InstalledVersion": "1.2",
"FixedVersion": "1.3",
},
{
"VulnerabilityID": "CVE-2026-0003",
"Severity": "HIGH",
"PkgName": "glibc",
"InstalledVersion": "2.3",
"FixedVersion": "",
},
{
"VulnerabilityID": "CVE-2026-0004",
"Severity": "MEDIUM",
"PkgName": "bash",
"InstalledVersion": "5",
"FixedVersion": "5.1",
},
]
},
# The same finding in a second target is one finding.
{
"Vulnerabilities": [
{
"VulnerabilityID": "CVE-2026-0001",
"Severity": "CRITICAL",
"PkgName": "openssl",
"InstalledVersion": "3.0.1",
"FixedVersion": "3.0.2",
},
]
},
{"Vulnerabilities": None},
]
}
parsed = parse_trivy(doc)
check(
"parse: unfixed and MEDIUM dropped, duplicates merged",
[f.id for f in parsed] == ["CVE-2026-0001", "CVE-2026-0002"],
)
check("parse: CRITICAL sorts first", parsed[0].severity == "CRITICAL")
# #985: velociraptor-server's own module, as trivy reported it, beside a
# dependency on a plain pseudo-version that must still be reported.
go = {
"Results": [
{
"Vulnerabilities": [
{
"VulnerabilityID": "CVE-2023-0242",
"Severity": "HIGH",
"PkgName": "www.velocidex.com/golang/velociraptor",
"InstalledVersion": "v0.0.0-20261004095043-7d287b0b988b+dirty",
"FixedVersion": "0.6.7-5",
},
{
"VulnerabilityID": "CVE-2026-0005",
"Severity": "HIGH",
"PkgName": "golang.org/x/crypto",
"InstalledVersion": "v0.0.0-20240101000000-abcdef123456",
"FixedVersion": "0.31.0",
},
{
"VulnerabilityID": "CVE-2026-0006",
"Severity": "HIGH",
"PkgName": "www.velocidex.com/golang/velociraptor",
"InstalledVersion": "v0.0.0-20261004095043-7d287b0b988b+dirty",
"FixedVersion": "",
},
]
}
]
}
check("parse: a +dirty main-module version is not compared", [f.id for f in parse_trivy(go)] == ["CVE-2026-0005"])
check("parse: a dependency's plain pseudo-version still is", parse_trivy(go)[0].package == "golang.org/x/crypto")
check("uncompared counts the fixable +dirty finding only", uncompared(go) == 1)
twin = dict(go["Results"][0]["Vulnerabilities"][0], InstalledVersion="v0.0.0-20250101000000-0123456789ab+dirty")
two = {"Results": [*go["Results"], {"Vulnerabilities": [twin]}]}
check("uncompared counts two dirty builds of one module twice", uncompared(two) == 2)
velo = Image("ghcr.io/velocidex/velociraptor-server:0.77.3@sha256:ff", ["soc"], parse_trivy(go), "", 1)
check("summary names what was not compared", "`ghcr.io/velocidex/velociraptor-server:0.77.3`: 1" in summary([velo]))
check("the issue body counts what was not compared", "1 finding(s)" in issue_body(velo.repo, [velo], ""))
dropped_only = Image(velo.ref, ["soc"], [], "", 1)
closed, _ = plan([dropped_only], [{"number": 985, "body": f"<!-- cve-scan: {velo.repo} -->"}], {"soc"})
check(
"a close that dropped findings says so",
closed[0].kind == "close" and "1 finding(s)" in closed[0].comment,
)
check(
"summary says nothing when nothing was dropped",
"+dirty" not in summary([Image("caddy:2@sha256:aa", ["lab"], [])]),
)
vuln = parse_trivy(doc)
labels = {"security", "sensitive", "wiki", "ci", "observability"}
images = [
Image("postgres:18.6@sha256:aa", ["sensitive"], vuln),
Image("postgres:17.11@sha256:bb", ["wiki"], []),
Image("caddy:2.11.4@sha256:cc", ["lab", "observability"], []),
Image("vaultwarden/server:1@sha256:dd", ["sensitive"], None, "timeout"),
Image("python:3.6-slim@sha256:ee", ["test"], vuln),
]
existing = [
{"number": 10, "body": "<!-- cve-scan: postgres -->\nCVE-2026-0001"},
{"number": 11, "body": "<!-- cve-scan: caddy -->"},
{"number": 12, "body": "<!-- cve-scan: vaultwarden/server -->"},
{"number": 13, "body": "<!-- cve-scan: gone/image -->"},
{"number": 14, "body": "an unrelated security issue"},
]
actions, warnings = plan(images, existing, labels)
by = {a.repo: a for a in actions}
check("postgres: one issue for two tags, edited", by["postgres"].kind == "edit" and by["postgres"].number == 10)
check(
"postgres: comment names only the new finding",
by["postgres"].comment == "New since the last scan: CVE-2026-0002.",
)
check(
"postgres: labelled by the stack with findings and the clean one",
by["postgres"].labels == ("security", "sensitive", "wiki"),
)
check("postgres: body carries the marker", "<!-- cve-scan: postgres -->" in by["postgres"].body)
check(
"postgres: only the fixable tag gets a section",
"postgres:18.6" in by["postgres"].body and "postgres:17.11" not in by["postgres"].body,
)
check("caddy: clean closes", by["caddy"].kind == "close" and by["caddy"].number == 11)
check("vaultwarden: scan error leaves the issue alone", "vaultwarden/server" not in by)
check("vaultwarden: and says so", any("vaultwarden/server" in w for w in warnings))
check("gone/image: no longer pinned closes", by["gone/image"].kind == "close" and by["gone/image"].number == 13)
check(
"python: --extra creates, labelled ci",
by["python"].kind == "create" and by["python"].labels == ("security", "ci"),
)
check("unrelated issue untouched", all(a.number != 14 for a in actions))
check("title counts", by["python"].title == "Fixable CVEs in python (1 critical, 1 high)")
actions, warnings = plan([Image("zeek/zeek:9@sha256:ff", ["sensor"], vuln)], [], labels)
check("missing label is dropped, not fatal", actions[0].labels == ("security",))
check("missing label is warned", any("`sensor`" in w for w in warnings))
actions, _ = plan(
[Image("postgres:18.6@sha256:aa", ["sensitive"], vuln)],
[{"number": 10, "body": "<!-- cve-scan: postgres -->\nCVE-2026-0001 CVE-2026-0002"}],
labels,
)
check("no new finding, no comment", actions[0].comment == "")
many = [
Finding(f"CVE-2026-{n:05}", "HIGH", "pkg" * 30, "1" * 40, "2" * 40, "", "https://x/" + "y" * 60)
for n in range(2000)
]
body = issue_body("big", [Image("big:1@sha256:aa", ["lab"], many)], "")
check("an oversized body is cut under the issue limit", len(body) <= BODY_LIMIT and "more." in body)
check("the cut body still records every finding ID", previous_ids(body) == {f.id for f in many})
actions, _ = plan([Image("big:1@sha256:aa", ["lab"], many)], [{"number": 20, "body": body}], labels | {"lab"})
check("an unchanged oversized report announces nothing new", actions[0].comment == "")
go = [
Finding("GO-2026-1234", "HIGH", "stdlib", "1.22.0", "1.22.5", "", ""),
Finding("ALAS2023-2026-999", "HIGH", "curl", "8.1", "8.2", "", ""),
]
gobody = issue_body("go", [Image("go:1@sha256:aa", ["lab"], go)], "")
actions, _ = plan([Image("go:1@sha256:aa", ["lab"], go)], [{"number": 21, "body": gobody}], labels | {"lab"})
check("non-CVE IDs round-trip: unchanged report, no comment", actions[0].comment == "")
actions, _ = plan(
[Image("go:1@sha256:aa", ["lab"], [*go, Finding("GO-2026-9999", "HIGH", "x", "1", "2", "", "")])],
[{"number": 21, "body": gobody}],
labels | {"lab"},
)
check("a new non-CVE ID is announced", actions[0].comment == "New since the last scan: GO-2026-9999.")
check(
"a corrupt ID marker falls back to the visible IDs",
previous_ids("<!-- cve-scan-ids: !!! --> CVE-2026-0001") == {"CVE-2026-0001"},
)
moved = {
"number": 22,
"body": "<!-- cve-scan: postgres -->",
"labels": [{"name": "security"}, {"name": "sensitive"}, {"name": "priority/critical"}],
}
actions, _ = plan(
[Image("postgres:17.11@sha256:bb", ["wiki"], vuln)], [moved], labels, {"sensitive", "wiki", "lab"}
)
check("a stack the image left loses its label", actions[0].remove_labels == ("sensitive",))
check("and the image's current stack is added", actions[0].labels == ("security", "wiki"))
check("labels the scanner does not own are kept", "priority/critical" not in actions[0].remove_labels)
tool = {
"number": 23,
"body": "<!-- cve-scan: example/linter -->",
"labels": [{"name": "security"}, {"name": "observability"}],
}
actions, _ = plan(
[Image("example/linter:1@sha256:cc", ["tooling"], vuln)],
[tool],
labels,
{"observability", "sensitive", "wiki"},
)
check("a lint/scan tool is labelled ci", actions[0].labels == ("security", "ci"))
check("and loses the stack that only pins it", actions[0].remove_labels == ("observability",))
text = summary(images)
check("summary lists every image, clean ones too", all(f"`{i.name}`" in text for i in images))
check("summary shows the scan error", "timeout" in text)
return 1 if failures else 0
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__.split("\n", 1)[0])
ap.add_argument("--dir", type=pathlib.Path, help="scan-images.sh --out directory")
ap.add_argument("--sync-issues", action="store_true", help="create, edit and close issues via gh")
ap.add_argument("--dry-run", action="store_true", help="with --sync-issues: print, change nothing")
ap.add_argument("--self-test", action="store_true")
args = ap.parse_args()
if args.self_test:
return self_test()
if args.dir is None:
ap.error("--dir is required")
images = load(args.dir)
if args.sync_issues:
return sync(images, args.dry_run)
sys.stdout.write(summary(images))
return 0
if __name__ == "__main__":
sys.exit(main())