Skip to content

Commit 5f69937

Browse files
authored
fix(sbom): fix error when parent of SPDX Relationships is not a package. (aquasecurity#6399)
1 parent 258d153 commit 5f69937

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

pkg/sbom/core/bom.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,20 @@ func (b *BOM) AddComponent(c *Component) {
238238
}
239239

240240
func (b *BOM) AddRelationship(parent, child *Component, relationshipType RelationshipType) {
241+
// Check the wrong parent to avoid `panic`
242+
if parent == nil {
243+
return
244+
}
241245
if parent.id == uuid.Nil {
242246
b.AddComponent(parent)
243247
}
244248

245249
if child == nil {
246-
b.relationships[parent.id] = nil // Meaning no dependencies
250+
// It is possible that `relationships` already contains this parent.
251+
// Check this to avoid overwriting.
252+
if _, ok := b.relationships[parent.id]; !ok {
253+
b.relationships[parent.id] = nil // Meaning no dependencies
254+
}
247255
return
248256
}
249257

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"files": [
3+
{
4+
"fileName": "./Modules/Microsoft.PowerShell.PSResourceGet/_manifest/spdx_2.2/manifest.spdx.json",
5+
"SPDXID": "SPDXRef-File--Modules-Microsoft.PowerShell.PSResourceGet--manifest-spdx-2.2-manifest.spdx.json-2B9FB98F5CA97DC84FD382A8F8E68F663C003362",
6+
"checksums": [
7+
{
8+
"algorithm": "SHA256",
9+
"checksumValue": "4201b0989938842ef8c11a006184e0b1466bd7f9bb2af61d89a4c8318d43466e"
10+
},
11+
{
12+
"algorithm": "SHA1",
13+
"checksumValue": "2b9fb98f5ca97dc84fd382a8f8e68f663c003362"
14+
}
15+
],
16+
"licenseConcluded": "NOASSERTION",
17+
"licenseInfoInFiles": [
18+
"NOASSERTION"
19+
],
20+
"copyrightText": "NOASSERTION",
21+
"fileTypes": [
22+
"SPDX"
23+
]
24+
}
25+
],
26+
"externalDocumentRefs": [],
27+
"relationships": [
28+
{
29+
"relationshipType": "DESCRIBES",
30+
"relatedSpdxElement": "SPDXRef-RootPackage",
31+
"spdxElementId": "SPDXRef-DOCUMENT"
32+
},
33+
{
34+
"relationshipType": "DESCRIBED_BY",
35+
"relatedSpdxElement": "SPDXRef-DOCUMENT",
36+
"spdxElementId": "SPDXRef-File--Modules-Microsoft.PowerShell.PSResourceGet--manifest-spdx-2.2-manifest.spdx.json-2B9FB98F5CA97DC84FD382A8F8E68F663C003362"
37+
}
38+
],
39+
"spdxVersion": "SPDX-2.2",
40+
"dataLicense": "CC0-1.0",
41+
"SPDXID": "SPDXRef-DOCUMENT",
42+
"name": "PowerShell Linux Arm32 7.5.0-preview.2",
43+
"documentNamespace": "https://sbom.microsoft/1:2QSF7qZlbE-F7QrUJlEo7g:pHp_nUFvDUijZ4LrJ4RhoQ/696:458654/PowerShell%20Linux%20Arm32:7.5.0-preview.2:pDkyTHXmgUOdzSXIq9CiqA",
44+
"creationInfo": {
45+
"created": "2024-02-22T00:43:53Z",
46+
"creators": [
47+
"Organization: Microsoft",
48+
"Tool: Microsoft.SBOMTool-2.2.3"
49+
]
50+
},
51+
"documentDescribes": [
52+
"SPDXRef-RootPackage"
53+
]
54+
}

pkg/sbom/spdx/unmarshal.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,16 @@ func (s *SPDX) unmarshal(spdxDocument *spdx.Document) error {
8787
continue
8888
}
8989

90-
compA := components[rel.RefA.ElementRefID]
91-
compB := components[rel.RefB.ElementRefID]
90+
compA, ok := components[rel.RefA.ElementRefID]
91+
if !ok { // Skip if parent is not Package
92+
continue
93+
}
94+
95+
compB, ok := components[rel.RefB.ElementRefID]
96+
if !ok { // Skip if child is not Package
97+
continue
98+
}
99+
92100
s.BOM.AddRelationship(compA, compB, s.parseRelationshipType(rel.Relationship))
93101
}
94102

pkg/sbom/spdx/unmarshal_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ func TestUnmarshaler_Unmarshal(t *testing.T) {
314314
},
315315
},
316316
},
317+
{
318+
name: "happy path with file as parent of relationship",
319+
inputFile: "testdata/happy/with-file-as-relationship-parent.json",
320+
want: types.SBOM{},
321+
},
317322
{
318323
name: "happy path only os component",
319324
inputFile: "testdata/happy/os-only-bom.json",

0 commit comments

Comments
 (0)