mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
refactor(config): keyword map (#1538)
This commit is contained in:
1 parent
a971a324fa
commit
c5b15c9d74
5 files changed
+57
-42
No files matched your search
+13
-9
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user