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