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:
bplaxco authored and GitHub committed 2021-05-03 20:22:20 -04:00
1 parent e7553b0168
commit 8250e40a75
52 files changed
+240 -50

No files matched your search

+2 -2
View File
@@ -121,7 +121,7 @@ func (cs *CommitScanner) Scan() (Report, error) {
continue
}
offender := rule.Inspect(line)
if offender == "" {
if offender.IsEmpty() {
continue
}
@@ -136,7 +136,7 @@ func (cs *CommitScanner) Scan() (Report, error) {
continue
}
leak := NewLeak(line, offender, defaultLineNumber).WithCommit(cs.commit)
leak := NewLeak(line, offender.ToString(), defaultLineNumber).WithCommit(cs.commit).WithEntropy(offender.EntropyLevel)
leak.File = to.Path()
leak.LineNumber = extractLine(patchContent, leak, lineLookup)
leak.RepoURL = cs.opts.RepoURL
+2 -3
View File
@@ -97,8 +97,7 @@ func (fs *FilesAtCommitScanner) Scan() (Report, error) {
}
offender := rule.Inspect(line)
if offender == "" {
if offender.IsEmpty() {
continue
}
@@ -113,7 +112,7 @@ func (fs *FilesAtCommitScanner) Scan() (Report, error) {
continue
}
leak := NewLeak(line, offender, defaultLineNumber).WithCommit(fs.commit)
leak := NewLeak(line, offender.ToString(), defaultLineNumber).WithCommit(fs.commit).WithEntropy(offender.EntropyLevel)
leak.File = f.Name
leak.LineNumber = i + 1
leak.RepoURL = fs.opts.RepoURL
+26 -17
View File
@@ -3,6 +3,7 @@ package scan
import (
"encoding/json"
"fmt"
"math"
"strings"
"time"
@@ -14,20 +15,21 @@ import (
// Leak is a struct that contains information about some line of code that contains
// sensitive information as determined by the rules set in a gitleaks config
type Leak struct {
Line string `json:"line"`
LineNumber int `json:"lineNumber"`
Offender string `json:"offender"`
Commit string `json:"commit"`
Repo string `json:"repo"`
RepoURL string `json:"repoURL"`
LeakURL string `json:"leakURL"`
Rule string `json:"rule"`
Message string `json:"commitMessage"`
Author string `json:"author"`
Email string `json:"email"`
File string `json:"file"`
Date time.Time `json:"date"`
Tags string `json:"tags"`
Line string `json:"line"`
LineNumber int `json:"lineNumber"`
Offender string `json:"offender"`
OffenderEntropy float64 `json:"offenderEntropy"`
Commit string `json:"commit"`
Repo string `json:"repo"`
RepoURL string `json:"repoURL"`
LeakURL string `json:"leakURL"`
Rule string `json:"rule"`
Message string `json:"commitMessage"`
Author string `json:"author"`
Email string `json:"email"`
File string `json:"file"`
Date time.Time `json:"date"`
Tags string `json:"tags"`
}
// RedactLeak will replace the offending string with "REDACTED" in both
@@ -41,9 +43,10 @@ func RedactLeak(leak Leak) Leak {
// NewLeak creates a new leak from common data all leaks must have, line, offender, linenumber
func NewLeak(line string, offender string, lineNumber int) Leak {
return Leak{
Line: line,
Offender: offender,
LineNumber: lineNumber,
Line: line,
Offender: offender,
LineNumber: lineNumber,
OffenderEntropy: -1, // -1 means not checked
}
}
@@ -57,6 +60,12 @@ func (leak Leak) WithCommit(commit *object.Commit) Leak {
return leak
}
// WithEntropy adds OffenderEntropy data to the leak
func (leak Leak) WithEntropy(entropyLevel float64) Leak {
leak.OffenderEntropy = math.Round(entropyLevel*1000) / 1000
return leak
}
// Log logs a leak and redacts if necessary
func (leak Leak) Log(opts options.Options) {
if !opts.Quiet && !opts.Verbose {
+2 -2
View File
@@ -105,7 +105,7 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
}
offender := rule.Inspect(line)
if offender == "" {
if offender.IsEmpty() {
continue
}
if ngs.cfg.Allowlist.RegexAllowed(line) {
@@ -119,7 +119,7 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
continue
}
leak := NewLeak(line, offender, defaultLineNumber)
leak := NewLeak(line, offender.ToString(), defaultLineNumber).WithEntropy(offender.EntropyLevel)
relPath, err := filepath.Rel(ngs.opts.Path, p)
if err != nil {
leak.File = p
+5 -1
View File
@@ -760,7 +760,7 @@ func fileCheck(wantPath, gotPath string) error {
}
}
if !found {
return fmt.Errorf("unable to find %+v in got leaks", wantLeak)
return fmt.Errorf("unable to find %+v in %s", wantLeak, gotPath)
}
}
@@ -779,6 +779,10 @@ func same(l1, l2 Leak) bool {
return false
}
if l1.OffenderEntropy != l2.OffenderEntropy {
return false
}
if l1.Line != l2.Line {
return false
}
+4 -4
View File
@@ -87,7 +87,7 @@ func (us *UnstagedScanner) Scan() (Report, error) {
lineNumber++
for _, rule := range us.cfg.Rules {
offender := rule.Inspect(line)
if offender == "" {
if offender.IsEmpty() {
continue
}
if us.cfg.Allowlist.RegexAllowed(line) ||
@@ -101,7 +101,7 @@ func (us *UnstagedScanner) Scan() (Report, error) {
if rule.Path.String() != "" && !rule.HasFilePathLeak(filepath.Base(workTreeFile.Name())) {
continue
}
leak := NewLeak(line, offender, defaultLineNumber).WithCommit(emptyCommit())
leak := NewLeak(line, offender.ToString(), defaultLineNumber).WithCommit(emptyCommit()).WithEntropy(offender.EntropyLevel)
leak.File = workTreeFile.Name()
leak.LineNumber = lineNumber
leak.Repo = us.repoName
@@ -206,7 +206,7 @@ func (us *UnstagedScanner) Scan() (Report, error) {
for _, line := range strings.Split(diffContents, "\n") {
for _, rule := range us.cfg.Rules {
offender := rule.Inspect(line)
if offender == "" {
if offender.IsEmpty() {
continue
}
if us.cfg.Allowlist.RegexAllowed(line) ||
@@ -220,7 +220,7 @@ func (us *UnstagedScanner) Scan() (Report, error) {
if rule.Path.String() != "" && !rule.HasFilePathLeak(filepath.Base(filename)) {
continue
}
leak := NewLeak(line, offender, defaultLineNumber).WithCommit(emptyCommit())
leak := NewLeak(line, offender.ToString(), defaultLineNumber).WithCommit(emptyCommit()).WithEntropy(offender.EntropyLevel)
leak.File = filename
leak.LineNumber = extractLine(prettyDiff, leak, lineLookup) + 1
leak.Repo = us.repoName