leaks.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "path/filepath"
  12. "strings"
  13. "sync"
  14. "syscall"
  15. )
  16. // LeakElem contains the line and commit of a leak
  17. type LeakElem struct {
  18. Line string `json:"line"`
  19. Commit string `json:"commit"`
  20. Offender string `json:"string"`
  21. Reason string `json:"reason"`
  22. Msg string `json:"commitMsg"`
  23. Time string `json:"time"`
  24. Author string `json:"author"`
  25. File string `json:"file"`
  26. RepoURL string `json:"repoURL"`
  27. }
  28. type Commit struct {
  29. Hash string
  30. Author string
  31. Time string
  32. Msg string
  33. }
  34. func rmTmp(owner *Owner) {
  35. if _, err := os.Stat(owner.path); err == nil {
  36. err := os.RemoveAll(owner.path)
  37. log.Printf("\nCleaning up tmp repos in %s\n", owner.path)
  38. if err != nil {
  39. log.Printf("failed to properly remove tmp gitleaks dir: %v", err)
  40. }
  41. }
  42. os.Exit(1)
  43. }
  44. // start
  45. func start(repos []RepoDesc, owner *Owner, opts *Options) {
  46. var report []LeakElem
  47. if opts.Tmp {
  48. defer rmTmp(owner)
  49. }
  50. // interrupt handling
  51. c := make(chan os.Signal, 2)
  52. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  53. go func() {
  54. <-c
  55. if opts.Tmp {
  56. rmTmp(owner)
  57. }
  58. os.Exit(1)
  59. }()
  60. // run checks on repos
  61. for _, repo := range repos {
  62. dotGitPath := filepath.Join(repo.path, ".git")
  63. if _, err := os.Stat(dotGitPath); err == nil {
  64. if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil {
  65. log.Fatal(err)
  66. }
  67. // use pre-cloned repo
  68. fmt.Printf("Checking \x1b[37;1m%s\x1b[0m...\n", repo.url)
  69. err := exec.Command("git", "fetch").Run()
  70. if err != nil {
  71. log.Printf("failed to fetch repo %v", err)
  72. return
  73. }
  74. report = getLeaks(repo, owner, opts)
  75. } else {
  76. // no repo present, clone it
  77. if err := os.Chdir(fmt.Sprintf(owner.path)); err != nil {
  78. log.Fatal(err)
  79. }
  80. fmt.Printf("Cloning \x1b[37;1m%s\x1b[0m...\n", repo.url)
  81. err := exec.Command("git", "clone", repo.url).Run()
  82. if err != nil {
  83. fmt.Printf("failed to clone repo %v", err)
  84. return
  85. }
  86. report = getLeaks(repo, owner, opts)
  87. }
  88. if len(report) == 0 {
  89. fmt.Printf("No Leaks detected for \x1b[35;2m%s\x1b[0m...\n\n", repo.url)
  90. }
  91. if opts.EnableJSON {
  92. outputGitLeaksReport(report, repo, opts)
  93. }
  94. }
  95. }
  96. // outputGitLeaksReport
  97. func outputGitLeaksReport(report []LeakElem, repo RepoDesc, opts *Options) {
  98. reportJSON, _ := json.MarshalIndent(report, "", "\t")
  99. if _, err := os.Stat(repo.owner.reportPath); os.IsNotExist(err) {
  100. os.Mkdir(repo.owner.reportPath, os.ModePerm)
  101. }
  102. reportFileName := fmt.Sprintf("%s_leaks.json", repo.name)
  103. reportFile := filepath.Join(repo.owner.reportPath, reportFileName)
  104. err := ioutil.WriteFile(reportFile, reportJSON, 0644)
  105. if err != nil {
  106. log.Fatalf("Can't write to file: %s", err)
  107. }
  108. fmt.Printf("Report written to %s\n", reportFile)
  109. }
  110. // getLeaks will attempt to find gitleaks
  111. func getLeaks(repo RepoDesc, owner *Owner, opts *Options) []LeakElem {
  112. var (
  113. out []byte
  114. err error
  115. commitWG sync.WaitGroup
  116. gitLeakReceiverWG sync.WaitGroup
  117. gitLeaks = make(chan LeakElem)
  118. report []LeakElem
  119. )
  120. semaphoreChan := make(chan struct{}, opts.Concurrency)
  121. go func(commitWG *sync.WaitGroup, gitLeakReceiverWG *sync.WaitGroup) {
  122. for gitLeak := range gitLeaks {
  123. b, err := json.MarshalIndent(gitLeak, "", " ")
  124. if err != nil {
  125. fmt.Println("failed to output leak:", err)
  126. }
  127. fmt.Println(string(b))
  128. report = append(report, gitLeak)
  129. gitLeakReceiverWG.Done()
  130. }
  131. }(&commitWG, &gitLeakReceiverWG)
  132. if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil {
  133. log.Fatal(err)
  134. }
  135. gitFormat := "--format=%H%n%an%n%s%n%ci"
  136. out, err = exec.Command("git", "rev-list", "--all",
  137. "--remotes", "--topo-order", gitFormat).Output()
  138. if err != nil {
  139. log.Fatalf("error retrieving commits%v\n", err)
  140. }
  141. revListLines := bytes.Split(out, []byte("\n"))
  142. commits := parseFormattedRevList(revListLines)
  143. for _, commit := range commits {
  144. if commit.Hash == "" {
  145. continue
  146. }
  147. commitWG.Add(1)
  148. go func(currCommit Commit, repoName string, commitWG *sync.WaitGroup,
  149. gitLeakReceiverWG *sync.WaitGroup, opts *Options) {
  150. defer commitWG.Done()
  151. if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil {
  152. log.Fatal(err)
  153. }
  154. commitCmp := fmt.Sprintf("%s^!", currCommit.Hash)
  155. semaphoreChan <- struct{}{}
  156. out, err := exec.Command("git", "diff", commitCmp).Output()
  157. <-semaphoreChan
  158. if err != nil {
  159. if strings.Contains(err.Error(), "too many files open") {
  160. log.Printf("error retrieving diff for commit %s. Try turning concurrency down. %v\n", currCommit, err)
  161. }
  162. if opts.Tmp {
  163. rmTmp(owner)
  164. }
  165. }
  166. leaks := doChecks(string(out), currCommit, opts, repo)
  167. if len(leaks) == 0 {
  168. return
  169. }
  170. for _, leak := range leaks {
  171. gitLeakReceiverWG.Add(1)
  172. gitLeaks <- leak
  173. }
  174. }(commit, repo.name, &commitWG, &gitLeakReceiverWG, opts)
  175. if commit.Hash == opts.SinceCommit {
  176. break
  177. }
  178. }
  179. commitWG.Wait()
  180. gitLeakReceiverWG.Wait()
  181. return report
  182. }
  183. func parseFormattedRevList(revList [][]byte) []Commit {
  184. var commits []Commit
  185. for i := 0; i < len(revList)-1; i = i + 5 {
  186. commit := Commit{
  187. Hash: string(revList[i+1]),
  188. Author: string(revList[i+2]),
  189. Msg: string(revList[i+3]),
  190. Time: string(revList[i+4]),
  191. }
  192. commits = append(commits, commit)
  193. }
  194. return commits
  195. }