diff --git a/.gitignore b/.gitignore index 8861092..f6c8065 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ _testmain.go .DS_Store .vscode +.idea # SBOMs generated during CI /bom.json diff --git a/helpers.go b/helpers.go index 50ce198..e5f2218 100644 --- a/helpers.go +++ b/helpers.go @@ -402,7 +402,7 @@ func PackagePaths(root string, excludes []*regexp.Regexp) ([]string, error) { err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error { if filepath.Ext(path) == ".go" { path = filepath.Dir(path) - if isExcluded(path, excludes) { + if isExcluded(filepath.ToSlash(path), excludes) { return nil } paths[path] = true @@ -437,7 +437,7 @@ func isExcluded(str string, excludes []*regexp.Regexp) bool { func ExcludedDirsRegExp(excludedDirs []string) []*regexp.Regexp { var exps []*regexp.Regexp for _, excludedDir := range excludedDirs { - str := fmt.Sprintf(`([\\/])?%s([\\/])?`, excludedDir) + str := fmt.Sprintf(`([\\/])?%s([\\/])?`, strings.ReplaceAll(filepath.ToSlash(excludedDir), "/", `\/`)) r := regexp.MustCompile(str) exps = append(exps, r) } diff --git a/helpers_test.go b/helpers_test.go index 10edd72..ee3114c 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -49,6 +49,18 @@ var _ = Describe("Helpers", func() { Expect(err).ShouldNot(HaveOccurred()) Expect(paths).Should(Equal([]string{dir})) }) + It("should exclude folder with subpath", func() { + nested := dir + "/pkg/generated" + err := os.MkdirAll(nested, 0755) + Expect(err).ShouldNot(HaveOccurred()) + _, err = os.Create(nested + "/test.go") + Expect(err).ShouldNot(HaveOccurred()) + exclude, err := regexp.Compile(`([\\/])?/pkg\/generated([\\/])?`) + Expect(err).ShouldNot(HaveOccurred()) + paths, err := gosec.PackagePaths(dir+"/...", []*regexp.Regexp{exclude}) + Expect(err).ShouldNot(HaveOccurred()) + Expect(paths).Should(Equal([]string{dir})) + }) It("should be empty when folder does not exist", func() { nested := dir + "/test" paths, err := gosec.PackagePaths(nested+"/...", nil) @@ -66,7 +78,7 @@ var _ = Describe("Helpers", func() { Expect(err).ShouldNot(HaveOccurred()) Expect(root).Should(Equal(filepath.Join(cwd, base))) }) - It("should retrun the absolute path from ellipsis path", func() { + It("should return the absolute path from ellipsis path", func() { base := "test" cwd, err := os.Getwd() Expect(err).ShouldNot(HaveOccurred()) @@ -86,6 +98,17 @@ var _ = Describe("Helpers", func() { Expect(match).Should(BeFalse()) }) + It("should create a proper regexp for dir with subdir", func() { + r := gosec.ExcludedDirsRegExp([]string{`test/generated`}) + Expect(len(r)).Should(Equal(1)) + match := r[0].MatchString("/home/go/src/project/test/generated") + Expect(match).Should(BeTrue()) + match = r[0].MatchString("/home/go/src/project/test/pkg") + Expect(match).Should(BeFalse()) + match = r[0].MatchString("/home/go/src/project/vendor/pkg") + Expect(match).Should(BeFalse()) + }) + It("should create no regexp when dir list is empty", func() { r := gosec.ExcludedDirsRegExp(nil) Expect(len(r)).Should(Equal(0))