Skip to content

Commit bd5baaf

Browse files
fix(sbom): preserve OS packages from multiple SBOMs (#8325)
Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
1 parent 1d5ab92 commit bd5baaf

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

pkg/fanal/analyzer/sbom/sbom.go

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func (a sbomAnalyzer) Analyze(ctx context.Context, input analyzer.AnalysisInput)
5252
handleBitnamiImages(path.Dir(input.FilePath), bom)
5353
}
5454

55+
// Add the filePath to avoid overwriting OS packages when merging packages from multiple SBOM files.
56+
// cf. https://github.com/aquasecurity/trivy/issues/8324
57+
for i, pkgInfo := range bom.Packages {
58+
bom.Packages[i].FilePath = path.Join(input.FilePath, pkgInfo.FilePath)
59+
}
60+
5561
// FilePath for apps with aggregatingTypes is empty.
5662
// Set the SBOM file path as Application.FilePath to correctly overwrite applications when merging layers.
5763
for i, app := range bom.Applications {

pkg/fanal/analyzer/sbom/sbom_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,42 @@ func Test_sbomAnalyzer_Analyze(t *testing.T) {
238238
},
239239
wantErr: require.NoError,
240240
},
241+
{
242+
name: "valid ca-certificates spdx file",
243+
file: "testdata/ca-certificates.spdx.json",
244+
filePath: "opt/bitnami/ca-certificates/.spdx-ca-certificates.spdx",
245+
want: &analyzer.AnalysisResult{
246+
PackageInfos: []types.PackageInfo{
247+
{
248+
FilePath: "opt/bitnami/ca-certificates/.spdx-ca-certificates.spdx",
249+
Packages: types.Packages{
250+
{
251+
ID: "ca-certificates@20230311",
252+
Name: "ca-certificates",
253+
Version: "20230311",
254+
Arch: "all",
255+
SrcName: "ca-certificates",
256+
SrcVersion: "20230311",
257+
Licenses: []string{"GPL-2.0-or-later AND GPL-2.0-only AND MPL-2.0"},
258+
Identifier: types.PkgIdentifier{
259+
PURL: &packageurl.PackageURL{
260+
Type: packageurl.TypeDebian,
261+
Namespace: "debian",
262+
Name: "ca-certificates",
263+
Version: "20230311",
264+
Qualifiers: packageurl.Qualifiers{
265+
{Key: "arch", Value: "all"},
266+
{Key: "distro", Value: "debian-12.9"},
267+
},
268+
},
269+
},
270+
},
271+
},
272+
},
273+
},
274+
},
275+
wantErr: require.NoError,
276+
},
241277
{
242278
name: "invalid spdx file",
243279
file: "testdata/invalid_spdx.json",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"spdxVersion": "SPDX-2.3",
3+
"dataLicense": "CC0-1.0",
4+
"SPDXID": "SPDXRef-DOCUMENT",
5+
"name": "host",
6+
"documentNamespace": "http://aquasecurity.github.io/trivy/filesystem/host-1022538e-83fe-4aa7-8718-a78839114c83",
7+
"creationInfo": {
8+
"creators": [
9+
"Organization: aquasecurity",
10+
"Tool: trivy-0.58.1"
11+
],
12+
"created": "2025-01-30T11:30:05Z"
13+
},
14+
"packages": [
15+
{
16+
"name": "ca-certificates",
17+
"SPDXID": "SPDXRef-Package-c1d4029824045f75",
18+
"versionInfo": "20230311",
19+
"supplier": "Organization: Julien Cristau <jcristau@debian.org>",
20+
"downloadLocation": "NONE",
21+
"filesAnalyzed": false,
22+
"sourceInfo": "built package from: ca-certificates 20230311",
23+
"licenseConcluded": "GPL-2.0-or-later AND GPL-2.0-only AND MPL-2.0",
24+
"licenseDeclared": "GPL-2.0-or-later AND GPL-2.0-only AND MPL-2.0",
25+
"externalRefs": [
26+
{
27+
"referenceCategory": "PACKAGE-MANAGER",
28+
"referenceType": "purl",
29+
"referenceLocator": "pkg:deb/debian/ca-certificates@20230311?arch=all&distro=debian-12.9"
30+
}
31+
],
32+
"primaryPackagePurpose": "LIBRARY"
33+
},
34+
{
35+
"name": "debian",
36+
"SPDXID": "SPDXRef-OperatingSystem-23c1e7dfb45d904f",
37+
"versionInfo": "12.9",
38+
"downloadLocation": "NONE",
39+
"filesAnalyzed": false,
40+
"primaryPackagePurpose": "OPERATING-SYSTEM"
41+
}
42+
],
43+
"relationships": [
44+
{
45+
"spdxElementId": "SPDXRef-OperatingSystem-23c1e7dfb45d904f",
46+
"relatedSpdxElement": "SPDXRef-Package-c1d4029824045f75",
47+
"relationshipType": "CONTAINS"
48+
},
49+
{
50+
"spdxElementId": "SPDXRef-DOCUMENT",
51+
"relatedSpdxElement": "SPDXRef-OperatingSystem-23c1e7dfb45d904f",
52+
"relationshipType": "DESCRIBES"
53+
}
54+
]
55+
}

0 commit comments

Comments
 (0)