allowlist.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package config
  2. import (
  3. "regexp"
  4. )
  5. // AllowList is struct containing items that if encountered will allowlist
  6. // a commit/line of code that would be considered a leak.
  7. type AllowList struct {
  8. Description string
  9. Regexes []*regexp.Regexp
  10. Commits []string
  11. Files []*regexp.Regexp
  12. Paths []*regexp.Regexp
  13. Repos []*regexp.Regexp
  14. }
  15. // CommitAllowed checks if a commit is allowlisted
  16. func (a *AllowList) CommitAllowed(commit string) bool {
  17. for _, hash := range a.Commits {
  18. if commit == hash {
  19. return true
  20. }
  21. }
  22. return false
  23. }
  24. // FileAllowed checks if a file is allowlisted
  25. func (a *AllowList) FileAllowed(fileName string) bool {
  26. return anyRegexMatch(fileName, a.Files)
  27. }
  28. // PathAllowed checks if a path is allowlisted
  29. func (a *AllowList) PathAllowed(filePath string) bool {
  30. return anyRegexMatch(filePath, a.Paths)
  31. }
  32. // RegexAllowed checks if a regex is allowlisted
  33. func (a *AllowList) RegexAllowed(content string) bool {
  34. return anyRegexMatch(content, a.Regexes)
  35. }
  36. // RepoAllowed checks if a regex is allowlisted
  37. func (a *AllowList) RepoAllowed(repo string) bool {
  38. return anyRegexMatch(repo, a.Repos)
  39. }
  40. // IgnoreDotGit appends a `.git$` rule to ignore all .git paths. This is used for --no-git scans
  41. func (a *AllowList) IgnoreDotGit() error {
  42. re, err := regexp.Compile(".git$")
  43. if err != nil {
  44. return err
  45. }
  46. a.Paths = append(a.Paths, re)
  47. return nil
  48. }