From de1112b7813bf77852c60608038e6a261a0074ab Mon Sep 17 00:00:00 2001 From: Or Toren Date: Sun, 4 Oct 2026 16:37:52 +0300 Subject: [PATCH 1/6] Strip license from unchanged root components in PR diff SBOM cdxutils.Exclude never removes root components from the diffed SBOM, by design, so a license attached to a root (local manifest or a Catalog name/version collision) persists unchanged across every PR scan and keeps firing a bound license policy even when nothing about that root changed. Strip the license on a root when it matches the target's component by purl, keeping the component for graph structure while a genuinely new/changed root still gets enforced. Co-Authored-By: Claude Sonnet 5 --- utils/formats/cdxutils/cyclonedxutils.go | 11 +++++++-- utils/formats/cdxutils/cyclonedxutils_test.go | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/utils/formats/cdxutils/cyclonedxutils.go b/utils/formats/cdxutils/cyclonedxutils.go index fc2a03618..fa450ad40 100644 --- a/utils/formats/cdxutils/cyclonedxutils.go +++ b/utils/formats/cdxutils/cyclonedxutils.go @@ -356,8 +356,15 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil filteredSbom = &bom bomIndex := NewBOMIndex(&bom, false) for _, compToExclude := range componentsToExclude { - if matchedBomComp := SearchComponentByCleanPurl(bom.Components, compToExclude.PackageURL); matchedBomComp == nil || bomIndex.GetComponentRelation(matchedBomComp.BOMRef) == RootRelation { - // If not a match or Root component, skip it + matchedBomComp := SearchComponentByCleanPurl(bom.Components, compToExclude.PackageURL) + if matchedBomComp == nil { + // Not a match, skip it + continue + } + if bomIndex.GetComponentRelation(matchedBomComp.BOMRef) == RootRelation { + // Root component unchanged since the target: keep it for graph structure, but strip its + // license so it stops triggering license violations on every scan + matchedBomComp.Licenses = nil continue } // Exclude the component from the dependencies diff --git a/utils/formats/cdxutils/cyclonedxutils_test.go b/utils/formats/cdxutils/cyclonedxutils_test.go index 3863738ba..de5fe95fd 100644 --- a/utils/formats/cdxutils/cyclonedxutils_test.go +++ b/utils/formats/cdxutils/cyclonedxutils_test.go @@ -1688,6 +1688,29 @@ func TestExclude(t *testing.T) { Dependencies: &[]cyclonedx.Dependency{}, }, }, + { + name: "Exclude unchanged root component - strip license, keep graph structure", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "web", Dependencies: &[]string{"dep1"}}, + }, + }, + // Simulates the target branch's root component, unchanged between source and target + exclude: []cyclonedx.Component{{BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}}, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "web", Dependencies: &[]string{"dep1"}}, + }, + }, + }, } for _, tt := range tests { From f9dc811526747bb33bfa846d2772fdef07a4a98c Mon Sep 17 00:00:00 2001 From: Or Toren Date: Mon, 5 Oct 2026 14:02:02 +0300 Subject: [PATCH 2/6] Fix: exclude fully-unchanged root components entirely, not just their license A license-only strip on an unchanged root doesn't survive the pipeline: the SCA scan-graph/Catalog enrich step runs after the diff and re-resolves license data fresh for every component still present, silently re-attaching whatever was cleared. A multi-root scan also always lists every root as a dependsOn child of its own wrapper entry, so a root is never "unreferenced" on its own merely by touching its declared-dependencies entry. An unchanged root with at least one surviving (new/changed) child is still kept as-is, untouched, to anchor the real diff. A root with nothing real left underneath it is now excluded entirely - from Components and from every dependsOn list that references it (including the wrapper's) - so it is never resubmitted for re-enrichment and can no longer trigger a recurring violation. Verified end-to-end against a live Xray instance: a README-only PR via a real scan-pull-request run went from "Found 1 violations: [1 License]" to "No violations found", with the uploaded diffed SBOM confirmed empty. Co-Authored-By: Claude Sonnet 5 --- utils/formats/cdxutils/cyclonedxutils.go | 23 ++++++- utils/formats/cdxutils/cyclonedxutils_test.go | 65 +++++++++++++++++-- 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/utils/formats/cdxutils/cyclonedxutils.go b/utils/formats/cdxutils/cyclonedxutils.go index fa450ad40..849915539 100644 --- a/utils/formats/cdxutils/cyclonedxutils.go +++ b/utils/formats/cdxutils/cyclonedxutils.go @@ -355,6 +355,10 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil } filteredSbom = &bom bomIndex := NewBOMIndex(&bom, false) + // Roots confirmed unchanged since the target (same purl). Their own declared-dependencies entry is + // not touched here - whether they end up excluded entirely depends on whether anything survives + // underneath them, decided below, once all their (non-root) children have been processed. + var unchangedRoots []cyclonedx.Component for _, compToExclude := range componentsToExclude { matchedBomComp := SearchComponentByCleanPurl(bom.Components, compToExclude.PackageURL) if matchedBomComp == nil { @@ -362,14 +366,27 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil continue } if bomIndex.GetComponentRelation(matchedBomComp.BOMRef) == RootRelation { - // Root component unchanged since the target: keep it for graph structure, but strip its - // license so it stops triggering license violations on every scan - matchedBomComp.Licenses = nil + unchangedRoots = append(unchangedRoots, *matchedBomComp) continue } // Exclude the component from the dependencies filteredSbom.Dependencies = excludeFromDependencies(bom.Dependencies, bom.Components, compToExclude) } + // A root whose declared children were all excluded above (nothing real survives underneath it) must + // be excluded entirely too, same as any other component - not just license-stripped. A multi-root + // scan's wrapper entry declares every root as one of its own dependsOn children regardless of whether + // anything changed, so a root is never "unreferenced" on its own; and the later SCA scan-graph/enrich + // step re-resolves license data fresh for every component still present, so clearing a field on a + // root that stays present would just get silently re-attached. Removing it from both the component + // list and the wrapper's dependsOn list is the only way to actually stop it being re-evaluated. + for _, root := range unchangedRoots { + rootEntry := SearchDependencyEntry(filteredSbom.Dependencies, root.BOMRef) + if rootEntry != nil && rootEntry.Dependencies != nil && len(*rootEntry.Dependencies) > 0 { + // At least one real (new/changed) child survives under this root - keep it as their anchor. + continue + } + filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, bom.Components, root) + } toExclude := datastructures.MakeSet[string]() for _, comp := range *filteredSbom.Components { if comp.Type != cyclonedx.ComponentTypeLibrary { diff --git a/utils/formats/cdxutils/cyclonedxutils_test.go b/utils/formats/cdxutils/cyclonedxutils_test.go index de5fe95fd..c2c12689b 100644 --- a/utils/formats/cdxutils/cyclonedxutils_test.go +++ b/utils/formats/cdxutils/cyclonedxutils_test.go @@ -1689,7 +1689,7 @@ func TestExclude(t *testing.T) { }, }, { - name: "Exclude unchanged root component - strip license, keep graph structure", + name: "Exclude unchanged root with a surviving child - root kept with its original data", bom: cyclonedx.BOM{ Components: &[]cyclonedx.Component{ {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, @@ -1699,11 +1699,12 @@ func TestExclude(t *testing.T) { {Ref: "web", Dependencies: &[]string{"dep1"}}, }, }, - // Simulates the target branch's root component, unchanged between source and target + // Simulates the target branch's root component, unchanged between source and target. + // dep1 is new/changed (not in target), so it is not excluded, and web must stay as its anchor. exclude: []cyclonedx.Component{{BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}}, expected: &cyclonedx.BOM{ Components: &[]cyclonedx.Component{ - {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, }, Dependencies: &[]cyclonedx.Dependency{ @@ -1711,6 +1712,57 @@ func TestExclude(t *testing.T) { }, }, }, + { + name: "Exclude fully unchanged root (no surviving children) - root excluded entirely", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "web", Dependencies: &[]string{"dep1"}}, + }, + }, + // Simulates a README-only PR: both the root and its only dependency are unchanged from target. + exclude: []cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}, + {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0"}, + }, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{}, + Dependencies: &[]cyclonedx.Dependency{}, + }, + }, + { + // Mirrors a real multi-root scan: a wrapper entry (e.g. the scanned file/workspace) lists every + // root as one of its own dependsOn children regardless of whether anything changed, so a root is + // never "unreferenced" on its own merely by removing its own declared-dependencies entry. + name: "Exclude fully unchanged roots referenced by a multi-root wrapper entry", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, + {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "ms", PackageURL: "pkg:npm/ms@2.1.3", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "file:workspace", Dependencies: &[]string{"api", "web"}}, + {Ref: "api", Dependencies: &[]string{"lodash"}}, + {Ref: "web", Dependencies: &[]string{"ms"}}, + }, + }, + // Simulates a README-only PR: every root and its only dependency are unchanged from target. + exclude: []cyclonedx.Component{ + {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0"}, + {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21"}, + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}, + {BOMRef: "ms", PackageURL: "pkg:npm/ms@2.1.3"}, + }, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{}, + Dependencies: &[]cyclonedx.Dependency{}, + }, + }, } for _, tt := range tests { @@ -1723,7 +1775,12 @@ func TestExclude(t *testing.T) { } else if len(*tt.expected.Components) == 0 { assert.NotNil(t, result.Components, "Expected components to not be nil after exclusion") assert.Len(t, *result.Components, 0, "Expected components to be empty after exclusion") - assert.Nil(t, result.Dependencies, "Expected dependencies to be nil after exclusion") + if tt.bom.Dependencies == nil { + assert.Nil(t, result.Dependencies, "Expected dependencies to be nil after exclusion") + } else { + assert.NotNil(t, result.Dependencies, "Expected dependencies to not be nil after exclusion") + assert.Len(t, *result.Dependencies, 0, "Expected dependencies to be empty after exclusion") + } return } assert.ElementsMatch(t, *tt.expected.Components, *result.Components, "Expected exclude result does not match") From 49705150cb08a1744251fd7fcd3718c9acb5ff36 Mon Sep 17 00:00:00 2001 From: Or Toren Date: Mon, 5 Oct 2026 14:10:12 +0300 Subject: [PATCH 3/6] Trim excess comments Co-Authored-By: Claude Sonnet 5 --- utils/formats/cdxutils/cyclonedxutils.go | 13 ++----------- utils/formats/cdxutils/cyclonedxutils_test.go | 9 ++------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/utils/formats/cdxutils/cyclonedxutils.go b/utils/formats/cdxutils/cyclonedxutils.go index 849915539..5e23971ed 100644 --- a/utils/formats/cdxutils/cyclonedxutils.go +++ b/utils/formats/cdxutils/cyclonedxutils.go @@ -355,9 +355,6 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil } filteredSbom = &bom bomIndex := NewBOMIndex(&bom, false) - // Roots confirmed unchanged since the target (same purl). Their own declared-dependencies entry is - // not touched here - whether they end up excluded entirely depends on whether anything survives - // underneath them, decided below, once all their (non-root) children have been processed. var unchangedRoots []cyclonedx.Component for _, compToExclude := range componentsToExclude { matchedBomComp := SearchComponentByCleanPurl(bom.Components, compToExclude.PackageURL) @@ -372,17 +369,11 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil // Exclude the component from the dependencies filteredSbom.Dependencies = excludeFromDependencies(bom.Dependencies, bom.Components, compToExclude) } - // A root whose declared children were all excluded above (nothing real survives underneath it) must - // be excluded entirely too, same as any other component - not just license-stripped. A multi-root - // scan's wrapper entry declares every root as one of its own dependsOn children regardless of whether - // anything changed, so a root is never "unreferenced" on its own; and the later SCA scan-graph/enrich - // step re-resolves license data fresh for every component still present, so clearing a field on a - // root that stays present would just get silently re-attached. Removing it from both the component - // list and the wrapper's dependsOn list is the only way to actually stop it being re-evaluated. + // A root is excluded entirely, including from any wrapper's dependsOn list, only once nothing + // real survives under it - re-enrichment would otherwise just re-attach its license anyway. for _, root := range unchangedRoots { rootEntry := SearchDependencyEntry(filteredSbom.Dependencies, root.BOMRef) if rootEntry != nil && rootEntry.Dependencies != nil && len(*rootEntry.Dependencies) > 0 { - // At least one real (new/changed) child survives under this root - keep it as their anchor. continue } filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, bom.Components, root) diff --git a/utils/formats/cdxutils/cyclonedxutils_test.go b/utils/formats/cdxutils/cyclonedxutils_test.go index c2c12689b..ce9b38264 100644 --- a/utils/formats/cdxutils/cyclonedxutils_test.go +++ b/utils/formats/cdxutils/cyclonedxutils_test.go @@ -1699,8 +1699,7 @@ func TestExclude(t *testing.T) { {Ref: "web", Dependencies: &[]string{"dep1"}}, }, }, - // Simulates the target branch's root component, unchanged between source and target. - // dep1 is new/changed (not in target), so it is not excluded, and web must stay as its anchor. + // dep1 is not in target, so web must stay as its anchor exclude: []cyclonedx.Component{{BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}}, expected: &cyclonedx.BOM{ Components: &[]cyclonedx.Component{ @@ -1723,7 +1722,6 @@ func TestExclude(t *testing.T) { {Ref: "web", Dependencies: &[]string{"dep1"}}, }, }, - // Simulates a README-only PR: both the root and its only dependency are unchanged from target. exclude: []cyclonedx.Component{ {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}, {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0"}, @@ -1734,9 +1732,7 @@ func TestExclude(t *testing.T) { }, }, { - // Mirrors a real multi-root scan: a wrapper entry (e.g. the scanned file/workspace) lists every - // root as one of its own dependsOn children regardless of whether anything changed, so a root is - // never "unreferenced" on its own merely by removing its own declared-dependencies entry. + // Mirrors a real multi-root scan, where a wrapper entry lists every root as a dependsOn child name: "Exclude fully unchanged roots referenced by a multi-root wrapper entry", bom: cyclonedx.BOM{ Components: &[]cyclonedx.Component{ @@ -1751,7 +1747,6 @@ func TestExclude(t *testing.T) { {Ref: "web", Dependencies: &[]string{"ms"}}, }, }, - // Simulates a README-only PR: every root and its only dependency are unchanged from target. exclude: []cyclonedx.Component{ {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0"}, {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21"}, From 35e1e4be442188bd133bd15e45ad7efb327bc4a9 Mon Sep 17 00:00:00 2001 From: Or Toren Date: Wed, 7 Oct 2026 09:01:01 +0300 Subject: [PATCH 4/6] Address review: cascade nested-root exclusion, fix no-deps guard Exclusion of unchanged roots now repeats until a full pass excludes nothing more, so a root that depends on another unchanged root (e.g. a monorepo wrapper listing app and lib, where app depends on lib) is no longer kept just because it was checked before its own child got removed in the same pass. The BOM-wide early return on a missing/empty Dependencies array no longer skips exclusion entirely - a library root with no declared dependencies of its own is excluded by a straightforward purl match instead, since getDiffSbom is also used for technologies whose dependency-free roots carry no Dependencies entry at all. Replaced the invalid multi-root test (its ad hoc "file:workspace" ref never actually classified anything as a root) with cases built on the real Metadata.Component wrapper structure, plus new cascade and no-dependency-graph cases. Co-Authored-By: Claude Sonnet 5 --- utils/formats/cdxutils/cyclonedxutils.go | 54 ++++++++-- utils/formats/cdxutils/cyclonedxutils_test.go | 99 ++++++++++++++++++- 2 files changed, 141 insertions(+), 12 deletions(-) diff --git a/utils/formats/cdxutils/cyclonedxutils.go b/utils/formats/cdxutils/cyclonedxutils.go index 5e23971ed..5e29557fa 100644 --- a/utils/formats/cdxutils/cyclonedxutils.go +++ b/utils/formats/cdxutils/cyclonedxutils.go @@ -349,10 +349,15 @@ func ConvertToAffectedVersions(affectedComponent cyclonedx.Component, fixedVersi } func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (filteredSbom *cyclonedx.BOM) { - if bom.Components == nil || len(*bom.Components) == 0 || bom.Dependencies == nil || len(*bom.Dependencies) == 0 { - // No components or dependencies to filter, return the original BOM + if bom.Components == nil || len(*bom.Components) == 0 { + // No components to filter, return the original BOM return &bom } + if bom.Dependencies == nil || len(*bom.Dependencies) == 0 { + // No dependency graph at all (e.g. a library root with no declared dependencies): excluding is + // just a purl match against componentsToExclude, nothing to untangle. + return excludeComponentsOnly(bom, componentsToExclude) + } filteredSbom = &bom bomIndex := NewBOMIndex(&bom, false) var unchangedRoots []cyclonedx.Component @@ -369,14 +374,27 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil // Exclude the component from the dependencies filteredSbom.Dependencies = excludeFromDependencies(bom.Dependencies, bom.Components, compToExclude) } - // A root is excluded entirely, including from any wrapper's dependsOn list, only once nothing - // real survives under it - re-enrichment would otherwise just re-attach its license anyway. - for _, root := range unchangedRoots { - rootEntry := SearchDependencyEntry(filteredSbom.Dependencies, root.BOMRef) - if rootEntry != nil && rootEntry.Dependencies != nil && len(*rootEntry.Dependencies) > 0 { - continue + // A root is excluded entirely, including from any wrapper's dependsOn list, only once nothing real + // survives under it. One pass isn't enough: excluding one root can empty out a parent root's own + // dependsOn (e.g. a monorepo wrapper listing app and lib, where app itself depends on lib), so keep + // going until a full pass excludes nothing more - re-enrichment would otherwise re-attach a license + // to any root left behind regardless of what we clear on it. + for { + excludedAny := false + var stillUnchanged []cyclonedx.Component + for _, root := range unchangedRoots { + rootEntry := SearchDependencyEntry(filteredSbom.Dependencies, root.BOMRef) + if rootEntry != nil && rootEntry.Dependencies != nil && len(*rootEntry.Dependencies) > 0 { + stillUnchanged = append(stillUnchanged, root) + continue + } + filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, bom.Components, root) + excludedAny = true + } + unchangedRoots = stillUnchanged + if !excludedAny { + break } - filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, bom.Components, root) } toExclude := datastructures.MakeSet[string]() for _, comp := range *filteredSbom.Components { @@ -418,6 +436,24 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil return filteredSbom } +// excludeComponentsOnly drops any component matching componentsToExclude by purl, used when the BOM has +// no dependency graph at all so there is no parent/child structure to preserve. +func excludeComponentsOnly(bom cyclonedx.BOM, componentsToExclude []cyclonedx.Component) *cyclonedx.BOM { + excludePurls := datastructures.MakeSet[string]() + for _, comp := range componentsToExclude { + excludePurls.Add(techutils.PurlToXrayComponentId(comp.PackageURL)) + } + kept := make([]cyclonedx.Component, 0, len(*bom.Components)) + for _, comp := range *bom.Components { + if excludePurls.Exists(techutils.PurlToXrayComponentId(comp.PackageURL)) { + continue + } + kept = append(kept, comp) + } + bom.Components = &kept + return &bom +} + func excludeFromComponents(components *[]cyclonedx.Component, excludeComponents ...string) *[]cyclonedx.Component { if components == nil || len(*components) == 0 || len(excludeComponents) == 0 { return components diff --git a/utils/formats/cdxutils/cyclonedxutils_test.go b/utils/formats/cdxutils/cyclonedxutils_test.go index ce9b38264..8db607802 100644 --- a/utils/formats/cdxutils/cyclonedxutils_test.go +++ b/utils/formats/cdxutils/cyclonedxutils_test.go @@ -1732,9 +1732,13 @@ func TestExclude(t *testing.T) { }, }, { - // Mirrors a real multi-root scan, where a wrapper entry lists every root as a dependsOn child - name: "Exclude fully unchanged roots referenced by a multi-root wrapper entry", + // Metadata.Component is the Xray-Lib-Plugin wrapper: GetRootDependenciesEntries treats every + // one of its dependsOn children as a root (see TestGetRootDependenciesEntries) + name: "Exclude fully unchanged roots under a Metadata.Component wrapper", bom: cyclonedx.BOM{ + Metadata: &cyclonedx.Metadata{ + Component: &cyclonedx.Component{BOMRef: "file-root", Type: cyclonedx.ComponentTypeFile}, + }, Components: &[]cyclonedx.Component{ {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21", Type: cyclonedx.ComponentTypeLibrary}, @@ -1742,7 +1746,7 @@ func TestExclude(t *testing.T) { {BOMRef: "ms", PackageURL: "pkg:npm/ms@2.1.3", Type: cyclonedx.ComponentTypeLibrary}, }, Dependencies: &[]cyclonedx.Dependency{ - {Ref: "file:workspace", Dependencies: &[]string{"api", "web"}}, + {Ref: "file-root", Dependencies: &[]string{"api", "web"}}, {Ref: "api", Dependencies: &[]string{"lodash"}}, {Ref: "web", Dependencies: &[]string{"ms"}}, }, @@ -1758,6 +1762,95 @@ func TestExclude(t *testing.T) { Dependencies: &[]cyclonedx.Dependency{}, }, }, + { + // app is itself a root that depends on another root, lib. Excluding app before lib empties + // app's own dependsOn, but app must not be kept just because it was checked before lib was + // removed - the exclusion pass must cascade until nothing more becomes childless. + name: "Exclude cascades when an unchanged root depends on another unchanged root", + bom: cyclonedx.BOM{ + Metadata: &cyclonedx.Metadata{ + Component: &cyclonedx.Component{BOMRef: "file-root", Type: cyclonedx.ComponentTypeFile}, + }, + Components: &[]cyclonedx.Component{ + {BOMRef: "app", PackageURL: "pkg:npm/app@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "lib", PackageURL: "pkg:npm/lib@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "file-root", Dependencies: &[]string{"app", "lib"}}, + {Ref: "app", Dependencies: &[]string{"lib"}}, + }, + }, + // app is listed first, before its own dependency lib is processed + exclude: []cyclonedx.Component{ + {BOMRef: "app", PackageURL: "pkg:npm/app@1.0.0"}, + {BOMRef: "lib", PackageURL: "pkg:npm/lib@1.0.0"}, + }, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{}, + Dependencies: &[]cyclonedx.Dependency{}, + }, + }, + { + name: "Exclude mixed roots under a wrapper - one kept with a surviving child, one dropped", + bom: cyclonedx.BOM{ + Metadata: &cyclonedx.Metadata{ + Component: &cyclonedx.Component{BOMRef: "file-root", Type: cyclonedx.ComponentTypeFile}, + }, + Components: &[]cyclonedx.Component{ + {BOMRef: "keep", PackageURL: "pkg:npm/keep@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "newchild", PackageURL: "pkg:npm/newchild@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "drop", PackageURL: "pkg:npm/drop@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "file-root", Dependencies: &[]string{"keep", "drop"}}, + {Ref: "keep", Dependencies: &[]string{"newchild"}}, + }, + }, + // newchild is new (not in target), so it is not excluded, and keep must stay as its anchor + exclude: []cyclonedx.Component{ + {BOMRef: "keep", PackageURL: "pkg:npm/keep@1.0.0"}, + {BOMRef: "drop", PackageURL: "pkg:npm/drop@1.0.0"}, + }, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "keep", PackageURL: "pkg:npm/keep@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "newchild", PackageURL: "pkg:npm/newchild@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "file-root", Dependencies: &[]string{"keep"}}, + {Ref: "keep", Dependencies: &[]string{"newchild"}}, + }, + }, + }, + { + // A component with no dependencies of its own is written with no Dependencies entry at all - + // a dependency-free root must still be excluded when unchanged, not skipped by a bom-wide guard + name: "Exclude unchanged root when the BOM has no dependency graph at all", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + }, + }, + exclude: []cyclonedx.Component{{BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}}, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{}, + }, + }, + { + name: "Do not exclude an unmatched component when the BOM has no dependency graph at all", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "newroot", PackageURL: "pkg:npm/newroot@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + }, + exclude: []cyclonedx.Component{{BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}}, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "newroot", PackageURL: "pkg:npm/newroot@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + }, + }, } for _, tt := range tests { From cbd4c5d43349ab4b719a8b1f3a2872cdf206de31 Mon Sep 17 00:00:00 2001 From: Or Toren Date: Thu, 8 Oct 2026 14:10:15 +0300 Subject: [PATCH 5/6] Address review: unify exclusion rule, fix transitive and cycle bugs Non-root matched components were still dropped wholesale via the original excludeFromDependencies call, discarding their own declared children outright regardless of whether any of them were actually new. A lockfile change that only adds a transitive dependency under an otherwise-unchanged direct dependency silently uploaded nothing. Replace the root-only deferred pass with a single rule applied to every matched (unchanged) component uniformly: exclude it, from both Components and every dependsOn list that references it, only once it cannot reach anything outside the matched set - no new or changed component, and no other matched component that itself can. This is computed once as a reachability fixed point over the whole graph (calculateEscapingRefs), which naturally handles arbitrarily deep chains and also resolves a cycle of mutually-unchanged roots that a single non-cyclic pass could never see as childless. Updated two pre-existing tests whose expectations encoded the old wholesale-deletion behavior for a matched node with unconfirmed (not independently excluded) children; added tests for the transitive leaf case, the root cycle, a root whose purl changed, and an unchanged root depending on a changed child root. Co-Authored-By: Claude Sonnet 5 --- sca/bom/bomgenerator_test.go | 7 +- utils/formats/cdxutils/cyclonedxutils.go | 73 ++++++---- utils/formats/cdxutils/cyclonedxutils_test.go | 136 +++++++++++++++++- 3 files changed, 179 insertions(+), 37 deletions(-) diff --git a/sca/bom/bomgenerator_test.go b/sca/bom/bomgenerator_test.go index d7a577085..d2dcf099e 100644 --- a/sca/bom/bomgenerator_test.go +++ b/sca/bom/bomgenerator_test.go @@ -74,6 +74,9 @@ func TestGetDiff(t *testing.T) { }, }, { + // component1 is unchanged (same purl as in target), but component2 is new - added as its + // child by this PR. component1 (and root above it) must stay untouched to anchor it, instead + // of being wholesale-dropped and silently hiding the new dependency from the scan. name: "Diff Mode, With results to compare", params: SbomGeneratorParams{ Target: &results.TargetResults{ScanTarget: results.ScanTarget{Target: "source bom"}}, @@ -107,10 +110,12 @@ func TestGetDiff(t *testing.T) { expectedSbom: &cyclonedx.BOM{ Components: &[]cyclonedx.Component{ {Type: cyclonedx.ComponentTypeLibrary, BOMRef: "root", Version: "1.0"}, + {Type: cyclonedx.ComponentTypeLibrary, BOMRef: "component1", PackageURL: "pkg:component1", Version: "1.0"}, {Type: cyclonedx.ComponentTypeLibrary, BOMRef: "component2", PackageURL: "pkg:component2", Version: "2.0"}, }, Dependencies: &[]cyclonedx.Dependency{ - {Ref: "root", Dependencies: &[]string{"component2"}}, + {Ref: "root", Dependencies: &[]string{"component1", "component2"}}, + {Ref: "component1", Dependencies: &[]string{"component2"}}, }, }, }, diff --git a/utils/formats/cdxutils/cyclonedxutils.go b/utils/formats/cdxutils/cyclonedxutils.go index 5e29557fa..5794d8b86 100644 --- a/utils/formats/cdxutils/cyclonedxutils.go +++ b/utils/formats/cdxutils/cyclonedxutils.go @@ -359,43 +359,31 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil return excludeComponentsOnly(bom, componentsToExclude) } filteredSbom = &bom - bomIndex := NewBOMIndex(&bom, false) - var unchangedRoots []cyclonedx.Component + matchedRefs := datastructures.MakeSet[string]() + matchedComponents := map[string]cyclonedx.Component{} for _, compToExclude := range componentsToExclude { matchedBomComp := SearchComponentByCleanPurl(bom.Components, compToExclude.PackageURL) if matchedBomComp == nil { - // Not a match, skip it + // Not a match (new, or its purl changed since the target), skip it continue } - if bomIndex.GetComponentRelation(matchedBomComp.BOMRef) == RootRelation { - unchangedRoots = append(unchangedRoots, *matchedBomComp) - continue - } - // Exclude the component from the dependencies - filteredSbom.Dependencies = excludeFromDependencies(bom.Dependencies, bom.Components, compToExclude) + matchedRefs.Add(matchedBomComp.BOMRef) + matchedComponents[matchedBomComp.BOMRef] = *matchedBomComp } - // A root is excluded entirely, including from any wrapper's dependsOn list, only once nothing real - // survives under it. One pass isn't enough: excluding one root can empty out a parent root's own - // dependsOn (e.g. a monorepo wrapper listing app and lib, where app itself depends on lib), so keep - // going until a full pass excludes nothing more - re-enrichment would otherwise re-attach a license - // to any root left behind regardless of what we clear on it. - for { - excludedAny := false - var stillUnchanged []cyclonedx.Component - for _, root := range unchangedRoots { - rootEntry := SearchDependencyEntry(filteredSbom.Dependencies, root.BOMRef) - if rootEntry != nil && rootEntry.Dependencies != nil && len(*rootEntry.Dependencies) > 0 { - stillUnchanged = append(stillUnchanged, root) - continue - } - filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, bom.Components, root) - excludedAny = true - } - unchangedRoots = stillUnchanged - if !excludedAny { - break + // A matched (unchanged) component is only excluded once it cannot reach anything real - no new or + // changed component, and no other matched component that itself can. Root or not, a matched node with + // a real descendant must stay to anchor it, and a closed group of matched nodes with no escape (a + // monorepo wrapper's two packages depending on each other, for instance) is excluded together. + escapingRefs := calculateEscapingRefs(bom.Dependencies, matchedRefs) + var toExcludeComponents []cyclonedx.Component + for ref, comp := range matchedComponents { + if !escapingRefs.Exists(ref) { + toExcludeComponents = append(toExcludeComponents, comp) } } + if len(toExcludeComponents) > 0 { + filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, bom.Components, toExcludeComponents...) + } toExclude := datastructures.MakeSet[string]() for _, comp := range *filteredSbom.Components { if comp.Type != cyclonedx.ComponentTypeLibrary { @@ -436,6 +424,33 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil return filteredSbom } +// calculateEscapingRefs returns the subset of matchedRefs that can reach, via any dependsOn path, a +// component outside matchedRefs - i.e. something new or changed that must not be silently dropped. A +// matched ref that cannot escape has nothing real left underneath it, including a ref stuck in a cycle +// made up entirely of other non-escaping matched refs. +func calculateEscapingRefs(dependencies *[]cyclonedx.Dependency, matchedRefs *datastructures.Set[string]) *datastructures.Set[string] { + escapes := datastructures.MakeSet[string]() + for { + changed := false + for _, ref := range matchedRefs.ToSlice() { + if escapes.Exists(ref) { + continue + } + for _, child := range GetDirectDependencies(dependencies, ref) { + if !matchedRefs.Exists(child) || escapes.Exists(child) { + escapes.Add(ref) + changed = true + break + } + } + } + if !changed { + break + } + } + return escapes +} + // excludeComponentsOnly drops any component matching componentsToExclude by purl, used when the BOM has // no dependency graph at all so there is no parent/child structure to preserve. func excludeComponentsOnly(bom cyclonedx.BOM, componentsToExclude []cyclonedx.Component) *cyclonedx.BOM { diff --git a/utils/formats/cdxutils/cyclonedxutils_test.go b/utils/formats/cdxutils/cyclonedxutils_test.go index 8db607802..d6c958551 100644 --- a/utils/formats/cdxutils/cyclonedxutils_test.go +++ b/utils/formats/cdxutils/cyclonedxutils_test.go @@ -1593,15 +1593,23 @@ func TestExclude(t *testing.T) { }, }, { - name: "Exclude single component with transitive dependencies", + // comp1 is matched (unchanged), but comp2 and comp3 are not independently confirmed unchanged, + // so comp1 must not be dropped wholesale - doing so would silently hide them if either were + // actually new or changed. Nothing is excluded here. + name: "Exclude does not remove a matched component with unmatched children", exclude: []cyclonedx.Component{{BOMRef: "comp1", PackageURL: "pkg:comp1"}}, bom: *bom, expected: &cyclonedx.BOM{ Components: &[]cyclonedx.Component{ {BOMRef: "root", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "comp1", PackageURL: "pkg:comp1", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "comp2", PackageURL: "pkg:comp2", Type: cyclonedx.ComponentTypeLibrary}, {BOMRef: "comp3", PackageURL: "pkg:comp3", Type: cyclonedx.ComponentTypeLibrary}, }, - Dependencies: &[]cyclonedx.Dependency{{Ref: "root", Dependencies: &[]string{"comp3"}}}, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "root", Dependencies: &[]string{"comp1", "comp3"}}, + {Ref: "comp1", Dependencies: &[]string{"comp2", "comp3"}}, + }, }, }, { @@ -1635,17 +1643,17 @@ func TestExclude(t *testing.T) { }, }, { + // comp1 is a leaf, so a successful hash-insensitive match removes it outright; comp2 is never + // in the exclude list and must survive untouched, proving the match is scoped to comp1 alone. name: "Exclude by same name+version while ignoring hash", bom: cyclonedx.BOM{ Components: &[]cyclonedx.Component{ {BOMRef: "root", Type: cyclonedx.ComponentTypeLibrary}, {BOMRef: "comp1", PackageURL: "pkg:npm/comp1@1.0.0?hash=4321", Type: cyclonedx.ComponentTypeLibrary}, {BOMRef: "comp2", PackageURL: "pkg:npm/comp2@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, - {BOMRef: "comp3", PackageURL: "pkg:npm/comp3@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, }, Dependencies: &[]cyclonedx.Dependency{ - {Ref: "root", Dependencies: &[]string{"comp1", "comp3"}}, - {Ref: "comp1", Dependencies: &[]string{"comp2", "comp3"}}, + {Ref: "root", Dependencies: &[]string{"comp1", "comp2"}}, }, }, // Exclude the same name+version, with a hash that should be ignored @@ -1653,10 +1661,10 @@ func TestExclude(t *testing.T) { expected: &cyclonedx.BOM{ Components: &[]cyclonedx.Component{ {BOMRef: "root", Type: cyclonedx.ComponentTypeLibrary}, - {BOMRef: "comp3", PackageURL: "pkg:npm/comp3@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "comp2", PackageURL: "pkg:npm/comp2@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, }, Dependencies: &[]cyclonedx.Dependency{ - {Ref: "root", Dependencies: &[]string{"comp3"}}, + {Ref: "root", Dependencies: &[]string{"comp2"}}, }, }, }, @@ -1778,6 +1786,7 @@ func TestExclude(t *testing.T) { Dependencies: &[]cyclonedx.Dependency{ {Ref: "file-root", Dependencies: &[]string{"app", "lib"}}, {Ref: "app", Dependencies: &[]string{"lib"}}, + {Ref: "lib", Dependencies: &[]string{}}, }, }, // app is listed first, before its own dependency lib is processed @@ -1851,6 +1860,119 @@ func TestExclude(t *testing.T) { }, }, }, + { + // root and mid are both unchanged, but leaf is new and has no dependency entry of its own (a + // plain leaf). Excluding mid's whole entry wholesale would orphan leaf and silently drop it + // from the scan - mid (and root above it) must stay, in full, as leaf's anchor. + name: "Exclude keeps an unchanged chain when a new transitive leaf has no dependency entry", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "root", PackageURL: "pkg:npm/root@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "mid", PackageURL: "pkg:npm/mid@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "leaf", PackageURL: "pkg:npm/leaf@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "root", Dependencies: &[]string{"mid"}}, + {Ref: "mid", Dependencies: &[]string{"leaf"}}, + }, + }, + // leaf is absent - it is new, added by this PR + exclude: []cyclonedx.Component{ + {BOMRef: "root", PackageURL: "pkg:npm/root@1.0.0"}, + {BOMRef: "mid", PackageURL: "pkg:npm/mid@1.0.0"}, + }, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "root", PackageURL: "pkg:npm/root@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "mid", PackageURL: "pkg:npm/mid@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "leaf", PackageURL: "pkg:npm/leaf@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "root", Dependencies: &[]string{"mid"}}, + {Ref: "mid", Dependencies: &[]string{"leaf"}}, + }, + }, + }, + { + // app and lib are unchanged roots that depend on each other, with no edge to anything new or + // changed. Neither ever becomes "childless" on its own, so a single non-cyclic pass would keep + // both forever; the whole closed pair must be recognized and excluded together. + name: "Exclude removes a cycle of unchanged roots with no escaping edge", + bom: cyclonedx.BOM{ + Metadata: &cyclonedx.Metadata{ + Component: &cyclonedx.Component{BOMRef: "file-root", Type: cyclonedx.ComponentTypeFile}, + }, + Components: &[]cyclonedx.Component{ + {BOMRef: "app", PackageURL: "pkg:npm/app@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "lib", PackageURL: "pkg:npm/lib@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "file-root", Dependencies: &[]string{"app", "lib"}}, + {Ref: "app", Dependencies: &[]string{"lib"}}, + {Ref: "lib", Dependencies: &[]string{"app"}}, + }, + }, + exclude: []cyclonedx.Component{ + {BOMRef: "app", PackageURL: "pkg:npm/app@1.0.0"}, + {BOMRef: "lib", PackageURL: "pkg:npm/lib@1.0.0"}, + }, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{}, + Dependencies: &[]cyclonedx.Dependency{}, + }, + }, + { + // web's version changed since the target (2.0.0 here vs. 1.0.0 in the exclude list), so it + // never matches and must be left completely untouched, license and dependency included. + name: "Exclude does not touch a root whose purl changed since the target", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@2.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "web", Dependencies: &[]string{"dep1"}}, + }, + }, + exclude: []cyclonedx.Component{{BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}}, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "web", PackageURL: "pkg:npm/web@2.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "web", Dependencies: &[]string{"dep1"}}, + }, + }, + }, + { + // outer is unchanged, but its child inner's purl changed since the target (2.0.0 vs. the + // 1.0.0 passed in the exclude list), so inner never matches either. outer must stay to anchor + // the changed inner, and inner itself is untouched since it was never a match in the first place. + name: "Exclude keeps an unchanged root whose child root's purl changed", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "outer", PackageURL: "pkg:npm/outer@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "inner", PackageURL: "pkg:npm/inner@2.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "outer", Dependencies: &[]string{"inner"}}, + }, + }, + exclude: []cyclonedx.Component{ + {BOMRef: "outer", PackageURL: "pkg:npm/outer@1.0.0"}, + {BOMRef: "inner", PackageURL: "pkg:npm/inner@1.0.0"}, + }, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "outer", PackageURL: "pkg:npm/outer@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + {BOMRef: "inner", PackageURL: "pkg:npm/inner@2.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "outer", Dependencies: &[]string{"inner"}}, + }, + }, + }, } for _, tt := range tests { From 17b9b77337832c1e2db323acfd0b6d8cfd8b3aa5 Mon Sep 17 00:00:00 2001 From: Or Toren Date: Wed, 14 Oct 2026 12:30:44 +0300 Subject: [PATCH 6/6] Address review: match by bom-ref, ignore dangling dependsOn refs Matching only tracked the first component SearchComponentByCleanPurl found for a given normalized purl, but removal matched by purl too - so two distinct components sharing a normalized purl (same name+version, different hash) could have one's new child dropped along with the other's wholesale deletion. Exclude and excludeFromDependencies now key everything off the matched bom-refs themselves, so only the refs actually decided for exclusion are touched. calculateEscapingRefs also treated any dependsOn ref outside the matched set as a real escape, including one that does not resolve to any component at all (a dangling or malformed entry). Such a ref now carries no signal either way, so a root whose only child is unresolvable is still correctly excluded. Co-Authored-By: Claude Sonnet 5 --- sca/bom/bomgenerator_test.go | 5 +- utils/formats/cdxutils/cyclonedxutils.go | 58 ++++++++--------- utils/formats/cdxutils/cyclonedxutils_test.go | 63 ++++++++++++++++--- 3 files changed, 83 insertions(+), 43 deletions(-) diff --git a/sca/bom/bomgenerator_test.go b/sca/bom/bomgenerator_test.go index d2dcf099e..df5a21693 100644 --- a/sca/bom/bomgenerator_test.go +++ b/sca/bom/bomgenerator_test.go @@ -96,6 +96,7 @@ func TestGetDiff(t *testing.T) { }, }, }, + // component2 is reachable only through component1, not directly from root sbom: &cyclonedx.BOM{ Components: &[]cyclonedx.Component{ {Type: cyclonedx.ComponentTypeLibrary, BOMRef: "root", Version: "1.0"}, @@ -103,7 +104,7 @@ func TestGetDiff(t *testing.T) { {Type: cyclonedx.ComponentTypeLibrary, BOMRef: "component2", PackageURL: "pkg:component2", Version: "2.0"}, }, Dependencies: &[]cyclonedx.Dependency{ - {Ref: "root", Dependencies: &[]string{"component1", "component2"}}, + {Ref: "root", Dependencies: &[]string{"component1"}}, {Ref: "component1", Dependencies: &[]string{"component2"}}, }, }, @@ -114,7 +115,7 @@ func TestGetDiff(t *testing.T) { {Type: cyclonedx.ComponentTypeLibrary, BOMRef: "component2", PackageURL: "pkg:component2", Version: "2.0"}, }, Dependencies: &[]cyclonedx.Dependency{ - {Ref: "root", Dependencies: &[]string{"component1", "component2"}}, + {Ref: "root", Dependencies: &[]string{"component1"}}, {Ref: "component1", Dependencies: &[]string{"component2"}}, }, }, diff --git a/utils/formats/cdxutils/cyclonedxutils.go b/utils/formats/cdxutils/cyclonedxutils.go index 5794d8b86..41ccad2ef 100644 --- a/utils/formats/cdxutils/cyclonedxutils.go +++ b/utils/formats/cdxutils/cyclonedxutils.go @@ -360,30 +360,28 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil } filteredSbom = &bom matchedRefs := datastructures.MakeSet[string]() - matchedComponents := map[string]cyclonedx.Component{} for _, compToExclude := range componentsToExclude { - matchedBomComp := SearchComponentByCleanPurl(bom.Components, compToExclude.PackageURL) - if matchedBomComp == nil { - // Not a match (new, or its purl changed since the target), skip it - continue + excludePurl := techutils.PurlToXrayComponentId(compToExclude.PackageURL) + for _, comp := range *bom.Components { + // Match by normalized purl: every component sharing it (e.g. same name+version with a + // different hash) is a distinct bom-ref and must be tracked - and later excluded - on its own. + if techutils.PurlToXrayComponentId(comp.PackageURL) == excludePurl { + matchedRefs.Add(comp.BOMRef) + } } - matchedRefs.Add(matchedBomComp.BOMRef) - matchedComponents[matchedBomComp.BOMRef] = *matchedBomComp } // A matched (unchanged) component is only excluded once it cannot reach anything real - no new or // changed component, and no other matched component that itself can. Root or not, a matched node with // a real descendant must stay to anchor it, and a closed group of matched nodes with no escape (a // monorepo wrapper's two packages depending on each other, for instance) is excluded together. - escapingRefs := calculateEscapingRefs(bom.Dependencies, matchedRefs) - var toExcludeComponents []cyclonedx.Component - for ref, comp := range matchedComponents { + escapingRefs := calculateEscapingRefs(bom.Dependencies, bom.Components, matchedRefs) + toExcludeRefs := datastructures.MakeSet[string]() + for _, ref := range matchedRefs.ToSlice() { if !escapingRefs.Exists(ref) { - toExcludeComponents = append(toExcludeComponents, comp) + toExcludeRefs.Add(ref) } } - if len(toExcludeComponents) > 0 { - filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, bom.Components, toExcludeComponents...) - } + filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, toExcludeRefs) toExclude := datastructures.MakeSet[string]() for _, comp := range *filteredSbom.Components { if comp.Type != cyclonedx.ComponentTypeLibrary { @@ -428,7 +426,7 @@ func Exclude(bom cyclonedx.BOM, componentsToExclude ...cyclonedx.Component) (fil // component outside matchedRefs - i.e. something new or changed that must not be silently dropped. A // matched ref that cannot escape has nothing real left underneath it, including a ref stuck in a cycle // made up entirely of other non-escaping matched refs. -func calculateEscapingRefs(dependencies *[]cyclonedx.Dependency, matchedRefs *datastructures.Set[string]) *datastructures.Set[string] { +func calculateEscapingRefs(dependencies *[]cyclonedx.Dependency, components *[]cyclonedx.Component, matchedRefs *datastructures.Set[string]) *datastructures.Set[string] { escapes := datastructures.MakeSet[string]() for { changed := false @@ -437,6 +435,10 @@ func calculateEscapingRefs(dependencies *[]cyclonedx.Dependency, matchedRefs *da continue } for _, child := range GetDirectDependencies(dependencies, ref) { + if SearchComponentByRef(components, child) == nil { + // Not a real component (e.g. a dangling/malformed dependsOn ref) - no signal either way + continue + } if !matchedRefs.Exists(child) || escapes.Exists(child) { escapes.Add(ref) changed = true @@ -486,25 +488,24 @@ func excludeFromComponents(components *[]cyclonedx.Component, excludeComponents return &filteredComponents } -func excludeFromDependencies(dependencies *[]cyclonedx.Dependency, components *[]cyclonedx.Component, excludeComponents ...cyclonedx.Component) *[]cyclonedx.Dependency { - if dependencies == nil || len(*dependencies) == 0 || len(excludeComponents) == 0 { +// excludeFromDependencies drops excludeRefs's own dependsOn entries entirely and strips them wherever they +// appear as someone else's child. Matching is by bom-ref, not purl: two distinct components can share a +// normalized purl (e.g. same name+version with a different hash), and only the refs actually decided for +// exclusion must be touched, never every component with the same identity. +func excludeFromDependencies(dependencies *[]cyclonedx.Dependency, excludeRefs *datastructures.Set[string]) *[]cyclonedx.Dependency { + if dependencies == nil || len(*dependencies) == 0 || len(excludeRefs.ToSlice()) == 0 { return dependencies } - excludePurls := datastructures.MakeSet[string]() - for _, component := range excludeComponents { - excludePurls.Add(techutils.PurlToXrayComponentId(component.PackageURL)) - } filteredDependencies := []cyclonedx.Dependency{} for _, dep := range *dependencies { - if excludePurls.Exists(GetTrimmedPurlByRef(dep.Ref, components)) { + if excludeRefs.Exists(dep.Ref) { // This dependency is excluded, skip it continue } filteredDep := cyclonedx.Dependency{Ref: dep.Ref} if dep.Dependencies != nil { - // Also filter the components from the dependencies of this dependency for _, depRef := range *dep.Dependencies { - if !excludePurls.Exists(GetTrimmedPurlByRef(depRef, components)) { + if !excludeRefs.Exists(depRef) { if filteredDep.Dependencies == nil { filteredDep.Dependencies = &[]string{} } @@ -517,15 +518,6 @@ func excludeFromDependencies(dependencies *[]cyclonedx.Dependency, components *[ return &filteredDependencies } -func GetTrimmedPurlByRef(dep string, components *[]cyclonedx.Component) string { - component := SearchComponentByRef(components, dep) - if component == nil { - // couldn't find component - return "" - } - return techutils.PurlToXrayComponentId(component.PackageURL) -} - // ScanLicenseToCycloneDx maps an Xray license key/name to a CycloneDX license. // CycloneDX allows only one of ID or Name, not both. func ScanLicenseToCycloneDx(key, name string) *cyclonedx.License { diff --git a/utils/formats/cdxutils/cyclonedxutils_test.go b/utils/formats/cdxutils/cyclonedxutils_test.go index d6c958551..57865ee04 100644 --- a/utils/formats/cdxutils/cyclonedxutils_test.go +++ b/utils/formats/cdxutils/cyclonedxutils_test.go @@ -1742,16 +1742,17 @@ func TestExclude(t *testing.T) { { // Metadata.Component is the Xray-Lib-Plugin wrapper: GetRootDependenciesEntries treats every // one of its dependsOn children as a root (see TestGetRootDependenciesEntries) + // Purls carry a ?hash= qualifier, as the real Xray-Lib Plugin output does. name: "Exclude fully unchanged roots under a Metadata.Component wrapper", bom: cyclonedx.BOM{ Metadata: &cyclonedx.Metadata{ Component: &cyclonedx.Component{BOMRef: "file-root", Type: cyclonedx.ComponentTypeFile}, }, Components: &[]cyclonedx.Component{ - {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, - {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21", Type: cyclonedx.ComponentTypeLibrary}, - {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, - {BOMRef: "ms", PackageURL: "pkg:npm/ms@2.1.3", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0?hash=98d521d8", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "ISC"}}}}, + {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21?hash=ab0bf448", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0?hash=4af913d9", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "ms", PackageURL: "pkg:npm/ms@2.1.3?hash=d485e609", Type: cyclonedx.ComponentTypeLibrary}, }, Dependencies: &[]cyclonedx.Dependency{ {Ref: "file-root", Dependencies: &[]string{"api", "web"}}, @@ -1759,11 +1760,12 @@ func TestExclude(t *testing.T) { {Ref: "web", Dependencies: &[]string{"ms"}}, }, }, + // Same name+version, different hash than the source - still an unchanged match exclude: []cyclonedx.Component{ - {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0"}, - {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21"}, - {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0"}, - {BOMRef: "ms", PackageURL: "pkg:npm/ms@2.1.3"}, + {BOMRef: "api", PackageURL: "pkg:npm/api@1.0.0?hash=00000000"}, + {BOMRef: "lodash", PackageURL: "pkg:npm/lodash@4.17.21?hash=00000000"}, + {BOMRef: "web", PackageURL: "pkg:npm/web@1.0.0?hash=00000000"}, + {BOMRef: "ms", PackageURL: "pkg:npm/ms@2.1.3?hash=00000000"}, }, expected: &cyclonedx.BOM{ Components: &[]cyclonedx.Component{}, @@ -1973,6 +1975,51 @@ func TestExclude(t *testing.T) { }, }, }, + { + // a and b share a normalized purl (same name+version, different hash) but are distinct bom-refs. + // a is a leaf and must be excluded; b has a new child (leaf) and must not be, even though + // matching by purl alone would conflate the two. + name: "Exclude does not conflate two components that share a normalized purl", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "a", PackageURL: "pkg:npm/same@1.0.0?hash=aaa", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "b", PackageURL: "pkg:npm/same@1.0.0?hash=bbb", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "leaf", PackageURL: "pkg:npm/leaf@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "b", Dependencies: &[]string{"leaf"}}, + }, + }, + exclude: []cyclonedx.Component{{BOMRef: "same", PackageURL: "pkg:npm/same@1.0.0?hash=ccc"}}, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "b", PackageURL: "pkg:npm/same@1.0.0?hash=bbb", Type: cyclonedx.ComponentTypeLibrary}, + {BOMRef: "leaf", PackageURL: "pkg:npm/leaf@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "b", Dependencies: &[]string{"leaf"}}, + }, + }, + }, + { + // root's only child, missing-ref, does not resolve to any real component (a dangling or + // malformed dependsOn entry). It must not count as a real/changed descendant, so root - with + // nothing actually new beneath it - is still excluded. + name: "Exclude treats a dependsOn ref that resolves to no component as no signal", + bom: cyclonedx.BOM{ + Components: &[]cyclonedx.Component{ + {BOMRef: "root", PackageURL: "pkg:npm/root@1.0.0", Type: cyclonedx.ComponentTypeLibrary, Licenses: &cyclonedx.Licenses{{License: &cyclonedx.License{ID: "MIT"}}}}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "root", Dependencies: &[]string{"missing-ref"}}, + }, + }, + exclude: []cyclonedx.Component{{BOMRef: "root", PackageURL: "pkg:npm/root@1.0.0"}}, + expected: &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{}, + Dependencies: &[]cyclonedx.Dependency{}, + }, + }, } for _, tt := range tests {