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.
This commit is contained in:
2026-11-23 19:54:30 +00:00
parent e6d8ed0f2e
commit 775ed8a4aa
2 changed files with 29 additions and 14 deletions
+13 -10
View File
@@ -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 {
+16 -4
View File
@@ -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) {