Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure that semantic is passed a valid models.Ecosystem #1116

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions internal/utility/vulns/vulnerability.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func eventVersion(e models.Event) string {
return ""
}

// convertEcosystem handles converting from a "lockfile" ecosystem to a "models" ecosystem.
//
// todo: this should go away in v2 once we've moved to a single ecosystem type
func convertLockfileEcosystem(version lockfile.Ecosystem) models.Ecosystem {
b, _, _ := strings.Cut(string(version), ":")

return models.Ecosystem(b)
}

func rangeContainsVersion(ar models.Range, pkg lockfile.PackageDetails) bool {
if ar.Type != models.RangeEcosystem && ar.Type != models.RangeSemVer {
return false
Expand All @@ -41,7 +50,7 @@ func rangeContainsVersion(ar models.Range, pkg lockfile.PackageDetails) bool {
return false
}

vp := semantic.MustParse(pkg.Version, models.Ecosystem(pkg.CompareAs))
vp := semantic.MustParse(pkg.Version, convertLockfileEcosystem(pkg.CompareAs))

sort.Slice(ar.Events, func(i, j int) bool {
a := ar.Events[i]
Expand All @@ -55,7 +64,7 @@ func rangeContainsVersion(ar models.Range, pkg lockfile.PackageDetails) bool {
return false
}

return semantic.MustParse(eventVersion(a), models.Ecosystem(pkg.CompareAs)).CompareStr(eventVersion(b)) < 0
return semantic.MustParse(eventVersion(a), convertLockfileEcosystem(pkg.CompareAs)).CompareStr(eventVersion(b)) < 0
})

var affected bool
Expand Down
26 changes: 26 additions & 0 deletions internal/utility/vulns/vulnerability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,29 @@ func TestOSV_IsAffected_OnlyVersions(t *testing.T) {
// an empty version should always be treated as affected
expectIsAffected(t, vuln, "", true)
}

func TestOSV_EcosystemsWithSuffix(t *testing.T) {
t.Parallel()

vuln := buildOSVWithAffected(
models.Affected{
Package: models.Package{Ecosystem: "Debian:12", Name: "my-package"},
Ranges: []models.Range{
buildSemverAffectsRange(
models.Event{Introduced: "0"},
),
},
},
)

pkg := lockfile.PackageDetails{
Name: "my-package",
Version: "0.0.0",
Ecosystem: "Debian:12",
CompareAs: "Debian:12",
}

if !vulns.IsAffected(vuln, pkg) {
t.Errorf("Expected OSV to affect package version %s but it did not", "0.0.0")
}
}
Loading