mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 21:24:21 +01:00
Use rule id for config validation error (#1463)
* chore(config): use rule id for error * feat(config): ensure rule.id is not empty
This commit is contained in:
1 parent
d3c4b9078e
commit
c0fda4358e
4 files changed
+19
-3
No files matched your search
+5
-1
@@ -82,6 +82,10 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
|||||||
rulesMap := make(map[string]Rule)
|
rulesMap := make(map[string]Rule)
|
||||||
|
|
||||||
for _, r := range vc.Rules {
|
for _, r := range vc.Rules {
|
||||||
|
if strings.TrimSpace(r.ID) == "" {
|
||||||
|
return Config{}, fmt.Errorf("rule ID is missing or empty, regex: %s", r.Regex)
|
||||||
|
}
|
||||||
|
|
||||||
var allowlistRegexes []*regexp.Regexp
|
var allowlistRegexes []*regexp.Regexp
|
||||||
for _, a := range r.Allowlist.Regexes {
|
for _, a := range r.Allowlist.Regexes {
|
||||||
allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
|
allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a))
|
||||||
@@ -135,7 +139,7 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
|||||||
orderedRules = append(orderedRules, r.RuleID)
|
orderedRules = append(orderedRules, r.RuleID)
|
||||||
|
|
||||||
if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
|
if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
|
||||||
return Config{}, fmt.Errorf("%s invalid regex secret group %d, max regex secret group %d", r.Description, r.SecretGroup, r.Regex.NumSubexp())
|
return Config{}, fmt.Errorf("%s: invalid regex secret group %d, max regex secret group %d", r.RuleID, r.SecretGroup, r.Regex.NumSubexp())
|
||||||
}
|
}
|
||||||
rulesMap[r.RuleID] = r
|
rulesMap[r.RuleID] = r
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,10 +86,15 @@ func TestTranslate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
cfgName: "missing_id",
|
||||||
|
cfg: Config{},
|
||||||
|
wantError: fmt.Errorf("rule ID is missing or empty, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
cfgName: "bad_entropy_group",
|
cfgName: "bad_entropy_group",
|
||||||
cfg: Config{},
|
cfg: Config{},
|
||||||
wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
|
wantError: fmt.Errorf("discord-api-key: invalid regex secret group 5, max regex secret group 3"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
cfgName: "base",
|
cfgName: "base",
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ func TestDetect(t *testing.T) {
|
|||||||
FilePath: "tmp.go",
|
FilePath: "tmp.go",
|
||||||
},
|
},
|
||||||
expectedFindings: []report.Finding{},
|
expectedFindings: []report.Finding{},
|
||||||
wantError: fmt.Errorf("Discord API key invalid regex secret group 5, max regex secret group 3"),
|
wantError: fmt.Errorf("discord-api-key: invalid regex secret group 5, max regex secret group 3"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
cfgName: "simple",
|
cfgName: "simple",
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
title = "gitleaks config"
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
description = "Discord API key"
|
||||||
|
regex = '''(?i)(discord[a-z0-9_ .\-,]{0,25})(=|>|:=|\|\|:|<=|=>|:).{0,5}['\"]([a-h0-9]{64})['\"]'''
|
||||||
|
secretGroup = 5
|
||||||
|
entropy = 3.5
|
||||||
Reference in new issue
Block a user