leaks.go 4.0 KB

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