include default newline pairs when calculating location (#1038)

* include default newline pairs when calculating location
This commit is contained in:
Zachary Rice authored and GitHub committed 2022-11-20 12:34:38 -06:00
1 parent d0733f96f3
commit 918e9bb226
2 files changed
+31 -16

No files matched your search

+21 -16
View File
@@ -18,8 +18,13 @@ const repoBasePath = "../testdata/repos/"
func TestDetect(t *testing.T) { func TestDetect(t *testing.T) {
tests := []struct { tests := []struct {
cfgName string cfgName string
fragment Fragment fragment Fragment
// NOTE: for expected findings, all line numbers will be 0
// because line deltas are added _after_ the finding is created.
// I.e, if the finding is from a --no-git file, the line number will be
// increase by 1 in DetectFromFiles(). If the finding is from git,
// the line number will be increased by the patch delta.
expectedFindings []report.Finding expectedFindings []report.Finding
wantError error wantError error
}{ }{
@@ -85,8 +90,8 @@ func TestDetect(t *testing.T) {
File: "tmp.go", File: "tmp.go",
RuleID: "pypi-upload-token", RuleID: "pypi-upload-token",
Tags: []string{"key", "pypi"}, Tags: []string{"key", "pypi"},
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 1, StartColumn: 1,
EndColumn: 86, EndColumn: 86,
Entropy: 1.9606875, Entropy: 1.9606875,
@@ -108,8 +113,8 @@ func TestDetect(t *testing.T) {
File: "tmp.go", File: "tmp.go",
RuleID: "aws-access-key", RuleID: "aws-access-key",
Tags: []string{"key", "AWS"}, Tags: []string{"key", "AWS"},
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 15, StartColumn: 15,
EndColumn: 34, EndColumn: 34,
Entropy: 3.0841837, Entropy: 3.0841837,
@@ -132,8 +137,8 @@ func TestDetect(t *testing.T) {
RuleID: "sidekiq-secret", RuleID: "sidekiq-secret",
Tags: []string{}, Tags: []string{},
Entropy: 2.6098502, Entropy: 2.6098502,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 8, StartColumn: 8,
EndColumn: 60, EndColumn: 60,
}, },
@@ -155,8 +160,8 @@ func TestDetect(t *testing.T) {
RuleID: "sidekiq-secret", RuleID: "sidekiq-secret",
Tags: []string{}, Tags: []string{},
Entropy: 2.6098502, Entropy: 2.6098502,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 21, StartColumn: 21,
EndColumn: 74, EndColumn: 74,
}, },
@@ -178,8 +183,8 @@ func TestDetect(t *testing.T) {
RuleID: "sidekiq-sensitive-url", RuleID: "sidekiq-sensitive-url",
Tags: []string{}, Tags: []string{},
Entropy: 2.984234, Entropy: 2.984234,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 8, StartColumn: 8,
EndColumn: 58, EndColumn: 58,
}, },
@@ -226,8 +231,8 @@ func TestDetect(t *testing.T) {
RuleID: "discord-api-key", RuleID: "discord-api-key",
Tags: []string{}, Tags: []string{},
Entropy: 3.7906237, Entropy: 3.7906237,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 7, StartColumn: 7,
EndColumn: 93, EndColumn: 93,
}, },
@@ -257,8 +262,8 @@ func TestDetect(t *testing.T) {
RuleID: "generic-api-key", RuleID: "generic-api-key",
Tags: []string{}, Tags: []string{},
Entropy: 3.7906237, Entropy: 3.7906237,
StartLine: 1, StartLine: 0,
EndLine: 1, EndLine: 0,
StartColumn: 22, StartColumn: 22,
EndColumn: 93, EndColumn: 93,
}, },
+10
View File
@@ -24,6 +24,16 @@ func location(fragment Fragment, matchIndex []int) Location {
// default startLineIndex to 0 // default startLineIndex to 0
location.startLineIndex = 0 location.startLineIndex = 0
// Fixes: https://github.com/zricethezav/gitleaks/issues/1037
// When a fragment does NOT have any newlines, a default "newline"
// will be counted to make the subsequent location calculation logic work
// for fragments will no newlines.
if len(fragment.newlineIndices) == 0 {
fragment.newlineIndices = [][]int{
{len(fragment.Raw), len(fragment.Raw) + 1},
}
}
for lineNum, pair := range fragment.newlineIndices { for lineNum, pair := range fragment.newlineIndices {
_lineNum = lineNum _lineNum = lineNum
newLineByteIndex := pair[0] newLineByteIndex := pair[0]