leaks.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // start clones and determines if there are any leaks
  21. func start(opts *Options) {
  22. fmt.Printf("\nEvaluating \x1b[37;1m%s\x1b[0m...\n", opts.RepoURL)
  23. c := make(chan os.Signal, 2)
  24. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  25. err := exec.Command("git", "clone", opts.RepoURL).Run()
  26. if err != nil {
  27. log.Printf("failed to clone repo %v", err)
  28. return
  29. }
  30. repoName := getLocalRepoName(opts.RepoURL)
  31. if err = os.Chdir(repoName); err != nil {
  32. log.Fatal(err)
  33. }
  34. go func() {
  35. <-c
  36. cleanup(repoName)
  37. os.Exit(1)
  38. }()
  39. report := getLeaks(repoName, opts.Concurrency)
  40. if len(report) == 0 {
  41. fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n\n", opts.RepoURL)
  42. }
  43. cleanup(repoName)
  44. reportJSON, _ := json.MarshalIndent(report, "", "\t")
  45. err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJSON, 0644)
  46. if err != nil {
  47. log.Fatalf("Can't write to file: %s", err)
  48. }
  49. }
  50. // getLocalRepoName generates the name of the local clone folder based on the given URL
  51. func getLocalRepoName(url string) string {
  52. splitSlashes := strings.Split(url, "/")
  53. name := splitSlashes[len(splitSlashes)-1]
  54. name = strings.TrimSuffix(name, ".git")
  55. splitColons := strings.Split(name, ":")
  56. name = splitColons[len(splitColons)-1]
  57. return name
  58. }
  59. // cleanup deletes the repo
  60. func cleanup(repoName string) {
  61. if err := os.Chdir(appRoot); err != nil {
  62. log.Fatalf("failed cleaning up repo. Does the repo exist? %v", err)
  63. }
  64. err := exec.Command("rm", "-rf", repoName).Run()
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. }
  69. // getLeaks will attempt to find gitleaks
  70. func getLeaks(repoName string, concurrency int) []LeakElem {
  71. var (
  72. out []byte
  73. err error
  74. commitWG sync.WaitGroup
  75. gitLeakReceiverWG sync.WaitGroup
  76. gitLeaks = make(chan LeakElem)
  77. report []LeakElem
  78. )
  79. if concurrency == 0 {
  80. concurrency = 100
  81. }
  82. semaphoreChan := make(chan struct{}, concurrency)
  83. go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
  84. for gitLeak := range gitLeaks {
  85. fmt.Println(gitLeak)
  86. report = append(report, gitLeak)
  87. gitLeakReceiverWG.Done()
  88. }
  89. }(&commitWG, &gitLeakReceiverWG)
  90. out, err = exec.Command("git", "rev-list", "--all", "--remotes", "--topo-order").Output()
  91. if err != nil {
  92. log.Fatalf("error retrieving commits%v\n", err)
  93. }
  94. commits := bytes.Split(out, []byte("\n"))
  95. commitWG.Add(len(commits))
  96. for _, currCommitB := range commits {
  97. currCommit := string(currCommitB)
  98. go func(currCommit string, repoName string, commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
  99. defer commitWG.Done()
  100. var leakPrs bool
  101. if err := os.Chdir(fmt.Sprintf("%s/%s", appRoot, repoName)); err != nil {
  102. log.Fatal(err)
  103. }
  104. commitCmp := fmt.Sprintf("%s^!", currCommit)
  105. semaphoreChan <- struct{}{}
  106. out, err := exec.Command("git", "diff", commitCmp).Output()
  107. <-semaphoreChan
  108. if err != nil {
  109. return
  110. }
  111. lines := checkRegex(string(out))
  112. if len(lines) == 0 {
  113. return
  114. }
  115. for _, line := range lines {
  116. leakPrs = checkEntropy(line)
  117. if leakPrs {
  118. gitLeakReceiverWG.Add(1)
  119. gitLeaks <- LeakElem{line, currCommit}
  120. }
  121. }
  122. }(currCommit, repoName, &commitWG, &gitLeakReceiverWG)
  123. }
  124. commitWG.Wait()
  125. gitLeakReceiverWG.Wait()
  126. return report
  127. }