-
Notifications
You must be signed in to change notification settings - Fork 571
/
ignore.go
167 lines (133 loc) · 4.83 KB
/
ignore.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
package match
import "github.com/bmatcuk/doublestar/v2"
// An IgnoredMatch is a vulnerability Match that has been ignored because one or more IgnoreRules applied to the match.
type IgnoredMatch struct {
Match
// AppliedIgnoreRules are the rules that were applied to the match that caused Grype to ignore it.
AppliedIgnoreRules []IgnoreRule
}
// An IgnoreRule specifies criteria for a vulnerability match to meet in order
// to be ignored. Not all criteria (fields) need to be specified, but all
// specified criteria must be met by the vulnerability match in order for the
// rule to apply.
type IgnoreRule struct {
Vulnerability string `yaml:"vulnerability" json:"vulnerability" mapstructure:"vulnerability"`
Package IgnoreRulePackage `yaml:"package" json:"package" mapstructure:"package"`
}
// IgnoreRulePackage describes the Package-specific fields that comprise the IgnoreRule.
type IgnoreRulePackage struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
Version string `yaml:"version" json:"version" mapstructure:"version"`
Type string `yaml:"type" json:"type" mapstructure:"type"`
Location string `yaml:"location" json:"location" mapstructure:"location"`
}
// ApplyIgnoreRules iterates through the provided matches and, for each match,
// determines if the match should be ignored, by evaluating if any of the
// provided IgnoreRules apply to the match. If any rules apply to the match, all
// applicable rules are attached to the Match to form an IgnoredMatch.
// ApplyIgnoreRules returns two collections: the matches that are not being
// ignored, and the matches that are being ignored.
func ApplyIgnoreRules(matches Matches, rules []IgnoreRule) (Matches, []IgnoredMatch) {
if len(rules) == 0 {
return matches, nil
}
var ignoredMatches []IgnoredMatch
remainingMatches := NewMatches()
for match := range matches.Enumerate() {
var applicableRules []IgnoreRule
for _, rule := range rules {
if shouldIgnore(match, rule) {
applicableRules = append(applicableRules, rule)
}
}
if len(applicableRules) > 0 {
ignoredMatches = append(ignoredMatches, IgnoredMatch{
Match: match,
AppliedIgnoreRules: applicableRules,
})
continue
}
remainingMatches.add(match.Package.ID, match)
}
return remainingMatches, ignoredMatches
}
func shouldIgnore(match Match, rule IgnoreRule) bool {
ignoreConditions := getIgnoreConditionsForRule(rule)
if len(ignoreConditions) == 0 {
// this rule specifies no criteria, so it doesn't apply to the Match
return false
}
for _, condition := range ignoreConditions {
if !condition(match) {
// as soon as one rule criterion doesn't apply, we know this rule doesn't apply to the Match
return false
}
}
// all criteria specified in the rule apply to this Match
return true
}
// An ignoreCondition is a function that returns a boolean indicating whether
// the given Match should be ignored.
type ignoreCondition func(match Match) bool
func getIgnoreConditionsForRule(rule IgnoreRule) []ignoreCondition {
var ignoreConditions []ignoreCondition
if v := rule.Vulnerability; v != "" {
ignoreConditions = append(ignoreConditions, ifVulnerabilityApplies(v))
}
if n := rule.Package.Name; n != "" {
ignoreConditions = append(ignoreConditions, ifPackageNameApplies(n))
}
if v := rule.Package.Version; v != "" {
ignoreConditions = append(ignoreConditions, ifPackageVersionApplies(v))
}
if t := rule.Package.Type; t != "" {
ignoreConditions = append(ignoreConditions, ifPackageTypeApplies(t))
}
if l := rule.Package.Location; l != "" {
ignoreConditions = append(ignoreConditions, ifPackageLocationApplies(l))
}
return ignoreConditions
}
func ifVulnerabilityApplies(vulnerability string) ignoreCondition {
return func(match Match) bool {
return vulnerability == match.Vulnerability.ID
}
}
func ifPackageNameApplies(name string) ignoreCondition {
return func(match Match) bool {
return name == match.Package.Name
}
}
func ifPackageVersionApplies(version string) ignoreCondition {
return func(match Match) bool {
return version == match.Package.Version
}
}
func ifPackageTypeApplies(t string) ignoreCondition {
return func(match Match) bool {
return t == string(match.Package.Type)
}
}
func ifPackageLocationApplies(location string) ignoreCondition {
return func(match Match) bool {
return ruleLocationAppliesToMatch(location, match)
}
}
func ruleLocationAppliesToMatch(location string, match Match) bool {
for _, packageLocation := range match.Package.Locations {
if ruleLocationAppliesToPath(location, packageLocation.RealPath) {
return true
}
if ruleLocationAppliesToPath(location, packageLocation.VirtualPath) {
return true
}
}
return false
}
func ruleLocationAppliesToPath(location, path string) bool {
doesMatch, err := doublestar.Match(location, path)
if err != nil {
return false
}
return doesMatch
}