-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(sbom): merge in-graph and out-of-graph OS packages in scan results #9194
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
Merged
DmitriyLewen
merged 3 commits into
aquasecurity:main
from
knqyf263:fix-sbom-orphan-packages
Jul 16, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,24 @@ var ( | |
| }, | ||
| Licenses: []string{"GPL-2.0"}, | ||
| } | ||
|
|
||
| orphanedApkComponent = &core.Component{ | ||
| Type: core.TypeLibrary, | ||
| Name: "orphaned-package", | ||
| Version: "1.0.0", | ||
| PkgIdentifier: ftypes.PkgIdentifier{ | ||
| PURL: &packageurl.PackageURL{ | ||
| Type: packageurl.TypeApk, | ||
| Name: "orphaned-package", | ||
| Version: "1.0.0", | ||
| Qualifiers: packageurl.Qualifiers{ | ||
| {Key: "arch", Value: "aarch64"}, | ||
| {Key: "distro", Value: "wolfi-20230201"}, | ||
| }, | ||
| }, | ||
| }, | ||
| Licenses: []string{"MIT"}, | ||
| } | ||
| ) | ||
|
|
||
| func TestDecoder_Decode_OSPackages(t *testing.T) { | ||
|
|
@@ -141,16 +159,17 @@ func TestDecoder_Decode_OSPackages(t *testing.T) { | |
| }, | ||
| }, | ||
| { | ||
| name: "multiple OS packages without OS metadata should be included", | ||
| name: "multiple OS packages without OS metadata should be included and merged", | ||
| setupBOM: func() *core.BOM { | ||
| bom := core.NewBOM(core.Options{}) | ||
| bom.SerialNumber = "test-multiple-no-os" | ||
| bom.Version = 1 | ||
|
|
||
| // Add multiple APK packages | ||
| // Add multiple APK packages including orphaned package | ||
| bom.AddComponent(apkToolsComponent) | ||
| bom.AddComponent(busyboxComponent) | ||
| bom.AddComponent(caCertificatesComponent) | ||
| bom.AddComponent(orphanedApkComponent) | ||
|
|
||
| return bom | ||
| }, | ||
|
|
@@ -197,6 +216,18 @@ func TestDecoder_Decode_OSPackages(t *testing.T) { | |
| PURL: caCertificatesComponent.PkgIdentifier.PURL, | ||
| }, | ||
| }, | ||
| { | ||
| ID: "[email protected]", | ||
| Name: "orphaned-package", | ||
| Version: "1.0.0", | ||
| Arch: "aarch64", | ||
| SrcName: "orphaned-package", | ||
| SrcVersion: "1.0.0", | ||
| Licenses: []string{"MIT"}, | ||
| Identifier: ftypes.PkgIdentifier{ | ||
| PURL: orphanedApkComponent.PkgIdentifier.PURL, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
|
|
@@ -233,6 +264,66 @@ func TestDecoder_Decode_OSPackages(t *testing.T) { | |
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "mixed OS packages (in-graph and out-of-graph) should be merged", | ||
| setupBOM: func() *core.BOM { | ||
| bom := core.NewBOM(core.Options{}) | ||
| bom.SerialNumber = "test-mixed-os-packages" | ||
| bom.Version = 1 | ||
|
|
||
| // Add OS component | ||
| bom.AddComponent(wolfiOSComponent) | ||
|
|
||
| // Add OS packages - busybox is connected to OS (in-graph) | ||
| bom.AddComponent(busyboxComponent) | ||
| // Add orphaned package not connected to OS (out-of-graph) | ||
| bom.AddComponent(orphanedApkComponent) | ||
|
|
||
| // Create relationship between OS and busybox only | ||
| bom.AddRelationship(wolfiOSComponent, busyboxComponent, core.RelationshipContains) | ||
| // orphanedApkComponent is not connected to OS component | ||
|
|
||
| return bom | ||
| }, | ||
| wantSBOM: types.SBOM{ | ||
| Metadata: types.Metadata{ | ||
| OS: &ftypes.OS{ | ||
| Family: ftypes.Wolfi, | ||
| Name: "20230201", | ||
| }, | ||
| }, | ||
| Packages: []ftypes.PackageInfo{ | ||
| { | ||
| Packages: ftypes.Packages{ | ||
| { | ||
| ID: "[email protected]", | ||
| Name: "busybox", | ||
| Version: "1.37.0-r42", | ||
| Arch: "aarch64", | ||
| SrcName: "busybox", | ||
| SrcVersion: "1.37.0-r42", | ||
| Licenses: []string{"GPL-2.0-only"}, | ||
| Identifier: ftypes.PkgIdentifier{ | ||
| PURL: busyboxComponent.PkgIdentifier.PURL, | ||
| }, | ||
| }, | ||
| { | ||
| ID: "[email protected]", | ||
| Name: "orphaned-package", | ||
| Version: "1.0.0", | ||
| Arch: "aarch64", | ||
| SrcName: "orphaned-package", | ||
| SrcVersion: "1.0.0", | ||
| Licenses: []string{"MIT"}, | ||
| Identifier: ftypes.PkgIdentifier{ | ||
| PURL: orphanedApkComponent.PkgIdentifier.PURL, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
|
|
@@ -252,7 +343,7 @@ func TestDecoder_Decode_OSPackages(t *testing.T) { | |
| tt.wantSBOM.BOM = gotSBOM.BOM | ||
|
|
||
| // Compare the entire SBOM structure | ||
| assert.Equal(t, tt.wantSBOM, gotSBOM) | ||
| assert.EqualExportedValues(t, tt.wantSBOM, gotSBOM) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to use this difficalt logic?
I understand you idea - detected OS packages don't have filepath (i mean PackageInfo struct):
trivy/pkg/sbom/io/decode.go
Lines 332 to 338 in e76c7ee
But in
addOrphanPkgsfunctionsbom.Packagesalways contains 0 or 1 element.And we check only first iteration of the
osPkgMaploop.trivy/pkg/sbom/io/decode.go
Line 408 in e76c7ee
It means that we don't need to check filePath and we can just add orphan packages into
sbom.Packages[0].e.g.:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a possibility that OS package file paths may be filled in the future, and I'm concerned that writing logic that's easily broken when other parts are changed could lead to bugs. However, as things stand now, you're right, so I simplified it.
1f0e04e