mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 12:44:22 +01:00
Include offenderEntropy in the JSON output (#549)
* Pass the entropy data back to the Leak struct Do this to make it easier to tune entropy checks and make decisions in systems consuming the output. ~ B'ezrat Hashem ~ * Return negative number when entropy not checked That way you can tell the difference between not checking or an actual entropy level of 0 ~ B'ezrat Hashem ~ * Make sure to handle range checks properly Make sure to show when something had an entropy returned but was outside range, or didn't have a hit at all, etc... ~ B'zrat Hashem ~ * Add a few doc strings Follow the project's conventiona add a comment above the methods ~ B'ezrat Hashem ~ * Update tests and get them to pass ~ B'ezrat Hashem ~ * Remove checked in `.got` files ~ B'ezrat Hashem ~ * Add `*.got` to the `.gitignore` Make sure the test output files aren't checked-in ~ B'ezrat Hashem ~
This commit is contained in:
1 parent
e7553b0168
commit
8250e40a75
52 files changed
+240
-50
No files matched your search
+60
-14
@@ -6,6 +6,23 @@ import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Offender is a struct that contains the information matched when searching
|
||||
// content and information on why it matched (i.e. the EntropyLevel)
|
||||
type Offender struct {
|
||||
Match string
|
||||
EntropyLevel float64
|
||||
}
|
||||
|
||||
// IsEmpty checks to see if nothing was found in the match
|
||||
func (o *Offender) IsEmpty() bool {
|
||||
return o.Match == ""
|
||||
}
|
||||
|
||||
// ToString the contents of the match
|
||||
func (o *Offender) ToString() string {
|
||||
return o.Match
|
||||
}
|
||||
|
||||
// Rule is a struct that contains information that is loaded from a gitleaks config.
|
||||
// This struct is used in the Config struct as an array of Rules and is iterated
|
||||
// over during an scan. Each rule will be checked. If a regex match is found AND
|
||||
@@ -23,28 +40,46 @@ type Rule struct {
|
||||
}
|
||||
|
||||
// Inspect checks the content of a line for a leak
|
||||
func (r *Rule) Inspect(line string) string {
|
||||
offender := r.Regex.FindString(line)
|
||||
if offender == "" {
|
||||
return ""
|
||||
func (r *Rule) Inspect(line string) *Offender {
|
||||
match := r.Regex.FindString(line)
|
||||
|
||||
// EntropyLevel -1 means not checked
|
||||
if match == "" {
|
||||
return &Offender{
|
||||
Match: "",
|
||||
EntropyLevel: -1,
|
||||
}
|
||||
}
|
||||
|
||||
// check if offender is allowed
|
||||
// EntropyLevel -1 means not checked
|
||||
if r.RegexAllowed(line) {
|
||||
return ""
|
||||
return &Offender{
|
||||
Match: "",
|
||||
EntropyLevel: -1,
|
||||
}
|
||||
}
|
||||
|
||||
// check entropy
|
||||
groups := r.Regex.FindStringSubmatch(offender)
|
||||
if len(r.Entropies) != 0 && !r.ContainsEntropyLeak(groups) {
|
||||
return ""
|
||||
groups := r.Regex.FindStringSubmatch(match)
|
||||
entropyWithinRange, entropyLevel := r.CheckEntropy(groups)
|
||||
|
||||
if len(r.Entropies) != 0 && !entropyWithinRange {
|
||||
return &Offender{
|
||||
Match: "",
|
||||
EntropyLevel: entropyLevel,
|
||||
}
|
||||
}
|
||||
|
||||
// 0 is a match for the full regex pattern
|
||||
if 0 < r.ReportGroup && r.ReportGroup < len(groups) {
|
||||
offender = groups[r.ReportGroup]
|
||||
match = groups[r.ReportGroup]
|
||||
}
|
||||
|
||||
return &Offender{
|
||||
Match: match,
|
||||
EntropyLevel: entropyLevel,
|
||||
}
|
||||
return offender
|
||||
}
|
||||
|
||||
// RegexAllowed checks if the content is allowlisted
|
||||
@@ -57,17 +92,28 @@ func (r *Rule) CommitAllowed(commit string) bool {
|
||||
return r.AllowList.CommitAllowed(commit)
|
||||
}
|
||||
|
||||
// ContainsEntropyLeak checks if there is an entropy leak
|
||||
func (r *Rule) ContainsEntropyLeak(groups []string) bool {
|
||||
// CheckEntropy checks if there is an entropy leak
|
||||
func (r *Rule) CheckEntropy(groups []string) (bool, float64) {
|
||||
var highestFound float64 = 0
|
||||
|
||||
for _, e := range r.Entropies {
|
||||
if len(groups) > e.Group {
|
||||
entropy := shannonEntropy(groups[e.Group])
|
||||
if entropy >= e.Min && entropy <= e.Max {
|
||||
return true
|
||||
return true, entropy
|
||||
} else if entropy > highestFound {
|
||||
highestFound = entropy
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
if len(r.Entropies) == 0 {
|
||||
// entropies not checked
|
||||
return false, -1
|
||||
}
|
||||
|
||||
// entropies checked but not within the range
|
||||
return false, highestFound
|
||||
}
|
||||
|
||||
// HasFileOrPathLeakOnly first checks if there are no entropy/regex rules, then checks if
|
||||
|
||||
Reference in new issue
Block a user