feat(config): update rule validation (#1466)

This commit is contained in:
Richard Gomez authored and GitHub committed 2024-08-22 09:55:44 -05:00
1 parent 6dfcf5e949
commit 1aae66d564
4 files changed
+64 -26

No files matched your search

+34 -3
View File
@@ -1,17 +1,19 @@
package config
import (
"fmt"
"regexp"
"strings"
)
// Rules contain information that define details on how to detect secrets
type Rule struct {
// Description is the description of the rule.
Description string
// RuleID is a unique identifier for this rule
RuleID string
// Description is the description of the rule.
Description string
// Entropy is a float representing the minimum shannon
// entropy a regex group must have to be considered a secret.
Entropy float64
@@ -41,3 +43,32 @@ type Rule struct {
// regexes, paths, and/or commits
Allowlist Allowlist
}
// Validate guards against common misconfigurations.
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
}
return fmt.Errorf("rule |id| is missing or empty" + context)
}
// Ensure the rule actually matches something.
if r.Regex == nil && r.Path == nil {
return fmt.Errorf("%s: both |regex| and |path| are empty, this rule will have no effect", r.RuleID)
}
// Ensure |secretGroup| works.
if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
return fmt.Errorf("%s: invalid regex secret group %d, max regex secret group %d", r.RuleID, r.SecretGroup, r.Regex.NumSubexp())
}
return nil
}