Skip to content

Commit 417212e

Browse files
authored
fix(cyclonedx): trim non-URL info for advisory.url (#6952)
1 parent 38b35dd commit 417212e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pkg/sbom/cyclonedx/marshal.go

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cyclonedx
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67
"slices"
78
"sort"
89
"strconv"
@@ -332,6 +333,10 @@ func (*Marshaler) affects(ref, version string) cdx.Affects {
332333
func (*Marshaler) advisories(refs []string) *[]cdx.Advisory {
333334
refs = lo.Uniq(refs)
334335
advs := lo.FilterMap(refs, func(ref string, _ int) (cdx.Advisory, bool) {
336+
// There are cases when `ref` contains extra info
337+
// But we need to use only URL.
338+
// cf. https://github.com/aquasecurity/trivy/issues/6801
339+
ref = trimNonUrlInfo(ref)
335340
return cdx.Advisory{URL: ref}, ref != ""
336341
})
337342

@@ -345,6 +350,17 @@ func (*Marshaler) advisories(refs []string) *[]cdx.Advisory {
345350
return &advs
346351
}
347352

353+
// trimNonUrlInfo returns first valid URL.
354+
func trimNonUrlInfo(ref string) string {
355+
ss := strings.Split(ref, " ")
356+
for _, s := range ss {
357+
if u, err := url.Parse(s); err == nil && u.Scheme != "" && u.Host != "" {
358+
return s
359+
}
360+
}
361+
return ""
362+
}
363+
348364
func (m *Marshaler) marshalVulnerability(bomRef string, vuln core.Vulnerability) *cdx.Vulnerability {
349365
v := &cdx.Vulnerability{
350366
ID: vuln.ID,

pkg/sbom/cyclonedx/marshal_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,8 @@ func TestMarshaler_MarshalReport(t *testing.T) {
847847
},
848848
},
849849
References: []string{
850-
"http://www.openwall.com/lists/oss-security/2022/02/11/5",
851-
"https://access.redhat.com/security/cve/CVE-2022-23633",
850+
" extraPrefix http://www.openwall.com/lists/oss-security/2022/02/11/5",
851+
"https://access.redhat.com/security/cve/CVE-2022-23633 (extra suffix)",
852852
},
853853
PublishedDate: lo.ToPtr(time.Date(2022, 2, 11, 21, 15, 0, 0, time.UTC)),
854854
LastModifiedDate: lo.ToPtr(time.Date(2022, 2, 22, 21, 47, 0, 0, time.UTC)),

0 commit comments

Comments
 (0)