fix(osv): take the fixed version from the queried package range

An advisory can cover several packages. The fixed version came from the first range, so x/net findings showed the stdlib fix 1.26.9 instead of the x/net fix 0.60.0.
This commit is contained in:
2026-11-23 19:54:33 +00:00
parent 775ed8a4aa
commit fac30f59e6
2 changed files with 44 additions and 8 deletions
+25 -8
View File
@@ -264,6 +264,7 @@ func (s *Scanner) cleanGoModuleName(packageName, version, ecosystem string) (str
func (s *Scanner) convertOSVResult(osvResp *OSVResponse, registry, packageName, version string) *metadata.ScanResult {
vulnerabilities := make([]metadata.Vulnerability, 0, len(osvResp.Vulns))
severityCounts := make(map[string]int)
queryName, _ := s.cleanGoModuleName(packageName, version, s.registryToEcosystem(registry))
for _, vuln := range osvResp.Vulns {
// Determine severity from various sources
@@ -277,7 +278,7 @@ func (s *Scanner) convertOSVResult(osvResp *OSVResponse, registry, packageName,
}
// Find fixed version
fixedVersion := s.findFixedVersion(&vuln, version)
fixedVersion := s.findFixedVersion(&vuln, queryName)
vulnerabilities = append(vulnerabilities, metadata.Vulnerability{
ID: vuln.ID,
@@ -352,14 +353,30 @@ func (s *Scanner) determineSeverity(vuln *OSVVulnerability) string {
return metadata.NormalizeSeverity(rawSeverity)
}
// findFixedVersion extracts the fixed version from OSV affected ranges
func (s *Scanner) findFixedVersion(vuln *OSVVulnerability, currentVersion string) string {
// findFixedVersion returns the first fixed version in the ranges for the queried package; an
// advisory can cover several packages (stdlib and golang.org/x/net) with different fixes.
func (s *Scanner) findFixedVersion(vuln *OSVVulnerability, name string) string {
fallback := ""
for _, affected := range vuln.Affected {
for _, r := range affected.Ranges {
for _, event := range r.Events {
if event.Fixed != "" {
return event.Fixed
}
fixed := firstFixed(affected.Ranges)
if fixed == "" {
continue
}
if strings.EqualFold(affected.Package.Name, name) {
return fixed
}
if fallback == "" {
fallback = fixed
}
}
return fallback
}
func firstFixed(ranges []OSVRange) string {
for _, r := range ranges {
for _, event := range r.Events {
if event.Fixed != "" {
return event.Fixed
}
}
}
+19
View File
@@ -44,3 +44,22 @@ func TestScan_FullQueryURLIsKept(t *testing.T) {
t.Errorf("request path = %q, want /v1/query", gotPath)
}
}
// An advisory that covers the stdlib and x/net lists the stdlib range first; the fixed version
// shown for x/net must come from the x/net range.
func TestScan_FixedVersionComesFromTheQueriedPackage(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"vulns":[{"id":"GO-2026-6612","summary":"s","affected":[
{"package":{"name":"stdlib","ecosystem":"Go"},"ranges":[{"type":"SEMVER","events":[{"introduced":"0"},{"fixed":"1.26.9"}]}]},
{"package":{"name":"golang.org/x/net","ecosystem":"Go"},"ranges":[{"type":"SEMVER","events":[{"introduced":"0"},{"fixed":"0.60.0"}]}]}]}]}`))
}))
t.Cleanup(srv.Close)
res, err := New(config.OSVConfig{APIURL: srv.URL}).Scan(context.Background(), "go", "golang.org/x/net/@v/v0.56.0.zip", "v0.56.0", "")
if err != nil {
t.Fatalf("Scan: %v", err)
}
if len(res.Vulnerabilities) != 1 || res.Vulnerabilities[0].FixedIn != "0.60.0" {
t.Fatalf("vulnerabilities = %+v, want one fixed in 0.60.0", res.Vulnerabilities)
}
}