govulncheck-gate: accept only a whole source-mode scan
Astra round 1 P2 on audit-govulncheck: a config message saying scan_level symbol was the only proof of a scan, so a config-only stream, query-mode output (symbol level, no calls analysed), a missing or unknown protocol version, or a stream cut off before its SBOM could read as clean, and a good first scan could cover for an empty second. readScan now requires: exactly one config, first; protocol_version v1.0.0 (what govulncheck v1.8.0 writes); scan_mode source; scan_level symbol; exactly one SBOM naming the scanned module and package roots. One rejection test per case, plus the masking case; all failed on the old checker. Real query-mode output is rejected. The Makefile's && chain stays and now says why: the JSON has no end marker, so a crash after the SBOM only shows in the exit status.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+101
−21
.amadan/README.md+2 −1Makefile+6 −0scripts/govulncheck-gate/main.go+53 −12scripts/govulncheck-gate/main_test.go+40 −8
CI cancelled — superseded by a newer push — run details
diff --git a/.amadan/README.md b/.amadan/README.md| index 0a47c06d6..70ba2d65b 100644 |
| --- a/.amadan/README.md |
| +++ b/.amadan/README.md |
| @@ -61,7 +61,8 @@ expiry date and the reason. The step fails when: |
| - 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. |
| +- 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 |
diff --git a/Makefile b/Makefile| index 5b58fa1eb..e72155ec7 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -86,6 +86,12 @@ staticcheck: |
| # 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; \ |
diff --git a/scripts/govulncheck-gate/main.go b/scripts/govulncheck-gate/main.go| index ebb0495cc..7681c9285 100644 |
| --- a/scripts/govulncheck-gate/main.go |
| +++ b/scripts/govulncheck-gate/main.go |
| @@ -112,11 +112,13 @@ type message struct { |
| 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"` |
| @@ -159,14 +161,29 @@ type vuln struct { |
| 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 refuses a stream that is not a |
| -// symbol-level scan: a package- or module-level scan reports no calls, |
| -// so judged by these rules it would pass whatever it found. |
| +// 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 |
| - sawConfig := false |
| + configs, sboms, n := 0, 0, 0 |
| summaries := map[string]string{} |
| for { |
| var m message |
| @@ -177,13 +194,37 @@ func readScan(r io.Reader, vulns map[string]*vuln) (string, error) { |
| 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: |
| - sawConfig = true |
| - if m.Config.ScanLevel != "symbol" { |
| - return "", fmt.Errorf("scan level is %q, want \"symbol\" (do not pass -scan to govulncheck)", m.Config.ScanLevel) |
| + 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") |
| } |
| - case m.SBOM != nil && len(m.SBOM.Modules) > 0: |
| mod = m.SBOM.Modules[0].Path |
| case m.OSV != nil: |
| summaries[m.OSV.ID] = m.OSV.Summary |
| @@ -216,9 +257,12 @@ func readScan(r io.Reader, vulns map[string]*vuln) (string, error) { |
| } |
| } |
| } |
| - if !sawConfig { |
| + 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 |
| @@ -354,9 +398,6 @@ func run(w io.Writer, excFile string, scans []string, now time.Time) error { |
| if err != nil { |
| return fmt.Errorf("%s: %w", path, err) |
| } |
| - if mod == "" { |
| - mod = path |
| - } |
| mods = append(mods, mod) |
| } |
| fmt.Fprintf(w, "govulncheck: scanned %s\n", strings.Join(mods, ", ")) |
diff --git a/scripts/govulncheck-gate/main_test.go b/scripts/govulncheck-gate/main_test.go| index 477855d34..7bde4c531 100644 |
| --- a/scripts/govulncheck-gate/main_test.go |
| +++ b/scripts/govulncheck-gate/main_test.go |
| @@ -13,8 +13,9 @@ import ( |
| // 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 scanHead = `{"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"}} |
| -{"SBOM":{"go_version":"go1.26.10","modules":[{"path":"example.com/shop"}]}} |
| +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..."}} |
| ` |
| @@ -149,20 +150,51 @@ func TestUncalledFindingsArePrintedNotFatal(t *testing.T) { |
| } |
| } |
| -func TestNotGovulncheckOutputFails(t *testing.T) { |
| - for name, scan := range map[string]string{ |
| - "empty": "", |
| - "garbage": "not json", |
| - "package level": strings.Replace(scanHead, `"scan_level":"symbol"`, `"scan_level":"package"`, 1), |
| +// 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) { |
| - if out, err := gate(t, "", scan); err == nil || err == errFindings { |
| + 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", |