Skip to content

Commit

Permalink
Feat/allowlist regex target (gitleaks#1107)
Browse files Browse the repository at this point in the history
* adding regexTarget entry in allowlist

* update readme

* add globalregextarget to check

* update readme
  • Loading branch information
zricethezav authored Feb 26, 2023
1 parent 343e693 commit 4b5e8e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ paths = [
'''go\.mod''',
'''go\.sum'''
]
# note: (rule) regexTarget defaults to check the _Secret_ in the finding.
# if regexTarget is not specified then _Secret_ will be used.
# Acceptable values for regexTarget are "match" and "line"
regexTarget = "match"
regexes = [
'''process''',
'''getenv''',
Expand All @@ -357,6 +361,12 @@ paths = [
'''gitleaks\.toml''',
'''(.*?)(jpg|gif|doc)'''
]

# note: (global) regexTarget defaults to check the _Secret_ in the finding.
# if regexTarget is not specified then _Secret_ will be used.
# Acceptable values for regexTarget are "match" and "line"
regexTarget = "match"

regexes = [
'''219-09-9999''',
'''078-05-1120''',
Expand Down
3 changes: 3 additions & 0 deletions config/allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type Allowlist struct {
// Regexes is slice of content regular expressions that are allowed to be ignored.
Regexes []*regexp.Regexp

// RegexTarget
RegexTarget string

// Paths is a slice of path regular expressions that are allowed to be ignored.
Paths []*regexp.Regexp

Expand Down
36 changes: 20 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ type ViperConfig struct {
Tags []string

Allowlist struct {
Regexes []string
Paths []string
Commits []string
StopWords []string
RegexTarget string
Regexes []string
Paths []string
Commits []string
StopWords []string
}
}
Allowlist struct {
Regexes []string
Paths []string
Commits []string
StopWords []string
RegexTarget string
Regexes []string
Paths []string
Commits []string
StopWords []string
}
}

Expand Down Expand Up @@ -122,10 +124,11 @@ func (vc *ViperConfig) Translate() (Config, error) {
Tags: r.Tags,
Keywords: r.Keywords,
Allowlist: Allowlist{
Regexes: allowlistRegexes,
Paths: allowlistPaths,
Commits: r.Allowlist.Commits,
StopWords: r.Allowlist.StopWords,
RegexTarget: r.Allowlist.RegexTarget,
Regexes: allowlistRegexes,
Paths: allowlistPaths,
Commits: r.Allowlist.Commits,
StopWords: r.Allowlist.StopWords,
},
}
orderedRules = append(orderedRules, r.RuleID)
Expand All @@ -148,10 +151,11 @@ func (vc *ViperConfig) Translate() (Config, error) {
Extend: vc.Extend,
Rules: rulesMap,
Allowlist: Allowlist{
Regexes: allowlistRegexes,
Paths: allowlistPaths,
Commits: vc.Allowlist.Commits,
StopWords: vc.Allowlist.StopWords,
RegexTarget: vc.Allowlist.RegexTarget,
Regexes: allowlistRegexes,
Paths: allowlistPaths,
Commits: vc.Allowlist.Commits,
StopWords: vc.Allowlist.StopWords,
},
Keywords: keywords,
orderedRules: orderedRules,
Expand Down
21 changes: 18 additions & 3 deletions detect/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,24 @@ func (d *Detector) detectRule(fragment Fragment, rule config.Rule) []report.Find
continue
}

// check if the secret is in the allowlist
if rule.Allowlist.RegexAllowed(finding.Secret) ||
d.Config.Allowlist.RegexAllowed(finding.Secret) {
// check if the regexTarget is defined in the allowlist "regexes" entry
allowlistTarget := finding.Secret
switch rule.Allowlist.RegexTarget {
case "match":
allowlistTarget = finding.Match
case "line":
allowlistTarget = finding.Line
}

globalAllowlistTarget := finding.Secret
switch d.Config.Allowlist.RegexTarget {
case "match":
globalAllowlistTarget = finding.Match
case "line":
globalAllowlistTarget = finding.Line
}
if rule.Allowlist.RegexAllowed(allowlistTarget) ||
d.Config.Allowlist.RegexAllowed(globalAllowlistTarget) {
continue
}

Expand Down

0 comments on commit 4b5e8e1

Please sign in to comment.