From 775ed8a4aab7f90a8387695304f28d88a4b95756 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 23 Nov 2026 19:54:30 +0000 Subject: [PATCH] fix(govulncheck): count findings in the scanned module only A module was charged with advisories against its dependencies at the minimum versions in its go.mod. A consuming build resolves its own versions, so these findings do not describe the module. On production golang.org/x/net v0.56.0 carried x/crypto and x/text advisories this way. Stdlib findings describe the scanner toolchain and are also dropped. --- pkg/scanner/govulncheck/govulncheck.go | 23 ++++++++++++--------- pkg/scanner/govulncheck/govulncheck_test.go | 20 ++++++++++++++---- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pkg/scanner/govulncheck/govulncheck.go b/pkg/scanner/govulncheck/govulncheck.go index a24d5c6..2af4bde 100644 --- a/pkg/scanner/govulncheck/govulncheck.go +++ b/pkg/scanner/govulncheck/govulncheck.go @@ -126,7 +126,7 @@ func (s *Scanner) Scan(ctx context.Context, registry, packageName, version strin cmd.Stderr = &stderr output, _ := cmd.Output() // govulncheck -json exits 0 whether or not it finds anything - vulns, err := parseFindings(output) + vulns, err := parseFindings(output, module) if err != nil { return s.errorResult(ctx, registry, packageName, version, fmt.Sprintf("govulncheck failed: %v: %s", err, strings.TrimSpace(stderr.String())), stderr.String()), nil @@ -175,8 +175,10 @@ func hasPathElement(importPath, element string) bool { } // parseFindings reads govulncheck's stream of JSON messages. An output without the config -// message did not come from a completed run. -func parseFindings(output []byte) ([]GovulncheckVuln, error) { +// message did not come from a completed run. Only findings in the scanned module count: +// dependency findings come from the minimum versions in its go.mod, which a consuming build +// replaces with its own, and stdlib findings describe the scanner's toolchain. +func parseFindings(output []byte, module string) ([]GovulncheckVuln, error) { summaries := make(map[string]string) var vulns []GovulncheckVuln seen := make(map[string]bool) @@ -194,14 +196,15 @@ func parseFindings(output []byte) ([]GovulncheckVuln, error) { sawConfig = true case msg.OSV != nil: summaries[msg.OSV.ID] = msg.OSV.Summary - case msg.Finding != nil && msg.Finding.OSV != "" && !seen[msg.Finding.OSV]: + case msg.Finding != nil && msg.Finding.OSV != "" && !seen[msg.Finding.OSV] && + len(msg.Finding.Trace) > 0 && msg.Finding.Trace[0].Module == module: seen[msg.Finding.OSV] = true - v := GovulncheckVuln{OSV: msg.Finding.OSV, FixedVersion: msg.Finding.FixedVersion} - if len(msg.Finding.Trace) > 0 { - v.Module = msg.Finding.Trace[0].Module - v.ModuleVersion = msg.Finding.Trace[0].Version - } - vulns = append(vulns, v) + vulns = append(vulns, GovulncheckVuln{ + OSV: msg.Finding.OSV, + FixedVersion: msg.Finding.FixedVersion, + Module: msg.Finding.Trace[0].Module, + ModuleVersion: msg.Finding.Trace[0].Version, + }) } } if !sawConfig { diff --git a/pkg/scanner/govulncheck/govulncheck_test.go b/pkg/scanner/govulncheck/govulncheck_test.go index 884a51a..982122a 100644 --- a/pkg/scanner/govulncheck/govulncheck_test.go +++ b/pkg/scanner/govulncheck/govulncheck_test.go @@ -86,16 +86,28 @@ func ids(res *metadata.ScanResult) map[string]string { func TestScanReadsPrettyPrintedFindings(t *testing.T) { // govulncheck -json prints indented objects, not one object per line. fakeGo(t, abs(t, "golist-gin-v1.6.0.txt"), 0, "") - fakeGovulncheck(t, abs(t, "gin-v1.6.0-source-scan.stream"), 0, "") + fakeGovulncheck(t, abs(t, "gin-v1.6.0.stream"), 0, "") res, err := New(config.GovulncheckConfig{}).Scan(context.Background(), "go", "github.com/gin-gonic/gin/@v/v1.6.0.zip", "v1.6.0", module(t)) require.NoError(t, err) assert.Equal(t, metadata.ScanStatusVulnerable, res.Status) assert.Equal(t, map[string]string{ - "GO-2022-0493": "v0.0.0-20220412211240-33da011f77ad", - "GO-2026-5024": "v0.44.0", - }, ids(res), "one entry per advisory, with the fixed version") + "GO-2021-0052": "v1.7.7", + "GO-2023-1737": "v1.9.1", + }, ids(res), "one entry per advisory against gin, with the fixed version; the x/sys findings belong to a dependency") +} + +func TestScanIgnoresFindingsInDependencies(t *testing.T) { + // gin's go.mod requires an old x/sys; a build that uses gin resolves its own x/sys version. + fakeGo(t, abs(t, "golist-gin-v1.6.0.txt"), 0, "") + fakeGovulncheck(t, abs(t, "gin-v1.6.0-source-scan.stream"), 0, "") + + res, err := New(config.GovulncheckConfig{}).Scan(context.Background(), "go", "github.com/gin-gonic/gin/@v/v1.6.0.zip", "v1.6.0", module(t)) + + require.NoError(t, err) + assert.Equal(t, metadata.ScanStatusClean, res.Status) + assert.Empty(t, res.Vulnerabilities) } func TestScanAuditsTheModuleItself(t *testing.T) {