rule.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package config
  2. import (
  3. "fmt"
  4. "strings"
  5. regexp "github.com/wasilibs/go-re2"
  6. )
  7. // Rules contain information that define details on how to detect secrets
  8. type Rule struct {
  9. // RuleID is a unique identifier for this rule
  10. RuleID string
  11. // Description is the description of the rule.
  12. Description string
  13. // Entropy is a float representing the minimum shannon
  14. // entropy a regex group must have to be considered a secret.
  15. Entropy float64
  16. // SecretGroup is an int used to extract secret from regex
  17. // match and used as the group that will have its entropy
  18. // checked if `entropy` is set.
  19. SecretGroup int
  20. // Regex is a golang regular expression used to detect secrets.
  21. Regex *regexp.Regexp
  22. // Path is a golang regular expression used to
  23. // filter secrets by path
  24. Path *regexp.Regexp
  25. // Tags is an array of strings used for metadata
  26. // and reporting purposes.
  27. Tags []string
  28. // Keywords are used for pre-regex check filtering. Rules that contain
  29. // keywords will perform a quick string compare check to make sure the
  30. // keyword(s) are in the content being scanned.
  31. Keywords []string
  32. // Allowlists allows a rule to be ignored for specific commits, paths, regexes, and/or stopwords.
  33. Allowlists []Allowlist
  34. }
  35. // Validate guards against common misconfigurations.
  36. func (r Rule) Validate() error {
  37. // Ensure |id| is present.
  38. if strings.TrimSpace(r.RuleID) == "" {
  39. // Try to provide helpful context, since |id| is empty.
  40. var context string
  41. if r.Regex != nil {
  42. context = ", regex: " + r.Regex.String()
  43. } else if r.Path != nil {
  44. context = ", path: " + r.Path.String()
  45. } else if r.Description != "" {
  46. context = ", description: " + r.Description
  47. }
  48. return fmt.Errorf("rule |id| is missing or empty" + context)
  49. }
  50. // Ensure the rule actually matches something.
  51. if r.Regex == nil && r.Path == nil {
  52. return fmt.Errorf("%s: both |regex| and |path| are empty, this rule will have no effect", r.RuleID)
  53. }
  54. // Ensure |secretGroup| works.
  55. if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
  56. return fmt.Errorf("%s: invalid regex secret group %d, max regex secret group %d", r.RuleID, r.SecretGroup, r.Regex.NumSubexp())
  57. }
  58. return nil
  59. }