mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
more comments
This commit is contained in:
1 parent
1a51d4ed36
commit
4091fda0fd
3 files changed
+59
-19
No files matched your search
+15
-3
@@ -2,13 +2,23 @@ package config
|
|||||||
|
|
||||||
import "regexp"
|
import "regexp"
|
||||||
|
|
||||||
|
// Allowlist allows a rule to be ignored for specific
|
||||||
|
// regexes, paths, and/or commits
|
||||||
type Allowlist struct {
|
type Allowlist struct {
|
||||||
|
// Short human readable description of the allowlist.
|
||||||
Description string
|
Description string
|
||||||
Regexes []*regexp.Regexp
|
|
||||||
Paths []*regexp.Regexp
|
// Regexes is slice of content regular expressions that are allowed to be ignored.
|
||||||
Commits []string
|
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 {
|
func (a *Allowlist) CommitAllowed(c string) bool {
|
||||||
if c == "" {
|
if c == "" {
|
||||||
return false
|
return false
|
||||||
@@ -21,10 +31,12 @@ func (a *Allowlist) CommitAllowed(c string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PathAllowed returns true if the path is allowed to be ignored.
|
||||||
func (a *Allowlist) PathAllowed(path string) bool {
|
func (a *Allowlist) PathAllowed(path string) bool {
|
||||||
return anyRegexMatch(path, a.Paths)
|
return anyRegexMatch(path, a.Paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegexAllowed returns true if the regex is allowed to be ignored.
|
||||||
func (a *Allowlist) RegexAllowed(s string) bool {
|
func (a *Allowlist) RegexAllowed(s string) bool {
|
||||||
return anyRegexMatch(s, a.Regexes)
|
return anyRegexMatch(s, a.Regexes)
|
||||||
}
|
}
|
||||||
+28
-6
@@ -4,13 +4,35 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Rules contain information that define details on how to detect secrets
|
||||||
type Rule struct {
|
type Rule struct {
|
||||||
|
// Description is the description of the rule.
|
||||||
Description string
|
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
|
SecretGroup int
|
||||||
Regex *regexp.Regexp
|
|
||||||
Path *regexp.Regexp
|
// Regex is a golang regular expression used to detect secrets.
|
||||||
Tags []string
|
Regex *regexp.Regexp
|
||||||
Allowlist Allowlist
|
|
||||||
|
// Path is a golang regular expression used to
|
||||||
|
// filter secrets by path
|
||||||
|
Path *regexp.Regexp
|
||||||
|
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
+16
-10
@@ -19,10 +19,14 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ScanType int
|
// Type used to differentiate between git scan types:
|
||||||
|
// $ gitleaks detect
|
||||||
|
// $ gitleaks protect
|
||||||
|
// $ gitleaks protect staged
|
||||||
|
type GitScanType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DetectType ScanType = iota
|
DetectType GitScanType = iota
|
||||||
ProtectType
|
ProtectType
|
||||||
ProtectStagedType
|
ProtectStagedType
|
||||||
)
|
)
|
||||||
@@ -213,24 +217,24 @@ func (d *Detector) detectRule(fragment Fragment, rule *config.Rule) []report.Fin
|
|||||||
// GitScan accepts a *gitdiff.File channel which contents a git history generated from
|
// GitScan accepts a *gitdiff.File channel which contents a git history generated from
|
||||||
// the output of `git log -p ...`. startGitScan will look at each file (patch) in the history
|
// the output of `git log -p ...`. startGitScan will look at each file (patch) in the history
|
||||||
// and determine if the patch contains any findings.
|
// and determine if the patch contains any findings.
|
||||||
func (d *Detector) DetectGit(source string, logOpts string, scanType ScanType) ([]report.Finding, error) {
|
func (d *Detector) DetectGit(source string, logOpts string, gitScanType GitScanType) ([]report.Finding, error) {
|
||||||
var (
|
var (
|
||||||
history <-chan *gitdiff.File
|
gitdiffFiles <-chan *gitdiff.File
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
switch scanType {
|
switch gitScanType {
|
||||||
case DetectType:
|
case DetectType:
|
||||||
history, err = git.GitLog(source, logOpts)
|
gitdiffFiles, err = git.GitLog(source, logOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return d.findings, err
|
return d.findings, err
|
||||||
}
|
}
|
||||||
case ProtectType:
|
case ProtectType:
|
||||||
history, err = git.GitDiff(source, false)
|
gitdiffFiles, err = git.GitDiff(source, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return d.findings, err
|
return d.findings, err
|
||||||
}
|
}
|
||||||
case ProtectStagedType:
|
case ProtectStagedType:
|
||||||
history, err = git.GitDiff(source, true)
|
gitdiffFiles, err = git.GitDiff(source, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return d.findings, err
|
return d.findings, err
|
||||||
}
|
}
|
||||||
@@ -238,7 +242,7 @@ func (d *Detector) DetectGit(source string, logOpts string, scanType ScanType) (
|
|||||||
|
|
||||||
s := semgroup.NewGroup(context.Background(), 4)
|
s := semgroup.NewGroup(context.Background(), 4)
|
||||||
|
|
||||||
for gitdiffFile := range history {
|
for gitdiffFile := range gitdiffFiles {
|
||||||
gitdiffFile := gitdiffFile
|
gitdiffFile := gitdiffFile
|
||||||
|
|
||||||
// skip binary files
|
// skip binary files
|
||||||
@@ -283,6 +287,8 @@ func (d *Detector) DetectGit(source string, logOpts string, scanType ScanType) (
|
|||||||
return d.findings, nil
|
return d.findings, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DetectFiles accepts a path to a source directory or file and begins a scan of the
|
||||||
|
// file or directory.
|
||||||
func (d *Detector) DetectFiles(source string) ([]report.Finding, error) {
|
func (d *Detector) DetectFiles(source string) ([]report.Finding, error) {
|
||||||
s := semgroup.NewGroup(context.Background(), 4)
|
s := semgroup.NewGroup(context.Background(), 4)
|
||||||
paths := make(chan string)
|
paths := make(chan string)
|
||||||
|
|||||||
Reference in new issue
Block a user