Просмотр исходного кода

Fix files whitelisting when checking Github PRs

Files whitelisting code was not executed when running the tool against a
Github PR so the config was ignored.

To avoid duplication, the whitelist files check has been moved to the `inspect`
function which is always executed.
Cristhian Amaya 7 лет назад
Родитель
Сommit
169a137e7f
2 измененных файлов с 27 добавлено и 13 удалено
  1. 18 0
      gitleaks_test.go
  2. 9 13
      main.go

+ 18 - 0
gitleaks_test.go

@@ -176,6 +176,7 @@ func TestRun(t *testing.T) {
 		description    string
 		description    string
 		expectedErrMsg string
 		expectedErrMsg string
 		whiteListRepos []string
 		whiteListRepos []string
+		whiteListFiles []*regexp.Regexp
 		numLeaks       int
 		numLeaks       int
 		configPath     string
 		configPath     string
 		commitPerPage  int
 		commitPerPage  int
@@ -290,6 +291,18 @@ func TestRun(t *testing.T) {
 			expectedErrMsg: "",
 			expectedErrMsg: "",
 			commitPerPage:  1,
 			commitPerPage:  1,
 		},
 		},
+		{
+			testOpts: Options{
+				GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
+			},
+			description:    "test github pr with whitelisted files",
+			numLeaks:       0,
+			expectedErrMsg: "",
+			commitPerPage:  1,
+			whiteListFiles: []*regexp.Regexp{
+				regexp.MustCompile("main.go"),
+			},
+		},
 	}
 	}
 	g := goblin.Goblin(t)
 	g := goblin.Goblin(t)
 	for _, test := range tests {
 	for _, test := range tests {
@@ -301,6 +314,11 @@ func TestRun(t *testing.T) {
 				if test.commitPerPage != 0 {
 				if test.commitPerPage != 0 {
 					githubPages = test.commitPerPage
 					githubPages = test.commitPerPage
 				}
 				}
+				if test.whiteListFiles != nil {
+					whiteListFiles = test.whiteListFiles
+				} else {
+					whiteListFiles = nil
+				}
 				opts = test.testOpts
 				opts = test.testOpts
 				leaks, err := run()
 				leaks, err := run()
 				if err != nil {
 				if err != nil {

+ 9 - 13
main.go

@@ -174,7 +174,7 @@ regex = '''xox[baprs]-([0-9a-zA-Z]{10,48})?'''
 
 
 [whitelist]
 [whitelist]
 files = [
 files = [
-  "(.*?)(jpg|gif|doc|pdf|bin)$" 
+  "(.*?)(jpg|gif|doc|pdf|bin)$"
 ]
 ]
 #commits = [
 #commits = [
 #  "BADHA5H1",
 #  "BADHA5H1",
@@ -486,11 +486,6 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
 				if bin || err != nil {
 				if bin || err != nil {
 					return nil
 					return nil
 				}
 				}
-				for _, re := range whiteListFiles {
-					if re.FindString(f.Name) != "" {
-						return nil
-					}
-				}
 				content, err := f.Contents()
 				content, err := f.Contents()
 				if err != nil {
 				if err != nil {
 					return nil
 					return nil
@@ -558,12 +553,6 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
 					} else if to != nil {
 					} else if to != nil {
 						filePath = to.Path()
 						filePath = to.Path()
 					}
 					}
-					for _, re := range whiteListFiles {
-						if re.FindString(filePath) != "" {
-							skipFile = true
-							break
-						}
-					}
 					if skipFile {
 					if skipFile {
 						continue
 						continue
 					}
 					}
@@ -602,12 +591,19 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
 // will skip lines that include a whitelisted regex. A list of leaks is returned.
 // will skip lines that include a whitelisted regex. A list of leaks is returned.
 // If verbose mode (-v/--verbose) is set, then checkDiff will log leaks as they are discovered.
 // If verbose mode (-v/--verbose) is set, then checkDiff will log leaks as they are discovered.
 func inspect(diff gitDiff) []Leak {
 func inspect(diff gitDiff) []Leak {
-	lines := strings.Split(diff.content, "\n")
 	var (
 	var (
 		leaks    []Leak
 		leaks    []Leak
 		skipLine bool
 		skipLine bool
 	)
 	)
 
 
+	for _, re := range whiteListFiles {
+		if re.FindString(diff.filePath) != "" {
+			return leaks
+		}
+	}
+
+	lines := strings.Split(diff.content, "\n")
+
 	for _, line := range lines {
 	for _, line := range lines {
 		skipLine = false
 		skipLine = false
 		for leakType, re := range regexes {
 		for leakType, re := range regexes {