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(cyclonedx): fix work when there are same pkgs from different dirs #5798

Closed
Show file tree
Hide file tree
Changes from 1 commit
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/cyclonedx/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,20 @@ func (e *Marshaler) marshalPackages(metadata types.Metadata, result types.Result

// Group vulnerabilities by package ID
vulns := lo.GroupBy(result.Vulnerabilities, func(v types.DetectedVulnerability) string {
return lo.Ternary(v.PkgID == "", fmt.Sprintf("%s@%s", v.PkgName, v.InstalledVersion), v.PkgID)
pkgID := lo.Ternary(v.PkgID == "", fmt.Sprintf("%s@%s", v.PkgName, v.InstalledVersion), v.PkgID)
if v.PkgPath != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same:

vulns[i].PkgPath = lib.FilePath

pkgID = fmt.Sprintf("%s@%s", pkgID, v.PkgPath)
}
return pkgID
})

// Create package map
pkgs := lo.SliceToMap(result.Packages, func(pkg ftypes.Package) (string, Package) {
pkgID := lo.Ternary(pkg.ID == "", fmt.Sprintf("%s@%s", pkg.Name, utils.FormatVersion(pkg)), pkg.ID)
// To avoid skip same packages with different paths
if pkg.FilePath != "" {
Copy link
Contributor Author

@DmitriyLewen DmitriyLewen Dec 18, 2023

Choose a reason for hiding this comment

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

This only applies to Packages (jar, package.json, etc.) because pkg.FilePath is only saved for Packages:

// Each package metadata have the file path, while the package from lock files does not have.
FilePath string `json:",omitempty"`

Copy link
Contributor Author

@DmitriyLewen DmitriyLewen Dec 18, 2023

Choose a reason for hiding this comment

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

pkgs map is used only for dependency iteration.
But Packages don't contain dependencies (DependsOn fields).

So it doesn't break logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about saving slice of ftypes.Package's or slice of FilePath's, but i understood that we still can't get correct package, because DependsOn doesn't currently contain info about filepath.

Looks like this update is enough for now.

If we will add Package with DependsOn - we will need to update DependsOn struct (and logic for this) and we can update logic for CycloneDX.

pkgID = fmt.Sprintf("%s@%s", pkgID, pkg.FilePath)
}
return pkgID, Package{
Type: result.Type,
Metadata: metadata,
Expand Down
81 changes: 80 additions & 1 deletion pkg/sbom/cyclonedx/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ func TestMarshaler_Marshal(t *testing.T) {
},
},
{
name: "happy path. 2 packages for 1 CVE",
name: "happy path. Multiple packages for 1 CVE (2 same packages from different dirs and 1 package with a different name)",
inputReport: types.Report{
SchemaVersion: report.SchemaVersion,
ArtifactName: "CVE-2023-34468",
Expand All @@ -1211,6 +1211,11 @@ func TestMarshaler_Marshal(t *testing.T) {
Version: "1.20.0",
FilePath: "nifi-hikari-dbcp-service-1.20.0.jar",
},
{
Name: "org.apache.nifi:nifi-hikari-dbcp-service",
Version: "1.20.0",
FilePath: "dir/nifi-hikari-dbcp-service-1.20.0.jar",
},
},
Vulnerabilities: []types.DetectedVulnerability{
{
Expand Down Expand Up @@ -1255,6 +1260,48 @@ func TestMarshaler_Marshal(t *testing.T) {
LastModifiedDate: lo.ToPtr(time.Date(2023, 6, 21, 02, 20, 0, 0, time.UTC)),
},
},
{
VulnerabilityID: "CVE-2023-34468",
PkgName: "org.apache.nifi:nifi-hikari-dbcp-service",
PkgPath: "dir/nifi-hikari-dbcp-service-1.20.0.jar",
InstalledVersion: "1.20.0",
FixedVersion: "1.22.0",
SeveritySource: vulnerability.GHSA,
PrimaryURL: "https://avd.aquasec.com/nvd/cve-2023-34468",
DataSource: &dtypes.DataSource{
ID: vulnerability.GHSA,
Name: "GitHub Security Advisory Maven",
URL: "https://github.com/advisories?query=type%3Areviewed+ecosystem%3Amaven",
},
Vulnerability: dtypes.Vulnerability{
Title: "Apache NiFi vulnerable to Code Injection",
Description: "The DBCPConnectionPool and HikariCPConnectionPool Controller Services in Apache NiFi 0.0.2 through 1.21.0...",
Severity: dtypes.SeverityHigh.String(),
CweIDs: []string{
"CWE-94",
},
VendorSeverity: dtypes.VendorSeverity{
vulnerability.GHSA: dtypes.SeverityHigh,
vulnerability.NVD: dtypes.SeverityHigh,
},
CVSS: dtypes.VendorCVSS{
vulnerability.GHSA: dtypes.CVSS{
V3Vector: "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H",
V3Score: 8.8,
},
vulnerability.NVD: dtypes.CVSS{
V3Vector: "CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H",
V3Score: 8.8,
},
},
References: []string{
"http://www.openwall.com/lists/oss-security/2023/06/12/3",
"https://github.com/advisories/GHSA-xm2m-2q6h-22jw",
},
PublishedDate: lo.ToPtr(time.Date(2023, 6, 12, 16, 15, 0, 0, time.UTC)),
LastModifiedDate: lo.ToPtr(time.Date(2023, 6, 21, 02, 20, 0, 0, time.UTC)),
},
},
{
VulnerabilityID: "CVE-2023-34468",
PkgName: "org.apache.nifi:nifi-hikari-dbcp-service",
Expand Down Expand Up @@ -1348,6 +1395,24 @@ func TestMarshaler_Marshal(t *testing.T) {
},
},
},
{
BOMRef: "pkg:maven/org.apache.nifi/[email protected]?file_path=dir%2Fnifi-hikari-dbcp-service-1.20.0.jar",
Type: "library",
Name: "nifi-hikari-dbcp-service",
Group: "org.apache.nifi",
Version: "1.20.0",
PackageURL: "pkg:maven/org.apache.nifi/[email protected]",
Properties: &[]cdx.Property{
{
Name: "aquasecurity:trivy:FilePath",
Value: "dir/nifi-hikari-dbcp-service-1.20.0.jar",
},
{
Name: "aquasecurity:trivy:PkgType",
Value: "jar",
},
},
},
{
BOMRef: "pkg:maven/org.apache.nifi/[email protected]?file_path=nifi-hikari-dbcp-service-1.20.0.jar",
Type: "library",
Expand All @@ -1372,13 +1437,18 @@ func TestMarshaler_Marshal(t *testing.T) {
Ref: "3ff14136-e09f-4df9-80ea-000000000002",
Dependencies: &[]string{
"pkg:maven/org.apache.nifi/[email protected]?file_path=nifi-dbcp-base-1.20.0.jar",
"pkg:maven/org.apache.nifi/[email protected]?file_path=dir%2Fnifi-hikari-dbcp-service-1.20.0.jar",
"pkg:maven/org.apache.nifi/[email protected]?file_path=nifi-hikari-dbcp-service-1.20.0.jar",
},
},
{
Ref: "pkg:maven/org.apache.nifi/[email protected]?file_path=nifi-dbcp-base-1.20.0.jar",
Dependencies: lo.ToPtr([]string{}),
},
{
Ref: "pkg:maven/org.apache.nifi/[email protected]?file_path=dir%2Fnifi-hikari-dbcp-service-1.20.0.jar",
Dependencies: lo.ToPtr([]string{}),
},
{
Ref: "pkg:maven/org.apache.nifi/[email protected]?file_path=nifi-hikari-dbcp-service-1.20.0.jar",
Dependencies: lo.ToPtr([]string{}),
Expand Down Expand Up @@ -1437,6 +1507,15 @@ func TestMarshaler_Marshal(t *testing.T) {
},
},
},
{
Ref: "pkg:maven/org.apache.nifi/[email protected]?file_path=dir%2Fnifi-hikari-dbcp-service-1.20.0.jar",
Range: &[]cdx.AffectedVersions{
{
Version: "1.20.0",
Status: cdx.VulnerabilityStatusAffected,
},
},
},
{
Ref: "pkg:maven/org.apache.nifi/[email protected]?file_path=nifi-hikari-dbcp-service-1.20.0.jar",
Range: &[]cdx.AffectedVersions{
Expand Down