diff --git a/pkg/sbom/cyclonedx_test.go b/pkg/sbom/cyclonedx_test.go index 8e46e59..dfb2e48 100644 --- a/pkg/sbom/cyclonedx_test.go +++ b/pkg/sbom/cyclonedx_test.go @@ -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 { diff --git a/pkg/sbom/inventory.go b/pkg/sbom/inventory.go index 46280ac..38d7071 100644 --- a/pkg/sbom/inventory.go +++ b/pkg/sbom/inventory.go @@ -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 } @@ -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 { diff --git a/pkg/sbom/inventory_test.go b/pkg/sbom/inventory_test.go index 1ae6c21..79805b0 100644 --- a/pkg/sbom/inventory_test.go +++ b/pkg/sbom/inventory_test.go @@ -29,10 +29,10 @@ 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 @@ -40,7 +40,10 @@ func TestComponentDisplayName(t *testing.T) { 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"},