mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 12:14:22 +01:00
* 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 ~
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package scan
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zricethezav/gitleaks/v7/options"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
)
|
|
|
|
// 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"`
|
|
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
|
|
// the offender and line field of the leak which.
|
|
func RedactLeak(leak Leak) Leak {
|
|
leak.Line = strings.Replace(leak.Line, leak.Offender, "REDACTED", -1)
|
|
leak.Offender = "REDACTED"
|
|
return 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,
|
|
OffenderEntropy: -1, // -1 means not checked
|
|
}
|
|
}
|
|
|
|
// WithCommit adds commit data to the leak
|
|
func (leak Leak) WithCommit(commit *object.Commit) Leak {
|
|
leak.Commit = commit.Hash.String()
|
|
leak.Author = commit.Author.Name
|
|
leak.Email = commit.Author.Email
|
|
leak.Message = commit.Message
|
|
leak.Date = commit.Author.When
|
|
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 {
|
|
return
|
|
}
|
|
if opts.Redact {
|
|
leak = RedactLeak(leak)
|
|
}
|
|
if opts.Quiet {
|
|
var b []byte
|
|
b, _ = json.Marshal(leak)
|
|
fmt.Println(string(b))
|
|
} else {
|
|
var b []byte
|
|
b, _ = json.MarshalIndent(leak, "", " ")
|
|
fmt.Println(string(b))
|
|
}
|
|
}
|
|
|
|
// URL generates a url to the leak if leak.RepoURL is set
|
|
func (leak Leak) URL() string {
|
|
if leak.RepoURL != "" {
|
|
return fmt.Sprintf("%s/blob/%s/%s#L%d", leak.RepoURL, leak.Commit, leak.File, leak.LineNumber)
|
|
}
|
|
return ""
|
|
}
|