allowlist.go 1.5 KB

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