From d22371873bd8bff8c66bd36a1417d2396c7f01aa Mon Sep 17 00:00:00 2001 From: Richard Gomez <32133502+rgmz@users.noreply.github.com> Date: Thu, 16 Oct 2025 11:54:59 -0400 Subject: [PATCH] fix(config): validate rules when [extend] is used (#1592) --- config/config.go | 5 +++-- config/config_test.go | 21 +++++++++++++++---- config/rule.go | 18 +++++++++------- .../config/invalid/extend_invalid_ruleid.toml | 7 +++++++ .../extend_rule_override_secret_group.toml | 2 +- 5 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 testdata/config/invalid/extend_invalid_ruleid.toml diff --git a/config/config.go b/config/config.go index 7a93cdc6..91cfbe9c 100644 --- a/config/config.go +++ b/config/config.go @@ -219,7 +219,8 @@ func (vc *ViperConfig) Translate() (Config, error) { } } - if maxExtendDepth != extendDepth { + currentExtendDepth := extendDepth + if maxExtendDepth != currentExtendDepth { // disallow both usedefault and path from being set if c.Extend.Path != "" && c.Extend.UseDefault { return Config{}, errors.New("unable to load config due to extend.path and extend.useDefault being set") @@ -236,7 +237,7 @@ func (vc *ViperConfig) Translate() (Config, error) { } // Validate the rules after everything has been assembled (including extended configs). - if extendDepth == 0 { + if currentExtendDepth == 0 { for _, rule := range c.Rules { if err := rule.Validate(); err != nil { return Config{}, err diff --git a/config/config_test.go b/config/config_test.go index 792a32ec..8c7c9209 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -93,7 +93,7 @@ func TestTranslate(t *testing.T) { { cfgName: "invalid/rule_missing_id", cfg: Config{}, - wantError: errors.New("rule |id| is missing or empty, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"), + wantError: errors.New("rule |id| is missing or empty, description: Discord API key, regex: (?i)(discord[a-z0-9_ .\\-,]{0,25})(=|>|:=|\\|\\|:|<=|=>|:).{0,5}['\\\"]([a-h0-9]{64})['\\\"]"), }, { cfgName: "invalid/rule_no_regex_or_path", @@ -449,7 +449,7 @@ func TestTranslateExtend(t *testing.T) { Rules: map[string]Rule{"aws-access-key": { RuleID: "aws-access-key", Description: "AWS Access Key", - Regex: regexp.MustCompile("(?:a)(?:a)"), + Regex: regexp.MustCompile("(a)(a)"), SecretGroup: 2, Keywords: []string{}, Tags: []string{"key", "AWS"}, @@ -563,6 +563,10 @@ func TestTranslateExtend(t *testing.T) { }, // Invalid + { + cfgName: "invalid/extend_invalid_ruleid", + wantError: errors.New("rule |id| is missing or empty"), + }, } for _, tt := range tests { @@ -589,8 +593,17 @@ func testTranslate(t *testing.T, test translateCase) { err = viper.Unmarshal(&vc) require.NoError(t, err) cfg, err := vc.Translate() - if err != nil && !assert.EqualError(t, err, test.wantError.Error()) { - return + if err != nil { + if test.wantError != nil { + assert.EqualError(t, err, test.wantError.Error()) + } else { + require.NoError(t, err) + } + } else { + if test.wantError != nil { + t.Fatalf("expected error but got none: %v", test.wantError) + return + } } if len(test.rules) > 0 { diff --git a/config/rule.go b/config/rule.go index 54ed444c..3ed22440 100644 --- a/config/rule.go +++ b/config/rule.go @@ -69,15 +69,17 @@ func (r *Rule) Validate() error { // Ensure |id| is present. if strings.TrimSpace(r.RuleID) == "" { // Try to provide helpful context, since |id| is empty. - var context string - if r.Regex != nil { - context = ", regex: " + r.Regex.String() - } else if r.Path != nil { - context = ", path: " + r.Path.String() - } else if r.Description != "" { - context = ", description: " + r.Description + var sb strings.Builder + if r.Description != "" { + sb.WriteString(", description: " + r.Description) } - return errors.New("rule |id| is missing or empty" + context) + if r.Regex != nil { + sb.WriteString(", regex: " + r.Regex.String()) + } + if r.Path != nil { + sb.WriteString(", path: " + r.Path.String()) + } + return errors.New("rule |id| is missing or empty" + sb.String()) } // Ensure the rule actually matches something. diff --git a/testdata/config/invalid/extend_invalid_ruleid.toml b/testdata/config/invalid/extend_invalid_ruleid.toml new file mode 100644 index 00000000..86a89bbb --- /dev/null +++ b/testdata/config/invalid/extend_invalid_ruleid.toml @@ -0,0 +1,7 @@ +[extend] +useDefault = true + +[[rules]] + +[[rules.allowlists]] +paths = ['^.*\.(xml|log|json)$'] diff --git a/testdata/config/valid/extend_rule_override_secret_group.toml b/testdata/config/valid/extend_rule_override_secret_group.toml index a557240e..f2ddeafa 100644 --- a/testdata/config/valid/extend_rule_override_secret_group.toml +++ b/testdata/config/valid/extend_rule_override_secret_group.toml @@ -5,5 +5,5 @@ path = "../testdata/config/simple.toml" [[rules]] id = "aws-access-key" -regex = '''(?:a)(?:a)''' +regex = '''(a)(a)''' secretGroup = 2