Skip to content

Commit 3be8d01

Browse files
authored
Merge pull request #1754 from l3montree-dev/1740-sbom-upload-fails-when-a-component-has-no-name
Set component Name to BOMRef if missing in SBOMGraphFromCycloneDX
2 parents 2f5480f + 4a788a5 commit 3be8d01

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

normalize/sbom_graph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ func SBOMGraphFromCycloneDX(bom *cdx.BOM, artifactName, infoSourceID string, kee
14961496
return nil, fmt.Errorf("component at index %d has missing BOMRef", idx)
14971497
}
14981498
if comp.Name == "" {
1499-
return nil, fmt.Errorf("component at index %d (%s) has missing Name", idx, comp.BOMRef)
1499+
comp.Name = comp.BOMRef // Default to BOMRef if name is missing
15001500
}
15011501

15021502
// Check for duplicate BOMRef

normalize/sbom_graph_cyclonedx_validation_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package normalize
33
import (
44
"bytes"
55
"encoding/json"
6+
"slices"
67
"testing"
78

89
cdx "github.com/CycloneDX/cyclonedx-go"
@@ -326,7 +327,7 @@ func TestInvalidComponentTypeValidation(t *testing.T) {
326327

327328
// TestSchemaBreakers tests various ways to break CycloneDX 1.6 schema validation
328329
func TestSchemaBreakers(t *testing.T) {
329-
t.Run("missing required component name returns error when trying to construct sbom graph", func(t *testing.T) {
330+
t.Run("missing component name defaults to bomRef and does not break graph construction", func(t *testing.T) {
330331
bom := &cdx.BOM{
331332
SpecVersion: cdx.SpecVersion1_6,
332333
BOMFormat: "CycloneDX",
@@ -341,16 +342,29 @@ func TestSchemaBreakers(t *testing.T) {
341342
Components: &[]cdx.Component{
342343
{
343344
BOMRef: "pkg:npm/test@1.0.0",
344-
// Name field is empty - should cause error in graph construction
345-
Type: cdx.ComponentTypeLibrary,
345+
// Name field is empty, so name would be bomRef
346+
Type: cdx.ComponentTypeLibrary,
347+
Version: "1.0.0",
348+
PackageURL: "pkg:npm/test@1.0.0",
349+
},
350+
},
351+
Dependencies: &[]cdx.Dependency{
352+
{
353+
Ref: "root",
354+
Dependencies: &[]string{"pkg:npm/test@1.0.0"},
346355
},
347356
},
348357
}
349358

350-
// SBOMGraphFromCycloneDX should return error for missing component name
359+
// SBOMGraphFromCycloneDX should handle this gracefully by using bomRef as the name if name is missing
351360
graph, err := SBOMGraphFromCycloneDX(bom, "test-artifact", "test-source", false)
352-
assert.NotNil(t, err, "Should return error for component with missing name")
353-
assert.Nil(t, graph, "Graph should be nil when error occurs")
361+
assert.NoError(t, err)
362+
363+
component := slices.Collect(graph.Components())[0]
364+
assert.Equal(t, "pkg:npm/test@1.0.0", component.Component.Name, "Component name should default to bomRef when name is missing")
365+
366+
assert.NoError(t, err)
367+
assert.NotNil(t, graph, "Graph should be created even with missing component name (bomRef will be used as name)")
354368
})
355369

356370
t.Run("invalid scope value returns error when building sbom graph", func(t *testing.T) {

0 commit comments

Comments
 (0)