Преглед изворни кода

Adding some logic to fix line number extractor when there are multipl… (#436)

* Adding some logic to fix line number extractor when there are multiple secrets of the same offender within the same patch

* formatting some consts
Zachary Rice пре 5 година
родитељ
комит
6c60c3dde3
1 измењених фајлова са 16 додато и 5 уклоњено
  1. 16 5
      scan/rule.go

+ 16 - 5
scan/rule.go

@@ -20,11 +20,12 @@ import (
 )
 
 const (
-	diffAddPrefix          = "+"
-	diffAddFilePrefix      = "+++ b"
-	diffAddFilePrefixSlash = "+++ b/"
-	diffLineSignature      = " @@"
-	defaultLineNumber      = -1
+	diffAddPrefix           = "+"
+	diffAddFilePrefix       = "+++ b"
+	diffAddFilePrefixSlash  = "+++ b/"
+	diffLineSignature       = " @@"
+	diffLineSignaturePrefix = "@@ "
+	defaultLineNumber       = -1
 )
 
 // CheckRules accepts bundle and checks each rule defined in the config against the bundle's content.
@@ -238,6 +239,16 @@ func extractAndInjectLineNumber(leak *manager.Leak, bundle *Bundle, repo *Repo)
 					leak.LineNumber = potentialLine
 					return
 				}
+			} else if strings.HasPrefix(txt, diffLineSignaturePrefix) && currStartDiffLine != 0 {
+				// This logic is used for when there are multiple leaks of the same offender within the same patch
+				i := strings.Index(txt, diffAddPrefix)
+				pairs := strings.Split(strings.Split(txt[i+1:], diffLineSignature)[0], ",")
+				currStartDiffLine, err = strconv.Atoi(pairs[0])
+				if err != nil {
+					log.Debug(err)
+					return
+				}
+				currLine = 0
 			}
 			currLine++
 		}