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

feat(vuln): remove duplicates in Fixed Version #5596

Merged
merged 2 commits into from
Dec 4, 2023
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
9 changes: 7 additions & 2 deletions pkg/detector/library/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/samber/lo"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy-db/pkg/db"
Expand Down Expand Up @@ -136,7 +137,7 @@ func (d *Driver) DetectVulnerabilities(pkgID, pkgName, pkgVer string) ([]types.D

func createFixedVersions(advisory dbTypes.Advisory) string {
if len(advisory.PatchedVersions) != 0 {
return strings.Join(advisory.PatchedVersions, ", ")
return joinFixedVersions(advisory.PatchedVersions)
}

var fixedVersions []string
Expand All @@ -149,5 +150,9 @@ func createFixedVersions(advisory dbTypes.Advisory) string {
}
}
}
return strings.Join(fixedVersions, ", ")
return joinFixedVersions(fixedVersions)
}

func joinFixedVersions(fixedVersions []string) string {
return strings.Join(lo.Uniq(fixedVersions), ", ")
DmitriyLewen marked this conversation as resolved.
Show resolved Hide resolved
}
25 changes: 25 additions & 0 deletions pkg/detector/library/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,31 @@ func TestDriver_Detect(t *testing.T) {
},
wantErr: "failed to unmarshal advisory JSON",
},
{
name: "duplicated version in advisory",
fixtures: []string{
"testdata/fixtures/pip.yaml",
"testdata/fixtures/data-source.yaml",
},
libType: ftypes.PythonPkg,
args: args{
pkgName: "Django",
pkgVer: "4.2.1",
},
want: []types.DetectedVulnerability{
{
VulnerabilityID: "CVE-2023-36053",
PkgName: "Django",
InstalledVersion: "4.2.1",
FixedVersion: "4.2.3",
DataSource: &dbTypes.DataSource{
ID: vulnerability.GHSA,
Name: "GitHub Security Advisory Pip",
URL: "https://github.com/advisories?query=type%3Areviewed+ecosystem%3Apip",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/detector/library/testdata/fixtures/data-source.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@
ID: "ruby-advisory-db"
Name: "Ruby Advisory Database"
URL: "https://github.com/rubysec/ruby-advisory-db"
- key: "pip::GitHub Security Advisory Pip"
value:
ID: "ghsa"
Name: "GitHub Security Advisory Pip"
URL: "https://github.com/advisories?query=type%3Areviewed+ecosystem%3Apip"
18 changes: 18 additions & 0 deletions pkg/detector/library/testdata/fixtures/pip.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- bucket: "pip::GitHub Security Advisory Pip"
pairs:
- bucket: Django
pairs:
- key: CVE-2023-36053
value:
PatchedVersions:
- 4.2.3
VulnerableVersions:
- < 4.2.3
- bucket: django
pairs:
- key: CVE-2023-36053
value:
PatchedVersions:
- 4.2.3
VulnerableVersions:
- < 4.2.3