-
Notifications
You must be signed in to change notification settings - Fork 117
/
ignores.go
67 lines (53 loc) · 1.33 KB
/
ignores.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
package cloudformation
import (
"fmt"
"strings"
"time"
"github.com/aquasecurity/defsec/pkg/scanners/cloudformation/parser"
"github.com/aquasecurity/defsec/pkg/scan"
)
func isIgnored(scanResult scan.Result) bool {
ref := scanResult.Metadata().Reference()
if cfRef, ok := ref.(*parser.CFReference); ok {
if ignore, err := parseIgnore(cfRef.Comment()); err == nil {
if ignore.RuleID != scanResult.Rule().AVDID && ignore.RuleID != scanResult.Rule().LongID() {
return false
}
if ignore.Expiry == nil || time.Now().Before(*ignore.Expiry) {
return true
}
}
}
return false
}
type Ignore struct {
RuleID string
Expiry *time.Time
}
func parseIgnore(comment string) (*Ignore, error) {
comment = strings.TrimSpace(comment)
comment = strings.TrimPrefix(comment, "#")
comment = strings.TrimPrefix(comment, "//")
comment = strings.TrimSpace(comment)
var ignore Ignore
if !strings.HasPrefix(comment, "cfsec:") {
return nil, fmt.Errorf("invalid ignore")
}
comment = comment[6:]
segments := strings.Split(comment, ":")
for i := 0; i < len(segments)-1; i += 2 {
key := segments[i]
val := segments[i+1]
switch key {
case "ignore":
ignore.RuleID = val
case "exp":
parsed, err := time.Parse("2006-01-02", val)
if err != nil {
return nil, err
}
ignore.Expiry = &parsed
}
}
return &ignore, nil
}