fix: Output only valid CPEs for CycloneDX OS components (#1339)

This commit is contained in:
Keith Zantow 2022-11-14 15:24:19 -05:00 committed by GitHub
parent 10f43d75e0
commit c1fdfce5f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View File

@ -92,13 +92,22 @@ func toOSComponent(distro *linux.Release) []cyclonedx.Component {
Name: distro.ID,
Version: distro.VersionID,
// TODO should we add a PURL?
CPE: distro.CPEName,
CPE: formatCPE(distro.CPEName),
ExternalReferences: eRefs,
Properties: properties,
},
}
}
func formatCPE(cpeString string) string {
cpe, err := pkg.NewCPE(cpeString)
if err != nil {
log.Debugf("skipping invalid CPE: %s", cpeString)
return ""
}
return pkg.CPEString(cpe)
}
// NewBomDescriptor returns a new BomDescriptor tailored for the current time and "syft" tool details.
func toBomDescriptor(name, version string, srcMetadata source.Metadata) *cyclonedx.Metadata {
return &cyclonedx.Metadata{

View File

@ -0,0 +1,34 @@
package cyclonedxhelpers
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_formatCPE(t *testing.T) {
tests := []struct {
cpe string
expected string
}{
{
cpe: "cpe:2.3:o:amazon:amazon_linux:2",
expected: "cpe:2.3:o:amazon:amazon_linux:2:*:*:*:*:*:*:*",
},
{
cpe: "cpe:/o:opensuse:leap:15.2",
expected: "cpe:2.3:o:opensuse:leap:15.2:*:*:*:*:*:*:*",
},
{
cpe: "invalid-cpe",
expected: "",
},
}
for _, test := range tests {
t.Run(test.cpe, func(t *testing.T) {
out := formatCPE(test.cpe)
assert.Equal(t, test.expected, out)
})
}
}