mirror of
https://github.com/anchore/syft.git
synced 2026-05-20 04:05:24 +02:00
fix(source): treat exclude paths with trailing slash as directories (#4892)
A trailing slash on --exclude (e.g. './lib/') is dropped during pattern normalization but doublestar.Match still requires an exact string match, so the resulting pattern silently matches nothing and the directory is not excluded. Strip a trailing slash so './lib/' and './lib' behave the same. Fixes #4839 Signed-off-by: ChrisJr404 <chris@hacknow.com>
This commit is contained in:
parent
48e91312e8
commit
1caf243d29
@ -145,6 +145,9 @@ func GetDirectoryExclusionFunctions(root string, exclusions []string) ([]fileres
|
||||
// check exclusions for supported paths, these are all relative to the "scan root"
|
||||
if strings.HasPrefix(exclusion, "./") || strings.HasPrefix(exclusion, "*/") || strings.HasPrefix(exclusion, "**/") {
|
||||
exclusion = strings.TrimPrefix(exclusion, "./")
|
||||
// a trailing slash signals a directory but is otherwise discarded by doublestar.Match,
|
||||
// causing the pattern to silently match nothing (see issue #4839)
|
||||
exclusion = strings.TrimSuffix(exclusion, "/")
|
||||
exclusions[idx] = root + exclusion
|
||||
} else {
|
||||
errors = append(errors, exclusion)
|
||||
|
||||
@ -175,6 +175,18 @@ func Test_DirectorySource_Exclusions(t *testing.T) {
|
||||
},
|
||||
exclusions: []string{"./target"},
|
||||
},
|
||||
{
|
||||
input: "testdata/image-simple",
|
||||
desc: "exclude explicit directory with trailing slash (issue #4839)",
|
||||
glob: "**",
|
||||
expected: []string{
|
||||
"Dockerfile",
|
||||
"file-1.txt",
|
||||
"file-2.txt",
|
||||
//"target/really/nested/file-3.txt", // explicitly skipped
|
||||
},
|
||||
exclusions: []string{"./target/"},
|
||||
},
|
||||
{
|
||||
input: "testdata/image-simple",
|
||||
desc: "exclude explicit file relative to the root",
|
||||
@ -329,6 +341,14 @@ func Test_getDirectoryExclusionFunctions_crossPlatform(t *testing.T) {
|
||||
finfo: file.ManualInfo{ModeValue: os.ModeDir},
|
||||
walkHint: fs.SkipDir,
|
||||
},
|
||||
{
|
||||
desc: "directory exclusion with trailing slash (issue #4839)",
|
||||
root: "/usr/var",
|
||||
path: "/usr/var/lib",
|
||||
exclude: "./lib/",
|
||||
finfo: file.ManualInfo{ModeValue: os.ModeDir},
|
||||
walkHint: fs.SkipDir,
|
||||
},
|
||||
{
|
||||
desc: "no file info",
|
||||
root: "/",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user