rule.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package config
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. type Rule struct {
  7. Description string
  8. RuleID string
  9. Entropy float64
  10. SecretGroup int
  11. Regex *regexp.Regexp
  12. Path *regexp.Regexp
  13. Tags []string
  14. Allowlist Allowlist
  15. }
  16. func (r *Rule) IncludeEntropy(secret string) (bool, float64) {
  17. // NOTE: this is a goofy hack to get around the fact there golang's regex engine
  18. // does not support positive lookaheads. Ideally we would want to add a
  19. // restriction on generic rules regex that requires the secret match group
  20. // contains both numbers and alphabetical characters. What this bit of code does is
  21. // check if the ruleid is prepended with "generic" and enforces the
  22. // secret contains both digits and alphabetical characters.
  23. if strings.HasPrefix(r.RuleID, "generic") {
  24. if !containsDigit(secret) {
  25. return false, 0.0
  26. }
  27. }
  28. // group = 0 will check the entropy of the whole regex match
  29. e := shannonEntropy(secret)
  30. if e > r.Entropy {
  31. return true, e
  32. }
  33. return false, e
  34. }
  35. func (r *Rule) EntropySet() bool {
  36. if r.Entropy == 0.0 {
  37. return false
  38. }
  39. return true
  40. }