diff --git a/config/allowlist.go b/config/allowlist.go index d81c7784..14639a2d 100644 --- a/config/allowlist.go +++ b/config/allowlist.go @@ -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,10 +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 { return anyRegexMatch(path, a.Paths) } +// RegexAllowed returns true if the regex is allowed to be ignored. func (a *Allowlist) RegexAllowed(s string) bool { return anyRegexMatch(s, a.Regexes) } diff --git a/config/rule.go b/config/rule.go index c6f93f5e..f45f091a 100644 --- a/config/rule.go +++ b/config/rule.go @@ -4,13 +4,35 @@ import ( "regexp" ) +// 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 + + // Regex is a golang regular expression used to detect secrets. + Regex *regexp.Regexp + + // 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 } diff --git a/detect/detect.go b/detect/detect.go index a9eb99f7..896be7eb 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -19,10 +19,14 @@ import ( "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 ( - DetectType ScanType = iota + DetectType GitScanType = iota ProtectType 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 // the output of `git log -p ...`. startGitScan will look at each file (patch) in the history // 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 ( - history <-chan *gitdiff.File - err error + gitdiffFiles <-chan *gitdiff.File + err error ) - switch scanType { + switch gitScanType { case DetectType: - history, err = git.GitLog(source, logOpts) + gitdiffFiles, err = git.GitLog(source, logOpts) if err != nil { return d.findings, err } case ProtectType: - history, err = git.GitDiff(source, false) + gitdiffFiles, err = git.GitDiff(source, false) if err != nil { return d.findings, err } case ProtectStagedType: - history, err = git.GitDiff(source, true) + gitdiffFiles, err = git.GitDiff(source, true) if err != nil { return d.findings, err } @@ -238,7 +242,7 @@ func (d *Detector) DetectGit(source string, logOpts string, scanType ScanType) ( s := semgroup.NewGroup(context.Background(), 4) - for gitdiffFile := range history { + for gitdiffFile := range gitdiffFiles { gitdiffFile := gitdiffFile // skip binary files @@ -283,6 +287,8 @@ func (d *Detector) DetectGit(source string, logOpts string, scanType ScanType) ( 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) { s := semgroup.NewGroup(context.Background(), 4) paths := make(chan string)