From f549dc36a9ce09774af8cdc3c09ae734673d6e43 Mon Sep 17 00:00:00 2001 From: aschaef19 Date: Mon, 27 Sep 2021 11:18:43 -0700 Subject: [PATCH] fix #14 --- pkg/file/fileUtil.go | 18 ++++++++++-------- pkg/file/fileUtil_test.go | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pkg/file/fileUtil.go b/pkg/file/fileUtil.go index 625f5be..8875178 100755 --- a/pkg/file/fileUtil.go +++ b/pkg/file/fileUtil.go @@ -70,7 +70,7 @@ func MultipartToScanFiles(files []*multipart.FileHeader, cfg cfgreader.Earlybird fileNameWithPathPrefix = pathSeparator + fileNameWithPathPrefix } //Skip file with extensions Earlybird ignores - if isIgnoredFile(fileNameWithPathPrefix) { + if isIgnoredFile(fileNameWithPathPrefix, cfg.SearchDir) { continue } @@ -142,7 +142,7 @@ func GetGitFiles(fileType string, cfg *cfgreader.EarlybirdConfig) (fileContext C fileList = parseGitFiles(output, cfg.VerboseEnabled, cfg.MaxFileSize) compressList, fileList = separateCompressedAndUncompressed(fileList) - compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList) //Get the files within our compressed list + compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList, cfg.SearchDir) //Get the files within our compressed list if err != nil { return fileContext, err } @@ -189,7 +189,7 @@ func GetFiles(searchDir, ignoreFile string, verbose bool, maxFileSize int64) (fi if err != nil { log.Println("Error reading directory: ", err) } - if !isIgnoredFile(path) { + if !isIgnoredFile(path, searchDir) { // Ignore the path if it's a directory pathIsDirectory, isDirErr := isDirectory(path) if !pathIsDirectory { @@ -224,7 +224,7 @@ func GetFiles(searchDir, ignoreFile string, verbose bool, maxFileSize int64) (fi var compressList []scan.File compressList, fileList = separateCompressedAndUncompressed(fileList) - compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList) //Get the files within our compressed list + compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList, searchDir) //Get the files within our compressed list if err != nil { return fileContext, err } @@ -360,9 +360,11 @@ func getIgnorePatterns(filePath, ignoreFile string, verbose bool) (ignorePattern } // If the file matches a pattern in one of the ignore files, return true -func isIgnoredFile(fileName string) bool { +func isIgnoredFile(fileName string, fileRoot string) bool { + // ignore root directory when checking ignore matching + trimmedName := strings.Replace(fileName, fileRoot, "", 1) for _, pattern := range ignorePatterns { - if wildcard.PatternMatch(fileName, pattern) { + if wildcard.PatternMatch(trimmedName, pattern) { return true } } @@ -417,7 +419,7 @@ func separateCompressedAndUncompressed(files []scan.File) (compressed, uncompres } //GetCompressedFiles provides all the files contained within compressed files -func GetCompressedFiles(files []scan.File) (newfiles []scan.File, compresspaths []string, err error) { +func GetCompressedFiles(files []scan.File, rootPath string) (newfiles []scan.File, compresspaths []string, err error) { //check if file list contains compressed files, if so, scan their contents for _, file := range files { //Unpack and append to file list @@ -431,7 +433,7 @@ func GetCompressedFiles(files []scan.File) (newfiles []scan.File, compresspaths return newfiles, compresspaths, err } for _, subfile := range filenames { - if !isIgnoredFile(subfile) && !scan.CompressPattern.MatchString(subfile) { + if !isIgnoredFile(subfile, rootPath) && !scan.CompressPattern.MatchString(subfile) { var curFile scan.File curFile.Path = file.Path + "/" + filepath.Base(subfile) curFile.Name = subfile //Build view file name in format: file.zip/contents/file diff --git a/pkg/file/fileUtil_test.go b/pkg/file/fileUtil_test.go index e02501c..900720c 100644 --- a/pkg/file/fileUtil_test.go +++ b/pkg/file/fileUtil_test.go @@ -131,6 +131,7 @@ func Test_isIgnoredFile(t *testing.T) { } type args struct { fileName string + rootPath string } tests := []struct { name string @@ -144,10 +145,18 @@ func Test_isIgnoredFile(t *testing.T) { }, want: true, }, + { + name: "Check if file is ignored", + args: args{ + fileName: "/test.js", + rootPath: "src/node_modules", + }, + want: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := isIgnoredFile(tt.args.fileName); got != tt.want { + if got := isIgnoredFile(tt.args.fileName, tt.args.rootPath); got != tt.want { t.Errorf("isIgnoredFile() = %v, want %v", got, tt.want) } }) @@ -272,6 +281,7 @@ func TestExists(t *testing.T) { func TestGetCompressedFiles(t *testing.T) { type args struct { files []scan.File + rootPath string } tests := []struct { name string @@ -291,7 +301,7 @@ func TestGetCompressedFiles(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotNewfiles, gotCompresspaths, err := GetCompressedFiles(tt.args.files) + gotNewfiles, gotCompresspaths, err := GetCompressedFiles(tt.args.files, tt.args.rootPath) if err != nil { t.Errorf("GetCompressedFiles() err = %v", err) }