From 6c60c3dde3fd0ada46a36914776126e87c457958 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Tue, 1 Sep 2020 20:48:43 -0400 Subject: [PATCH] =?UTF-8?q?Adding=20some=20logic=20to=20fix=20line=20numbe?= =?UTF-8?q?r=20extractor=20when=20there=20are=20multipl=E2=80=A6=20(#436)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adding some logic to fix line number extractor when there are multiple secrets of the same offender within the same patch * formatting some consts --- scan/rule.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/scan/rule.go b/scan/rule.go index 0e199508..4e5480d7 100644 --- a/scan/rule.go +++ b/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++ }