-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
filter.go
382 lines (333 loc) · 11.5 KB
/
filter.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
package result
import (
"context"
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"github.com/open-policy-agent/opa/rego"
"github.com/samber/lo"
"golang.org/x/xerrors"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/fanal/walker"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/types"
"github.com/aquasecurity/trivy/pkg/vex"
)
const (
// DefaultIgnoreFile is the file name to be evaluated
DefaultIgnoreFile = ".trivyignore"
)
type FilterOptions struct {
Severities []dbTypes.Severity
IgnoreStatuses []dbTypes.Status
IncludeNonFailures bool
IgnoreFile string
PolicyFile string
IgnoreLicenses []string
CacheDir string
VEXSources []vex.Source
SkipDirs []string
SkipFiles []string
}
// Filter filters out the report
func Filter(ctx context.Context, report types.Report, opts FilterOptions) error {
ignoreConf, err := ParseIgnoreFile(ctx, opts.IgnoreFile)
if err != nil {
return xerrors.Errorf("%s error: %w", opts.IgnoreFile, err)
}
for i := range report.Results {
if err = FilterResult(ctx, &report.Results[i], ignoreConf, opts); err != nil {
return xerrors.Errorf("unable to filter vulnerabilities: %w", err)
}
}
// Filter out vulnerabilities based on the given VEX document.
if err = vex.Filter(ctx, &report, vex.Options{
CacheDir: opts.CacheDir,
Sources: opts.VEXSources,
}); err != nil {
return xerrors.Errorf("VEX error: %w", err)
}
return nil
}
// FilterResult filters out the result
func FilterResult(ctx context.Context, result *types.Result, ignoreConf IgnoreConfig, opt FilterOptions) error {
// Convert dbTypes.Severity to string
severities := lo.Map(opt.Severities, func(s dbTypes.Severity, _ int) string {
return s.String()
})
filterVulnerabilities(result, severities, opt.IgnoreStatuses, ignoreConf)
filterMisconfigurations(result, severities, opt.IncludeNonFailures, opt.SkipFiles, opt.SkipDirs, ignoreConf)
filterSecrets(result, severities, ignoreConf)
filterLicenses(result, severities, opt.IgnoreLicenses, ignoreConf)
if opt.PolicyFile != "" {
if err := applyPolicy(ctx, result, opt.PolicyFile); err != nil {
return xerrors.Errorf("failed to apply the policy: %w", err)
}
}
sort.Sort(types.BySeverity(result.Vulnerabilities))
return nil
}
func filterVulnerabilities(result *types.Result, severities []string, ignoreStatuses []dbTypes.Status, ignoreConfig IgnoreConfig) {
uniqVulns := make(map[string]types.DetectedVulnerability)
for _, vuln := range result.Vulnerabilities {
if vuln.Severity == "" {
vuln.Severity = dbTypes.SeverityUnknown.String()
}
switch {
// Filter by severity
case !slices.Contains(severities, vuln.Severity):
continue
// Filter by status
case slices.Contains(ignoreStatuses, vuln.Status):
continue
}
// Filter by ignore file
if f := ignoreConfig.MatchVulnerability(vuln.VulnerabilityID, result.Target, vuln.PkgPath, vuln.PkgIdentifier.PURL); f != nil {
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(vuln, types.FindingStatusIgnored, f.Statement, ignoreConfig.FilePath))
continue
}
// Check if there is a duplicate vulnerability
key := fmt.Sprintf("%s/%s/%s/%s", vuln.VulnerabilityID, vuln.PkgName, vuln.InstalledVersion, vuln.PkgPath)
if old, ok := uniqVulns[key]; ok && !shouldOverwrite(old, vuln) {
continue
}
uniqVulns[key] = vuln
}
// Override the detected vulnerabilities
result.Vulnerabilities = lo.Values(uniqVulns)
if len(result.Vulnerabilities) == 0 {
result.Vulnerabilities = nil
}
}
func checkGlobMatch(misconf types.DetectedMisconfiguration, result *types.Result, filename string, skips []string) bool {
if walker.SkipPath(filepath.ToSlash(filepath.Clean(filename)), walker.CleanSkipPaths(skips)) {
log.Debug("skipping path based on glob match", log.String("file", filename))
result.MisconfSummary.Exceptions++
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(misconf, types.FindingStatusIgnored, fmt.Sprintf("skipped due to glob match: %s", skips), "trivy cli args"))
return true
}
return false
}
func checkSubstrMatch(misconf types.DetectedMisconfiguration, result *types.Result, filename string, skips []string) bool {
for _, skipDir := range skips {
if strings.Contains(filepath.ToSlash(filepath.Clean(filename)), skipDir) {
log.Debug("skipping path based on substring match", log.String("file", filename))
result.MisconfSummary.Exceptions++
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(misconf, types.FindingStatusIgnored, fmt.Sprintf("skipped due to substring match: %s", skips), "trivy cli args"))
return true
}
}
return false
}
func checkPathMatch(misconf types.DetectedMisconfiguration, result *types.Result, skips []string) bool {
for _, o := range misconf.CauseMetadata.Occurrences {
if checkGlobMatch(misconf, result, o.Filename, skips) {
return true
}
if checkSubstrMatch(misconf, result, o.Filename, skips) {
return true
}
}
return false
}
func filterMisconfigurations(result *types.Result, severities []string, includeNonFailures bool, skipFiles, skipDirs []string,
ignoreConfig IgnoreConfig) {
var filtered []types.DetectedMisconfiguration
result.MisconfSummary = new(types.MisconfSummary)
for _, misconf := range result.Misconfigurations {
// Filter by skip dirs
if checkPathMatch(misconf, result, skipDirs) {
continue
}
// Filter by skip files
if checkPathMatch(misconf, result, skipFiles) {
continue
}
// Filter by severity
if !slices.Contains(severities, misconf.Severity) {
continue
}
// Filter by ignore file
if f := ignoreConfig.MatchMisconfiguration(misconf.ID, misconf.AVDID, result.Target); f != nil {
result.MisconfSummary.Exceptions++
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(misconf, types.FindingStatusIgnored, f.Statement, ignoreConfig.FilePath))
continue
}
// Count successes, failures, and exceptions
summarize(misconf.Status, result.MisconfSummary)
if misconf.Status != types.MisconfStatusFailure && !includeNonFailures {
continue
}
filtered = append(filtered, misconf)
}
result.Misconfigurations = filtered
if result.MisconfSummary.Empty() {
result.Misconfigurations = nil
result.MisconfSummary = nil
}
}
func filterSecrets(result *types.Result, severities []string, ignoreConfig IgnoreConfig) {
var filtered []types.DetectedSecret
for _, secret := range result.Secrets {
if !slices.Contains(severities, secret.Severity) {
// Filter by severity
continue
} else if f := ignoreConfig.MatchSecret(secret.RuleID, result.Target); f != nil {
// Filter by ignore file
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(secret, types.FindingStatusIgnored, f.Statement, ignoreConfig.FilePath))
continue
}
filtered = append(filtered, secret)
}
result.Secrets = filtered
}
func filterLicenses(result *types.Result, severities, ignoreLicenseNames []string, ignoreConfig IgnoreConfig) {
// Merge ignore license names into ignored findings
var ignoreLicenses IgnoreConfig
for _, licenseName := range ignoreLicenseNames {
ignoreLicenses.Licenses = append(ignoreLicenses.Licenses, IgnoreFinding{
ID: licenseName,
})
}
var filtered []types.DetectedLicense
for _, l := range result.Licenses {
// Filter by severity
if !slices.Contains(severities, l.Severity) {
continue
}
// Filter by `--ignored-licenses`
if f := ignoreLicenses.MatchLicense(l.Name, l.FilePath); f != nil {
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(l, types.FindingStatusIgnored, "", "--ignored-licenses"))
continue
}
// Filter by ignore file
if f := ignoreConfig.MatchLicense(l.Name, l.FilePath); f != nil {
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(l, types.FindingStatusIgnored, f.Statement, ignoreConfig.FilePath))
continue
}
filtered = append(filtered, l)
}
result.Licenses = filtered
}
func summarize(status types.MisconfStatus, summary *types.MisconfSummary) {
switch status {
case types.MisconfStatusFailure:
summary.Failures++
case types.MisconfStatusPassed:
summary.Successes++
case types.MisconfStatusException:
summary.Exceptions++
}
}
func applyPolicy(ctx context.Context, result *types.Result, policyFile string) error {
policy, err := os.ReadFile(policyFile)
if err != nil {
return xerrors.Errorf("unable to read the policy file: %w", err)
}
query, err := rego.New(
rego.Query("data.trivy.ignore"),
rego.Module("lib.rego", module),
rego.Module("trivy.rego", string(policy)),
).PrepareForEval(ctx)
if err != nil {
return xerrors.Errorf("unable to prepare for eval: %w", err)
}
policyFile = filepath.ToSlash(filepath.Clean(policyFile))
// Vulnerabilities
var filteredVulns []types.DetectedVulnerability
for _, vuln := range result.Vulnerabilities {
ignored, err := evaluate(ctx, query, vuln)
if err != nil {
return err
}
if ignored {
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(vuln, types.FindingStatusIgnored, "Filtered by Rego", policyFile))
continue
}
filteredVulns = append(filteredVulns, vuln)
}
result.Vulnerabilities = filteredVulns
// Misconfigurations
var filteredMisconfs []types.DetectedMisconfiguration
for _, misconf := range result.Misconfigurations {
ignored, err := evaluate(ctx, query, misconf)
if err != nil {
return err
}
if ignored {
result.MisconfSummary.Exceptions++
switch misconf.Status {
case types.MisconfStatusFailure:
result.MisconfSummary.Failures--
case types.MisconfStatusPassed:
result.MisconfSummary.Successes--
}
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(misconf, types.FindingStatusIgnored, "Filtered by Rego", policyFile))
continue
}
filteredMisconfs = append(filteredMisconfs, misconf)
}
result.Misconfigurations = filteredMisconfs
// Secrets
var filteredSecrets []types.DetectedSecret
for _, scrt := range result.Secrets {
ignored, err := evaluate(ctx, query, scrt)
if err != nil {
return err
}
if ignored {
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(scrt, types.FindingStatusIgnored, "Filtered by Rego", policyFile))
continue
}
filteredSecrets = append(filteredSecrets, scrt)
}
result.Secrets = filteredSecrets
// Licenses
var filteredLicenses []types.DetectedLicense
for _, lic := range result.Licenses {
ignored, err := evaluate(ctx, query, lic)
if err != nil {
return err
}
if ignored {
result.ModifiedFindings = append(result.ModifiedFindings,
types.NewModifiedFinding(lic, types.FindingStatusIgnored, "Filtered by Rego", policyFile))
continue
}
filteredLicenses = append(filteredLicenses, lic)
}
result.Licenses = filteredLicenses
return nil
}
func evaluate(ctx context.Context, query rego.PreparedEvalQuery, input any) (bool, error) {
results, err := query.Eval(ctx, rego.EvalInput(input))
if err != nil {
return false, xerrors.Errorf("unable to evaluate the policy: %w", err)
} else if len(results) == 0 {
// Handle undefined result.
return false, nil
}
ignore, ok := results[0].Expressions[0].Value.(bool)
if !ok {
// Handle unexpected result type.
return false, xerrors.New("the policy must return boolean")
}
return ignore, nil
}
func shouldOverwrite(oldVuln, newVuln types.DetectedVulnerability) bool {
// The same vulnerability must be picked always.
return oldVuln.FixedVersion < newVuln.FixedVersion
}