mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
Refactor detect, add entropy to all findings (#804)
Refactor `detect`, add `entropy` to all findings
This commit is contained in:
1 parent
9326f35380
commit
6e72472b60
24 files changed
+1097
-924
No files matched your search
+17
-11
@@ -2,13 +2,23 @@ package config
|
||||
|
||||
import "regexp"
|
||||
|
||||
// Allowlist allows a rule to be ignored for specific
|
||||
// regexes, paths, and/or commits
|
||||
type Allowlist struct {
|
||||
// Short human readable description of the allowlist.
|
||||
Description string
|
||||
Regexes []*regexp.Regexp
|
||||
Paths []*regexp.Regexp
|
||||
Commits []string
|
||||
|
||||
// Regexes is slice of content regular expressions that are allowed to be ignored.
|
||||
Regexes []*regexp.Regexp
|
||||
|
||||
// Paths is a slice of path regular expressions that are allowed to be ignored.
|
||||
Paths []*regexp.Regexp
|
||||
|
||||
// Commits is a slice of commit SHAs that are allowed to be ignored.
|
||||
Commits []string
|
||||
}
|
||||
|
||||
// CommitAllowed returns true if the commit is allowed to be ignored.
|
||||
func (a *Allowlist) CommitAllowed(c string) bool {
|
||||
if c == "" {
|
||||
return false
|
||||
@@ -21,16 +31,12 @@ func (a *Allowlist) CommitAllowed(c string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// PathAllowed returns true if the path is allowed to be ignored.
|
||||
func (a *Allowlist) PathAllowed(path string) bool {
|
||||
if anyRegexMatch(path, a.Paths) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return anyRegexMatch(path, a.Paths)
|
||||
}
|
||||
|
||||
// RegexAllowed returns true if the regex is allowed to be ignored.
|
||||
func (a *Allowlist) RegexAllowed(s string) bool {
|
||||
if anyRegexMatch(s, a.Regexes) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return anyRegexMatch(s, a.Regexes)
|
||||
}
|
||||
+4
-51
@@ -103,7 +103,10 @@ func TestTranslate(t *testing.T) {
|
||||
}
|
||||
|
||||
var vc ViperConfig
|
||||
viper.Unmarshal(&vc)
|
||||
err = viper.Unmarshal(&vc)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
cfg, err := vc.Translate()
|
||||
if tt.wantError != nil {
|
||||
if err == nil {
|
||||
@@ -115,53 +118,3 @@ func TestTranslate(t *testing.T) {
|
||||
assert.Equal(t, cfg.Rules, tt.cfg.Rules)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIncludeEntropy(t *testing.T) {
|
||||
tests := []struct {
|
||||
rule Rule
|
||||
secret string
|
||||
entropy float32
|
||||
include bool
|
||||
}{
|
||||
{
|
||||
rule: Rule{
|
||||
RuleID: "generic-api-key",
|
||||
SecretGroup: 4,
|
||||
Entropy: 3.5,
|
||||
Regex: regexp.MustCompile(`(?i)((key|api|token|secret|password)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]`),
|
||||
},
|
||||
secret: `e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5`,
|
||||
entropy: 3.7906235872459746,
|
||||
include: true,
|
||||
},
|
||||
{
|
||||
rule: Rule{
|
||||
RuleID: "generic-api-key",
|
||||
SecretGroup: 4,
|
||||
Entropy: 4,
|
||||
Regex: regexp.MustCompile(`(?i)((key|api|token|secret|password)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]`),
|
||||
},
|
||||
secret: `e7322523fb86ed64c836a979cf8465fbd436378c653c1db38f9ae87bc62a6fd5`,
|
||||
entropy: 3.7906235872459746,
|
||||
include: false,
|
||||
},
|
||||
{
|
||||
rule: Rule{
|
||||
RuleID: "generic-api-key",
|
||||
SecretGroup: 4,
|
||||
Entropy: 3.0,
|
||||
Regex: regexp.MustCompile(`(?i)((key|api|token|secret|password)[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([0-9a-zA-Z\-_=]{8,64})['\"]`),
|
||||
},
|
||||
secret: `ssh-keyboard-interactive`,
|
||||
entropy: 0,
|
||||
include: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
include, entropy := tt.rule.IncludeEntropy(tt.secret)
|
||||
assert.Equal(t, true, tt.rule.EntropySet())
|
||||
assert.Equal(t, tt.entropy, float32(entropy))
|
||||
assert.Equal(t, tt.include, include)
|
||||
}
|
||||
}
|
||||
+25
-32
@@ -2,44 +2,37 @@ package config
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Rules contain information that define details on how to detect secrets
|
||||
type Rule struct {
|
||||
// Description is the description of the rule.
|
||||
Description string
|
||||
RuleID string
|
||||
Entropy float64
|
||||
|
||||
// RuleID is a unique identifier for this rule
|
||||
RuleID string
|
||||
|
||||
// Entropy is a float representing the minimum shannon
|
||||
// entropy a regex group must have to be considered a secret.
|
||||
Entropy float64
|
||||
|
||||
// SecretGroup is an int used to extract secret from regex
|
||||
// match and used as the group that will have its entropy
|
||||
// checked if `entropy` is set.
|
||||
SecretGroup int
|
||||
Regex *regexp.Regexp
|
||||
Path *regexp.Regexp
|
||||
Tags []string
|
||||
Allowlist Allowlist
|
||||
}
|
||||
|
||||
func (r *Rule) IncludeEntropy(secret string) (bool, float64) {
|
||||
// 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.
|
||||
if strings.HasPrefix(r.RuleID, "generic") {
|
||||
if !containsDigit(secret) {
|
||||
return false, 0.0
|
||||
}
|
||||
}
|
||||
// group = 0 will check the entropy of the whole regex match
|
||||
e := shannonEntropy(secret)
|
||||
if e > r.Entropy {
|
||||
return true, e
|
||||
}
|
||||
// Regex is a golang regular expression used to detect secrets.
|
||||
Regex *regexp.Regexp
|
||||
|
||||
return false, e
|
||||
}
|
||||
// Path is a golang regular expression used to
|
||||
// filter secrets by path
|
||||
Path *regexp.Regexp
|
||||
|
||||
func (r *Rule) EntropySet() bool {
|
||||
if r.Entropy == 0.0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
// Tags is an array of strings used for metadata
|
||||
// and reporting purposes.
|
||||
Tags []string
|
||||
|
||||
// Allowlist allows a rule to be ignored for specific
|
||||
// regexes, paths, and/or commits
|
||||
Allowlist Allowlist
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"math"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
@@ -23,38 +22,3 @@ func regexMatched(f string, re *regexp.Regexp) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// shannonEntropy calculates the entropy of data using the formula defined here:
|
||||
// https://en.wiktionary.org/wiki/Shannon_entropy
|
||||
// Another way to think about what this is doing is calculating the number of bits
|
||||
// needed to on average encode the data. So, the higher the entropy, the more random the data, the
|
||||
// more bits needed to encode that data.
|
||||
func shannonEntropy(data string) (entropy float64) {
|
||||
if data == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
charCounts := make(map[rune]int)
|
||||
for _, char := range data {
|
||||
charCounts[char]++
|
||||
}
|
||||
|
||||
invLength := 1.0 / float64(len(data))
|
||||
for _, count := range charCounts {
|
||||
freq := float64(count) * invLength
|
||||
entropy -= freq * math.Log2(freq)
|
||||
}
|
||||
|
||||
return entropy
|
||||
}
|
||||
Reference in new issue
Block a user