-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scanner.go
547 lines (459 loc) · 13.1 KB
/
scanner.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package secret
import (
"bytes"
"errors"
"os"
"regexp"
"slices"
"sort"
"strings"
"sync"
"github.com/samber/lo"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
)
var lineSep = []byte{'\n'}
type Scanner struct {
logger *log.Logger
*Global
}
type Config struct {
// Enable only specified built-in rules. If only one ID is specified, all other rules are disabled.
// All the built-in rules are enabled if this field is not specified. It doesn't affect custom rules.
EnableBuiltinRuleIDs []string `yaml:"enable-builtin-rules"`
// Disable rules. It is applied to enabled IDs.
DisableRuleIDs []string `yaml:"disable-rules"`
// Disable allow rules.
DisableAllowRuleIDs []string `yaml:"disable-allow-rules"`
CustomRules []Rule `yaml:"rules"`
CustomAllowRules AllowRules `yaml:"allow-rules"`
ExcludeBlock ExcludeBlock `yaml:"exclude-block"`
}
type Global struct {
Rules []Rule
AllowRules AllowRules
ExcludeBlock ExcludeBlock
}
// Allow checks if the match is allowed
func (g Global) Allow(match string) bool {
return g.AllowRules.Allow(match)
}
// AllowPath checks if the path is allowed
func (g Global) AllowPath(path string) bool {
return g.AllowRules.AllowPath(path)
}
// Regexp adds unmarshalling from YAML for regexp.Regexp
type Regexp struct {
*regexp.Regexp
}
func MustCompile(str string) *Regexp {
return &Regexp{regexp.MustCompile(str)}
}
// UnmarshalYAML unmarshals YAML into a regexp.Regexp
func (r *Regexp) UnmarshalYAML(value *yaml.Node) error {
var v string
if err := value.Decode(&v); err != nil {
return err
}
regex, err := regexp.Compile(v)
if err != nil {
return xerrors.Errorf("regexp compile error: %w", err)
}
r.Regexp = regex
return nil
}
type Rule struct {
ID string `yaml:"id"`
Category types.SecretRuleCategory `yaml:"category"`
Title string `yaml:"title"`
Severity string `yaml:"severity"`
Regex *Regexp `yaml:"regex"`
Keywords []string `yaml:"keywords"`
Path *Regexp `yaml:"path"`
AllowRules AllowRules `yaml:"allow-rules"`
ExcludeBlock ExcludeBlock `yaml:"exclude-block"`
SecretGroupName string `yaml:"secret-group-name"`
}
func (s *Scanner) FindLocations(r Rule, content []byte) []Location {
if r.Regex == nil {
return nil
}
if r.SecretGroupName != "" {
return s.FindSubmatchLocations(r, content)
}
var locs []Location
indices := r.Regex.FindAllIndex(content, -1)
for _, index := range indices {
loc := Location{
Start: index[0],
End: index[1],
}
if s.AllowLocation(r, content, loc) {
continue
}
locs = append(locs, loc)
}
return locs
}
func (s *Scanner) FindSubmatchLocations(r Rule, content []byte) []Location {
var submatchLocations []Location
matchsIndices := r.Regex.FindAllSubmatchIndex(content, -1)
for _, matchIndices := range matchsIndices {
matchLocation := Location{
// first two indexes are always start and end of the whole match
Start: matchIndices[0],
End: matchIndices[1],
}
if s.AllowLocation(r, content, matchLocation) {
continue
}
matchSubgroupsLocations := r.getMatchSubgroupsLocations(matchIndices)
if len(matchSubgroupsLocations) > 0 {
submatchLocations = append(submatchLocations, matchSubgroupsLocations...)
}
}
return submatchLocations
}
func (s *Scanner) AllowLocation(r Rule, content []byte, loc Location) bool {
match := string(content[loc.Start:loc.End])
return s.Allow(match) || r.Allow(match)
}
func (r *Rule) getMatchSubgroupsLocations(matchLocs []int) []Location {
var locations []Location
for i, name := range r.Regex.SubexpNames() {
if name == r.SecretGroupName {
startLocIndex := 2 * i
endLocIndex := startLocIndex + 1
locations = append(locations, Location{
Start: matchLocs[startLocIndex],
End: matchLocs[endLocIndex],
})
}
}
return locations
}
func (r *Rule) MatchPath(path string) bool {
return r.Path == nil || r.Path.MatchString(path)
}
func (r *Rule) MatchKeywords(content []byte) bool {
if len(r.Keywords) == 0 {
return true
}
for _, kw := range r.Keywords {
if bytes.Contains(bytes.ToLower(content), []byte(strings.ToLower(kw))) {
return true
}
}
return false
}
func (r *Rule) AllowPath(path string) bool {
return r.AllowRules.AllowPath(path)
}
func (r *Rule) Allow(match string) bool {
return r.AllowRules.Allow(match)
}
type AllowRule struct {
ID string `yaml:"id"`
Description string `yaml:"description"`
Regex *Regexp `yaml:"regex"`
Path *Regexp `yaml:"path"`
}
type AllowRules []AllowRule
func (rules AllowRules) AllowPath(path string) bool {
for _, rule := range rules {
if rule.Path != nil && rule.Path.MatchString(path) {
return true
}
}
return false
}
func (rules AllowRules) Allow(match string) bool {
for _, rule := range rules {
if rule.Regex != nil && rule.Regex.MatchString(match) {
return true
}
}
return false
}
type ExcludeBlock struct {
Description string `yaml:"description"`
Regexes []*Regexp `yaml:"regexes"`
}
type Location struct {
Start int
End int
}
func (l Location) Match(loc Location) bool {
return l.Start <= loc.Start && loc.End <= l.End
}
type Blocks struct {
content []byte
regexes []*Regexp
locs []Location
once *sync.Once
}
func newBlocks(content []byte, regexes []*Regexp) Blocks {
return Blocks{
content: content,
regexes: regexes,
once: new(sync.Once),
}
}
func (b *Blocks) Match(block Location) bool {
b.once.Do(b.find)
for _, loc := range b.locs {
if loc.Match(block) {
return true
}
}
return false
}
func (b *Blocks) find() {
for _, regex := range b.regexes {
results := regex.FindAllIndex(b.content, -1)
if len(results) == 0 {
continue
}
for _, r := range results {
b.locs = append(b.locs, Location{
Start: r[0],
End: r[1],
})
}
}
}
func ParseConfig(configPath string) (*Config, error) {
// If no config is passed, use built-in rules and allow rules.
if configPath == "" {
return nil, nil
}
logger := log.WithPrefix("secret").With("config_path", configPath)
f, err := os.Open(configPath)
if errors.Is(err, os.ErrNotExist) {
// If the specified file doesn't exist, it just uses built-in rules and allow rules.
logger.Debug("No secret config detected")
return nil, nil
} else if err != nil {
return nil, xerrors.Errorf("file open error %s: %w", configPath, err)
}
defer f.Close()
logger.Info("Loading the config file for secret scanning...")
var config Config
if err = yaml.NewDecoder(f).Decode(&config); err != nil {
return nil, xerrors.Errorf("secrets config decode error: %w", err)
}
// Update severity for custom rules
for i := range config.CustomRules {
config.CustomRules[i].Severity = convertSeverity(logger, config.CustomRules[i].Severity)
}
return &config, nil
}
// convertSeverity checks the severity and converts it to uppercase or uses "UNKNOWN" for the wrong severity.
func convertSeverity(logger *log.Logger, severity string) string {
switch strings.ToLower(severity) {
case "low", "medium", "high", "critical", "unknown":
return strings.ToUpper(severity)
default:
logger.Warn("Incorrect severity", log.String("severity", severity))
return "UNKNOWN"
}
}
func NewScanner(config *Config) Scanner {
logger := log.WithPrefix("secret")
// Use the default rules
if config == nil {
return Scanner{
logger: logger,
Global: &Global{
Rules: builtinRules,
AllowRules: builtinAllowRules,
},
}
}
enabledRules := builtinRules
if len(config.EnableBuiltinRuleIDs) != 0 {
// Enable only specified built-in rules
enabledRules = lo.Filter(builtinRules, func(v Rule, _ int) bool {
return slices.Contains(config.EnableBuiltinRuleIDs, v.ID)
})
}
// Custom rules are enabled regardless of "enable-builtin-rules".
enabledRules = append(enabledRules, config.CustomRules...)
// Disable specified rules
rules := lo.Filter(enabledRules, func(v Rule, _ int) bool {
return !slices.Contains(config.DisableRuleIDs, v.ID)
})
// Disable specified allow rules
allowRules := append(builtinAllowRules, config.CustomAllowRules...)
allowRules = lo.Filter(allowRules, func(v AllowRule, _ int) bool {
return !slices.Contains(config.DisableAllowRuleIDs, v.ID)
})
return Scanner{
logger: logger,
Global: &Global{
Rules: rules,
AllowRules: allowRules,
ExcludeBlock: config.ExcludeBlock,
},
}
}
type ScanArgs struct {
FilePath string
Content []byte
}
type Match struct {
Rule Rule
Location Location
}
func (s *Scanner) Scan(args ScanArgs) types.Secret {
logger := s.logger.With("file_path", args.FilePath)
// Global allowed paths
if s.AllowPath(args.FilePath) {
logger.Debug("Skipped secret scanning matching allowed paths")
return types.Secret{
FilePath: args.FilePath,
}
}
var censored []byte
var copyCensored sync.Once
var matched []Match
var findings []types.SecretFinding
globalExcludedBlocks := newBlocks(args.Content, s.ExcludeBlock.Regexes)
for _, rule := range s.Rules {
ruleLogger := logger.With("rule_id", rule.ID)
// Check if the file path should be scanned by this rule
if !rule.MatchPath(args.FilePath) {
ruleLogger.Debug("Skipped secret scanning as non-compliant to the rule")
continue
}
// Check if the file path should be allowed
if rule.AllowPath(args.FilePath) {
ruleLogger.Debug("Skipped secret scanning as allowed")
continue
}
// Check if the file content contains keywords and should be scanned
if !rule.MatchKeywords(args.Content) {
continue
}
// Detect secrets
locs := s.FindLocations(rule, args.Content)
if len(locs) == 0 {
continue
}
localExcludedBlocks := newBlocks(args.Content, rule.ExcludeBlock.Regexes)
for _, loc := range locs {
// Skip the secret if it is within excluded blocks.
if globalExcludedBlocks.Match(loc) || localExcludedBlocks.Match(loc) {
continue
}
matched = append(matched, Match{
Rule: rule,
Location: loc,
})
copyCensored.Do(func() {
censored = make([]byte, len(args.Content))
copy(censored, args.Content)
})
censored = censorLocation(loc, censored)
}
}
for _, match := range matched {
findings = append(findings, toFinding(match.Rule, match.Location, censored))
}
if len(findings) == 0 {
return types.Secret{}
}
sort.Slice(findings, func(i, j int) bool {
if findings[i].RuleID != findings[j].RuleID {
return findings[i].RuleID < findings[j].RuleID
}
return findings[i].Match < findings[j].Match
})
return types.Secret{
FilePath: args.FilePath,
Findings: findings,
}
}
func censorLocation(loc Location, input []byte) []byte {
return append(
input[:loc.Start],
append(
bytes.Repeat([]byte("*"), loc.End-loc.Start),
input[loc.End:]...,
)...,
)
}
func toFinding(rule Rule, loc Location, content []byte) types.SecretFinding {
startLine, endLine, code, matchLine := findLocation(loc.Start, loc.End, content)
return types.SecretFinding{
RuleID: rule.ID,
Category: rule.Category,
Severity: lo.Ternary(rule.Severity == "", "UNKNOWN", rule.Severity),
Title: rule.Title,
Match: matchLine,
StartLine: startLine,
EndLine: endLine,
Code: code,
}
}
const (
secretHighlightRadius = 2 // number of lines above + below each secret to include in code output
maxLineLength = 2000 //
)
func findLocation(start, end int, content []byte) (int, int, types.Code, string) {
startLineNum := bytes.Count(content[:start], lineSep)
lineStart := bytes.LastIndex(content[:start], lineSep)
if lineStart == -1 {
lineStart = 0
} else {
lineStart += 1
}
lineEnd := bytes.Index(content[start:], lineSep)
if lineEnd == -1 {
lineEnd = len(content)
} else {
lineEnd += start
}
if lineEnd-lineStart > 100 {
lineStart = lo.Ternary(start-30 < 0, 0, start-30)
lineEnd = lo.Ternary(end+20 > len(content), len(content), end+20)
}
matchLine := string(content[lineStart:lineEnd])
endLineNum := startLineNum + bytes.Count(content[start:end], lineSep)
var code types.Code
lines := bytes.Split(content, lineSep)
codeStart := lo.Ternary(startLineNum-secretHighlightRadius < 0, 0, startLineNum-secretHighlightRadius)
codeEnd := lo.Ternary(endLineNum+secretHighlightRadius > len(lines), len(lines), endLineNum+secretHighlightRadius)
rawLines := lines[codeStart:codeEnd]
var foundFirst bool
for i, rawLine := range rawLines {
realLine := codeStart + i
inCause := realLine >= startLineNum && realLine <= endLineNum
var strRawLine string
if len(rawLine) > maxLineLength {
strRawLine = lo.Ternary(inCause, matchLine, string(rawLine[:maxLineLength]))
} else {
strRawLine = string(rawLine)
}
code.Lines = append(code.Lines, types.Line{
Number: codeStart + i + 1,
Content: strRawLine,
IsCause: inCause,
Highlighted: strRawLine,
FirstCause: !foundFirst && inCause,
LastCause: false,
})
foundFirst = foundFirst || inCause
}
if len(code.Lines) > 0 {
for i := len(code.Lines) - 1; i >= 0; i-- {
if code.Lines[i].IsCause {
code.Lines[i].LastCause = true
break
}
}
}
return startLineNum + 1, endLineNum + 1, code, matchLine
}