rule.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/zricethezav/gitleaks/v8/regexp"
  7. )
  8. // Rules contain information that define details on how to detect secrets
  9. type Rule struct {
  10. // RuleID is a unique identifier for this rule
  11. RuleID string
  12. // Description is the description of the rule.
  13. Description string
  14. // Entropy is a float representing the minimum shannon
  15. // entropy a regex group must have to be considered a secret.
  16. Entropy float64
  17. // SecretGroup is an int used to extract secret from regex
  18. // match and used as the group that will have its entropy
  19. // checked if `entropy` is set.
  20. SecretGroup int
  21. // Regex is a golang regular expression used to detect secrets.
  22. Regex *regexp.Regexp
  23. // Path is a golang regular expression used to
  24. // filter secrets by path
  25. Path *regexp.Regexp
  26. // Tags is an array of strings used for metadata
  27. // and reporting purposes.
  28. Tags []string
  29. // Keywords are used for pre-regex check filtering. Rules that contain
  30. // keywords will perform a quick string compare check to make sure the
  31. // keyword(s) are in the content being scanned.
  32. Keywords []string
  33. // Allowlists allows a rule to be ignored for specific commits, paths, regexes, and/or stopwords.
  34. Allowlists []*Allowlist
  35. // validated is an internal flag to track whether `Validate()` has been called.
  36. validated bool
  37. // If a rule has RequiredRules, it makes the rule dependent on the RequiredRules.
  38. // In otherwords, this rule is now a composite rule.
  39. RequiredRules []*Required
  40. SkipReport bool
  41. SmartFilter bool
  42. }
  43. type Required struct {
  44. RuleID string
  45. WithinLines *int
  46. WithinColumns *int
  47. }
  48. // Validate guards against common misconfigurations.
  49. func (r *Rule) Validate() error {
  50. if r.validated {
  51. return nil
  52. }
  53. // Ensure |id| is present.
  54. if strings.TrimSpace(r.RuleID) == "" {
  55. // Try to provide helpful context, since |id| is empty.
  56. var context string
  57. if r.Regex != nil {
  58. context = ", regex: " + r.Regex.String()
  59. } else if r.Path != nil {
  60. context = ", path: " + r.Path.String()
  61. } else if r.Description != "" {
  62. context = ", description: " + r.Description
  63. }
  64. return errors.New("rule |id| is missing or empty" + context)
  65. }
  66. // Ensure the rule actually matches something.
  67. if r.Regex == nil && r.Path == nil {
  68. return errors.New(r.RuleID + ": both |regex| and |path| are empty, this rule will have no effect")
  69. }
  70. // Ensure |secretGroup| works.
  71. if r.Regex != nil && r.SecretGroup > r.Regex.NumSubexp() {
  72. return fmt.Errorf("%s: invalid regex secret group %d, max regex secret group %d", r.RuleID, r.SecretGroup, r.Regex.NumSubexp())
  73. }
  74. for _, allowlist := range r.Allowlists {
  75. // This will probably never happen.
  76. if allowlist == nil {
  77. continue
  78. }
  79. if err := allowlist.Validate(); err != nil {
  80. return fmt.Errorf("%s: %w", r.RuleID, err)
  81. }
  82. }
  83. r.validated = true
  84. return nil
  85. }