allowlist.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package config
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. // Allowlist allows a rule to be ignored for specific
  7. // regexes, paths, and/or commits
  8. type Allowlist struct {
  9. // Short human readable description of the allowlist.
  10. Description string
  11. // Regexes is slice of content regular expressions that are allowed to be ignored.
  12. Regexes []*regexp.Regexp
  13. // Can be `match` or `line`.
  14. //
  15. // If `match` the _Regexes_ will be tested against the match of the _Rule.Regex_.
  16. //
  17. // If `line` the _Regexes_ will be tested against the entire line.
  18. //
  19. // If RegexTarget is empty, it will be tested against the found secret.
  20. RegexTarget string
  21. // Paths is a slice of path regular expressions that are allowed to be ignored.
  22. Paths []*regexp.Regexp
  23. // Commits is a slice of commit SHAs that are allowed to be ignored.
  24. Commits []string
  25. // StopWords is a slice of stop words that are allowed to be ignored.
  26. // This targets the _secret_, not the content of the regex match like the
  27. // Regexes slice.
  28. StopWords []string
  29. }
  30. // CommitAllowed returns true if the commit is allowed to be ignored.
  31. func (a *Allowlist) CommitAllowed(c string) bool {
  32. if c == "" {
  33. return false
  34. }
  35. for _, commit := range a.Commits {
  36. if commit == c {
  37. return true
  38. }
  39. }
  40. return false
  41. }
  42. // PathAllowed returns true if the path is allowed to be ignored.
  43. func (a *Allowlist) PathAllowed(path string) bool {
  44. return anyRegexMatch(path, a.Paths)
  45. }
  46. // RegexAllowed returns true if the regex is allowed to be ignored.
  47. func (a *Allowlist) RegexAllowed(s string) bool {
  48. return anyRegexMatch(s, a.Regexes)
  49. }
  50. func (a *Allowlist) ContainsStopWord(s string) bool {
  51. s = strings.ToLower(s)
  52. for _, stopWord := range a.StopWords {
  53. if strings.Contains(s, strings.ToLower(stopWord)) {
  54. return true
  55. }
  56. }
  57. return false
  58. }