refactor(config): keyword map (#1538)

This commit is contained in:
Richard Gomez authored and GitHub committed 2024-09-30 07:32:25 -05:00
1 parent a971a324fa
commit c5b15c9d74
5 files changed
+57 -42

No files matched your search

+13 -9
View File
@@ -59,7 +59,7 @@ type Config struct {
Description string
Rules map[string]Rule
Allowlist Allowlist
Keywords []string
Keywords map[string]struct{}
// used to keep sarif results consistent
OrderedRules []string
@@ -75,10 +75,10 @@ type Extend struct {
func (vc *ViperConfig) Translate() (Config, error) {
var (
keywords []string
keywords = make(map[string]struct{})
orderedRules []string
rulesMap = make(map[string]Rule)
)
rulesMap := make(map[string]Rule)
for _, r := range vc.Rules {
var allowlistRegexes []*regexp.Regexp
@@ -94,7 +94,7 @@ func (vc *ViperConfig) Translate() (Config, error) {
r.Keywords = []string{}
} else {
for _, k := range r.Keywords {
keywords = append(keywords, strings.ToLower(k))
keywords[strings.ToLower(k)] = struct{}{}
}
}
@@ -245,7 +245,9 @@ func (c *Config) extend(extensionConfig Config) {
if !ok {
// Rule doesn't exist, add it to the config.
c.Rules[ruleID] = baseRule
c.Keywords = append(c.Keywords, baseRule.Keywords...)
for _, k := range baseRule.Keywords {
c.Keywords[k] = struct{}{}
}
c.OrderedRules = append(c.OrderedRules, ruleID)
} else {
// Rule exists, merge our changes into the base.
@@ -255,10 +257,12 @@ func (c *Config) extend(extensionConfig Config) {
baseRule.Allowlist.RegexTarget = currentRule.Allowlist.RegexTarget
baseRule.Allowlist.StopWords = append(baseRule.Allowlist.StopWords, currentRule.Allowlist.StopWords...)
// The keywords from the base rule and the extended rule must be merged into the global keywords list
c.Keywords = append(c.Keywords, baseRule.Keywords...)
c.Keywords = append(c.Keywords, currentRule.Keywords...)
delete(c.Rules, ruleID)
for _, k := range baseRule.Keywords {
c.Keywords[k] = struct{}{}
}
for _, k := range currentRule.Keywords {
c.Keywords[k] = struct{}{}
}
c.Rules[ruleID] = baseRule
}
}