Skip to content

Commit

Permalink
feat(cve): expand search domain to cve description and package info (#…
Browse files Browse the repository at this point in the history
…2086)

* feat(cve): add reference url for cve

Signed-off-by: Laurentiu Niculae <[email protected]>

* feat(cve): expand search domain to cve description and package info

Signed-off-by: Laurentiu Niculae <[email protected]>

---------

Signed-off-by: Laurentiu Niculae <[email protected]>
  • Loading branch information
laurentiuNiculae authored Nov 29, 2023
1 parent e59d8da commit 90d27ff
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/term v0.14.0 // indirect
Expand Down
11 changes: 10 additions & 1 deletion pkg/extensions/search/cve/cve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

godigest "github.com/opencontainers/go-digest"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/exp/slices"

zerr "zotregistry.io/zot/errors"
zcommon "zotregistry.io/zot/pkg/common"
Expand Down Expand Up @@ -334,7 +335,15 @@ func filterCVEList(cveMap map[string]cvemodel.CVE, searchedCVE string, pageFinde

for _, cve := range cveMap {
if strings.Contains(strings.ToUpper(cve.Title), searchedCVE) ||
strings.Contains(strings.ToUpper(cve.ID), searchedCVE) {
strings.Contains(strings.ToUpper(cve.ID), searchedCVE) ||
strings.Contains(strings.ToUpper(cve.Description), searchedCVE) ||
strings.Contains(strings.ToUpper(cve.Reference), searchedCVE) ||
strings.Contains(strings.ToUpper(cve.Severity), searchedCVE) ||
slices.ContainsFunc(cve.PackageList, func(pack cvemodel.Package) bool {
return strings.Contains(strings.ToUpper(pack.Name), searchedCVE) ||
strings.Contains(strings.ToUpper(pack.FixedVersion), searchedCVE) ||
strings.Contains(strings.ToUpper(pack.InstalledVersion), searchedCVE)
}) {
pageFinder.Add(cve)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/extensions/search/cve/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CVE struct {
Description string `json:"Description"`
Severity string `json:"Severity"`
Title string `json:"Title"`
Reference string `json:"Reference"`
PackageList []Package `json:"PackageList"`
}

Expand Down
30 changes: 30 additions & 0 deletions pkg/extensions/search/cve/trivy/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"strings"
"sync"

"github.com/aquasecurity/trivy-db/pkg/metadata"
Expand Down Expand Up @@ -427,6 +428,7 @@ func (scanner Scanner) scanManifest(ctx context.Context, repo, digest string) (m
ID: vulnerability.VulnerabilityID,
Title: vulnerability.Title,
Description: vulnerability.Description,
Reference: getCVEReference(vulnerability.PrimaryURL, vulnerability.References),
Severity: convertSeverity(vulnerability.Severity),
PackageList: newPkgList,
}
Expand All @@ -439,6 +441,34 @@ func (scanner Scanner) scanManifest(ctx context.Context, repo, digest string) (m
return cveidMap, nil
}

func getCVEReference(primaryURL string, references []string) string {
if primaryURL != "" {
return primaryURL
}

if len(references) > 0 {
nvdReference, found := getNVDReference(references)

if found {
return nvdReference
}

return references[0]
}

return ""
}

func getNVDReference(references []string) (string, bool) {
for i := range references {
if strings.Contains(references[i], "nvd.nist.gov") {
return references[i], true
}
}

return "", false
}

func (scanner Scanner) scanIndex(ctx context.Context, repo, digest string) (map[string]cvemodel.CVE, error) {
if cachedMap := scanner.cache.Get(digest); cachedMap != nil {
return cachedMap, nil
Expand Down
16 changes: 16 additions & 0 deletions pkg/extensions/search/cve/trivy/scanner_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,19 @@ func TestIsIndexScannableErrors(t *testing.T) {
})
})
}

func TestGetCVEReference(t *testing.T) {
Convey("getCVEReference", t, func() {
ref := getCVEReference("primary", []string{})
So(ref, ShouldResemble, "primary")

ref = getCVEReference("", []string{"secondary"})
So(ref, ShouldResemble, "secondary")

ref = getCVEReference("", []string{""})
So(ref, ShouldResemble, "")

ref = getCVEReference("", []string{"https://nvd.nist.gov/vuln/detail/CVE-2023-2650"})
So(ref, ShouldResemble, "https://nvd.nist.gov/vuln/detail/CVE-2023-2650")
})
}
57 changes: 57 additions & 0 deletions pkg/extensions/search/gql_generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/extensions/search/gql_generated/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/extensions/search/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func getCVEListForImage(
desc := cveDetail.Description
title := cveDetail.Title
severity := cveDetail.Severity
referenceURL := cveDetail.Reference

pkgList := make([]*gql_generated.PackageInfo, 0)

Expand All @@ -249,6 +250,7 @@ func getCVEListForImage(
Title: &title,
Description: &desc,
Severity: &severity,
Reference: &referenceURL,
PackageList: pkgList,
},
)
Expand Down
4 changes: 4 additions & 0 deletions pkg/extensions/search/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type CVE {
"""
Description: String
"""
Reference for the given CVE
"""
Reference: String
"""
The impact the CVE has, one of "UNKNOWN", "LOW", "MEDIUM", "HIGH", "CRITICAL"
"""
Severity: String
Expand Down
6 changes: 1 addition & 5 deletions pkg/extensions/search/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90d27ff

Please sign in to comment.