allowlist.go 1.5 KB

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