From c0fda4358e64742a8a7aac4fee4f3162d344a25c Mon Sep 17 00:00:00 2001 From: Richard Gomez <32133502+rgmz@users.noreply.github.com> Date: Thu, 8 Aug 2024 16:47:34 -0400 Subject: [PATCH] Use rule id for config validation error (#1463) * chore(config): use rule id for error * feat(config): ensure rule.id is not empty --- config/config.go | 6 +++++- config/config_test.go | 7 ++++++- detect/detect_test.go | 2 +- testdata/config/missing_id.toml | 7 +++++++ 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100755 testdata/config/missing_id.toml diff --git a/config/config.go b/config/config.go index e3706e01..c08b1174 100644 --- a/config/config.go +++ b/config/config.go @@ -82,6 +82,10 @@ func (vc *ViperConfig) Translate() (Config, error) { rulesMap := make(map[string]Rule) 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 for _, a := range r.Allowlist.Regexes { allowlistRegexes = append(allowlistRegexes, regexp.MustCompile(a)) @@ -135,7 +139,7 @@ func (vc *ViperConfig) Translate() (Config, error) { orderedRules = append(orderedRules, r.RuleID) 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 } diff --git a/config/config_test.go b/config/config_test.go index 60d42e3c..dff4bf75 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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", 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", diff --git a/detect/detect_test.go b/detect/detect_test.go index 201829f9..c7879313 100644 --- a/detect/detect_test.go +++ b/detect/detect_test.go @@ -295,7 +295,7 @@ func TestDetect(t *testing.T) { FilePath: "tmp.go", }, 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", diff --git a/testdata/config/missing_id.toml b/testdata/config/missing_id.toml new file mode 100755 index 00000000..a43fd531 --- /dev/null +++ b/testdata/config/missing_id.toml @@ -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