Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: index file itself when file scan path has symlink #2359

Merged
merged 2 commits into from
Nov 28, 2023
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
17 changes: 14 additions & 3 deletions syft/source/file_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,10 @@ func (s FileSource) FileResolver(_ Scope) (file.Resolver, error) {
}
isArchiveAnalysis := fi.IsDir()

absAnalysisPath, err := filepath.Abs(s.analysisPath)
absParentDir, err := absoluteSymlinkFreePathToParent(s.analysisPath)
if err != nil {
return nil, fmt.Errorf("unable to get absolute path for analysis path=%q: %w", s.analysisPath, err)
return nil, err
}
absParentDir := filepath.Dir(absAnalysisPath)

var res *fileresolver.Directory
if isArchiveAnalysis {
Expand Down Expand Up @@ -210,6 +209,18 @@ func (s FileSource) FileResolver(_ Scope) (file.Resolver, error) {
return s.resolver, nil
}

func absoluteSymlinkFreePathToParent(path string) (string, error) {
absAnalysisPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("unable to get absolute path for analysis path=%q: %w", path, err)
}
dereferencedAbsAnalysisPath, err := filepath.EvalSymlinks(absAnalysisPath)
if err != nil {
return "", fmt.Errorf("unable to get absolute path for analysis path=%q: %w", path, err)
}
return filepath.Dir(dereferencedAbsAnalysisPath), nil
}

func (s *FileSource) Close() error {
if s.closer == nil {
return nil
Expand Down
16 changes: 16 additions & 0 deletions syft/source/file_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ func TestNewFromFile(t *testing.T) {
},
expRefs: 1,
},
{
desc: "normal path",
input: "test-fixtures/actual-path/empty",
testPathFn: func(resolver file.Resolver) ([]file.Location, error) {
return resolver.FilesByPath("empty")
},
expRefs: 1,
},
{
desc: "path containing symlink",
input: "test-fixtures/symlink/empty",
testPathFn: func(resolver file.Resolver) ([]file.Location, error) {
return resolver.FilesByPath("empty")
},
expRefs: 1,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
Expand Down
Empty file.
1 change: 1 addition & 0 deletions syft/source/test-fixtures/symlink
13 changes: 13 additions & 0 deletions test/cli/symlink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cli

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_RequestedPathIncludesSymlink(t *testing.T) {
// path contains a symlink
path := "test-fixtures/image-pkg-coverage/pkgs/java/example-java-app-maven-0.1.0.jar"
_, stdout, _ := runSyft(t, nil, "packages", path)
assert.Contains(t, stdout, "example-java-app-maven")
}
Loading