-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
utils.go
227 lines (210 loc) Β· 5.9 KB
/
utils.go
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
package scan
import (
"bufio"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/zricethezav/gitleaks/v7/options"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
log "github.com/sirupsen/logrus"
)
const (
diffAddPrefix = "+"
diffDelPrefix = "-"
diffLineSignature = " @@"
defaultLineNumber = 1
)
func obtainCommit(repo *git.Repository, commitSha string) (*object.Commit, error) {
if commitSha == "latest" {
ref, err := repo.Head()
if err != nil {
return nil, err
}
commitSha = ref.Hash().String()
}
return repo.CommitObject(plumbing.NewHash(commitSha))
}
func getRepoName(opts options.Options) string {
if opts.RepoURL != "" {
return filepath.Base(opts.RepoURL)
}
if opts.Path != "" {
return filepath.Base(opts.Path)
}
if opts.CheckUncommitted() {
dir, _ := os.Getwd()
return filepath.Base(dir)
}
return ""
}
func getRepo(opts options.Options) (*git.Repository, error) {
if opts.OpenLocal() {
if opts.Path != "" {
log.Infof("opening %s\n", opts.Path)
} else {
log.Info("opening .")
}
return git.PlainOpen(opts.Path)
}
if opts.CheckUncommitted() {
// open git repo from PWD
dir, err := os.Getwd()
if err != nil {
return nil, err
}
log.Debugf("opening %s as a repo\n", dir)
return git.PlainOpen(dir)
}
return cloneRepo(opts)
}
func cloneRepo(opts options.Options) (*git.Repository, error) {
cloneOpts, err := opts.CloneOptions()
if err != nil {
return nil, err
}
if opts.ClonePath != "" {
log.Infof("cloning... %s to %s", cloneOpts.URL, opts.ClonePath)
return git.PlainClone(opts.ClonePath, false, cloneOpts)
}
log.Infof("cloning... %s", cloneOpts.URL)
return git.Clone(memory.NewStorage(), nil, cloneOpts)
}
// depthReached checks if i meets the depth (--depth=) if set
func depthReached(i int, opts options.Options) bool {
if opts.Depth != 0 && opts.Depth == i {
log.Warnf("Exceeded depth limit (%d)", i)
return true
}
return false
}
// emptyCommit generates an empty commit used for scanning uncommitted changes
func emptyCommit() *object.Commit {
return &object.Commit{
Hash: plumbing.Hash{},
Message: "",
Author: object.Signature{
Name: "",
Email: "",
When: time.Unix(0, 0).UTC(),
},
}
}
// howManyThreads will return a number 1-GOMAXPROCS which is the number
// of goroutines that will spawn during gitleaks execution
func howManyThreads(threads int) int {
maxThreads := runtime.GOMAXPROCS(0)
if threads == 0 {
return 1
} else if threads > maxThreads {
log.Warnf("%d threads set too high, setting to system max, %d", threads, maxThreads)
return maxThreads
}
return threads
}
// getLogOptions determines what log options are used when iterating through commits.
// It is similar to `git log {branch}`. Default behavior is to log ALL branches so
// gitleaks gets the full git history.
func logOptions(repo *git.Repository, opts options.Options) (*git.LogOptions, error) {
var logOpts git.LogOptions
const dateformat string = "2006-01-02"
const timeformat string = "2006-01-02T15:04:05-0700"
if opts.CommitFrom != "" {
logOpts.From = plumbing.NewHash(opts.CommitFrom)
}
if opts.CommitSince != "" {
if t, err := time.Parse(timeformat, opts.CommitSince); err == nil {
logOpts.Since = &t
} else if t, err := time.Parse(dateformat, opts.CommitSince); err == nil {
logOpts.Since = &t
} else {
return nil, err
}
logOpts.All = true
}
if opts.CommitUntil != "" {
if t, err := time.Parse(timeformat, opts.CommitUntil); err == nil {
logOpts.Until = &t
} else if t, err := time.Parse(dateformat, opts.CommitUntil); err == nil {
logOpts.Until = &t
} else {
return nil, err
}
logOpts.All = true
}
if opts.Branch != "" {
ref, err := repo.Storer.Reference(plumbing.NewBranchReferenceName(opts.Branch))
if err != nil {
return nil, fmt.Errorf("could not find branch %s", opts.Branch)
}
logOpts = git.LogOptions{
From: ref.Hash(),
}
if logOpts.From.IsZero() {
return nil, fmt.Errorf("could not find branch %s", opts.Branch)
}
return &logOpts, nil
}
if !logOpts.From.IsZero() || logOpts.Since != nil || logOpts.Until != nil {
return &logOpts, nil
}
return &git.LogOptions{All: true}, nil
}
func optsToCommits(opts options.Options) ([]string, error) {
if opts.Commits != "" {
return strings.Split(opts.Commits, ","), nil
}
file, err := os.Open(opts.CommitsFile)
if err != nil {
return []string{}, err
}
defer rable(file.Close)
scanner := bufio.NewScanner(file)
var commits []string
for scanner.Scan() {
commits = append(commits, scanner.Text())
}
return commits, nil
}
func extractLine(patchContent string, leak Leak, lineLookup map[string]bool) int {
i := strings.Index(patchContent, fmt.Sprintf("\n+++ b/%s", leak.File))
filePatchContent := patchContent[i+1:]
i = strings.Index(filePatchContent, "diff --git")
if i != -1 {
filePatchContent = filePatchContent[:i]
}
chunkStartLine := 0
currLine := 0
for _, patchLine := range strings.Split(filePatchContent, "\n") {
if strings.HasPrefix(patchLine, "@@") {
i := strings.Index(patchLine, diffAddPrefix)
pairs := strings.Split(strings.Split(patchLine[i+1:], diffLineSignature)[0], ",")
chunkStartLine, _ = strconv.Atoi(pairs[0])
currLine = -1
}
if strings.HasPrefix(patchLine, diffDelPrefix) {
currLine--
}
if strings.HasPrefix(patchLine, diffAddPrefix) && strings.Contains(patchLine, leak.Line) {
lineNumber := chunkStartLine + currLine
if _, ok := lineLookup[fmt.Sprintf("%s%s%d%s", leak.Offender, leak.Line, lineNumber, leak.File)]; !ok {
lineLookup[fmt.Sprintf("%s%s%d%s", leak.Offender, leak.Line, lineNumber, leak.File)] = true
return lineNumber
}
}
currLine++
}
return defaultLineNumber
}
// rable is the second half of deferrable... mainly used for defer file.Close()
func rable(f func() error) {
if err := f(); err != nil {
log.Error(err)
}
}