allowlist.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package config
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. )
  7. type AllowlistMatchCondition int
  8. const (
  9. AllowlistMatchOr AllowlistMatchCondition = iota
  10. AllowlistMatchAnd
  11. )
  12. func (a AllowlistMatchCondition) String() string {
  13. return [...]string{
  14. "OR",
  15. "AND",
  16. }[a]
  17. }
  18. // Allowlist allows a rule to be ignored for specific
  19. // regexes, paths, and/or commits
  20. type Allowlist struct {
  21. // Short human readable description of the allowlist.
  22. Description string
  23. // MatchCondition determines whether all criteria must match.
  24. MatchCondition AllowlistMatchCondition
  25. // Commits is a slice of commit SHAs that are allowed to be ignored. Defaults to "OR".
  26. Commits []string
  27. // Paths is a slice of path regular expressions that are allowed to be ignored.
  28. Paths []*regexp.Regexp
  29. // Regexes is slice of content regular expressions that are allowed to be ignored.
  30. Regexes []*regexp.Regexp
  31. // Can be `match` or `line`.
  32. //
  33. // If `match` the _Regexes_ will be tested against the match of the _Rule.Regex_.
  34. //
  35. // If `line` the _Regexes_ will be tested against the entire line.
  36. //
  37. // If RegexTarget is empty, it will be tested against the found secret.
  38. RegexTarget string
  39. // StopWords is a slice of stop words that are allowed to be ignored.
  40. // This targets the _secret_, not the content of the regex match like the
  41. // Regexes slice.
  42. StopWords []string
  43. }
  44. // CommitAllowed returns true if the commit is allowed to be ignored.
  45. func (a *Allowlist) CommitAllowed(c string) bool {
  46. if c == "" {
  47. return false
  48. }
  49. for _, commit := range a.Commits {
  50. if commit == c {
  51. return true
  52. }
  53. }
  54. return false
  55. }
  56. // PathAllowed returns true if the path is allowed to be ignored.
  57. func (a *Allowlist) PathAllowed(path string) bool {
  58. return anyRegexMatch(path, a.Paths)
  59. }
  60. // RegexAllowed returns true if the regex is allowed to be ignored.
  61. func (a *Allowlist) RegexAllowed(secret string) bool {
  62. return anyRegexMatch(secret, a.Regexes)
  63. }
  64. func (a *Allowlist) ContainsStopWord(s string) bool {
  65. s = strings.ToLower(s)
  66. for _, stopWord := range a.StopWords {
  67. if strings.Contains(s, strings.ToLower(stopWord)) {
  68. return true
  69. }
  70. }
  71. return false
  72. }
  73. func (a *Allowlist) Validate() error {
  74. // Disallow empty allowlists.
  75. if len(a.Commits) == 0 &&
  76. len(a.Paths) == 0 &&
  77. len(a.Regexes) == 0 &&
  78. len(a.StopWords) == 0 {
  79. return fmt.Errorf("[[rules.allowlists]] must contain at least one check for: commits, paths, regexes, or stopwords")
  80. }
  81. return nil
  82. }