Skip to content

Commit 9359810

Browse files
committed
chore: fix unknown coordinate lookups
Signed-off-by: Keith Zantow <[email protected]>
1 parent ffc7c9b commit 9359810

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

internal/task/unknowns_tasks.go

+35-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55

66
"github.com/mholt/archiver/v3"
7-
"github.com/scylladb/go-set/strset"
87

8+
"github.com/anchore/syft/internal/log"
99
"github.com/anchore/syft/internal/sbomsync"
1010
"github.com/anchore/syft/syft/file"
1111
"github.com/anchore/syft/syft/pkg"
@@ -29,14 +29,16 @@ func NewUnknownsFinalizeTask(cfg UnknownsConfig) Task {
2929
}
3030

3131
// processUnknowns removes unknown entries that have valid packages reported for the locations
32-
func (c UnknownsConfig) processUnknowns(_ context.Context, _ file.Resolver, builder sbomsync.Builder) error {
32+
func (c UnknownsConfig) processUnknowns(_ context.Context, resolver file.Resolver, builder sbomsync.Builder) error {
3333
accessor := builder.(sbomsync.Accessor)
34-
accessor.WriteToSBOM(c.finalize)
34+
accessor.WriteToSBOM(func(s *sbom.SBOM) {
35+
c.finalize(resolver, s)
36+
})
3537
return nil
3638
}
3739

38-
func (c UnknownsConfig) finalize(s *sbom.SBOM) {
39-
hasPackageReference := coordinateReferenceLookup(s)
40+
func (c UnknownsConfig) finalize(resolver file.Resolver, s *sbom.SBOM) {
41+
hasPackageReference := coordinateReferenceLookup(resolver, s)
4042

4143
for coords := range s.Artifacts.Unknowns {
4244
if !hasPackageReference(coords) {
@@ -67,22 +69,44 @@ func (c UnknownsConfig) finalize(s *sbom.SBOM) {
6769
}
6870
}
6971

70-
func coordinateReferenceLookup(s *sbom.SBOM) func(coords file.Coordinates) bool {
72+
func coordinateReferenceLookup(resolver file.Resolver, s *sbom.SBOM) func(coords file.Coordinates) bool {
7173
allPackageCoords := file.NewCoordinateSet()
74+
75+
// include all directly included locations that result in packages
7276
for p := range s.Artifacts.Packages.Enumerate() {
7377
allPackageCoords.Add(p.Locations.CoordinateSet().ToSlice()...)
7478
}
7579

76-
allMetadataFiles := strset.New()
80+
// include owned files, for example specified by package managers.
81+
// relationships for these owned files may be disabled, but we always want to include them
7782
for p := range s.Artifacts.Packages.Enumerate() {
7883
if f, ok := p.Metadata.(pkg.FileOwner); ok {
79-
for _, o := range f.OwnedFiles() {
80-
allMetadataFiles.Add(o)
84+
for _, ownedFilePath := range f.OwnedFiles() {
85+
// resolve these owned files, as they may have symlinks
86+
// but coordinates we will test against are always absolute paths
87+
locations, err := resolver.FilesByPath(ownedFilePath)
88+
if err != nil {
89+
log.Debugf("unable to resolve owned file '%s': %v", ownedFilePath, err)
90+
}
91+
for _, loc := range locations {
92+
allPackageCoords.Add(loc.Coordinates)
93+
}
8194
}
8295
}
8396
}
8497

85-
return func(coords file.Coordinates) bool {
86-
return allPackageCoords.Contains(coords) || allMetadataFiles.Has(coords.RealPath)
98+
// include relationships
99+
for _, r := range s.Relationships {
100+
_, fromPkgOk := r.From.(pkg.Package)
101+
fromFile, fromFileOk := r.From.(file.Coordinates)
102+
_, toPkgOk := r.To.(pkg.Package)
103+
toFile, toFileOk := r.To.(file.Coordinates)
104+
if fromPkgOk && toFileOk {
105+
allPackageCoords.Add(toFile)
106+
} else if fromFileOk && toPkgOk {
107+
allPackageCoords.Add(fromFile)
108+
}
87109
}
110+
111+
return allPackageCoords.Contains
88112
}

0 commit comments

Comments
 (0)