Skip to content

Commit f92ea09

Browse files
authored
fix(sbom): fix panic for convert mode when scanning json file derived from sbom file (#6808)
1 parent aa59489 commit f92ea09

File tree

2 files changed

+163
-2
lines changed

2 files changed

+163
-2
lines changed

pkg/sbom/io/encode.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,15 @@ func (e *Encoder) rootComponent(r types.Report) (*core.Component, error) {
8383
root.Type = core.TypeFilesystem
8484
case artifact.TypeRepository:
8585
root.Type = core.TypeRepository
86-
case artifact.TypeCycloneDX:
87-
return r.BOM.Root(), nil
86+
case artifact.TypeCycloneDX, artifact.TypeSPDX:
87+
// When we scan SBOM file
88+
if r.BOM != nil {
89+
return r.BOM.Root(), nil
90+
}
91+
// When we scan a `json` file (meaning a file in `json` format) which was created from the SBOM file.
92+
// e.g. for use in `convert` mode.
93+
// See https://github.com/aquasecurity/trivy/issues/6780
94+
root.Type = core.TypeFilesystem
8895
}
8996

9097
if r.Metadata.Size != 0 {

pkg/sbom/io/encode_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,97 @@ func TestEncoder_Encode(t *testing.T) {
535535
},
536536
wantVulns: make(map[uuid.UUID][]core.Vulnerability),
537537
},
538+
{
539+
name: "SBOM file",
540+
report: types.Report{
541+
SchemaVersion: 2,
542+
ArtifactName: "report.cdx.json",
543+
ArtifactType: artifact.TypeCycloneDX,
544+
Results: []types.Result{
545+
{
546+
Target: "Java",
547+
Type: ftypes.Jar,
548+
Class: types.ClassLangPkg,
549+
Packages: []ftypes.Package{
550+
{
551+
ID: "org.apache.logging.log4j:log4j-core:2.23.1",
552+
Name: "org.apache.logging.log4j:log4j-core",
553+
Version: "2.23.1",
554+
Identifier: ftypes.PkgIdentifier{
555+
PURL: &packageurl.PackageURL{
556+
Type: packageurl.TypeMaven,
557+
Namespace: "org.apache.logging.log4j",
558+
Name: "log4j-core",
559+
Version: "2.23.1",
560+
},
561+
},
562+
FilePath: "log4j-core-2.23.1.jar",
563+
},
564+
},
565+
},
566+
},
567+
BOM: newTestBOM(t),
568+
},
569+
wantComponents: map[uuid.UUID]*core.Component{
570+
uuid.MustParse("2ff14136-e09f-4df9-80ea-000000000001"): appComponent,
571+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000001"): libComponent,
572+
},
573+
wantRels: map[uuid.UUID][]core.Relationship{
574+
uuid.MustParse("2ff14136-e09f-4df9-80ea-000000000001"): {
575+
{
576+
Dependency: uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000001"),
577+
Type: core.RelationshipContains,
578+
},
579+
},
580+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000001"): nil,
581+
},
582+
wantVulns: make(map[uuid.UUID][]core.Vulnerability),
583+
},
584+
{
585+
name: "json file created from SBOM file (BOM is empty)",
586+
report: types.Report{
587+
SchemaVersion: 2,
588+
ArtifactName: "report.cdx.json",
589+
ArtifactType: artifact.TypeCycloneDX,
590+
Results: []types.Result{
591+
{
592+
Target: "Java",
593+
Type: ftypes.Jar,
594+
Class: types.ClassLangPkg,
595+
Packages: []ftypes.Package{
596+
{
597+
ID: "org.apache.logging.log4j:log4j-core:2.23.1",
598+
Name: "org.apache.logging.log4j:log4j-core",
599+
Version: "2.23.1",
600+
Identifier: ftypes.PkgIdentifier{
601+
PURL: &packageurl.PackageURL{
602+
Type: packageurl.TypeMaven,
603+
Namespace: "org.apache.logging.log4j",
604+
Name: "log4j-core",
605+
Version: "2.23.1",
606+
},
607+
},
608+
FilePath: "log4j-core-2.23.1.jar",
609+
},
610+
},
611+
},
612+
},
613+
},
614+
wantComponents: map[uuid.UUID]*core.Component{
615+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000001"): fsComponent,
616+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000002"): libComponent,
617+
},
618+
wantRels: map[uuid.UUID][]core.Relationship{
619+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000001"): {
620+
{
621+
Dependency: uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000002"),
622+
Type: core.RelationshipContains,
623+
},
624+
},
625+
uuid.MustParse("3ff14136-e09f-4df9-80ea-000000000002"): nil,
626+
},
627+
wantVulns: make(map[uuid.UUID][]core.Vulnerability),
628+
},
538629
{
539630
name: "invalid digest",
540631
report: types.Report{
@@ -580,3 +671,66 @@ func TestEncoder_Encode(t *testing.T) {
580671
})
581672
}
582673
}
674+
675+
var (
676+
appComponent = &core.Component{
677+
Root: true,
678+
Type: core.TypeApplication,
679+
Name: "log4j-core-2.23.1.jar",
680+
}
681+
fsComponent = &core.Component{
682+
Root: true,
683+
Type: core.TypeFilesystem,
684+
Name: "report.cdx.json",
685+
PkgIdentifier: ftypes.PkgIdentifier{
686+
BOMRef: "3ff14136-e09f-4df9-80ea-000000000001",
687+
},
688+
Properties: core.Properties{
689+
{
690+
Name: "SchemaVersion",
691+
Value: "2",
692+
},
693+
},
694+
}
695+
libComponent = &core.Component{
696+
Type: core.TypeLibrary,
697+
Name: "log4j-core",
698+
Group: "org.apache.logging.log4j",
699+
Version: "2.23.1",
700+
PkgIdentifier: ftypes.PkgIdentifier{
701+
BOMRef: "pkg:maven/org.apache.logging.log4j/log4j-core@2.23.1",
702+
PURL: &packageurl.PackageURL{
703+
Type: packageurl.TypeMaven,
704+
Namespace: "org.apache.logging.log4j",
705+
Name: "log4j-core",
706+
Version: "2.23.1",
707+
},
708+
},
709+
Files: []core.File{
710+
{
711+
Path: "log4j-core-2.23.1.jar",
712+
},
713+
},
714+
Properties: core.Properties{
715+
{
716+
Name: "FilePath",
717+
Value: "log4j-core-2.23.1.jar",
718+
},
719+
{
720+
Name: "PkgID",
721+
Value: "org.apache.logging.log4j:log4j-core:2.23.1",
722+
},
723+
{
724+
Name: "PkgType",
725+
Value: "jar",
726+
},
727+
},
728+
}
729+
)
730+
731+
func newTestBOM(t *testing.T) *core.BOM {
732+
uuid.SetFakeUUID(t, "2ff14136-e09f-4df9-80ea-%012d")
733+
bom := core.NewBOM(core.Options{})
734+
bom.AddComponent(appComponent)
735+
return bom
736+
}

0 commit comments

Comments
 (0)