main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "regexp"
  6. )
  7. // ExitClean : no leaks have been found
  8. const ExitClean = 0
  9. // ExitFailure : gitleaks has encountered an error or SIGINT
  10. const ExitFailure = 1
  11. // ExitLeaks : leaks are present in scanned repos
  12. const ExitLeaks = 2
  13. // package globals
  14. var (
  15. regexes map[string]*regexp.Regexp
  16. stopWords []string
  17. base64Chars string
  18. hexChars string
  19. assignRegex *regexp.Regexp
  20. fileDiffRegex *regexp.Regexp
  21. opts *Options
  22. pwd string
  23. )
  24. func init() {
  25. base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  26. hexChars = "1234567890abcdefABCDEF"
  27. stopWords = []string{"setting", "info", "env", "environment"}
  28. fileDiffRegex = regexp.MustCompile("diff --git a.+b/")
  29. assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
  30. // TODO Externalize regex... this is tricky making it yml compliant
  31. regexes = map[string]*regexp.Regexp{
  32. "PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"),
  33. "RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"),
  34. "DSA": regexp.MustCompile("-----BEGIN DSA PRIVATE KEY-----"),
  35. "SSH": regexp.MustCompile("-----BEGIN OPENSSH PRIVATE KEY-----"),
  36. "Facebook": regexp.MustCompile("(?i)facebook.*['\"][0-9a-f]{32}['\"]"),
  37. "Twitter": regexp.MustCompile("(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]"),
  38. "Github": regexp.MustCompile("(?i)github.*['\"][0-9a-zA-Z]{35,40}['\"]"),
  39. "AWS": regexp.MustCompile("AKIA[0-9A-Z]{16}"),
  40. "Reddit": regexp.MustCompile("(?i)reddit.*['\"][0-9a-zA-Z]{14}['\"]"),
  41. "Heroku": regexp.MustCompile("(?i)heroku.*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"),
  42. }
  43. }
  44. func main() {
  45. args := os.Args[1:]
  46. opts = newOpts(args)
  47. owner := newOwner()
  48. os.Exit(owner.auditRepos())
  49. }
  50. func failF(format string, args ...interface{}) {
  51. fmt.Fprintf(os.Stderr, format, args...)
  52. os.Exit(ExitFailure)
  53. }