allowlist.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // RegexTarget
  14. RegexTarget string
  15. // Paths is a slice of path regular expressions that are allowed to be ignored.
  16. Paths []*regexp.Regexp
  17. // Commits is a slice of commit SHAs that are allowed to be ignored.
  18. Commits []string
  19. // StopWords is a slice of stop words that are allowed to be ignored.
  20. // This targets the _secret_, not the content of the regex match like the
  21. // Regexes slice.
  22. StopWords []string
  23. }
  24. // CommitAllowed returns true if the commit is allowed to be ignored.
  25. func (a *Allowlist) CommitAllowed(c string) bool {
  26. if c == "" {
  27. return false
  28. }
  29. for _, commit := range a.Commits {
  30. if commit == c {
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. // PathAllowed returns true if the path is allowed to be ignored.
  37. func (a *Allowlist) PathAllowed(path string) bool {
  38. return anyRegexMatch(path, a.Paths)
  39. }
  40. // RegexAllowed returns true if the regex is allowed to be ignored.
  41. func (a *Allowlist) RegexAllowed(s string) bool {
  42. return anyRegexMatch(s, a.Regexes)
  43. }
  44. func (a *Allowlist) ContainsStopWord(s string) bool {
  45. s = strings.ToLower(s)
  46. for _, stopWord := range a.StopWords {
  47. if strings.Contains(s, strings.ToLower(stopWord)) {
  48. return true
  49. }
  50. }
  51. return false
  52. }