Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions sca/bom/bomgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand All @@ -93,24 +96,27 @@ 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"},
{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{"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"},
Comment thread
orto17 marked this conversation as resolved.
Resolved
{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"}},
},
},
},
Expand Down
110 changes: 84 additions & 26 deletions utils/formats/cdxutils/cyclonedxutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Comment thread
orto17 marked this conversation as resolved.
Resolved
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
Expand All @@ -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{}
}
Expand All @@ -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 {
Expand Down
Loading
Loading