Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sbom): fix error when parent of SPDX Relationships is not a package. #6399

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/sbom/core/bom.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,20 @@ func (b *BOM) AddComponent(c *Component) {
}

func (b *BOM) AddRelationship(parent, child *Component, relationshipType RelationshipType) {
// Check the wrong parent to avoid `panic`
if parent == nil {
return
}
Comment on lines +241 to +244
Copy link
Contributor Author

@DmitriyLewen DmitriyLewen Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added parent check in SPDX package (like we do for CycloneDX). But I added 1 more check here so that we don't panic if we use this function later somewhere else.

This may be additional change or we may want to add a log message here.
@knqyf263 wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

if parent.id == uuid.Nil {
b.AddComponent(parent)
}

if child == nil {
b.relationships[parent.id] = nil // Meaning no dependencies
// It is possible that `relationships` already contains this parent.
// Check this to avoid overwriting.
if _, ok := b.relationships[parent.id]; !ok {
b.relationships[parent.id] = nil // Meaning no dependencies
}
knqyf263 marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"files": [
{
"fileName": "./Modules/Microsoft.PowerShell.PSResourceGet/_manifest/spdx_2.2/manifest.spdx.json",
"SPDXID": "SPDXRef-File--Modules-Microsoft.PowerShell.PSResourceGet--manifest-spdx-2.2-manifest.spdx.json-2B9FB98F5CA97DC84FD382A8F8E68F663C003362",
"checksums": [
{
"algorithm": "SHA256",
"checksumValue": "4201b0989938842ef8c11a006184e0b1466bd7f9bb2af61d89a4c8318d43466e"
},
{
"algorithm": "SHA1",
"checksumValue": "2b9fb98f5ca97dc84fd382a8f8e68f663c003362"
}
],
"licenseConcluded": "NOASSERTION",
"licenseInfoInFiles": [
"NOASSERTION"
],
"copyrightText": "NOASSERTION",
"fileTypes": [
"SPDX"
]
}
],
"externalDocumentRefs": [],
"relationships": [
{
"relationshipType": "DESCRIBES",
"relatedSpdxElement": "SPDXRef-RootPackage",
"spdxElementId": "SPDXRef-DOCUMENT"
},
{
"relationshipType": "DESCRIBED_BY",
"relatedSpdxElement": "SPDXRef-DOCUMENT",
"spdxElementId": "SPDXRef-File--Modules-Microsoft.PowerShell.PSResourceGet--manifest-spdx-2.2-manifest.spdx.json-2B9FB98F5CA97DC84FD382A8F8E68F663C003362"
}
],
"spdxVersion": "SPDX-2.2",
"dataLicense": "CC0-1.0",
"SPDXID": "SPDXRef-DOCUMENT",
"name": "PowerShell Linux Arm32 7.5.0-preview.2",
"documentNamespace": "https://sbom.microsoft/1:2QSF7qZlbE-F7QrUJlEo7g:pHp_nUFvDUijZ4LrJ4RhoQ/696:458654/PowerShell%20Linux%20Arm32:7.5.0-preview.2:pDkyTHXmgUOdzSXIq9CiqA",
"creationInfo": {
"created": "2024-02-22T00:43:53Z",
"creators": [
"Organization: Microsoft",
"Tool: Microsoft.SBOMTool-2.2.3"
]
},
"documentDescribes": [
"SPDXRef-RootPackage"
]
}
12 changes: 10 additions & 2 deletions pkg/sbom/spdx/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ func (s *SPDX) unmarshal(spdxDocument *spdx.Document) error {
continue
}

compA := components[rel.RefA.ElementRefID]
compB := components[rel.RefB.ElementRefID]
compA, ok := components[rel.RefA.ElementRefID]
if !ok { // Skip if parent is not Package
continue
}

compB, ok := components[rel.RefB.ElementRefID]
if !ok { // Skip if child is not Package
continue
}

s.BOM.AddRelationship(compA, compB, s.parseRelationshipType(rel.Relationship))
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/sbom/spdx/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ func TestUnmarshaler_Unmarshal(t *testing.T) {
},
},
},
{
name: "happy path with file as parent of relationship",
inputFile: "testdata/happy/with-file-as-relationship-parent.json",
want: types.SBOM{},
},
{
name: "happy path only os component",
inputFile: "testdata/happy/os-only-bom.json",
Expand Down