mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 05:04:22 +01:00
Composite rules (#1905)
* composite rules * add test config * tests * taking care of some pr comments * naming
This commit is contained in:
1 parent
72977e41b6
commit
b1c9c7ecbe
9 files changed
+473
-21
No files matched your search
+37
-2
@@ -43,15 +43,25 @@ type ViperConfig struct {
|
||||
|
||||
// Deprecated: this is a shim for backwards-compatibility.
|
||||
// TODO: Remove this in 9.x.
|
||||
AllowList *viperRuleAllowlist
|
||||
AllowList *viperRuleAllowlist
|
||||
|
||||
Allowlists []*viperRuleAllowlist
|
||||
Required []*viperRequired
|
||||
SkipReport bool
|
||||
}
|
||||
// Deprecated: this is a shim for backwards-compatibility.
|
||||
// TODO: Remove this in 9.x.
|
||||
AllowList *viperGlobalAllowlist
|
||||
AllowList *viperGlobalAllowlist
|
||||
|
||||
Allowlists []*viperGlobalAllowlist
|
||||
}
|
||||
|
||||
type viperRequired struct {
|
||||
ID string
|
||||
WithinLines *int `mapstructure:"withinLines"`
|
||||
WithinColumns *int `mapstructure:"withinColumns"`
|
||||
}
|
||||
|
||||
type viperRuleAllowlist struct {
|
||||
Description string
|
||||
Condition string
|
||||
@@ -130,6 +140,7 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
||||
Path: pathPat,
|
||||
Keywords: vr.Keywords,
|
||||
Tags: vr.Tags,
|
||||
SkipReport: vr.SkipReport,
|
||||
}
|
||||
|
||||
// Parse the rule allowlists, including the older format for backwards compatibility.
|
||||
@@ -147,10 +158,34 @@ func (vc *ViperConfig) Translate() (Config, error) {
|
||||
}
|
||||
cr.Allowlists = append(cr.Allowlists, allowlist)
|
||||
}
|
||||
|
||||
for _, r := range vr.Required {
|
||||
if r.ID == "" {
|
||||
return Config{}, fmt.Errorf("%s: [[rules.required]] rule ID is empty", cr.RuleID)
|
||||
}
|
||||
requiredRule := Required{
|
||||
RuleID: r.ID,
|
||||
WithinLines: r.WithinLines,
|
||||
WithinColumns: r.WithinColumns,
|
||||
// Distance: r.Distance,
|
||||
}
|
||||
cr.RequiredRules = append(cr.RequiredRules, &requiredRule)
|
||||
}
|
||||
|
||||
orderedRules = append(orderedRules, cr.RuleID)
|
||||
rulesMap[cr.RuleID] = cr
|
||||
}
|
||||
|
||||
// after all the rules have been processed, let's ensure the required rules
|
||||
// actually exist.
|
||||
for _, r := range rulesMap {
|
||||
for _, rr := range r.RequiredRules {
|
||||
if _, ok := rulesMap[rr.RuleID]; !ok {
|
||||
return Config{}, fmt.Errorf("%s: [[rules.required]] rule ID '%s' does not exist", r.RuleID, rr.RuleID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assemble the config.
|
||||
c := Config{
|
||||
Title: vc.Title,
|
||||
|
||||
@@ -46,6 +46,18 @@ type Rule struct {
|
||||
|
||||
// validated is an internal flag to track whether `Validate()` has been called.
|
||||
validated bool
|
||||
|
||||
// If a rule has RequiredRules, it makes the rule dependent on the RequiredRules.
|
||||
// In otherwords, this rule is now a composite rule.
|
||||
RequiredRules []*Required
|
||||
|
||||
SkipReport bool
|
||||
}
|
||||
|
||||
type Required struct {
|
||||
RuleID string
|
||||
WithinLines *int
|
||||
WithinColumns *int
|
||||
}
|
||||
|
||||
// Validate guards against common misconfigurations.
|
||||
|
||||
Reference in new issue
Block a user