checks.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/nbutton23/zxcvbn-go"
  5. "os/exec"
  6. "strings"
  7. )
  8. // checkDiff operates on a single diff between to chronological commits
  9. func checkDiff(commit1 string, commit2 string) []string {
  10. var leakPrs bool
  11. var leaks []string
  12. _, seen := cache[commit1+commit2]
  13. if seen {
  14. fmt.Println("WE HAVE SEEN THIS")
  15. return []string{}
  16. }
  17. out, err := exec.Command("git", "diff", commit1, commit2).Output()
  18. if err != nil {
  19. return []string{}
  20. }
  21. cache[commit1+commit2] = true
  22. lines := checkRegex(string(out))
  23. if len(lines) == 0 {
  24. return []string{}
  25. }
  26. for _, line := range lines {
  27. leakPrs = checkEntropy(line)
  28. if leakPrs {
  29. leaks = append(leaks, line)
  30. }
  31. }
  32. return leaks
  33. }
  34. // check each line of a diff and see if there are any potential secrets
  35. // [1] https://people.eecs.berkeley.edu/~rohanpadhye/files/key_leaks-msr15.pdf
  36. func checkRegex(diff string) []string {
  37. var match string
  38. var results []string
  39. lines := strings.Split(diff, "\n")
  40. for _, line := range lines {
  41. // doubtful a leak would be on a line > 120 characters
  42. if len(line) == 0 || len(line) > 80 {
  43. continue
  44. }
  45. for _, re := range regexes {
  46. match = re.FindString(line)
  47. if len(match) == 0 {
  48. continue
  49. }
  50. results = append(results, line)
  51. }
  52. }
  53. return results
  54. }
  55. // checkEntropy determines whether target contains enough
  56. // entropy for a hash
  57. // TODO remove stop words:
  58. // setting(s), config(s), property(s), etc
  59. func checkEntropy(target string) bool {
  60. index := assignRegex.FindStringIndex(target)
  61. if len(index) == 0 {
  62. return false
  63. }
  64. // TODO check for stop words here
  65. target = strings.Trim(target[index[1]:len(target)], " ")
  66. if len(target) > 70 {
  67. return false
  68. }
  69. // entropy := shannonEntropy(target)
  70. entropy := zxcvbn.PasswordStrength(target, nil).Entropy
  71. // tune this/make option
  72. if entropy > 70 {
  73. return true
  74. }
  75. return false
  76. }