checks.go 2.1 KB

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