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 wrong overwriting of applications obtained from different sbom files but having same app type #8052

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
9 changes: 9 additions & 0 deletions pkg/fanal/analyzer/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path"
"slices"
"strings"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -51,6 +52,14 @@ func (a sbomAnalyzer) Analyze(ctx context.Context, input analyzer.AnalysisInput)
handleBitnamiImages(path.Dir(input.FilePath), bom)
}

// FilePath for apps with aggregatingTypes is empty.
// Set the SBOM file path as Application.FilePath to correctly overwrite applications when merging layers.
for i, app := range bom.Applications {
if slices.Contains(ftypes.AggregatingTypes, app.Type) && app.FilePath == "" {
bom.Applications[i].FilePath = input.FilePath
}
}

return &analyzer.AnalysisResult{
PackageInfos: bom.Packages,
Applications: bom.Applications,
Expand Down
58 changes: 30 additions & 28 deletions pkg/fanal/analyzer/sbom/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,34 @@ func Test_sbomAnalyzer_Analyze(t *testing.T) {
want: &analyzer.AnalysisResult{
Applications: []types.Application{
{
Type: types.Jar,
Type: types.Bitnami,
FilePath: "opt/bitnami/elasticsearch",
Packages: types.Packages{
{
ID: "[email protected]",
Name: "elasticsearch",
Version: "8.9.1",
Arch: "arm64",
Licenses: []string{"Elastic-2.0"},
Identifier: types.PkgIdentifier{
PURL: &packageurl.PackageURL{
Type: packageurl.TypeBitnami,
Name: "elasticsearch",
Version: "8.9.1",
Qualifiers: packageurl.Qualifiers{
{
Key: "arch",
Value: "arm64",
},
},
},
},
},
},
},
{
Type: types.Jar,
FilePath: "opt/bitnami/elasticsearch/.spdx-elasticsearch.spdx",
Packages: types.Packages{
{
ID: "co.elastic.apm:apm-agent:1.36.0",
Expand Down Expand Up @@ -88,32 +115,6 @@ func Test_sbomAnalyzer_Analyze(t *testing.T) {
},
},
},
{
Type: types.Bitnami,
FilePath: "opt/bitnami/elasticsearch",
Packages: types.Packages{
{
ID: "[email protected]",
Name: "elasticsearch",
Version: "8.9.1",
Arch: "arm64",
Licenses: []string{"Elastic-2.0"},
Identifier: types.PkgIdentifier{
PURL: &packageurl.PackageURL{
Type: packageurl.TypeBitnami,
Name: "elasticsearch",
Version: "8.9.1",
Qualifiers: packageurl.Qualifiers{
{
Key: "arch",
Value: "arm64",
},
},
},
},
},
},
},
},
},
wantErr: require.NoError,
Expand All @@ -125,7 +126,8 @@ func Test_sbomAnalyzer_Analyze(t *testing.T) {
want: &analyzer.AnalysisResult{
Applications: []types.Application{
{
Type: types.Jar,
Type: types.Jar,
FilePath: "opt/bitnami/elasticsearch/.spdx-elasticsearch.cdx",
Packages: types.Packages{
{
FilePath: "opt/bitnami/elasticsearch/modules/apm/elastic-apm-agent-1.36.0.jar",
Expand Down
Loading