leaks.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "os/signal"
  11. "strings"
  12. "sync"
  13. "syscall"
  14. )
  15. // LeakElem contains the line and commit of a leak
  16. type LeakElem struct {
  17. Line string `json:"line"`
  18. Commit string `json:"commit"`
  19. }
  20. func start(opts *Options) {
  21. c := make(chan os.Signal, 2)
  22. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  23. err := exec.Command("git", "clone", opts.RepoURL).Run()
  24. if err != nil {
  25. log.Fatalf("failed to clone repo %v", err)
  26. }
  27. repoName := getLocalRepoName(opts.RepoURL)
  28. if err = os.Chdir(repoName); err != nil {
  29. log.Fatal(err)
  30. }
  31. go func() {
  32. <-c
  33. cleanup(repoName)
  34. os.Exit(1)
  35. }()
  36. report := getLeaks(repoName)
  37. cleanup(repoName)
  38. reportJSON, _ := json.MarshalIndent(report, "", "\t")
  39. err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJSON, 0644)
  40. if err != nil {
  41. log.Fatalf("Can't write to file: %s", err)
  42. }
  43. }
  44. // getLocalRepoName generates the name of the local clone folder based on the given URL
  45. func getLocalRepoName(url string) string {
  46. splitSlashes := strings.Split(url, "/")
  47. name := splitSlashes[len(splitSlashes)-1]
  48. name = strings.TrimSuffix(name, ".git")
  49. splitColons := strings.Split(name, ":")
  50. name = splitColons[len(splitColons)-1]
  51. return name
  52. }
  53. func cleanup(repoName string) {
  54. if err := os.Chdir(appRoot); err != nil {
  55. log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)
  56. }
  57. err := exec.Command("rm", "-rf", repoName).Run()
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. }
  62. func getLeaks(repoName string) []LeakElem {
  63. var (
  64. out []byte
  65. err error
  66. commitWG sync.WaitGroup
  67. gitLeakReceiverWG sync.WaitGroup
  68. concurrent = 100
  69. semaphoreChan = make(chan struct{}, concurrent)
  70. gitLeaks = make(chan LeakElem)
  71. report []LeakElem
  72. )
  73. go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
  74. for gitLeak := range gitLeaks {
  75. fmt.Println(gitLeak)
  76. report = append(report, gitLeak)
  77. gitLeakReceiverWG.Done()
  78. }
  79. }(&commitWG, &gitLeakReceiverWG)
  80. out, err = exec.Command("git", "rev-list", "--all", "--remotes", "--topo-order").Output()
  81. if err != nil {
  82. log.Fatalf("error retrieving commits%v\n", err)
  83. }
  84. commits := bytes.Split(out, []byte("\n"))
  85. commitWG.Add(len(commits))
  86. for _, currCommitB := range commits {
  87. currCommit := string(currCommitB)
  88. go func(currCommit string, repoName string, commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
  89. defer commitWG.Done()
  90. var leakPrs bool
  91. if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
  92. log.Fatal(err)
  93. }
  94. commitCmp := fmt.Sprintf("%s^!", currCommit)
  95. semaphoreChan <- struct{}{}
  96. out, err := exec.Command("git", "diff", commitCmp).Output()
  97. <-semaphoreChan
  98. if err != nil {
  99. return
  100. }
  101. lines := checkRegex(string(out))
  102. if len(lines) == 0 {
  103. return
  104. }
  105. for _, line := range lines {
  106. leakPrs = checkEntropy(line)
  107. if leakPrs {
  108. gitLeakReceiverWG.Add(1)
  109. gitLeaks <- LeakElem{line, currCommit}
  110. }
  111. }
  112. }(currCommit, repoName, &commitWG, &gitLeakReceiverWG)
  113. }
  114. commitWG.Wait()
  115. gitLeakReceiverWG.Wait()
  116. return report
  117. }