mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
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:
+25
-8
@@ -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 {
|
func (s *Scanner) convertOSVResult(osvResp *OSVResponse, registry, packageName, version string) *metadata.ScanResult {
|
||||||
vulnerabilities := make([]metadata.Vulnerability, 0, len(osvResp.Vulns))
|
vulnerabilities := make([]metadata.Vulnerability, 0, len(osvResp.Vulns))
|
||||||
severityCounts := make(map[string]int)
|
severityCounts := make(map[string]int)
|
||||||
|
queryName, _ := s.cleanGoModuleName(packageName, version, s.registryToEcosystem(registry))
|
||||||
|
|
||||||
for _, vuln := range osvResp.Vulns {
|
for _, vuln := range osvResp.Vulns {
|
||||||
// Determine severity from various sources
|
// Determine severity from various sources
|
||||||
@@ -277,7 +278,7 @@ func (s *Scanner) convertOSVResult(osvResp *OSVResponse, registry, packageName,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find fixed version
|
// Find fixed version
|
||||||
fixedVersion := s.findFixedVersion(&vuln, version)
|
fixedVersion := s.findFixedVersion(&vuln, queryName)
|
||||||
|
|
||||||
vulnerabilities = append(vulnerabilities, metadata.Vulnerability{
|
vulnerabilities = append(vulnerabilities, metadata.Vulnerability{
|
||||||
ID: vuln.ID,
|
ID: vuln.ID,
|
||||||
@@ -352,14 +353,30 @@ func (s *Scanner) determineSeverity(vuln *OSVVulnerability) string {
|
|||||||
return metadata.NormalizeSeverity(rawSeverity)
|
return metadata.NormalizeSeverity(rawSeverity)
|
||||||
}
|
}
|
||||||
|
|
||||||
// findFixedVersion extracts the fixed version from OSV affected ranges
|
// findFixedVersion returns the first fixed version in the ranges for the queried package; an
|
||||||
func (s *Scanner) findFixedVersion(vuln *OSVVulnerability, currentVersion string) string {
|
// 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 _, affected := range vuln.Affected {
|
||||||
for _, r := range affected.Ranges {
|
fixed := firstFixed(affected.Ranges)
|
||||||
for _, event := range r.Events {
|
if fixed == "" {
|
||||||
if event.Fixed != "" {
|
continue
|
||||||
return event.Fixed
|
}
|
||||||
}
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,3 +44,22 @@ func TestScan_FullQueryURLIsKept(t *testing.T) {
|
|||||||
t.Errorf("request path = %q, want /v1/query", gotPath)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user