Skip to content

Commit

Permalink
sort fixed versions
Browse files Browse the repository at this point in the history
Signed-off-by: tomersein <[email protected]>
  • Loading branch information
tomersein committed Dec 13, 2024
1 parent a86115f commit dac6726
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions grype/db/v5/search/cpe.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,35 @@ func addNewMatch(matchesByFingerprint map[match.Fingerprint]match.Match, vuln vu
candidateMatch = existingMatch
}

sortFixedVersions(candidateMatch, p)

candidateMatch.Details = addMatchDetails(candidateMatch.Details,
match.Detail{
Type: match.CPEMatch,
Confidence: 0.9, // TODO: this is hard coded for now
Matcher: upstreamMatcher,
SearchedBy: CPEParameters{
Namespace: vuln.Namespace,
CPEs: []string{
searchedByCPE.Attributes.BindToFmtString(),
},
Package: CPEPackageParameter{
Name: p.Name,
Version: p.Version,
},
},
Found: CPEResult{
VulnerabilityID: vuln.ID,
VersionConstraint: vuln.Constraint.String(),
CPEs: cpesToString(filterCPEsByVersion(searchVersion, vuln.CPEs)),
},
},
)

matchesByFingerprint[candidateMatch.Fingerprint()] = candidateMatch
}

func sortFixedVersions(candidateMatch match.Match, p pkg.Package) {

Check failure on line 208 in grype/db/v5/search/cpe.go

View workflow job for this annotation

GitHub Actions / Static analysis

cognitive complexity 34 of func `sortFixedVersions` is high (> 30) (gocognit)
// sort fixed versions (higher values than the package version will be at the beginning of the array)
if len(candidateMatch.Vulnerability.Fix.Versions) > 1 {
checkSatisfaction := func(constraint version.Constraint, v *version.Version) (bool, error) {
Expand Down Expand Up @@ -236,31 +265,6 @@ func addNewMatch(matchesByFingerprint map[match.Fingerprint]match.Match, vuln vu
return !v1Satisfied
})
}

candidateMatch.Details = addMatchDetails(candidateMatch.Details,
match.Detail{
Type: match.CPEMatch,
Confidence: 0.9, // TODO: this is hard coded for now
Matcher: upstreamMatcher,
SearchedBy: CPEParameters{
Namespace: vuln.Namespace,
CPEs: []string{
searchedByCPE.Attributes.BindToFmtString(),
},
Package: CPEPackageParameter{
Name: p.Name,
Version: p.Version,
},
},
Found: CPEResult{
VulnerabilityID: vuln.ID,
VersionConstraint: vuln.Constraint.String(),
CPEs: cpesToString(filterCPEsByVersion(searchVersion, vuln.CPEs)),
},
},
)

matchesByFingerprint[candidateMatch.Fingerprint()] = candidateMatch
}

func addMatchDetails(existingDetails []match.Detail, newDetails match.Detail) []match.Detail {
Expand Down

0 comments on commit dac6726

Please sign in to comment.