leaks.go 3.9 KB

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