mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 12:44:22 +01:00
introducing secretGroup, the best group (#734)
* working on deduping * my eyes... oh god my poor eyes * more readme * more readme * more readme * more readme and formatting
This commit is contained in:
1 parent
9358e84588
commit
98d5648f6e
22 files changed
+248
-107
No files matched your search
+48
-5
@@ -6,6 +6,7 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/zricethezav/gitleaks/v8/config"
|
||||
"github.com/zricethezav/gitleaks/v8/report"
|
||||
)
|
||||
@@ -42,7 +43,7 @@ func DetectFindings(cfg config.Config, b []byte, filePath string, commit string)
|
||||
Description: r.Description,
|
||||
File: filePath,
|
||||
RuleID: r.RuleID,
|
||||
Context: fmt.Sprintf("file detected: %s", filePath),
|
||||
Match: fmt.Sprintf("file detected: %s", filePath),
|
||||
Tags: r.Tags,
|
||||
}
|
||||
findings = append(findings, f)
|
||||
@@ -59,6 +60,7 @@ func DetectFindings(cfg config.Config, b []byte, filePath string, commit string)
|
||||
matchIndices := r.Regex.FindAllIndex(b, -1)
|
||||
for _, m := range matchIndices {
|
||||
location := getLocation(linePairs, m[0], m[1])
|
||||
secret := strings.Trim(string(b[m[0]:m[1]]), "\n")
|
||||
f := report.Finding{
|
||||
Description: r.Description,
|
||||
File: filePath,
|
||||
@@ -67,8 +69,8 @@ func DetectFindings(cfg config.Config, b []byte, filePath string, commit string)
|
||||
EndLine: location.endLine,
|
||||
StartColumn: location.startColumn,
|
||||
EndColumn: location.endColumn,
|
||||
Secret: strings.Trim(string(b[m[0]:m[1]]), "\n"),
|
||||
Context: limit(strings.Trim(string(b[location.startLineIndex:location.endLineIndex]), "\n")),
|
||||
Secret: secret,
|
||||
Match: secret,
|
||||
Tags: r.Tags,
|
||||
}
|
||||
|
||||
@@ -76,8 +78,20 @@ func DetectFindings(cfg config.Config, b []byte, filePath string, commit string)
|
||||
continue
|
||||
}
|
||||
|
||||
// extract secret from secret group if set
|
||||
if r.SecretGroup != 0 {
|
||||
groups := r.Regex.FindStringSubmatch(secret)
|
||||
if len(groups)-1 > r.SecretGroup || len(groups) == 0 {
|
||||
// Config validation should prevent this
|
||||
break
|
||||
}
|
||||
secret = groups[r.SecretGroup]
|
||||
f.Secret = secret
|
||||
}
|
||||
|
||||
// extract secret from secret group if set
|
||||
if r.EntropySet() {
|
||||
include, entropy := r.IncludeEntropy(strings.Trim(string(b[m[0]:m[1]]), "\n"))
|
||||
include, entropy := r.IncludeEntropy(secret)
|
||||
if include {
|
||||
f.Entropy = float32(entropy)
|
||||
findings = append(findings, f)
|
||||
@@ -88,7 +102,7 @@ func DetectFindings(cfg config.Config, b []byte, filePath string, commit string)
|
||||
}
|
||||
}
|
||||
|
||||
return findings
|
||||
return dedupe(findings)
|
||||
}
|
||||
|
||||
func limit(s string) string {
|
||||
@@ -103,3 +117,32 @@ func printFinding(f report.Finding) {
|
||||
b, _ = json.MarshalIndent(f, "", " ")
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
|
||||
func dedupe(findings []report.Finding) []report.Finding {
|
||||
var retFindings []report.Finding
|
||||
for _, f := range findings {
|
||||
include := true
|
||||
if strings.Contains(strings.ToLower(f.RuleID), "generic") {
|
||||
for _, fPrime := range findings {
|
||||
if f.StartLine == fPrime.StartLine &&
|
||||
f.EndLine == fPrime.EndLine &&
|
||||
f.Commit == fPrime.Commit &&
|
||||
f.RuleID != fPrime.RuleID &&
|
||||
strings.Contains(fPrime.Secret, f.Secret) &&
|
||||
!strings.Contains(strings.ToLower(fPrime.RuleID), "generic") {
|
||||
|
||||
genericMatch := strings.Replace(f.Match, f.Secret, "REDACTED", -1)
|
||||
betterMatch := strings.Replace(fPrime.Match, fPrime.Secret, "REDACTED", -1)
|
||||
log.Debug().Msgf("skipping %s finding (%s), %s rule takes precendence (%s)", f.RuleID, genericMatch, fPrime.RuleID, betterMatch)
|
||||
include = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if include {
|
||||
retFindings = append(retFindings, f)
|
||||
}
|
||||
}
|
||||
|
||||
return retFindings
|
||||
}
|
||||
@@ -30,6 +30,7 @@ func TestDetectFindings(t *testing.T) {
|
||||
{
|
||||
Description: "AWS Access Key",
|
||||
Secret: "AKIALALEMEL33243OLIA",
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
File: "tmp.go",
|
||||
RuleID: "aws-access-key",
|
||||
Tags: []string{"key", "AWS"},
|
||||
@@ -62,7 +63,8 @@ func TestDetectFindings(t *testing.T) {
|
||||
expectedFindings: []report.Finding{
|
||||
{
|
||||
Description: "Discord API key",
|
||||
Secret: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
||||
Match: "Discord_Public_Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
||||
Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
|
||||
File: "tmp.go",
|
||||
RuleID: "discord-api-key",
|
||||
Tags: []string{},
|
||||
@@ -83,7 +85,8 @@ func TestDetectFindings(t *testing.T) {
|
||||
expectedFindings: []report.Finding{
|
||||
{
|
||||
Description: "Generic API Key",
|
||||
Secret: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
||||
Match: "Key = \"e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5\"",
|
||||
Secret: "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5",
|
||||
File: "tmp.py",
|
||||
RuleID: "generic-api-key",
|
||||
Tags: []string{},
|
||||
@@ -98,7 +101,7 @@ func TestDetectFindings(t *testing.T) {
|
||||
expectedFindings: []report.Finding{
|
||||
{
|
||||
Description: "Python Files",
|
||||
Context: "file detected: tmp.py",
|
||||
Match: "file detected: tmp.py",
|
||||
File: "tmp.py",
|
||||
RuleID: "python-files-only",
|
||||
Tags: []string{},
|
||||
@@ -110,7 +113,7 @@ func TestDetectFindings(t *testing.T) {
|
||||
bytes: []byte(`const Discord_Public_Key = "e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5"`),
|
||||
filePath: "tmp.go",
|
||||
expectedFindings: []report.Finding{},
|
||||
wantError: fmt.Errorf("Discord API key invalid regex entropy group 5, max regex entropy group 3"),
|
||||
wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
|
||||
},
|
||||
{
|
||||
cfgName: "simple",
|
||||
@@ -148,10 +151,6 @@ func TestDetectFindings(t *testing.T) {
|
||||
}
|
||||
|
||||
findings := DetectFindings(cfg, tt.bytes, tt.filePath, tt.commit)
|
||||
for _, f := range findings {
|
||||
f.Context = "" // remove lines cause copying and pasting them has some wack formatting
|
||||
f.Date = ""
|
||||
}
|
||||
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func TestFromFiles(t *testing.T) {
|
||||
EndLine: 19,
|
||||
StartColumn: 16,
|
||||
EndColumn: 35,
|
||||
Context: "\tawsToken := \"AKIALALEMEL33243OLIA\"",
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
Secret: "AKIALALEMEL33243OLIA",
|
||||
File: "../testdata/repos/nogit/main.go",
|
||||
RuleID: "aws-access-key",
|
||||
@@ -47,7 +47,7 @@ func TestFromFiles(t *testing.T) {
|
||||
EndLine: 19,
|
||||
StartColumn: 16,
|
||||
EndColumn: 35,
|
||||
Context: "\tawsToken := \"AKIALALEMEL33243OLIA\"",
|
||||
Match: "AKIALALEMEL33243OLIA",
|
||||
Secret: "AKIALALEMEL33243OLIA",
|
||||
File: "../testdata/repos/nogit/main.go",
|
||||
RuleID: "aws-access-key",
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ func FromGit(files <-chan *gitdiff.File, cfg config.Config, outputOptions Option
|
||||
|
||||
for _, fi := range DetectFindings(cfg, []byte(tf.Raw(gitdiff.OpAdd)), f.NewName, commitSHA) {
|
||||
// don't add to start/end lines if finding is from a file only rule
|
||||
if !strings.HasPrefix(fi.Context, "file detected") {
|
||||
if !strings.HasPrefix(fi.Match, "file detected") {
|
||||
fi.StartLine += int(tf.NewPosition)
|
||||
fi.EndLine += int(tf.NewPosition)
|
||||
}
|
||||
|
||||
+1
-1
@@ -119,7 +119,7 @@ func TestFromGit(t *testing.T) {
|
||||
|
||||
findings := FromGit(files, cfg, tt.opts)
|
||||
for _, f := range findings {
|
||||
f.Context = "" // remove lines cause copying and pasting them has some wack formatting
|
||||
f.Match = "" // remove lines cause copying and pasting them has some wack formatting
|
||||
f.Date = ""
|
||||
}
|
||||
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
||||
|
||||
Reference in new issue
Block a user