From 7f229faf12d01459d7f064766a8bd998d616ee85 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Sun, 20 Nov 2022 12:34:38 -0600 Subject: [PATCH] include default newline pairs when calculating location (#1038) * include default newline pairs when calculating location --- detect/detect_test.go | 37 +++++++++++++++++++++---------------- detect/location.go | 10 ++++++++++ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/detect/detect_test.go b/detect/detect_test.go index eb7f24a2..60584b5c 100644 --- a/detect/detect_test.go +++ b/detect/detect_test.go @@ -18,8 +18,13 @@ const repoBasePath = "../testdata/repos/" func TestDetect(t *testing.T) { tests := []struct { - cfgName string - fragment Fragment + cfgName string + 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 wantError error }{ @@ -85,8 +90,8 @@ func TestDetect(t *testing.T) { File: "tmp.go", RuleID: "pypi-upload-token", Tags: []string{"key", "pypi"}, - StartLine: 1, - EndLine: 1, + StartLine: 0, + EndLine: 0, StartColumn: 1, EndColumn: 86, Entropy: 1.9606875, @@ -108,8 +113,8 @@ func TestDetect(t *testing.T) { File: "tmp.go", RuleID: "aws-access-key", Tags: []string{"key", "AWS"}, - StartLine: 1, - EndLine: 1, + StartLine: 0, + EndLine: 0, StartColumn: 15, EndColumn: 34, Entropy: 3.0841837, @@ -132,8 +137,8 @@ func TestDetect(t *testing.T) { RuleID: "sidekiq-secret", Tags: []string{}, Entropy: 2.6098502, - StartLine: 1, - EndLine: 1, + StartLine: 0, + EndLine: 0, StartColumn: 8, EndColumn: 60, }, @@ -155,8 +160,8 @@ func TestDetect(t *testing.T) { RuleID: "sidekiq-secret", Tags: []string{}, Entropy: 2.6098502, - StartLine: 1, - EndLine: 1, + StartLine: 0, + EndLine: 0, StartColumn: 21, EndColumn: 74, }, @@ -178,8 +183,8 @@ func TestDetect(t *testing.T) { RuleID: "sidekiq-sensitive-url", Tags: []string{}, Entropy: 2.984234, - StartLine: 1, - EndLine: 1, + StartLine: 0, + EndLine: 0, StartColumn: 8, EndColumn: 58, }, @@ -226,8 +231,8 @@ func TestDetect(t *testing.T) { RuleID: "discord-api-key", Tags: []string{}, Entropy: 3.7906237, - StartLine: 1, - EndLine: 1, + StartLine: 0, + EndLine: 0, StartColumn: 7, EndColumn: 93, }, @@ -257,8 +262,8 @@ func TestDetect(t *testing.T) { RuleID: "generic-api-key", Tags: []string{}, Entropy: 3.7906237, - StartLine: 1, - EndLine: 1, + StartLine: 0, + EndLine: 0, StartColumn: 22, EndColumn: 93, }, diff --git a/detect/location.go b/detect/location.go index ac94d415..63b95380 100644 --- a/detect/location.go +++ b/detect/location.go @@ -24,6 +24,16 @@ func location(fragment Fragment, matchIndex []int) Location { // default startLineIndex to 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 { _lineNum = lineNum newLineByteIndex := pair[0]