forked from gitleaks/gitleaks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
286 lines (261 loc) Β· 7.63 KB
/
options.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const usage = `
usage: gitleaks [options] <URL>/<path_to_repo>
Options:
-u --user Git user mode
-r --repo Git repo mode
-o --org Git organization mode
-l --local Local mode, gitleaks will look for local repo in <path>
-v --verbose Verbose mode, will output leaks as gitleaks finds them
--report-path=<STR> Report output, default $GITLEAKS_HOME/report
--clone-path=<STR> Gitleaks will clone repos here, default $GITLEAKS_HOME/clones
-t --temp Clone to temporary directory
--concurrency=<INT> Upper bound on concurrent "git diff"
--since=<STR> Commit to stop at
--b64Entropy=<INT> Base64 entropy cutoff (default is 70)
--hexEntropy=<INT> Hex entropy cutoff (default is 40)
-e --entropy Enable entropy
-h --help Display this message
--token=<STR> Github API token
--stopwords Enables stopwords
`
// Options for gitleaks
type Options struct {
URL string
RepoPath string
ReportPath string
ClonePath string
Concurrency int
B64EntropyCutoff int
HexEntropyCutoff int
UserMode bool
OrgMode bool
RepoMode bool
LocalMode bool
Strict bool
Entropy bool
SinceCommit string
Tmp bool
Token string
Verbose bool
RegexFile string
}
// help prints the usage string and exits
func help() {
os.Stderr.WriteString(usage)
}
// optionsNextInt is a parseOptions helper that returns the value (int) of an option if valid
func (opts *Options) nextInt(args []string, i *int) int {
if len(args) > *i+1 {
*i++
} else {
help()
}
argInt, err := strconv.Atoi(args[*i])
if err != nil {
opts.failF("Invalid %s option: %s\n", args[*i-1], args[*i])
}
return argInt
}
// optionsNextString is a parseOptions helper that returns the value (string) of an option if valid
func (opts *Options) nextString(args []string, i *int) string {
if len(args) > *i+1 {
*i++
} else {
opts.failF("Invalid %s option: %s\n", args[*i-1], args[*i])
}
return args[*i]
}
// optInt grabs the string ...
func (opts *Options) optString(arg string, prefixes ...string) (bool, string) {
for _, prefix := range prefixes {
if strings.HasPrefix(arg, prefix) {
return true, arg[len(prefix):]
}
}
return false, ""
}
// optInt grabs the int ...
func (opts *Options) optInt(arg string, prefixes ...string) (bool, int) {
for _, prefix := range prefixes {
if strings.HasPrefix(arg, prefix) {
i, err := strconv.Atoi(arg[len(prefix):])
if err != nil {
opts.failF("Invalid %s int option\n", prefix)
}
return true, i
}
}
return false, 0
}
// newOpts generates opts and parses arguments
func newOpts(args []string) *Options {
opts, err := defaultOptions()
if err != nil {
opts.failF("%v", err)
}
err = opts.parseOptions(args)
if err != nil {
opts.failF("%v", err)
}
return opts
}
// deafultOptions provides the default options used by newOpts
func defaultOptions() (*Options, error) {
return &Options{
Concurrency: 10,
B64EntropyCutoff: 70,
HexEntropyCutoff: 40,
}, nil
}
// parseOptions will parse options supplied by the user.
func (opts *Options) parseOptions(args []string) error {
if len(args) == 0 {
opts.LocalMode = true
opts.RepoPath, _ = os.Getwd()
}
for i := 0; i < len(args); i++ {
arg := args[i]
switch arg {
case "--stopwords":
opts.Strict = true
case "-e", "--entropy":
opts.Entropy = true
case "-o", "--org":
opts.OrgMode = true
case "-u", "--user":
opts.UserMode = true
case "-r", "--repo":
opts.RepoMode = true
case "-l", "--local":
opts.LocalMode = true
case "-v", "--verbose":
opts.Verbose = true
case "-t", "--temp":
opts.Tmp = true
case "-h", "--help":
help()
os.Exit(ExitClean)
default:
if match, value := opts.optString(arg, "--token="); match {
opts.Token = value
} else if match, value := opts.optString(arg, "--since="); match {
opts.SinceCommit = value
} else if match, value := opts.optString(arg, "--report-path="); match {
opts.ReportPath = value
} else if match, value := opts.optString(arg, "--clone-path="); match {
opts.ClonePath = value
} else if match, value := opts.optInt(arg, "--b64Entropy="); match {
opts.B64EntropyCutoff = value
} else if match, value := opts.optInt(arg, "--hexEntropy="); match {
opts.HexEntropyCutoff = value
} else if match, value := opts.optInt(arg, "--concurrency="); match {
opts.Concurrency = value
} else if match, value := opts.optString(arg, "--regex-file="); match {
opts.RegexFile = value
} else if i == len(args)-1 {
if opts.LocalMode {
opts.RepoPath = filepath.Clean(args[i])
} else {
if isGithubTarget(args[i]) {
opts.URL = args[i]
} else {
help()
return fmt.Errorf("Unknown option %s\n", arg)
}
}
} else {
help()
return fmt.Errorf("Unknown option %s\n", arg)
}
}
}
if opts.RegexFile != "" {
err := opts.loadExternalRegex()
if err != nil {
return fmt.Errorf("unable to load regex from file %s: %v",
opts.RegexFile, err)
}
}
if !opts.RepoMode && !opts.UserMode && !opts.OrgMode && !opts.LocalMode {
if opts.URL != "" {
opts.RepoMode = true
err := opts.guards()
if err != nil {
return err
}
return nil
}
pwd, _ = os.Getwd()
// check if pwd contains a .git, if it does, run local mode
dotGitPath := filepath.Join(pwd, ".git")
if _, err := os.Stat(dotGitPath); os.IsNotExist(err) {
return fmt.Errorf("gitleaks has no target: %v", err)
} else {
opts.LocalMode = true
opts.RepoPath = pwd
opts.RepoMode = false
}
}
err := opts.guards()
if err != nil {
return err
}
return err
}
// loadExternalRegex loads regexes from a text file if available.
func (opts *Options) loadExternalRegex() error {
file, err := os.Open(opts.RegexFile)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
externalRegex = append(externalRegex, regexp.MustCompile(scanner.Text()))
}
return nil
}
// failF prints a failure message out to stderr, displays help
// and exits with a exit code 2
func (opts *Options) failF(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
help()
os.Exit(ExitFailure)
}
// guards will prevent gitleaks from continuing if any invalid options
// are found.
func (opts *Options) guards() error {
if (opts.RepoMode || opts.OrgMode || opts.UserMode) && opts.LocalMode {
return fmt.Errorf("Cannot run Gitleaks on repo/user/org mode and local mode\n")
} else if (opts.RepoMode || opts.OrgMode || opts.UserMode) && !isGithubTarget(opts.URL) {
return fmt.Errorf("Not valid github target %s\n", opts.URL)
} else if (opts.RepoMode || opts.UserMode) && opts.OrgMode {
return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
} else if (opts.OrgMode || opts.UserMode) && opts.RepoMode {
return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
} else if (opts.OrgMode || opts.RepoMode) && opts.UserMode {
return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
} else if opts.LocalMode && opts.Tmp {
return fmt.Errorf("Cannot run Gitleaks with temp settings and local mode\n")
} else if opts.SinceCommit != "" && (opts.OrgMode || opts.UserMode) {
return fmt.Errorf("Cannot run Gitleaks with since commit flag and a owner mode\n")
} else if opts.ClonePath != "" && opts.Tmp {
return fmt.Errorf("Cannot run Gitleaks with --clone-path set and temporary repo\n")
}
return nil
}
// isGithubTarget checks if url is a valid github target
func isGithubTarget(url string) bool {
re := regexp.MustCompile("github.com")
return re.MatchString(url)
}