Skip to content
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
27 changes: 27 additions & 0 deletions pkg/sbom/cyclonedx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ func TestCycloneDX_SpecAndMetadata(t *testing.T) {
}
}

// scanoss/scanoss-cli#138: Grype keys Go vulnerabilities on the component name,
// so otel/sdk v1.45.0 (two known advisories) named "sdk" came out clean. The
// writer must name it by its module path.
func TestCycloneDX_GoComponentNamedByModulePath(t *testing.T) {
var inv Inventory
inv.Add(Component{
Purl: "pkg:golang/go.opentelemetry.io/otel/sdk",
Version: "v1.45.0",
Scope: ScopeDeclared,
})
out, err := Generate(inv, FormatCycloneDX)
if err != nil {
t.Fatalf("Generate: %v", err)
}
bom := decodeBOM(t, out)
if bom.Components == nil || len(*bom.Components) != 1 {
t.Fatalf("want 1 component, got %v", bom.Components)
}
c := (*bom.Components)[0]
if c.Name != "go.opentelemetry.io/otel/sdk" {
t.Errorf("name = %q, want the full module path", c.Name)
}
if c.PackageURL != "pkg:golang/go.opentelemetry.io/otel/sdk@v1.45.0" {
t.Errorf("purl = %q", c.PackageURL)
}
}

func TestCycloneDX_Components(t *testing.T) {
out, err := Generate(sampleInventory(), FormatCycloneDX)
if err != nil {
Expand Down
30 changes: 25 additions & 5 deletions pkg/sbom/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,18 @@ const (

// DisplayName is the component's name for an SBOM package entry.
//
// Detected components carry Name from the scan result; declared ones (sourced
// from a manifest) carry only a PURL, so the name is taken from its last
// segment — "pkg:golang/github.com/spf13/cobra" yields "cobra". Falling back to
// the whole PURL would put "pkg:golang/github.com/spf13/cobra" in a field that
// consumers render as a package name.
// A Go module's name is its full module path, whatever the scan called it:
// scanners such as Grype look Go vulnerabilities up by that name, so
// "github.com/spf13/cobra" shortened to "cobra" matches nothing.
//
// Otherwise detected components carry Name from the scan result; declared ones
// (sourced from a manifest) carry only a PURL, so the name is taken from its
// last segment — "pkg:npm/lodash" yields "lodash". Falling back to the whole
// PURL would put a PURL in a field that consumers render as a package name.
func (c Component) DisplayName() string {
if path, ok := goModulePath(c.Purl); ok {
return path
}
if c.Name != "" {
return c.Name
}
Expand All @@ -215,6 +221,20 @@ func (c Component) DisplayName() string {
return name
}

// goModulePath returns the module path of a "pkg:golang/..." PURL, without any
// version, qualifiers or subpath, and false for any other PURL.
func goModulePath(purl string) (string, bool) {
path, ok := strings.CutPrefix(purl, "pkg:golang/")
if !ok {
return "", false
}
if i := strings.IndexAny(path, "@?#"); i >= 0 {
path = path[:i]
}
path = strings.TrimSuffix(path, "/")
return path, path != ""
}

// IsDeclared reports whether the component is a declared dependency rather than a
// scan-detected match. The zero-value scope counts as detected.
func (c Component) IsDeclared() bool {
Expand Down
13 changes: 8 additions & 5 deletions pkg/sbom/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@ import (
"testing"
)

// The SBOM name field must hold a package name, not a PURL. Detected components
// carry one from the scan result; declared ones (sourced from a manifest) carry
// only a PURL, so the last segment stands in — otherwise every declared package
// in an SBOM would be named "pkg:golang/github.com/spf13/cobra".
// The SBOM name field must hold a package name, not a PURL. A Go module is named
// by its full module path, which is what scanners look Go vulnerabilities up by.
// Other detected components carry a name from the scan result; declared ones
// (sourced from a manifest) carry only a PURL, so the last segment stands in.
func TestComponentDisplayName(t *testing.T) {
for _, tc := range []struct {
name string
comp Component
want string
}{
{"detected keeps its name", Component{Purl: "pkg:github/madler/zlib", Name: "zlib"}, "zlib"},
{"declared falls back to the purl tail", Component{Purl: "pkg:golang/github.com/spf13/cobra"}, "cobra"},
{"declared go module is its full module path", Component{Purl: "pkg:golang/github.com/spf13/cobra"}, "github.com/spf13/cobra"},
{"detected go module is its full module path too", Component{Purl: "pkg:golang/go.opentelemetry.io/otel/sdk", Name: "sdk"}, "go.opentelemetry.io/otel/sdk"},
{"go purl drops version, qualifiers and subpath", Component{Purl: "pkg:golang/golang.org/x/net@v0.19.0?type=module#http2"}, "golang.org/x/net"},
{"empty go purl falls back", Component{Purl: "pkg:golang/"}, "pkg:golang/"},
{"short purl", Component{Purl: "pkg:npm/lodash"}, "lodash"},
{"maven namespace", Component{Purl: "pkg:maven/org.apache.commons/commons-lang3"}, "commons-lang3"},
{"no slash at all", Component{Purl: "weird"}, "weird"},
Expand Down
Loading