-
Notifications
You must be signed in to change notification settings - Fork 513
/
processor_test.go
312 lines (293 loc) · 7.64 KB
/
processor_test.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
package vex
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v5 "github.com/anchore/grype/grype/db/v5"
"github.com/anchore/grype/grype/match"
"github.com/anchore/grype/grype/pkg"
"github.com/anchore/grype/grype/vulnerability"
"github.com/anchore/syft/syft/source"
)
func TestProcessor_ApplyVEX(t *testing.T) {
pkgContext := &pkg.Context{
Source: &source.Description{
Name: "alpine",
Version: "3.17",
Metadata: source.StereoscopeImageSourceMetadata{
UserInput: "alpine@sha256:124c7d2707904eea7431fffe91522a01e5a861a624ee31d03372cc1d138a3126",
},
},
Distro: nil,
}
libCryptoPackage := pkg.Package{
ID: "cc8f90662d91481d",
Name: "libcrypto3",
Version: "3.0.8-r3",
Type: "apk",
PURL: "pkg:apk/alpine/libcrypto3@3.0.8-r3?arch=x86_64&upstream=openssl&distro=alpine-3.17.3",
Upstreams: []pkg.UpstreamPackage{
{
Name: "openssl",
},
},
}
libCryptoCVE_2023_3817 := match.Match{
Vulnerability: vulnerability.Vulnerability{
ID: "CVE-2023-3817",
Namespace: "alpine:distro:alpine:3.17",
Fix: vulnerability.Fix{
Versions: []string{"3.0.10-r0"},
State: v5.FixedState,
},
},
Package: libCryptoPackage,
}
libCryptoCVE_2023_1255 := match.Match{
Vulnerability: vulnerability.Vulnerability{
ID: "CVE-2023-1255",
Namespace: "alpine:distro:alpine:3.17",
Fix: vulnerability.Fix{
Versions: []string{"3.0.8-r4"},
State: v5.FixedState,
},
},
Package: libCryptoPackage,
}
libCryptoCVE_2023_2975 := match.Match{
Vulnerability: vulnerability.Vulnerability{
ID: "CVE-2023-2975",
Namespace: "alpine:distro:alpine:3.17",
Fix: vulnerability.Fix{
Versions: []string{"3.0.9-r2"},
State: v5.FixedState,
},
},
Package: libCryptoPackage,
}
getSubject := func() *match.Matches {
s := match.NewMatches(
// not-affected justification example
libCryptoCVE_2023_3817,
// fixed status example + matching CVE
libCryptoCVE_2023_1255,
// fixed status example
libCryptoCVE_2023_2975,
)
return &s
}
metchesRef := func(ms ...match.Match) *match.Matches {
m := match.NewMatches(ms...)
return &m
}
type args struct {
pkgContext *pkg.Context
matches *match.Matches
ignoredMatches []match.IgnoredMatch
}
tests := []struct {
name string
options ProcessorOptions
args args
wantMatches *match.Matches
wantIgnoredMatches []match.IgnoredMatch
wantErr require.ErrorAssertionFunc
}{
{
name: "openvex-demo1 - ignore by fixed status",
options: ProcessorOptions{
Documents: []string{
"testdata/vex-docs/openvex-demo1.json",
},
IgnoreRules: []match.IgnoreRule{
{
VexStatus: "fixed",
},
},
},
args: args{
pkgContext: pkgContext,
matches: getSubject(),
},
wantMatches: metchesRef(libCryptoCVE_2023_3817, libCryptoCVE_2023_2975),
wantIgnoredMatches: []match.IgnoredMatch{
{
Match: libCryptoCVE_2023_1255,
AppliedIgnoreRules: []match.IgnoreRule{
{
Namespace: "vex", // note: an additional namespace was added
VexStatus: "fixed",
},
},
},
},
},
{
name: "openvex-demo1 - ignore by fixed status and CVE", // no real difference from the first test other than the AppliedIgnoreRules
options: ProcessorOptions{
Documents: []string{
"testdata/vex-docs/openvex-demo1.json",
},
IgnoreRules: []match.IgnoreRule{
{
Vulnerability: "CVE-2023-1255", // note: this is the difference between this test and the last test
VexStatus: "fixed",
},
},
},
args: args{
pkgContext: pkgContext,
matches: getSubject(),
},
wantMatches: metchesRef(libCryptoCVE_2023_3817, libCryptoCVE_2023_2975),
wantIgnoredMatches: []match.IgnoredMatch{
{
Match: libCryptoCVE_2023_1255,
AppliedIgnoreRules: []match.IgnoreRule{
{
Namespace: "vex",
Vulnerability: "CVE-2023-1255", // note: this is the difference between this test and the last test
VexStatus: "fixed",
},
},
},
},
},
{
name: "openvex-demo2 - ignore by fixed status",
options: ProcessorOptions{
Documents: []string{
"testdata/vex-docs/openvex-demo2.json",
},
IgnoreRules: []match.IgnoreRule{
{
VexStatus: "fixed",
},
},
},
args: args{
pkgContext: pkgContext,
matches: getSubject(),
},
wantMatches: metchesRef(libCryptoCVE_2023_3817),
wantIgnoredMatches: []match.IgnoredMatch{
{
Match: libCryptoCVE_2023_1255,
AppliedIgnoreRules: []match.IgnoreRule{
{
Namespace: "vex",
VexStatus: "fixed",
},
},
},
{
Match: libCryptoCVE_2023_2975,
AppliedIgnoreRules: []match.IgnoreRule{
{
Namespace: "vex",
VexStatus: "fixed",
},
},
},
},
},
{
name: "openvex-demo2 - ignore by fixed status and CVE",
options: ProcessorOptions{
Documents: []string{
"testdata/vex-docs/openvex-demo2.json",
},
IgnoreRules: []match.IgnoreRule{
{
Vulnerability: "CVE-2023-1255", // note: this is the difference between this test and the last test
VexStatus: "fixed",
},
},
},
args: args{
pkgContext: pkgContext,
matches: getSubject(),
},
wantMatches: metchesRef(libCryptoCVE_2023_3817, libCryptoCVE_2023_2975),
wantIgnoredMatches: []match.IgnoredMatch{
{
Match: libCryptoCVE_2023_1255,
AppliedIgnoreRules: []match.IgnoreRule{
{
Namespace: "vex",
Vulnerability: "CVE-2023-1255", // note: this is the difference between this test and the last test
VexStatus: "fixed",
},
},
},
},
},
{
name: "openvex-demo1 - ignore by not_affected status and vulnerable_code_not_present justification",
options: ProcessorOptions{
Documents: []string{
"testdata/vex-docs/openvex-demo1.json",
},
IgnoreRules: []match.IgnoreRule{
{
VexStatus: "not_affected",
VexJustification: "vulnerable_code_not_present",
},
},
},
args: args{
pkgContext: pkgContext,
matches: getSubject(),
},
// nothing gets ignored!
wantMatches: metchesRef(libCryptoCVE_2023_3817, libCryptoCVE_2023_2975, libCryptoCVE_2023_1255),
wantIgnoredMatches: []match.IgnoredMatch{},
},
{
name: "openvex-demo2 - ignore by not_affected status and vulnerable_code_not_present justification",
options: ProcessorOptions{
Documents: []string{
"testdata/vex-docs/openvex-demo2.json",
},
IgnoreRules: []match.IgnoreRule{
{
VexStatus: "not_affected",
VexJustification: "vulnerable_code_not_present",
},
},
},
args: args{
pkgContext: pkgContext,
matches: getSubject(),
},
wantMatches: metchesRef(libCryptoCVE_2023_2975, libCryptoCVE_2023_1255),
wantIgnoredMatches: []match.IgnoredMatch{
{
Match: libCryptoCVE_2023_3817,
AppliedIgnoreRules: []match.IgnoreRule{
{
Namespace: "vex",
VexStatus: "not_affected",
VexJustification: "vulnerable_code_not_present",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantErr == nil {
tt.wantErr = require.NoError
}
p := NewProcessor(tt.options)
actualMatches, actualIgnoredMatches, err := p.ApplyVEX(tt.args.pkgContext, tt.args.matches, tt.args.ignoredMatches)
tt.wantErr(t, err)
if err != nil {
return
}
assert.Equal(t, tt.wantMatches.Sorted(), actualMatches.Sorted())
assert.Equal(t, tt.wantIgnoredMatches, actualIgnoredMatches)
})
}
}