gate: govulncheck as its own CI step, with dated exceptions
From the Go audit (#1123), batch 2: - make govulncheck runs govulncheck v1.8.0 (pinned, go run) in JSON over the root module and launchctx; .amadan/ci.d/50-govulncheck runs it. - scripts/govulncheck-gate fails on any called finding not listed in .amadan/govulncheck-exceptions.tsv, on an expired or stale exception, and on anything that is not one whole source-mode symbol scan per module. Imported-only and required-only findings print, not fatal. - One exception: GO-2026-6859 under the go1.26.10 pin (#897, #904), expiring 2027-01-31. - Linux on the runner's architecture only for now; README says so. Reviewed by Astra (round 1, READY AFTER FIXES; the P2 fixed in 1e20fe90). Outside the census. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from audit-govulncheck into main.
9 files changed,
+812
−2
.amadan/README.md+36 −1.amadan/ci.d/50-govulncheck+25 −0.amadan/govulncheck-exceptions.tsv+9 −0AGENTS.md+7 −0Makefile+28 −1docs/superpowers/reviews/FINDINGS-audit-govulncheck-R1.md+32 −0docs/superpowers/reviews/REVIEW-BRIEF-audit-govulncheck.md+35 −0scripts/govulncheck-gate/main.go+416 −0scripts/govulncheck-gate/main_test.go+224 −0
CI passed — run details
diff --git a/.amadan/README.md b/.amadan/README.md| index 3406166fc..70ba2d65b 100644 |
| --- a/.amadan/README.md |
| +++ b/.amadan/README.md |
| @@ -33,7 +33,7 @@ no longer exists. |
| | `minio-contract` | `30-minio-contract` | MinIO on loopback, no container | |
| | `gofmt` | `10-gate` | repo-wide, via `make test` — see below | |
| -Two checks have no ancestor; the deleted workflow never ran them. They |
| +Three checks have no ancestor; the deleted workflow never ran them. They |
| were added on 2026-11-16 after a code audit found the gate could not see |
| them. |
| @@ -41,6 +41,41 @@ them. |
| |---|---|---| |
| | tagged vet | `10-gate` | `go vet -tags rootrunner,carlos_cutover_witness ./...`, via `make test`: the root-runner tests are type-checked, not run | |
| | `staticcheck` | `40-staticcheck` | `make staticcheck`: v0.8.2, bug checks only; `staticcheck.conf` names the families still off | |
| +| `govulncheck` | `50-govulncheck` | `make govulncheck`: v1.8.0 over this module and `launchctx`; fails only on vulnerabilities the code calls, less the exceptions file; see below | |
| + |
| +## `govulncheck`: what turns it red |
| + |
| +govulncheck sorts each known vulnerability by how close the code gets to |
| +it: **called** (a call path reaches the vulnerable function), **imported** |
| +(the package is imported but nothing reaches the function) or |
| +**required** (the module is in `go.mod` but the package is not |
| +imported). Only **called** findings can fail the step. The other two are |
| +printed, so they stay visible, but they never turn the step red. |
| + |
| +govulncheck has no way to accept a finding, so it runs with |
| +`-format json` and `scripts/govulncheck-gate` judges the output against |
| +`.amadan/govulncheck-exceptions.tsv`: one line per OSV id, with an |
| +expiry date and the reason. The step fails when: |
| + |
| +- a called finding is not in the file: upgrade, or add an exception; |
| +- an exception is past its expiry date: re-check it, then fix or renew; |
| +- an exception matches no called finding: remove it, so the file cannot |
| + rot; |
| +- either scan fails to run, or its output is not one whole source-mode |
| + scan (protocol v1.0.0, a config first, an SBOM naming what it scanned). |
| + |
| +The one exception today is GO-2026-6859 in `net/http`, fixed in |
| +go1.26.11. CI stays on go1.26.10 because 1.26.11 deadlocks the edge |
| +(#897, `go-toolchain.sh`), and #904 found 6859 unreachable in practice. |
| +When the toolchain moves past the fix, the exception goes stale and the |
| +step says to remove it. |
| + |
| +It scans release code only (no `-test`) for the runner's own platform. |
| +It needs the network: the Go module proxy to fetch govulncheck (every |
| +step already reaches it, because amadan jobs share the worker's network |
| +rather than getting `--unshare-net`) and `vuln.go.dev` for the database, |
| +which is fetched fresh on every run. If either is unreachable the step |
| +fails; it does not pass on an empty answer. |
| ## `gofmt`: ported by widening it, not by reproducing it |
diff --git a/.amadan/ci.d/50-govulncheck b/.amadan/ci.d/50-govulncheck| new file mode 100755 |
| index 000000000..0586c5ffc |
| --- /dev/null |
| +++ b/.amadan/ci.d/50-govulncheck |
| @@ -0,0 +1,25 @@ |
| +#!/bin/sh |
| +# Known vulnerabilities (vuln.go.dev) that the code actually calls: |
| +# govulncheck v1.8.0, pinned, over this module and launchctx's. |
| +# `make govulncheck` is the one definition; this step only runs it, so a |
| +# developer gets the same answer with the same command. |
| +# |
| +# Its own step, not part of `make test`, for staticcheck's reasons: a red |
| +# here is a finding, not a failed test, and `make test` stays no slower. |
| +# |
| +# Red means one of: a called vulnerability that |
| +# .amadan/govulncheck-exceptions.tsv does not list (upgrade, or add an |
| +# exception with an expiry and a reason); an exception past its expiry; an |
| +# exception nothing calls any more (remove it); or a scan that could not |
| +# run. Imported-only and required-only findings are printed, never fatal. |
| +# |
| +# The stdlib's findings follow the toolchain sourced below. Moving it to a |
| +# Go that fixes an excepted vulnerability makes that exception stale, and |
| +# the step then says to remove it. |
| +# |
| +# Network: the Go module proxy, as every step already uses, and |
| +# vuln.go.dev, fetched fresh on every run. |
| +set -eu |
| +. .amadan/go-toolchain.sh |
| + |
| +make govulncheck |
diff --git a/.amadan/govulncheck-exceptions.tsv b/.amadan/govulncheck-exceptions.tsv| new file mode 100644 |
| index 000000000..8d50ebab4 |
| --- /dev/null |
| +++ b/.amadan/govulncheck-exceptions.tsv |
| @@ -0,0 +1,9 @@ |
| +# Vulnerabilities `make govulncheck` (.amadan/ci.d/50-govulncheck) lets |
| +# through although the code calls them. One per line, tab-separated: |
| +# |
| +# OSV id <TAB> expires (YYYY-MM-DD, valid through that day) <TAB> reason |
| +# |
| +# The step fails once an exception expires, and fails when one no longer |
| +# matches a called finding (remove it then). Lines starting with # are |
| +# comments. scripts/govulncheck-gate reads this file. |
| +GO-2026-6859 2027-01-31 net/http MaxBytesReader header race, fixed in go1.26.11. CI and releases stay on go1.26.10 because 1.26.11 deadlocks the edge's Expect: 100-continue path (#897, .amadan/go-toolchain.sh); #904 checked every production MaxBytesReader site and found it unreachable. |
diff --git a/AGENTS.md b/AGENTS.md| index 3ed0091e9..022fca76c 100644 |
| --- a/AGENTS.md |
| +++ b/AGENTS.md |
| @@ -288,6 +288,13 @@ anything. |
| `make test` also vets with `-tags rootrunner,carlos_cutover_witness`, |
| so the root-runner tests must compile even though only the root runner |
| runs them. |
| +- `50-govulncheck` runs `make govulncheck` (v1.8.0, pinned) over this |
| + module and `launchctx`. It fails on a vulnerability the code calls |
| + unless `.amadan/govulncheck-exceptions.tsv` lists it (id, expiry, |
| + reason), and on an exception that has expired or no longer matches |
| + anything. Imported-only and required-only findings are printed, not |
| + fatal. It needs `vuln.go.dev` and the module proxy; `.amadan/README.md` |
| + has the details. |
| - `20-cross@5` adds `GOOS=darwin`/`windows`/`linux-arm64` compiles, one |
| per shard. **The gate is not Linux-only**: `internal/host` is linked |
| into the CLI, which people run on Macs, so a Linux-only type assumption |
diff --git a/Makefile b/Makefile| index cd6258f01..e72155ec7 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -7,7 +7,7 @@ LDFLAGS := -s -w -X github.com/carlosframework/platform/internal/version.Build=$ |
| GOOS ?= |
| GOARCH ?= |
| -.PHONY: test build build-console fmt staticcheck census-card census-propose console-mockup-kit parity-doc |
| +.PHONY: test build build-console fmt staticcheck govulncheck census-card census-propose console-mockup-kit parity-doc |
| # The build gate: vet, format, tests. `.amadan/ci.d/10-gate` runs this same |
| # target, so what CI checks and what you check are one definition. |
| @@ -72,6 +72,33 @@ staticcheck: |
| if [ $$rc -ne 0 ] || [ -n "$$out" ]; then echo "staticcheck: findings above (staticcheck.conf says which checks run)" >&2; exit 1; fi; \ |
| echo "staticcheck: clean" |
| +# Known vulnerabilities the code calls, as CI checks them |
| +# (.amadan/ci.d/50-govulncheck). Not part of `make test`, like staticcheck, |
| +# and pinned and fetched with `go run pkg@version` for the same reason (no |
| +# tool line in go.mod). |
| +# |
| +# Both modules the gate tests: this one and launchctx, which has its own |
| +# go.mod. Release code only (no -test), for the platform make runs on. |
| +# govulncheck cannot accept a finding, so it writes JSON and |
| +# scripts/govulncheck-gate judges it: a called finding fails unless |
| +# .amadan/govulncheck-exceptions.tsv lists it, and an expired or unused |
| +# exception fails too. Imported-only and required-only findings are |
| +# printed, not fatal. In JSON mode govulncheck exits 0 whatever it finds, |
| +# so a non-zero exit is a scan that failed (no vuln.go.dev, no module |
| +# proxy) and fails the target. |
| +# |
| +# Keep the `&&` chain: the checker must only see output from scans that |
| +# exited 0. The JSON has no end marker, so a scan that crashes after its |
| +# SBOM leaves a stream that looks complete, and only the exit status |
| +# tells the two apart. The checker rejects everything else that is not |
| +# a whole source scan. |
| +GOVULNCHECK := golang.org/x/vuln/cmd/govulncheck@v1.8.0 |
| +govulncheck: |
| + @dir=$$(mktemp -d) || exit 1; trap 'rm -rf "$$dir"' EXIT; \ |
| + go run $(GOVULNCHECK) -format json ./... >"$$dir/platform.json" && \ |
| + (cd launchctx && go run $(GOVULNCHECK) -format json ./...) >"$$dir/launchctx.json" && \ |
| + go run ./scripts/govulncheck-gate -exceptions .amadan/govulncheck-exceptions.tsv "$$dir/platform.json" "$$dir/launchctx.json" |
| + |
| # Regenerates docs/console-mockup-kit/shell.html: a signed-in console page |
| # from the real templates with every stylesheet inlined and an empty |
| # <main> to draw a mockup in (docs/console-mockup-kit/README.md). |
diff --git a/docs/superpowers/reviews/FINDINGS-audit-govulncheck-R1.md b/docs/superpowers/reviews/FINDINGS-audit-govulncheck-R1.md| new file mode 100644 |
| index 000000000..f5e7e9212 |
| --- /dev/null |
| +++ b/docs/superpowers/reviews/FINDINGS-audit-govulncheck-R1.md |
| @@ -0,0 +1,32 @@ |
| +Reviewed `origin/main...efec794771b8c364aa701bb1e6470df1955560ef`, round 1 of 3. The difference from the implementation commit named in the brief is only the brief itself. |
| + |
| +No P1 findings. |
| + |
| +**P2 — Reject config-only and wrong-mode streams.** [main.go:181](/home/paulca/amadan.net/carlos/platform-audit-govulncheck/scripts/govulncheck-gate/main.go:181), [main.go:219](/home/paulca/amadan.net/carlos/platform-audit-govulncheck/scripts/govulncheck-gate/main.go:219). Fault family: only valid symbol-level source scans may contribute to a green verdict. |
| + |
| +The only required evidence of a scan is a config message containing `"scan_level":"symbol"`. Consequently: |
| + |
| +- `{"config":{"scan_level":"symbol"}}` is accepted despite containing no scan results or module inventory. |
| +- Actual govulncheck query-mode output is accepted: v1.8.0 reports `scan_level: symbol` in query mode but emits OSV records without analyzing calls. |
| +- Missing or unsupported protocol versions are accepted. |
| +- Truncation between complete JSON messages is accepted, although truncation inside a message is rejected. |
| + |
| +With an empty exceptions file, these inputs can produce “clean.” With the checked-in exception, a valid first scan satisfying that exception can mask a config-only second scan. |
| + |
| +**Fix:** validate the supported protocol version and source scan mode, require a single leading config and an SBOM identifying scanned modules and package roots, and add rejection tests. [main_test.go:152](/home/paulca/amadan.net/carlos/platform-audit-govulncheck/scripts/govulncheck-gate/main_test.go:152) currently covers only empty input, garbage and package-level scans. |
| + |
| +Keep the successful-process-exit requirement in the Makefile: govulncheck’s JSON protocol has no completion marker, so structural validation alone cannot detect every truncation between messages. The current `&&` chain already prevents ordinary scanner crashes and network failures from reaching the checker; this finding does **not** establish a bypass of that protection. |
| + |
| +The remaining checks were satisfactory on inspection: |
| + |
| +- Called findings retain their highest observed level across scans. Unexcepted calls fail; expired and stale exceptions fail independently. Expiry is inclusive through the stated UTC day. |
| +- Both modules are scanned using pinned govulncheck v1.8.0. Default Go caches remain untouched. A file-free `dash` harness confirmed that failures in either scan or the checker preserve failure status through cleanup, and temporary-directory creation failure stops execution. |
| +- GO-2026-6859’s exception and 2027-01-31 expiry are consistent with the recorded go1.26.10 pin and the justification in `.amadan/go-toolchain.sh`. I did not independently repeat #904’s production call-site audit. |
| +- Runner-only platform coverage is acceptable for this first version because it is explicitly documented. Darwin, Windows and the other Linux architecture remain useful follow-up work; cross-compilation does not provide vulnerability coverage. |
| +- README and AGENTS.md otherwise match the implementation. Existing policy tests assert specific outcomes and diagnostics, rather than accepting any error indiscriminately. |
| + |
| +`git diff --check`, formatting checks on both new Go files, and shell syntax validation passed. The focused Go test command could not start because the read-only sandbox denied creation of its temporary build directory. No full suites or live vulnerability scans ran. No files were changed. |
| + |
| +With no P1 findings, apply the P2 fix without reopening review, per the stopping rule. |
| + |
| +READY AFTER FIXES |
| \ No newline at end of file |
diff --git a/docs/superpowers/reviews/REVIEW-BRIEF-audit-govulncheck.md b/docs/superpowers/reviews/REVIEW-BRIEF-audit-govulncheck.md| new file mode 100644 |
| index 000000000..f3a145218 |
| --- /dev/null |
| +++ b/docs/superpowers/reviews/REVIEW-BRIEF-audit-govulncheck.md |
| @@ -0,0 +1,35 @@ |
| +# Review brief: branch audit-govulncheck, round 1 of 3 |
| + |
| +Repo carlos/platform, branch `audit-govulncheck`, commit 4b961a7b9bee11f61fdb79fdcd1835c82c665cc4. |
| +Under review: the diff `origin/main...4b961a7b9bee11f61fdb79fdcd1835c82c665cc4` (base c4b44c34). |
| +Authorities: AGENTS.md on main ("Build & test", "Stopping rules"), |
| +.amadan/README.md. Context: amadan #1123 batch 2. Outside the census |
| +(scripts/, gate config). |
| + |
| +## What to check |
| + |
| +1. `scripts/govulncheck-gate`: does it fail on every called finding not |
| + excepted, on an expired exception and on a stale one, and refuse input |
| + that is not a symbol-level govulncheck JSON scan? Can a broken or empty |
| + scan ever pass (network down, db unreachable, wrong mode, crash midway, |
| + truncated JSON)? |
| +2. The Makefile target: both modules scanned; exit statuses preserved |
| + through the temp dir and cleanup (dash); pinned version; default caches. |
| +3. `.amadan/govulncheck-exceptions.tsv`: GO-2026-6859 under the 1.26.10 pin |
| + (read .amadan/go-toolchain.sh); expiry and reason sensible. |
| +4. Scope: linux only on the runner's arch; is leaving darwin/windows out |
| + acceptable for a first version (the CLI ships for them)? |
| +5. README and AGENTS.md text matches the scripts. |
| +6. Tests: would each fail against a checker that got that case wrong? |
| + |
| +## Severity |
| + |
| +- **P1**: unsafe, would not compile, a test fails, or it would not work. |
| +- **P2**: wrong or missing. Fixed without reopening this review. |
| +- **P3**: clarity or polish. Follow-up list only. |
| + |
| +## Output |
| + |
| +Findings ranked P1, P2, P3 with file:line, failure scenario and fix. End |
| +with exactly one verdict: READY, READY AFTER FIXES, or NOT READY. Do not |
| +edit files, spawn reviewers or run full suites. |
diff --git a/scripts/govulncheck-gate/main.go b/scripts/govulncheck-gate/main.go| new file mode 100644 |
| index 000000000..7681c9285 |
| --- /dev/null |
| +++ b/scripts/govulncheck-gate/main.go |
| @@ -0,0 +1,416 @@ |
| +// Command govulncheck-gate reads govulncheck's -format json output and |
| +// decides whether CI's 50-govulncheck step passes. govulncheck itself has |
| +// no way to accept a finding, so the exceptions live in a file here |
| +// (.amadan/govulncheck-exceptions.tsv) and this program applies them. |
| +// |
| +// The rules, in order of how often they matter: |
| +// |
| +// - A finding the code actually calls (govulncheck's symbol level) fails |
| +// the step unless its OSV id is in the exceptions file. |
| +// - Findings the code only imports, or only requires through go.mod, are |
| +// printed and do not fail: govulncheck found no call path to them. |
| +// - An exception past its expiry date fails, so a "for now" cannot |
| +// quietly become forever. |
| +// - An exception that matches no called finding fails too: it is stale |
| +// and should be removed, so the file cannot rot. |
| +// |
| +// Usage: |
| +// |
| +// govulncheck-gate -exceptions FILE SCAN.json [SCAN.json ...] |
| +// |
| +// Each SCAN.json is one `govulncheck -format json` run (one per module). |
| +// The findings of all of them are judged together, so an exception is |
| +// stale only when no scan reaches it. |
| +package main |
| + |
| +import ( |
| + "bufio" |
| + "encoding/json" |
| + "errors" |
| + "flag" |
| + "fmt" |
| + "io" |
| + "os" |
| + "regexp" |
| + "sort" |
| + "strings" |
| + "time" |
| +) |
| + |
| +// level is how close govulncheck says the code gets to a vulnerability. |
| +type level int |
| + |
| +const ( |
| + levelModule level = iota + 1 // a required module has it; no import |
| + levelPackage // a vulnerable package is imported; no call |
| + levelSymbol // a vulnerable function is called |
| +) |
| + |
| +func (l level) String() string { |
| + switch l { |
| + case levelSymbol: |
| + return "called" |
| + case levelPackage: |
| + return "imported" |
| + default: |
| + return "required" |
| + } |
| +} |
| + |
| +// exception is one line of the exceptions file. |
| +type exception struct { |
| + id string |
| + expires time.Time // the last day it is valid, inclusive |
| + reason string |
| + line int |
| +} |
| + |
| +var osvID = regexp.MustCompile(`^GO-[0-9]{4}-[0-9]{4,}$`) |
| + |
| +// parseExceptions reads the exceptions file: one tab-separated |
| +// "ID<TAB>EXPIRES<TAB>REASON" line per exception, EXPIRES as YYYY-MM-DD. |
| +// Blank lines and lines starting with # are skipped. |
| +func parseExceptions(r io.Reader) ([]exception, error) { |
| + var out []exception |
| + seen := map[string]int{} |
| + sc := bufio.NewScanner(r) |
| + n := 0 |
| + for sc.Scan() { |
| + n++ |
| + text := strings.TrimRight(sc.Text(), "\r") |
| + if strings.TrimSpace(text) == "" || strings.HasPrefix(text, "#") { |
| + continue |
| + } |
| + fields := strings.Split(text, "\t") |
| + if len(fields) != 3 { |
| + return nil, fmt.Errorf("line %d: want ID<TAB>EXPIRES<TAB>REASON, got %d field(s)", n, len(fields)) |
| + } |
| + id, date, reason := strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1]), strings.TrimSpace(fields[2]) |
| + if !osvID.MatchString(id) { |
| + return nil, fmt.Errorf("line %d: %q is not a Go vulnerability id (GO-YYYY-NNNN)", n, id) |
| + } |
| + expires, err := time.Parse(time.DateOnly, date) |
| + if err != nil { |
| + return nil, fmt.Errorf("line %d: expiry %q is not YYYY-MM-DD", n, date) |
| + } |
| + if reason == "" { |
| + return nil, fmt.Errorf("line %d: %s has no reason", n, id) |
| + } |
| + if prev, ok := seen[id]; ok { |
| + return nil, fmt.Errorf("line %d: %s is already listed on line %d", n, id, prev) |
| + } |
| + seen[id] = n |
| + out = append(out, exception{id: id, expires: expires, reason: reason, line: n}) |
| + } |
| + return out, sc.Err() |
| +} |
| + |
| +// The parts of govulncheck's JSON protocol (v1.0.0) this program reads. |
| +type message struct { |
| + Config *struct { |
| + ProtocolVersion string `json:"protocol_version"` |
| + ScannerVersion string `json:"scanner_version"` |
| + GoVersion string `json:"go_version"` |
| + ScanLevel string `json:"scan_level"` |
| + ScanMode string `json:"scan_mode"` |
| + } `json:"config"` |
| + SBOM *struct { |
| + Modules []struct { |
| + Path string `json:"path"` |
| + } `json:"modules"` |
| + Roots []string `json:"roots"` |
| + } `json:"SBOM"` |
| + OSV *struct { |
| + ID string `json:"id"` |
| + Summary string `json:"summary"` |
| + } `json:"osv"` |
| + Finding *struct { |
| + OSV string `json:"osv"` |
| + FixedVersion string `json:"fixed_version"` |
| + Trace []frame `json:"trace"` |
| + } `json:"finding"` |
| +} |
| + |
| +type frame struct { |
| + Module string `json:"module"` |
| + Version string `json:"version"` |
| + Package string `json:"package"` |
| + Function string `json:"function"` |
| + Receiver string `json:"receiver"` |
| + Position *struct { |
| + Filename string `json:"filename"` |
| + Line int `json:"line"` |
| + } `json:"position"` |
| +} |
| + |
| +// symbol renders a frame as pkg.Func or pkg.(*T).Func. |
| +func (f frame) symbol() string { |
| + if f.Receiver != "" { |
| + return fmt.Sprintf("%s.(%s).%s", f.Package, f.Receiver, f.Function) |
| + } |
| + return f.Package + "." + f.Function |
| +} |
| + |
| +// vuln is everything the scans said about one OSV id. |
| +type vuln struct { |
| + id string |
| + summary string |
| + level level |
| + module string // the vulnerable module, with its version |
| + fixed string |
| + sites map[string]bool // for called findings: where our code calls in |
| +} |
| + |
| +// protocolVersion is the JSON protocol govulncheck v1.8.0 writes. A |
| +// govulncheck that writes another one is a pin bump that must come with a |
| +// look at this program. |
| +const protocolVersion = "v1.0.0" |
| + |
| +// readScan adds one govulncheck JSON stream's findings to vulns and |
| +// returns the scanned module's path. |
| +// |
| +// It accepts only what `govulncheck -format json ./...` writes for a |
| +// source scan: one config message first, naming protocolVersion, source |
| +// mode and symbol level, and one SBOM naming the scanned modules and |
| +// package roots. Anything else could pass while having looked at nothing: |
| +// a package- or module-level scan, or query mode (which says "symbol" but |
| +// analyses no calls), reports no calls at all; a stream cut off before |
| +// its SBOM never scanned anything. |
| +// |
| +// The protocol has no end marker, so a stream cut off after the SBOM |
| +// cannot be told from a complete one. govulncheck's exit status catches |
| +// that, which is why the Makefile only runs this after both scans exit 0. |
| +func readScan(r io.Reader, vulns map[string]*vuln) (string, error) { |
| + dec := json.NewDecoder(r) |
| + var mod string |
| + configs, sboms, n := 0, 0, 0 |
| + summaries := map[string]string{} |
| + for { |
| + var m message |
| + err := dec.Decode(&m) |
| + if errors.Is(err, io.EOF) { |
| + break |
| + } |
| + if err != nil { |
| + return "", fmt.Errorf("decoding govulncheck output: %w", err) |
| + } |
| + n++ |
| + if n == 1 && m.Config == nil { |
| + return "", errors.New("the first message is not a config: this is not govulncheck -format json output") |
| + } |
| + switch { |
| + case m.Config != nil: |
| + configs++ |
| + if configs > 1 { |
| + return "", errors.New("more than one config message: one scan per file") |
| + } |
| + c := m.Config |
| + if c.ProtocolVersion != protocolVersion { |
| + return "", fmt.Errorf("protocol version is %q, want %q (the one govulncheck v1.8.0 writes)", c.ProtocolVersion, protocolVersion) |
| + } |
| + if c.ScanMode != "source" { |
| + return "", fmt.Errorf("scan mode is %q, want \"source\" (do not pass -mode to govulncheck)", c.ScanMode) |
| + } |
| + if c.ScanLevel != "symbol" { |
| + return "", fmt.Errorf("scan level is %q, want \"symbol\" (do not pass -scan to govulncheck)", c.ScanLevel) |
| + } |
| + case m.SBOM != nil: |
| + sboms++ |
| + if sboms > 1 { |
| + return "", errors.New("more than one SBOM message: one scan per file") |
| + } |
| + if len(m.SBOM.Modules) == 0 || m.SBOM.Modules[0].Path == "" { |
| + return "", errors.New("the SBOM names no scanned module") |
| + } |
| + if len(m.SBOM.Roots) == 0 { |
| + return "", errors.New("the SBOM names no package roots: nothing was scanned") |
| + } |
| + mod = m.SBOM.Modules[0].Path |
| + case m.OSV != nil: |
| + summaries[m.OSV.ID] = m.OSV.Summary |
| + case m.Finding != nil: |
| + f := m.Finding |
| + if len(f.Trace) == 0 { |
| + return "", fmt.Errorf("finding %s has no trace", f.OSV) |
| + } |
| + v := vulns[f.OSV] |
| + if v == nil { |
| + top := f.Trace[0] |
| + v = &vuln{id: f.OSV, module: top.Module, fixed: f.FixedVersion, sites: map[string]bool{}} |
| + if top.Version != "" { |
| + v.module += "@" + top.Version |
| + } |
| + vulns[f.OSV] = v |
| + } |
| + lv := levelModule |
| + switch top := f.Trace[0]; { |
| + case top.Function != "": |
| + lv = levelSymbol |
| + case top.Package != "": |
| + lv = levelPackage |
| + } |
| + if lv > v.level { |
| + v.level = lv |
| + } |
| + if lv == levelSymbol { |
| + v.sites[callSite(f.Trace)] = true |
| + } |
| + } |
| + } |
| + if configs == 0 { |
| + return "", errors.New("no config message: this is not govulncheck -format json output") |
| + } |
| + if sboms == 0 { |
| + return "", errors.New("no SBOM message: the scan ended before it named what it scanned, or this is not a source scan") |
| + } |
| + for id, s := range summaries { |
| + if v := vulns[id]; v != nil && v.summary == "" { |
| + v.summary = s |
| + } |
| + } |
| + return mod, nil |
| +} |
| + |
| +// callSite describes where a called finding enters from our code: the |
| +// last frame of the trace is ours, the first is the vulnerable symbol. |
| +func callSite(trace []frame) string { |
| + entry, vuln := trace[len(trace)-1], trace[0] |
| + where := entry.symbol() |
| + if entry.Position != nil { |
| + where = fmt.Sprintf("%s:%d: %s", entry.Position.Filename, entry.Position.Line, where) |
| + } |
| + return where + " reaches " + vuln.symbol() |
| +} |
| + |
| +// maxSites is how many call sites to print for a finding that fails. |
| +const maxSites = 10 |
| + |
| +// judge prints a report of vulns against the exceptions to w and returns |
| +// the problems that fail the step (none means green). today is a date in |
| +// UTC; an exception is valid through its expiry date. |
| +func judge(w io.Writer, vulns map[string]*vuln, excs []exception, today time.Time, excFile string) []string { |
| + byID := map[string]exception{} |
| + for _, e := range excs { |
| + byID[e.id] = e |
| + } |
| + ids := make([]string, 0, len(vulns)) |
| + for id := range vulns { |
| + ids = append(ids, id) |
| + } |
| + // Called first, then imported, then required; by id within a level. |
| + sort.Slice(ids, func(i, j int) bool { |
| + a, b := vulns[ids[i]], vulns[ids[j]] |
| + if a.level != b.level { |
| + return a.level > b.level |
| + } |
| + return a.id < b.id |
| + }) |
| + |
| + var problems []string |
| + for _, id := range ids { |
| + v := vulns[id] |
| + fmt.Fprintf(w, "%s %s %s, fixed in %s\n", v.id, v.level, v.module, orNone(v.fixed)) |
| + if v.summary != "" { |
| + fmt.Fprintf(w, " %s\n", v.summary) |
| + } |
| + if v.level != levelSymbol { |
| + fmt.Fprintf(w, " not fatal: no call path to the vulnerable code\n") |
| + continue |
| + } |
| + e, ok := byID[id] |
| + if ok { |
| + fmt.Fprintf(w, " excepted until %s (%s:%d): %s\n", e.expires.Format(time.DateOnly), excFile, e.line, e.reason) |
| + continue |
| + } |
| + sites := make([]string, 0, len(v.sites)) |
| + for s := range v.sites { |
| + sites = append(sites, s) |
| + } |
| + sort.Strings(sites) |
| + for i, s := range sites { |
| + if i == maxSites { |
| + fmt.Fprintf(w, " ... and %d more call path(s)\n", len(sites)-maxSites) |
| + break |
| + } |
| + fmt.Fprintf(w, " %s\n", s) |
| + } |
| + problems = append(problems, fmt.Sprintf("%s is called and not in %s: upgrade %s, or add an exception with an expiry and a reason", id, excFile, v.module)) |
| + } |
| + |
| + for _, e := range excs { |
| + if v := vulns[e.id]; v == nil || v.level != levelSymbol { |
| + problems = append(problems, fmt.Sprintf("%s:%d: %s is excepted but no scan calls it any more: remove the exception", excFile, e.line, e.id)) |
| + } |
| + if today.After(e.expires) { |
| + problems = append(problems, fmt.Sprintf("%s:%d: the exception for %s expired on %s: re-check it, then fix the finding or renew the exception with a new date and reason", excFile, e.line, e.id, e.expires.Format(time.DateOnly))) |
| + } |
| + } |
| + return problems |
| +} |
| + |
| +func orNone(s string) string { |
| + if s == "" { |
| + return "no release yet" |
| + } |
| + return s |
| +} |
| + |
| +func main() { |
| + excFile := flag.String("exceptions", ".amadan/govulncheck-exceptions.tsv", "the exceptions file") |
| + flag.Usage = func() { |
| + fmt.Fprintln(os.Stderr, "usage: govulncheck-gate [-exceptions FILE] SCAN.json [SCAN.json ...]") |
| + flag.PrintDefaults() |
| + } |
| + flag.Parse() |
| + if flag.NArg() == 0 { |
| + flag.Usage() |
| + os.Exit(2) |
| + } |
| + if err := run(os.Stdout, *excFile, flag.Args(), time.Now().UTC()); err != nil { |
| + fmt.Fprintln(os.Stderr, "govulncheck:", err) |
| + os.Exit(1) |
| + } |
| +} |
| + |
| +// errFindings is returned when the report itself has listed the problems. |
| +var errFindings = errors.New("failed; see above") |
| + |
| +func run(w io.Writer, excFile string, scans []string, now time.Time) error { |
| + f, err := os.Open(excFile) |
| + if err != nil { |
| + return err |
| + } |
| + excs, err := parseExceptions(f) |
| + f.Close() |
| + if err != nil { |
| + return fmt.Errorf("%s: %w", excFile, err) |
| + } |
| + |
| + vulns := map[string]*vuln{} |
| + var mods []string |
| + for _, path := range scans { |
| + f, err := os.Open(path) |
| + if err != nil { |
| + return err |
| + } |
| + mod, err := readScan(f, vulns) |
| + f.Close() |
| + if err != nil { |
| + return fmt.Errorf("%s: %w", path, err) |
| + } |
| + mods = append(mods, mod) |
| + } |
| + fmt.Fprintf(w, "govulncheck: scanned %s\n", strings.Join(mods, ", ")) |
| + |
| + today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) |
| + problems := judge(w, vulns, excs, today, excFile) |
| + if len(problems) > 0 { |
| + fmt.Fprintln(w) |
| + for _, p := range problems { |
| + fmt.Fprintln(w, "FAIL:", p) |
| + } |
| + return errFindings |
| + } |
| + fmt.Fprintf(w, "govulncheck: clean (%d finding(s), none called outside %s)\n", len(vulns), excFile) |
| + return nil |
| +} |
diff --git a/scripts/govulncheck-gate/main_test.go b/scripts/govulncheck-gate/main_test.go| new file mode 100644 |
| index 000000000..7bde4c531 |
| --- /dev/null |
| +++ b/scripts/govulncheck-gate/main_test.go |
| @@ -0,0 +1,224 @@ |
| +package main |
| + |
| +import ( |
| + "fmt" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// The scan below is shaped like real govulncheck v1.8.0 -format json |
| +// output: a config, an SBOM, OSV records, then findings whose first frame |
| +// is the vulnerable symbol (module level: no package; package level: no |
| +// function) and whose last frame is the caller in our code. |
| +const configMsg = `{"config":{"protocol_version":"v1.0.0","scanner_name":"govulncheck","scanner_version":"v1.8.0","go_version":"go1.26.10","scan_level":"symbol","scan_mode":"source"}}` + "\n" |
| + |
| +const scanHead = configMsg + `{"SBOM":{"go_version":"go1.26.10","modules":[{"path":"example.com/shop"},{"path":"stdlib","version":"v1.26.10"}],"roots":["example.com/shop/web"]}} |
| +{"progress":{"message":"Checking the code against the vulnerabilities..."}} |
| +` |
| + |
| +func osvMsg(id string) string { |
| + return fmt.Sprintf(`{"osv":{"id":%q,"summary":"summary of %s"}}`+"\n", id, id) |
| +} |
| + |
| +func calledMsg(id string) string { |
| + return fmt.Sprintf(`{"finding":{"osv":%q,"fixed_version":"v1.26.11","trace":[ |
| +{"module":"stdlib","version":"v1.26.10","package":"net/http","function":"Read","receiver":"*body","position":{"filename":"src/net/http/transfer.go","line":838}}, |
| +{"module":"example.com/shop","package":"example.com/shop/web","function":"Serve","position":{"filename":"web/serve.go","line":12}}]}}`+"\n", id) |
| +} |
| + |
| +func requiredMsg(id string) string { |
| + return fmt.Sprintf(`{"finding":{"osv":%q,"fixed_version":"v0.58.1","trace":[{"module":"golang.org/x/crypto","version":"v0.58.0"}]}}`+"\n", id) |
| +} |
| + |
| +func importedMsg(id string) string { |
| + return fmt.Sprintf(`{"finding":{"osv":%q,"fixed_version":"v0.58.1","trace":[{"module":"golang.org/x/crypto","version":"v0.58.0","package":"golang.org/x/crypto/openpgp"}]}}`+"\n", id) |
| +} |
| + |
| +var today = time.Date(2026, 11, 16, 0, 0, 0, 0, time.UTC) |
| + |
| +// gate writes the exceptions and scans to files and runs the gate on them. |
| +func gate(t *testing.T, exceptions string, scans ...string) (string, error) { |
| + t.Helper() |
| + dir := t.TempDir() |
| + exc := filepath.Join(dir, "exceptions.tsv") |
| + if err := os.WriteFile(exc, []byte(exceptions), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + var paths []string |
| + for i, s := range scans { |
| + p := filepath.Join(dir, fmt.Sprintf("scan%d.json", i)) |
| + if err := os.WriteFile(p, []byte(s), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + paths = append(paths, p) |
| + } |
| + var out strings.Builder |
| + err := run(&out, exc, paths, today) |
| + return out.String(), err |
| +} |
| + |
| +const exc6859 = "# id\texpires\treason\nGO-2026-6859\t2027-01-31\tpinned to go1.26.10 by #897; #904 found it unreachable\n" |
| + |
| +func TestUnknownCalledFindingFails(t *testing.T) { |
| + out, err := gate(t, exc6859, scanHead+osvMsg("GO-2026-6859")+calledMsg("GO-2026-6859")+osvMsg("GO-2026-7000")+calledMsg("GO-2026-7000")) |
| + if err == nil { |
| + t.Fatalf("an unlisted called finding passed:\n%s", out) |
| + } |
| + if !strings.Contains(out, "FAIL: GO-2026-7000 is called and not in") { |
| + t.Errorf("no failure line for GO-2026-7000:\n%s", out) |
| + } |
| + if !strings.Contains(out, "web/serve.go:12: example.com/shop/web.Serve reaches net/http.(*body).Read") { |
| + t.Errorf("the call site is not shown:\n%s", out) |
| + } |
| + if strings.Contains(out, "FAIL: GO-2026-6859") { |
| + t.Errorf("the listed finding failed too:\n%s", out) |
| + } |
| +} |
| + |
| +func TestListedCalledFindingPasses(t *testing.T) { |
| + out, err := gate(t, exc6859, scanHead+osvMsg("GO-2026-6859")+requiredMsg("GO-2026-6859")+calledMsg("GO-2026-6859")) |
| + if err != nil { |
| + t.Fatalf("a listed finding failed: %v\n%s", err, out) |
| + } |
| + if !strings.Contains(out, "excepted until 2027-01-31") { |
| + t.Errorf("the exception is not reported:\n%s", out) |
| + } |
| +} |
| + |
| +func TestExceptionValidThroughItsExpiryDay(t *testing.T) { |
| + exc := "GO-2026-6859\t2026-11-16\treason\n" |
| + if out, err := gate(t, exc, scanHead+calledMsg("GO-2026-6859")); err != nil { |
| + t.Fatalf("an exception failed on its own expiry day: %v\n%s", err, out) |
| + } |
| +} |
| + |
| +func TestExpiredExceptionFails(t *testing.T) { |
| + exc := "GO-2026-6859\t2026-11-15\tpinned for now\n" |
| + out, err := gate(t, exc, scanHead+calledMsg("GO-2026-6859")) |
| + if err == nil { |
| + t.Fatalf("an expired exception passed:\n%s", out) |
| + } |
| + if !strings.Contains(out, "the exception for GO-2026-6859 expired on 2026-11-15") { |
| + t.Errorf("no expiry failure:\n%s", out) |
| + } |
| +} |
| + |
| +func TestStaleExceptionFails(t *testing.T) { |
| + for name, scan := range map[string]string{ |
| + "absent": scanHead, |
| + "required": scanHead + requiredMsg("GO-2026-6859"), |
| + "imported": scanHead + importedMsg("GO-2026-6859"), |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + out, err := gate(t, exc6859, scan) |
| + if err == nil { |
| + t.Fatalf("a stale exception passed:\n%s", out) |
| + } |
| + if !strings.Contains(out, "GO-2026-6859 is excepted but no scan calls it any more: remove the exception") { |
| + t.Errorf("no stale failure:\n%s", out) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// An exception is judged against every scan together: it is not stale |
| +// because one module's scan does not call it, if another's does. |
| +func TestExceptionCalledInAnyScanIsNotStale(t *testing.T) { |
| + out, err := gate(t, exc6859, scanHead+requiredMsg("GO-2026-6859"), scanHead+calledMsg("GO-2026-6859")) |
| + if err != nil { |
| + t.Fatalf("%v\n%s", err, out) |
| + } |
| +} |
| + |
| +func TestUncalledFindingsArePrintedNotFatal(t *testing.T) { |
| + out, err := gate(t, "", scanHead+osvMsg("GO-2026-5932")+requiredMsg("GO-2026-5932")+osvMsg("GO-2026-5933")+importedMsg("GO-2026-5933")) |
| + if err != nil { |
| + t.Fatalf("uncalled findings failed: %v\n%s", err, out) |
| + } |
| + for _, want := range []string{ |
| + "GO-2026-5932 required golang.org/x/crypto@v0.58.0, fixed in v0.58.1", |
| + "GO-2026-5933 imported golang.org/x/crypto@v0.58.0", |
| + "summary of GO-2026-5932", |
| + "not fatal", |
| + } { |
| + if !strings.Contains(out, want) { |
| + t.Errorf("output lacks %q:\n%s", want, out) |
| + } |
| + } |
| +} |
| + |
| +// Only a whole source-mode, symbol-level scan in the protocol v1.8.0 |
| +// writes may count: anything else could be "clean" having looked at |
| +// nothing. |
| +func TestNotASourceScanFails(t *testing.T) { |
| + sbom := strings.TrimPrefix(scanHead, configMsg) |
| + for name, c := range map[string]struct{ scan, want string }{ |
| + "empty": {"", "no config message"}, |
| + "garbage": {"not json", "decoding govulncheck output"}, |
| + "package level": {strings.Replace(scanHead, `"scan_level":"symbol"`, `"scan_level":"package"`, 1), `scan level is "package"`}, |
| + "config only": {`{"config":{"scan_level":"symbol"}}`, `protocol version is ""`}, |
| + "config only, every field": {configMsg, "no SBOM message"}, |
| + // What govulncheck v1.8.0 -mode query -format json writes: a |
| + // symbol-level config, no SBOM, OSV records, no calls analysed. |
| + "query mode": {strings.Replace(configMsg, `"source"`, `"query"`, 1) + osvMsg("GO-2026-7000"), `scan mode is "query"`}, |
| + "binary mode": {strings.Replace(scanHead, `"source"`, `"binary"`, 1) + calledMsg("GO-2026-7000"), `scan mode is "binary"`}, |
| + "no protocol version": {strings.Replace(scanHead, `"protocol_version":"v1.0.0",`, "", 1), `protocol version is ""`}, |
| + "unsupported protocol": {strings.Replace(scanHead, `"v1.0.0"`, `"v2.0.0"`, 1), `protocol version is "v2.0.0"`}, |
| + "config not first": {sbom + configMsg, "the first message is not a config"}, |
| + "two configs": {scanHead + configMsg, "more than one config message"}, |
| + "two SBOMs": {scanHead + sbom, "more than one SBOM message"}, |
| + "truncated before the SBOM": {configMsg + `{"progress":{"message":"Scanning your code..."}}` + "\n", "no SBOM message"}, |
| + "SBOM without roots": {strings.Replace(scanHead, `,"roots":["example.com/shop/web"]`, "", 1), "names no package roots"}, |
| + "SBOM without modules": {strings.Replace(scanHead, `"modules":[{"path":"example.com/shop"},{"path":"stdlib","version":"v1.26.10"}]`, `"modules":[]`, 1), "names no scanned module"}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + out, err := gate(t, "", c.scan) |
| + if err == nil || err == errFindings { |
| + t.Fatalf("accepted (err %v):\n%s", err, out) |
| + } |
| + if !strings.Contains(err.Error(), c.want) { |
| + t.Errorf("error %q does not say %q", err, c.want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A good scan that satisfies the exception must not cover for a second |
| +// scan that never ran. |
| +func TestValidScanDoesNotMaskAConfigOnlyOne(t *testing.T) { |
| + out, err := gate(t, exc6859, scanHead+calledMsg("GO-2026-6859"), configMsg) |
| + if err == nil || err == errFindings { |
| + t.Fatalf("accepted (err %v):\n%s", err, out) |
| + } |
| +} |
| + |
| +func TestBadExceptionsFileFails(t *testing.T) { |
| + for name, exc := range map[string]string{ |
| + "two fields": "GO-2026-6859\t2027-01-31\n", |
| + "bad id": "CVE-2026-1\t2027-01-31\treason\n", |
| + "bad date": "GO-2026-6859\t31/01/2027\treason\n", |
| + "no reason": "GO-2026-6859\t2027-01-31\t \n", |
| + "duplicate": exc6859 + "GO-2026-6859\t2027-02-28\tagain\n", |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + if out, err := gate(t, exc, scanHead+calledMsg("GO-2026-6859")); err == nil || err == errFindings { |
| + t.Fatalf("accepted (err %v):\n%s", err, out) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The checked-in exceptions file must always parse. |
| +func TestRepoExceptionsFileParses(t *testing.T) { |
| + f, err := os.Open("../../.amadan/govulncheck-exceptions.tsv") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer f.Close() |
| + if _, err := parseExceptions(f); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |