8000 fix: only fallback to language if language is non-blank by willmurphyscode · Pull Request #2621 · anchore/grype · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

fix: only fallback to language if language is non-blank #2621

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

Merged
merged 2 commits into from
Apr 25, 2025
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
15 changes: 12 additions & 3 deletions grype/db/v6/vulnerability_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,20 @@ func (vp vulnerabilityProvider) FindVulnerabilities(criteria ...vulnerability.Cr
pkgSpec = &PackageSpecifier{}
}
// the v6 store normalizes ecosystems around the syft package type, so that field is preferred
if c.PackageType != "" && c.PackageType != syftPkg.UnknownPkg {
pkgSpec.Ecosystem = string(c.PackageType)
switch {
case c.PackageType != "" && c.PackageType != syftPkg.UnknownPkg:
// prefer to match by a non-blank, known package type
pkgType = c.PackageType
} else {
pkgSpec.Ecosystem = string(c.PackageType)
case c.Language != "":
// if there's no known package type, but there is a non-blank language
// try that.
pkgSpec.Ecosystem = string(c.Language)
case c.PackageType == syftPkg.UnknownPkg:
// if language is blank, and package type is explicitly "UnknownPkg" and not
// just blank, use that.
pkgType = c.PackageType
pkgSpec.Ecosystem = string(c.PackageType)
}
applied = true
case *search.IDCriteria:
Expand Down
53 changes: 49 additions & 4 deletions grype/db/v6/vulnerability_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,56 @@ func Test_FindVulnerabilitiesByByID(t *testing.T) {
}

func Test_FindVulnerabilitiesByEcosystem_UnknownPackageType(t *testing.T) {
tests := []struct {
name string
packageName string
packageType syftPkg.Type
language syftPkg.Language
expectedIDs []string
}{
{
name: "known package type",
packageName: "Newtonsoft.Json",
packageType: syftPkg.DotnetPkg,
language: syftPkg.Java, // deliberately wrong to prove we're using package type
expectedIDs: []string{"GHSA-5crp-9r3c-p9vr"},
},
{
name: "unknown package type, known language",
packageName: "Newtonsoft.Json",
packageType: syftPkg.UnknownPkg,
language: syftPkg.Dotnet,
expectedIDs: []string{"GHSA-5crp-9r3c-p9vr"},
},
{
name: "unknown package type, unknown language",
packageName: "Newtonsoft.Json",
packageType: syftPkg.UnknownPkg,
language: syftPkg.UnknownLanguage,
// The vuln GHSA-5crp-9r3c-p9vr is specifically associated
// with the dotnet ecosystem, so it should not be returned here.
// In a real search for UnknownPkg + UnknownLanguage, there should
// be a separate search.ByCPE run that _does_ return it.
expectedIDs: []string{},
},
}
provider := testVulnerabilityProvider(t)
actual, err := provider.FindVulnerabilities(search.ByEcosystem(syftPkg.Dotnet, syftPkg.UnknownPkg))
require.NoError(t, err)
require.NotEmpty(t, actual)
require.Equal(t, actual[0].Reference.ID, "GHSA-5crp-9r3c-p9vr")
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := provider.FindVulnerabilities(
search.ByEcosystem(test.language, test.packageType),
search.ByPackageName(test.packageName),
)
require.NoError(t, err)
actualIDs := make([]string, len(actual))
for idx, vuln := range actual {
actualIDs[idx] = vuln.ID
}
if d := cmp.Diff(test.expectedIDs, actualIDs); d != "" {
t.Errorf("diff: %+v", d)
}
})
}
}

func Test_DataSource(t *testing.T) {
Expand Down
Loading
0