From be9d0f8930061f7acd89c2453edce9dd46e4bc95 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 10 Oct 2024 14:50:29 -0700 Subject: [PATCH] Fix rule extension (#1556) * Reimplement the ability to override built-in rules PR #1524 broke the ability to specify a `[[rules]]` with the same ID as a built-in rule in order to extend the default configuration. This PR restores this ability, but merges the provided configuration with the default rule instead of overwriting the default rule wholesale. * Add tests * Append keywords and tags --- config/config.go | 17 +++ config/config_test.go | 118 ++++++++++++++++++++- testdata/config/override_description.toml | 8 ++ testdata/config/override_entropy.toml | 8 ++ testdata/config/override_keywords.toml | 8 ++ testdata/config/override_path.toml | 8 ++ testdata/config/override_regex.toml | 8 ++ testdata/config/override_secret_group.toml | 9 ++ testdata/config/override_tags.toml | 8 ++ 9 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 testdata/config/override_description.toml create mode 100644 testdata/config/override_entropy.toml create mode 100644 testdata/config/override_keywords.toml create mode 100644 testdata/config/override_path.toml create mode 100644 testdata/config/override_regex.toml create mode 100644 testdata/config/override_secret_group.toml create mode 100644 testdata/config/override_tags.toml diff --git a/config/config.go b/config/config.go index 6b4e286b..50934a9a 100644 --- a/config/config.go +++ b/config/config.go @@ -252,6 +252,23 @@ func (c *Config) extend(extensionConfig Config) { c.OrderedRules = append(c.OrderedRules, ruleID) } else { // Rule exists, merge our changes into the base. + if currentRule.Description != "" { + baseRule.Description = currentRule.Description + } + if currentRule.Entropy != 0 { + baseRule.Entropy = currentRule.Entropy + } + if currentRule.SecretGroup != 0 { + baseRule.SecretGroup = currentRule.SecretGroup + } + if currentRule.Regex != nil { + baseRule.Regex = currentRule.Regex + } + if currentRule.Path != nil { + baseRule.Path = currentRule.Path + } + baseRule.Tags = append(baseRule.Tags, currentRule.Tags...) + baseRule.Keywords = append(baseRule.Keywords, currentRule.Keywords...) baseRule.Allowlist.Commits = append(baseRule.Allowlist.Commits, currentRule.Allowlist.Commits...) baseRule.Allowlist.Paths = append(baseRule.Allowlist.Paths, currentRule.Allowlist.Paths...) baseRule.Allowlist.Regexes = append(baseRule.Allowlist.Regexes, currentRule.Allowlist.Regexes...) diff --git a/config/config_test.go b/config/config_test.go index cbec9791..53d4b122 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -15,8 +15,13 @@ const configPath = "../testdata/config/" func TestTranslate(t *testing.T) { tests := []struct { - cfgName string - cfg Config + // Configuration file basename to load, from `../testdata/config/`. + cfgName string + // Expected result. + cfg Config + // Rules to compare. + rules []string + // Error to expect. wantError error }{ { @@ -174,6 +179,107 @@ func TestTranslate(t *testing.T) { }, }, }, + { + cfgName: "override_description", + rules: []string{"aws-access-key"}, + cfg: Config{ + Rules: map[string]Rule{"aws-access-key": { + RuleID: "aws-access-key", + Description: "Puppy Doggy", + Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"), + Keywords: []string{}, + Tags: []string{"key", "AWS"}, + }, + }, + }, + }, + { + cfgName: "override_entropy", + rules: []string{"aws-access-key"}, + cfg: Config{ + Rules: map[string]Rule{"aws-access-key": { + RuleID: "aws-access-key", + Description: "AWS Access Key", + Entropy: 999.0, + Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"), + Keywords: []string{}, + Tags: []string{"key", "AWS"}, + }, + }, + }, + }, + { + cfgName: "override_secret_group", + rules: []string{"aws-access-key"}, + cfg: Config{ + Rules: map[string]Rule{"aws-access-key": { + RuleID: "aws-access-key", + Description: "AWS Access Key", + Regex: regexp.MustCompile("(?:a)(?:a)"), + SecretGroup: 2, + Keywords: []string{}, + Tags: []string{"key", "AWS"}, + }, + }, + }, + }, + { + cfgName: "override_regex", + rules: []string{"aws-access-key"}, + cfg: Config{ + Rules: map[string]Rule{"aws-access-key": { + RuleID: "aws-access-key", + Description: "AWS Access Key", + Regex: regexp.MustCompile("(?:a)"), + Keywords: []string{}, + Tags: []string{"key", "AWS"}, + }, + }, + }, + }, + { + cfgName: "override_path", + rules: []string{"aws-access-key"}, + cfg: Config{ + Rules: map[string]Rule{"aws-access-key": { + RuleID: "aws-access-key", + Description: "AWS Access Key", + Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"), + Path: regexp.MustCompile("(?:puppy)"), + Keywords: []string{}, + Tags: []string{"key", "AWS"}, + }, + }, + }, + }, + { + cfgName: "override_tags", + rules: []string{"aws-access-key"}, + cfg: Config{ + Rules: map[string]Rule{"aws-access-key": { + RuleID: "aws-access-key", + Description: "AWS Access Key", + Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"), + Keywords: []string{}, + Tags: []string{"key", "AWS", "puppy"}, + }, + }, + }, + }, + { + cfgName: "override_keywords", + rules: []string{"aws-access-key"}, + cfg: Config{ + Rules: map[string]Rule{"aws-access-key": { + RuleID: "aws-access-key", + Description: "AWS Access Key", + Regex: regexp.MustCompile("(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z0-9]{16}"), + Keywords: []string{"puppy"}, + Tags: []string{"key", "AWS"}, + }, + }, + }, + }, } for _, tt := range tests { @@ -197,6 +303,14 @@ func TestTranslate(t *testing.T) { return } + if len(tt.rules) > 0 { + rules := make(map[string]Rule) + for _, name := range tt.rules { + rules[name] = cfg.Rules[name] + } + cfg.Rules = rules + } + var regexComparer = func(x, y *regexp.Regexp) bool { // Compare the string representation of the regex patterns. if x == nil || y == nil { diff --git a/testdata/config/override_description.toml b/testdata/config/override_description.toml new file mode 100644 index 00000000..59af87fb --- /dev/null +++ b/testdata/config/override_description.toml @@ -0,0 +1,8 @@ +title = "override a built-in rule's description" + +[extend] +path = "../testdata/config/simple.toml" + +[[rules]] +id = "aws-access-key" +description = "Puppy Doggy" diff --git a/testdata/config/override_entropy.toml b/testdata/config/override_entropy.toml new file mode 100644 index 00000000..47032226 --- /dev/null +++ b/testdata/config/override_entropy.toml @@ -0,0 +1,8 @@ +title = "override a built-in rule's entropy" + +[extend] +path = "../testdata/config/simple.toml" + +[[rules]] +id = "aws-access-key" +entropy = 999 diff --git a/testdata/config/override_keywords.toml b/testdata/config/override_keywords.toml new file mode 100644 index 00000000..51082a14 --- /dev/null +++ b/testdata/config/override_keywords.toml @@ -0,0 +1,8 @@ +title = "override a built-in rule's keywords" + +[extend] +path = "../testdata/config/simple.toml" + +[[rules]] +id = "aws-access-key" +keywords = ["puppy"] diff --git a/testdata/config/override_path.toml b/testdata/config/override_path.toml new file mode 100644 index 00000000..dfc4535f --- /dev/null +++ b/testdata/config/override_path.toml @@ -0,0 +1,8 @@ +title = "override a built-in rule's path" + +[extend] +path = "../testdata/config/simple.toml" + +[[rules]] +id = "aws-access-key" +path = '''(?:puppy)''' diff --git a/testdata/config/override_regex.toml b/testdata/config/override_regex.toml new file mode 100644 index 00000000..8cf01119 --- /dev/null +++ b/testdata/config/override_regex.toml @@ -0,0 +1,8 @@ +title = "override a built-in rule's regex" + +[extend] +path = "../testdata/config/simple.toml" + +[[rules]] +id = "aws-access-key" +regex = '''(?:a)''' diff --git a/testdata/config/override_secret_group.toml b/testdata/config/override_secret_group.toml new file mode 100644 index 00000000..a557240e --- /dev/null +++ b/testdata/config/override_secret_group.toml @@ -0,0 +1,9 @@ +title = "override a built-in rule's secretGroup" + +[extend] +path = "../testdata/config/simple.toml" + +[[rules]] +id = "aws-access-key" +regex = '''(?:a)(?:a)''' +secretGroup = 2 diff --git a/testdata/config/override_tags.toml b/testdata/config/override_tags.toml new file mode 100644 index 00000000..73dde821 --- /dev/null +++ b/testdata/config/override_tags.toml @@ -0,0 +1,8 @@ +title = "override a built-in rule's tags" + +[extend] +path = "../testdata/config/simple.toml" + +[[rules]] +id = "aws-access-key" +tags = ["puppy"]