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

fix(cdx): validate external refs before encoding #2091

Merged
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
17 changes: 13 additions & 4 deletions syft/formats/common/cyclonedxhelpers/external_references.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cyclonedxhelpers

import (
"fmt"
"net/url"
"strings"

"github.com/CycloneDX/cyclonedx-go"
Expand All @@ -15,9 +16,11 @@ import (
func encodeExternalReferences(p pkg.Package) *[]cyclonedx.ExternalReference {
var refs []cyclonedx.ExternalReference
if hasMetadata(p) {
// Skip adding extracted URL and Homepage metadata
// as "external_reference" if the metadata isn't IRI-compliant
switch metadata := p.Metadata.(type) {
case pkg.ApkMetadata:
if metadata.URL != "" {
if metadata.URL != "" && isValidExternalRef(metadata.URL) {
refs = append(refs, cyclonedx.ExternalReference{
URL: metadata.URL,
Type: cyclonedx.ERTypeDistribution,
Expand All @@ -31,20 +34,20 @@ func encodeExternalReferences(p pkg.Package) *[]cyclonedx.ExternalReference {
})
}
case pkg.NpmPackageJSONMetadata:
if metadata.URL != "" {
if metadata.URL != "" && isValidExternalRef(metadata.URL) {
refs = append(refs, cyclonedx.ExternalReference{
URL: metadata.URL,
Type: cyclonedx.ERTypeDistribution,
})
}
if metadata.Homepage != "" {
if metadata.Homepage != "" && isValidExternalRef(metadata.Homepage) {
refs = append(refs, cyclonedx.ExternalReference{
URL: metadata.Homepage,
Type: cyclonedx.ERTypeWebsite,
})
}
case pkg.GemMetadata:
if metadata.Homepage != "" {
if metadata.Homepage != "" && isValidExternalRef(metadata.Homepage) {
refs = append(refs, cyclonedx.ExternalReference{
URL: metadata.Homepage,
Type: cyclonedx.ERTypeWebsite,
Expand Down Expand Up @@ -158,3 +161,9 @@ func refComment(c *cyclonedx.Component, typ cyclonedx.ExternalReferenceType) str
}
return ""
}

// isValidExternalRef checks for IRI-comppliance for input string to be added into "external_reference"
func isValidExternalRef(s string) bool {
parsed, err := url.Parse(s)
return err == nil && parsed != nil && parsed.Host != ""
}
43 changes: 42 additions & 1 deletion syft/formats/common/cyclonedxhelpers/external_references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_encodeExternalReferences(t *testing.T) {
},
},
{
name: "from npm",
name: "from npm with valid URL",
input: pkg.Package{
Metadata: pkg.NpmPackageJSONMetadata{
URL: "http://a-place.gov",
Expand All @@ -42,6 +42,18 @@ func Test_encodeExternalReferences(t *testing.T) {
{URL: "http://a-place.gov", Type: cyclonedx.ERTypeDistribution},
},
},
{
name: "from npm with invalid URL but valid Homepage",
input: pkg.Package{
Metadata: pkg.NpmPackageJSONMetadata{
URL: "b-place",
Homepage: "http://b-place.gov",
},
},
expected: &[]cyclonedx.ExternalReference{
{URL: "http://b-place.gov", Type: cyclonedx.ERTypeWebsite},
},
},
{
name: "from cargo lock",
input: pkg.Package{
Expand Down Expand Up @@ -132,3 +144,32 @@ func Test_encodeExternalReferences(t *testing.T) {
})
}
}

func Test_isValidExternalRef(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
name: "valid URL for external_reference, git protocol",
input: "git+https://github.com/abc/def.git",
expected: true,
},
{
name: "valid URL for external_reference, git protocol",
input: "git+https://github.com/abc/def.git",
expected: true,
},
{
name: "invalid URL for external_reference",
input: "abc/def",
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, isValidExternalRef(test.input))
})
}
}
Loading