Files
syft/internal/task/file_tasks_test.go
T
Alex Goodman 9d820c6d94 fix: only look up owned-by-package files in the resolver's own frame
with `FilesOwnedByPackageSelection` the file catalogers take every package location in the SBOM and look it up by `RealPath` in the resolver they were given. Inside an archive, the java package's location is the archive itself *in its parent's frame* (`WEB-INF/lib/dep.jar`), so the sub-pipeline looked that path up inside `dep.jar`'s own resolver: a miss, or an unrelated same-named entry by coincidence. At the root, coordinates from inside archives were looked up against the scan root the same way.

`coordinatesForSelection` now keeps only coordinates whose `ArchivePath` matches the current frame (the traversal's `ContentsArchivePath()`, or blank at the root).

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
2026-09-24 13:30:33 -04:00

129 lines
3.5 KiB
Go

package task
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anchore/syft/internal/archive"
"github.com/anchore/syft/internal/sbomsync"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/sbom"
)
func Test_coordinatesForSelection(t *testing.T) {
tests := []struct {
name string
selection file.Selection
sbom *sbom.SBOM
files []file.Coordinates
ok bool
}{
{
name: "all files",
selection: file.AllFilesSelection,
files: nil,
ok: true,
},
{
name: "no files",
selection: file.NoFilesSelection,
files: nil,
ok: false,
},
{
name: "specific files with hits",
selection: file.FilesOwnedByPackageSelection,
sbom: &sbom.SBOM{
Relationships: []artifact.Relationship{
{
From: pkg.Package{},
To: file.Coordinates{
RealPath: "path",
FileSystemID: "fs",
},
Type: artifact.ContainsRelationship,
},
},
},
files: []file.Coordinates{
{
RealPath: "path",
FileSystemID: "fs",
},
},
ok: true,
},
{
name: "specific files no hits (by wrong type)",
selection: file.FilesOwnedByPackageSelection,
sbom: &sbom.SBOM{
Relationships: []artifact.Relationship{
{
From: pkg.Package{},
To: file.Coordinates{
RealPath: "path",
FileSystemID: "fs",
},
// wrong type
Type: artifact.DependencyOfRelationship,
},
},
},
files: nil,
ok: false,
},
{
name: "specific files no hits (by wrong node types)",
selection: file.FilesOwnedByPackageSelection,
sbom: &sbom.SBOM{
Relationships: []artifact.Relationship{
{
From: file.Coordinates{}, // wrong type
To: file.Coordinates{
RealPath: "path",
FileSystemID: "fs",
},
Type: artifact.ContainsRelationship,
},
},
},
files: nil,
ok: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
files, ok := coordinatesForSelection(context.Background(), tt.selection, sbomsync.NewBuilder(tt.sbom).(sbomsync.Accessor))
assert.Equal(t, tt.files, files)
assert.Equal(t, tt.ok, ok)
})
}
}
func Test_coordinatesForSelection_onlyTheResolversFrame(t *testing.T) {
// a package located at an archive in its parent's frame must not send the file catalogers looking up
// that path inside the archive's own resolver
atRoot := file.Coordinates{RealPath: "/app.war"}
inWar := file.Coordinates{RealPath: "WEB-INF/lib/dep.jar", ArchivePath: "/app.war"}
s := &sbom.SBOM{Artifacts: sbom.Artifacts{Packages: pkg.NewCollection(
pkg.Package{Name: "app", Locations: file.NewLocationSet(file.NewLocationFromCoordinates(atRoot))},
pkg.Package{Name: "dep", Locations: file.NewLocationSet(file.NewLocationFromCoordinates(inWar))},
)}}
accessor := sbomsync.NewBuilder(s).(sbomsync.Accessor)
got, ok := coordinatesForSelection(context.Background(), file.FilesOwnedByPackageSelection, accessor)
require.True(t, ok)
assert.Equal(t, []file.Coordinates{atRoot}, got, "at the scan root")
ctx := archive.WithTraversal(context.Background(), &archive.Traversal{Location: file.NewLocationFromCoordinates(atRoot)})
got, ok = coordinatesForSelection(ctx, file.FilesOwnedByPackageSelection, accessor)
require.True(t, ok)
assert.Equal(t, []file.Coordinates{inWar}, got, "inside app.war")
}