Skip to content

Commit

Permalink
feat(vuln): remove duplicates in Fixed Version (#5596)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
Co-authored-by: knqyf263 <[email protected]>
  • Loading branch information
ilpianista and knqyf263 authored Dec 4, 2023
1 parent 99c04c4 commit a54d1e9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
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), ", ")
}
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

0 comments on commit a54d1e9

Please sign in to comment.