From c4526b2c55928689a2780eab501103cc7d899ad8 Mon Sep 17 00:00:00 2001 From: Richard Gomez <32133502+rgmz@users.noreply.github.com> Date: Mon, 13 Jan 2025 20:57:25 -0500 Subject: [PATCH] refactor(generic-api-key): remove hard-coded 'magic' (#1600) --- cmd/generate/config/rules/generic.go | 7 +++++++ config/gitleaks.toml | 4 ++++ detect/detect.go | 12 ------------ detect/utils.go | 11 ----------- 4 files changed, 11 insertions(+), 23 deletions(-) diff --git a/cmd/generate/config/rules/generic.go b/cmd/generate/config/rules/generic.go index 0d71b3ca..0f9e70be 100644 --- a/cmd/generate/config/rules/generic.go +++ b/cmd/generate/config/rules/generic.go @@ -39,6 +39,13 @@ func GenericCredential() *config.Rule { }, Entropy: 3.5, Allowlists: []config.Allowlist{ + { + // 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 ensure the secret contains both numbers and alphabetical characters, not just alphabetical characters. + Regexes: []*regexp.Regexp{ + regexp.MustCompile(`^[a-zA-Z_.-]+$`), + }, + }, { Description: "Allowlist for Generic API Keys", MatchCondition: config.AllowlistMatchOr, diff --git a/config/gitleaks.toml b/config/gitleaks.toml index 289ae420..9663cb71 100644 --- a/config/gitleaks.toml +++ b/config/gitleaks.toml @@ -551,6 +551,10 @@ keywords = [ "token", ] [[rules.allowlists]] +regexes = [ + '''^[a-zA-Z_.-]+$''', +] +[[rules.allowlists]] regexTarget = "match" regexes = [ '''(?i)(accessor|access[_.-]?id|api[_.-]?(version|id)|rapid|capital|[a-z0-9-]*?api[a-z0-9-]*?:jar:|author|X-MS-Exchange-Organization-Auth|Authentication-Results|(credentials?[_.-]?id|withCredentials)|(bucket|foreign|hot|natural|primary|schema|sequence)[_.-]?key|key[_.-]?(alias|board|code|ring|selector|size|stone|storetype|word|up|down|left|right)|key(store|tab)[_.-]?(file|path)|issuerkeyhash|(?-i:[DdMm]onkey|[DM]ONKEY)|keying|(secret)[_.-]?name|UserSecretsId|(api|credentials|token)[_.-]?(endpoint|ur[il])|public[_.-]?(key|token)|(key|token)[_.-]?file)''', diff --git a/detect/detect.go b/detect/detect.go index faab296f..35e66170 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -431,18 +431,6 @@ MatchLoop: // 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, not just 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(r.RuleID, "generic") { - if !containsDigit(finding.Secret) { - continue - } - } } // check if the regexTarget is defined in the allowlist "regexes" entry // or if the secret is in the list of stopwords diff --git a/detect/utils.go b/detect/utils.go index 96d88103..0a656ef0 100644 --- a/detect/utils.go +++ b/detect/utils.go @@ -180,17 +180,6 @@ func printFinding(f report.Finding, noColor bool) { fmt.Println("") } -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 -} - func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' }