From 06641cfda20cfe5e4198fe23e14d3c69e5935a6a Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 5 Jan 2021 18:48:28 -0500 Subject: [PATCH] prefer real paths for glob results Signed-off-by: Alex Goodman --- syft/source/all_layers_resolver.go | 6 ++++-- syft/source/all_layers_resolver_test.go | 8 ++++++++ syft/source/image_squash_resolver.go | 3 ++- syft/source/image_squash_resolver_test.go | 8 ++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/syft/source/all_layers_resolver.go b/syft/source/all_layers_resolver.go index f2f248ee2..a12985e0a 100644 --- a/syft/source/all_layers_resolver.go +++ b/syft/source/all_layers_resolver.go @@ -114,7 +114,8 @@ func (r *AllLayersResolver) FilesByPath(paths ...string) ([]Location, error) { return nil, err } for _, result := range results { - uniqueLocations = append(uniqueLocations, NewLocationFromImage(path, result, r.img)) + // we always prefer the REAL path (not the user given path which may have symlinks) + uniqueLocations = append(uniqueLocations, NewLocationFromImage(string(result.RealPath), result, r.img)) } } } @@ -153,7 +154,8 @@ func (r *AllLayersResolver) FilesByGlob(patterns ...string) ([]Location, error) return nil, err } for _, refResult := range refResults { - uniqueLocations = append(uniqueLocations, NewLocationFromImage(string(result.MatchPath), refResult, r.img)) + // we always prefer the REAL path (not the user given path which may have symlinks) + uniqueLocations = append(uniqueLocations, NewLocationFromImage(string(refResult.RealPath), refResult, r.img)) } } } diff --git a/syft/source/all_layers_resolver_test.go b/syft/source/all_layers_resolver_test.go index d180ea3de..91e8f57f9 100644 --- a/syft/source/all_layers_resolver_test.go +++ b/syft/source/all_layers_resolver_test.go @@ -117,6 +117,10 @@ func TestAllLayersResolver_FilesByPath(t *testing.T) { t.Errorf("bad resolve path: '%s'!='%s'", string(actual.ref.RealPath), expected.path) } + if expected.path != "" && string(actual.ref.RealPath) != actual.Path { + t.Errorf("we should always prefer real paths over ones with links") + } + entry, err := img.FileCatalog.Get(actual.ref) if err != nil { t.Fatalf("failed to get metadata: %+v", err) @@ -221,6 +225,10 @@ func TestAllLayersResolver_FilesByGlob(t *testing.T) { t.Errorf("bad resolve path: '%s'!='%s'", string(actual.ref.RealPath), expected.path) } + if expected.path != "" && string(actual.ref.RealPath) != actual.Path { + t.Errorf("we should always prefer real paths over ones with links") + } + entry, err := img.FileCatalog.Get(actual.ref) if err != nil { t.Fatalf("failed to get metadata: %+v", err) diff --git a/syft/source/image_squash_resolver.go b/syft/source/image_squash_resolver.go index 137d498b5..ed40bd839 100644 --- a/syft/source/image_squash_resolver.go +++ b/syft/source/image_squash_resolver.go @@ -66,7 +66,8 @@ func (r *ImageSquashResolver) FilesByPath(paths ...string) ([]Location, error) { if resolvedRef != nil && !uniqueFileIDs.Contains(*resolvedRef) { uniqueFileIDs.Add(*resolvedRef) - uniqueLocations = append(uniqueLocations, NewLocationFromImage(path, *resolvedRef, r.img)) + // we always prefer the REAL path (not the user given path which may have symlinks) + uniqueLocations = append(uniqueLocations, NewLocationFromImage(string(resolvedRef.RealPath), *resolvedRef, r.img)) } } diff --git a/syft/source/image_squash_resolver_test.go b/syft/source/image_squash_resolver_test.go index 93b07caa5..45f8a4d50 100644 --- a/syft/source/image_squash_resolver_test.go +++ b/syft/source/image_squash_resolver_test.go @@ -106,6 +106,10 @@ func TestImageSquashResolver_FilesByPath(t *testing.T) { t.Errorf("bad resolve path: '%s'!='%s'", string(actual.ref.RealPath), c.resolvePath) } + if c.resolvePath != "" && string(actual.ref.RealPath) != actual.Path { + t.Errorf("we should always prefer real paths over ones with links") + } + entry, err := img.FileCatalog.Get(actual.ref) if err != nil { t.Fatalf("failed to get metadata: %+v", err) @@ -208,6 +212,10 @@ func TestImageSquashResolver_FilesByGlob(t *testing.T) { t.Errorf("bad resolve path: '%s'!='%s'", string(actual.ref.RealPath), c.resolvePath) } + if c.resolvePath != "" && string(actual.ref.RealPath) != actual.Path { + t.Errorf("we should always prefer real paths over ones with links") + } + entry, err := img.FileCatalog.Get(actual.ref) if err != nil { t.Fatalf("failed to get metadata: %+v", err)