Skip to content

Commit 3d4ae8b

Browse files
authored
fix(sbom): fix panic when scanning SBOM file without root component into SBOM format (#7051)
1 parent 55ccd06 commit 3d4ae8b

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

pkg/sbom/io/encode.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ func (e *Encoder) rootComponent(r types.Report) (*core.Component, error) {
8585
root.Type = core.TypeRepository
8686
case artifact.TypeCycloneDX, artifact.TypeSPDX:
8787
// When we scan SBOM file
88-
if r.BOM != nil {
88+
// If SBOM file doesn't contain root component - use filesystem
89+
if r.BOM != nil && r.BOM.Root() != nil {
8990
return r.BOM.Root(), nil
9091
}
9192
// When we scan a `json` file (meaning a file in `json` format) which was created from the SBOM file.

pkg/sbom/io/encode_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,53 @@ func TestEncoder_Encode(t *testing.T) {
705705
},
706706
wantVulns: make(map[uuid.UUID][]core.Vulnerability),
707707
},
708+
{
709+
name: "SBOM file without root component",
710+
report: types.Report{
711+
SchemaVersion: 2,
712+
ArtifactName: "report.cdx.json",
713+
ArtifactType: artifact.TypeCycloneDX,
714+
Results: []types.Result{
715+
{
716+
Target: "Java",
717+
Type: ftypes.Jar,
718+
Class: types.ClassLangPkg,
719+
Packages: []ftypes.Package{
720+
{
721+
ID: "org.apache.logging.log4j:log4j-core:2.23.1",
722+
Name: "org.apache.logging.log4j:log4j-core",
723+
Version: "2.23.1",
724+
Identifier: ftypes.PkgIdentifier{
725+
UID: "6C0AE96901617503",
726+
PURL: &packageurl.PackageURL{
727+
Type: packageurl.TypeMaven,
728+
Namespace: "org.apache.logging.log4j",
729+
Name: "log4j-core",
730+
Version: "2.23.1",
731+
},
732+
},
733+
FilePath: "log4j-core-2.23.1.jar",
734+
},
735+
},
736+
},
737+
},
738+
BOM: newTestBOM2(t),
739+
},
740+
wantComponents: map[uuid.UUID]*core.Component{
741+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000001"): fsComponent,
742+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000002"): libComponent,
743+
},
744+
wantRels: map[uuid.UUID][]core.Relationship{
745+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000001"): {
746+
{
747+
Dependency: uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000002"),
748+
Type: core.RelationshipContains,
749+
},
750+
},
751+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000002"): nil,
752+
},
753+
wantVulns: make(map[uuid.UUID][]core.Vulnerability),
754+
},
708755
{
709756
name: "json file created from SBOM file (BOM is empty)",
710757
report: types.Report{
@@ -860,3 +907,11 @@ func newTestBOM(t *testing.T) *core.BOM {
860907
bom.AddComponent(appComponent)
861908
return bom
862909
}
910+
911+
// BOM without root component
912+
func newTestBOM2(t *testing.T) *core.BOM {
913+
uuid.SetFakeUUID(t, "2ff14136-e09f-4df9-80ea-%012d")
914+
bom := core.NewBOM(core.Options{})
915+
bom.AddComponent(libComponent)
916+
return bom
917+
}

0 commit comments

Comments
 (0)