From c53e0fc98d5e29578ea3e78ae51ce01395c931b2 Mon Sep 17 00:00:00 2001 From: Zach Rice Date: Fri, 11 Mar 2022 17:55:50 -0600 Subject: [PATCH] maintaining parity between current master --- detect/detect.go | 23 +++++++++++++++++++---- detect/utils.go | 11 +++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/detect/detect.go b/detect/detect.go index e9e238c3..0248b8ee 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -9,13 +9,13 @@ import ( "strings" "sync" - "github.com/rs/zerolog/log" "github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/detect/git" "github.com/zricethezav/gitleaks/v8/report" "github.com/fatih/semgroup" "github.com/gitleaks/go-gitdiff/gitdiff" + "github.com/rs/zerolog/log" "github.com/spf13/viper" ) @@ -172,10 +172,25 @@ func (d *Detector) detectRule(fragment Fragment, rule *config.Rule) []report.Fin // check entropy entropy := shannonEntropy(finding.Secret) finding.Entropy = float32(entropy) - if rule.EntropySet() && entropy <= rule.Entropy { - // entropy is too low, skip this finding - continue + if rule.EntropySet() { + if entropy <= rule.Entropy { + // entropy is too low, skip this finding + continue + } + // NOTE: this is a goofy hack to get around the fact there golang's regex engine + // does not support positive lookaheads. Ideally we would want to add a + // restriction on generic rules regex that requires the secret match group + // contains both numbers and alphabetical characters. What this bit of code does is + // check if the ruleid is prepended with "generic" and enforces the + // secret contains both digits and alphabetical characters. + // TODO: this should be replaced with stop words + if strings.HasPrefix(rule.RuleID, "generic") { + if !containsDigit(secret) { + continue + } + } } + findings = append(findings, finding) } return findings diff --git a/detect/utils.go b/detect/utils.go index 6ea1efc3..9e4f3e56 100644 --- a/detect/utils.go +++ b/detect/utils.go @@ -94,3 +94,14 @@ func printFinding(f report.Finding) { b, _ = json.MarshalIndent(f, "", " ") fmt.Println(string(b)) } + +func containsDigit(s string) bool { + for _, c := range s { + switch c { + case '1', '2', '3', '4', '5', '6', '7', '8', '9': + return true + } + + } + return false +}