diff --git a/sca/bom/bomgenerator_test.go b/sca/bom/bomgenerator_test.go index d7a577085..df5a21693 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"}}, @@ -93,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"}, @@ -100,17 +104,19 @@ 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"}}, }, }, 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"}}, + {Ref: "component1", Dependencies: &[]string{"component2"}}, }, }, }, diff --git a/utils/formats/cdxutils/cyclonedxutils.go b/utils/formats/cdxutils/cyclonedxutils.go index fc2a03618..41ccad2ef 100644 --- a/utils/formats/cdxutils/cyclonedxutils.go +++ b/utils/formats/cdxutils/cyclonedxutils.go @@ -349,20 +349,39 @@ 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) + matchedRefs := datastructures.MakeSet[string]() 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 - 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) + } } - // Exclude the component from the dependencies - filteredSbom.Dependencies = excludeFromDependencies(bom.Dependencies, bom.Components, compToExclude) } + // 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, bom.Components, matchedRefs) + toExcludeRefs := datastructures.MakeSet[string]() + for _, ref := range matchedRefs.ToSlice() { + if !escapingRefs.Exists(ref) { + toExcludeRefs.Add(ref) + } + } + filteredSbom.Dependencies = excludeFromDependencies(filteredSbom.Dependencies, toExcludeRefs) toExclude := datastructures.MakeSet[string]() for _, comp := range *filteredSbom.Components { if comp.Type != cyclonedx.ComponentTypeLibrary { @@ -403,6 +422,55 @@ 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, components *[]cyclonedx.Component, 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 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 + 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 { + 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 @@ -420,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{} } @@ -451,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 3863738ba..57865ee04 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"}}, }, }, }, @@ -1688,6 +1696,330 @@ func TestExclude(t *testing.T) { Dependencies: &[]cyclonedx.Dependency{}, }, }, + { + 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"}}}}, + {BOMRef: "dep1", PackageURL: "pkg:npm/dep1@1.0.0", Type: cyclonedx.ComponentTypeLibrary}, + }, + Dependencies: &[]cyclonedx.Dependency{ + {Ref: "web", Dependencies: &[]string{"dep1"}}, + }, + }, + // 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{ + {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"}}, + }, + }, + }, + { + 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"}}, + }, + }, + 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{}, + }, + }, + { + // 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?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"}}, + {Ref: "api", Dependencies: &[]string{"lodash"}}, + {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?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{}, + 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"}}, + {Ref: "lib", Dependencies: &[]string{}}, + }, + }, + // 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}, + }, + }, + }, + { + // 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"}}, + }, + }, + }, + { + // 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 { @@ -1700,7 +2032,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")