mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 20:24:22 +01:00
feat(rule): validate & sort rule when generating (#1817)
This commit is contained in:
1 parent
107a41827b
commit
cfdeb0d7e0
18 files changed
+145
-115
No files matched your search
+23
-20
@@ -473,26 +473,29 @@ MatchLoop:
|
||||
continue
|
||||
}
|
||||
}
|
||||
// check if the regexTarget is defined in the allowlist "regexes" entry
|
||||
// or if the secret is in the list of stopwords
|
||||
globalAllowlistTarget := finding.Secret
|
||||
switch d.Config.Allowlist.RegexTarget {
|
||||
case "match":
|
||||
globalAllowlistTarget = finding.Match
|
||||
case "line":
|
||||
globalAllowlistTarget = currentLine
|
||||
}
|
||||
if d.Config.Allowlist.RegexAllowed(globalAllowlistTarget) {
|
||||
logger.Trace().
|
||||
Str("finding", globalAllowlistTarget).
|
||||
Msg("skipping finding: global allowlist regex")
|
||||
continue
|
||||
} else if ok, word := d.Config.Allowlist.ContainsStopWord(finding.Secret); ok {
|
||||
logger.Trace().
|
||||
Str("finding", finding.Secret).
|
||||
Str("allowed-stopword", word).
|
||||
Msg("skipping finding: global allowlist stopword")
|
||||
continue
|
||||
|
||||
if d.Config.Allowlist != nil {
|
||||
// check if the regexTarget is defined in the allowlist "regexes" entry
|
||||
// or if the secret is in the list of stopwords
|
||||
globalAllowlistTarget := finding.Secret
|
||||
switch d.Config.Allowlist.RegexTarget {
|
||||
case "match":
|
||||
globalAllowlistTarget = finding.Match
|
||||
case "line":
|
||||
globalAllowlistTarget = currentLine
|
||||
}
|
||||
if d.Config.Allowlist.RegexAllowed(globalAllowlistTarget) {
|
||||
logger.Trace().
|
||||
Str("finding", globalAllowlistTarget).
|
||||
Msg("skipping finding: global allowlist regex")
|
||||
continue
|
||||
} else if ok, word := d.Config.Allowlist.ContainsStopWord(finding.Secret); ok {
|
||||
logger.Trace().
|
||||
Str("finding", finding.Secret).
|
||||
Str("allowed-stopword", word).
|
||||
Msg("skipping finding: global allowlist stopword")
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// check if the result matches any of the rule allowlists.
|
||||
|
||||
+24
-24
@@ -915,7 +915,7 @@ func TestDetectWithSymlinks(t *testing.T) {
|
||||
func TestDetectRuleAllowlist(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
fragment Fragment
|
||||
allowlist config.Allowlist
|
||||
allowlist *config.Allowlist
|
||||
expected []report.Finding
|
||||
}{
|
||||
// Commit / path
|
||||
@@ -923,7 +923,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
fragment: Fragment{
|
||||
CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
|
||||
},
|
||||
},
|
||||
@@ -931,7 +931,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
fragment: Fragment{
|
||||
FilePath: "package-lock.json",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
|
||||
},
|
||||
},
|
||||
@@ -940,7 +940,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
|
||||
FilePath: "package-lock.json",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
|
||||
@@ -951,7 +951,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
|
||||
FilePath: "package.json",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
|
||||
@@ -974,7 +974,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
|
||||
FilePath: "package-lock.json",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
|
||||
@@ -998,7 +998,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
|
||||
FilePath: "package-lock.json",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchOr,
|
||||
Commits: []string{"704178e7dca77ff143778a31cff0fc192d59b030"},
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
|
||||
@@ -1008,19 +1008,19 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
// Regex / stopwords
|
||||
"regex allowed": {
|
||||
fragment: Fragment{},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
|
||||
},
|
||||
},
|
||||
"stopwords allowed": {
|
||||
fragment: Fragment{},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
StopWords: []string{"summer"},
|
||||
},
|
||||
},
|
||||
"regex AND stopword allowed": {
|
||||
fragment: Fragment{},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
|
||||
StopWords: []string{"2024"},
|
||||
@@ -1031,7 +1031,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
CommitSHA: "41edf1f7f612199f401ccfc3144c2ebd0d7aeb48",
|
||||
FilePath: "config.js",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`config.js`)},
|
||||
@@ -1043,7 +1043,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
fragment: Fragment{
|
||||
FilePath: "config.js",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`config.js`)},
|
||||
@@ -1065,7 +1065,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
},
|
||||
"regex AND stopword NOT allowed": {
|
||||
fragment: Fragment{},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Regexes: []*regexp.Regexp{
|
||||
regexp.MustCompile(`(?i)winter.+`),
|
||||
@@ -1089,7 +1089,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
CommitSHA: "a060c9d2d5e90c992763f1bd4c3cd2a6f121241b",
|
||||
FilePath: "config.js",
|
||||
},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Commits: []string{"41edf1f7f612199f401ccfc3144c2ebd0d7aeb48"},
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`package-lock.json`)},
|
||||
@@ -1111,7 +1111,7 @@ func TestDetectRuleAllowlist(t *testing.T) {
|
||||
},
|
||||
"regex OR stopword allowed": {
|
||||
fragment: Fragment{},
|
||||
allowlist: config.Allowlist{
|
||||
allowlist: &config.Allowlist{
|
||||
MatchCondition: config.AllowlistMatchOr,
|
||||
Regexes: []*regexp.Regexp{regexp.MustCompile(`(?i)summer.+`)},
|
||||
StopWords: []string{"winter"},
|
||||
@@ -1126,7 +1126,7 @@ let password = 'Summer2024!';`
|
||||
rule := config.Rule{
|
||||
RuleID: "test-rule",
|
||||
Regex: regexp.MustCompile(`Summer2024!`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
tc.allowlist,
|
||||
},
|
||||
}
|
||||
@@ -1320,7 +1320,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "unix-rule",
|
||||
Regex: regexp.MustCompile(`s3cr3t`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||
},
|
||||
@@ -1336,7 +1336,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "windows-rule",
|
||||
Regex: regexp.MustCompile(`s3cr3t`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||
},
|
||||
@@ -1364,7 +1364,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "unix-rule",
|
||||
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||
@@ -1382,7 +1382,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "windows-rule",
|
||||
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||
@@ -1414,7 +1414,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "unix-rule",
|
||||
Regex: regexp.MustCompile(`s3cr3t`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||
},
|
||||
@@ -1431,7 +1431,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "windows-rule",
|
||||
Regex: regexp.MustCompile(`s3cr3t`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||
},
|
||||
@@ -1448,7 +1448,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "unix-rule",
|
||||
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||
@@ -1467,7 +1467,7 @@ func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||
rule: config.Rule{
|
||||
RuleID: "windows-rule",
|
||||
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||
Allowlists: []config.Allowlist{
|
||||
Allowlists: []*config.Allowlist{
|
||||
{
|
||||
MatchCondition: config.AllowlistMatchAnd,
|
||||
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||
|
||||
Reference in new issue
Block a user