main.go 1.8 KB

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