forked from aquasecurity/trivy-db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvulnerability_test.go
More file actions
369 lines (358 loc) · 12.6 KB
/
Copy pathvulnerability_test.go
File metadata and controls
369 lines (358 loc) · 12.6 KB
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
package vulnerability_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/aquasecurity/trivy-db/pkg/dbtest"
"github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy-db/pkg/utils"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
)
func TestGetDetails(t *testing.T) {
testCases := []struct {
name string
vulnID string
fixtures []string
want map[types.SourceID]types.VulnerabilityDetail
}{
{
name: "happy path",
vulnID: "CVE-2020-1234",
fixtures: []string{"testdata/fixtures/happy.yaml"},
want: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.NVD: {
CvssScore: 4.2,
CvssVector: "AV:N/AC:M/Au:N/C:N/I:P/A:N",
CvssScoreV3: 5.6,
CvssVectorV3: "CVSS:3.0/AV:A/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
SeverityV3: types.SeverityHigh,
CweIDs: []string{"CWE-125", "CWE-200"},
LastModifiedDate: utils.MustTimeParse("2020-01-01T01:02:03Z"),
PublishedDate: utils.MustTimeParse("2001-01-01T01:02:03Z"),
},
vulnerability.RedHat: {
CvssScoreV3: 6.7,
CvssVectorV3: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
SeverityV3: types.SeverityHigh,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
References: []string{"http://foo-bar.com/baz"},
},
},
},
{
name: "no advisories are returned",
fixtures: []string{"testdata/fixtures/happy.yaml"},
vulnID: "CVE-2020-9999",
want: nil,
},
{
name: "GetVulnerabilityDetail returns an error",
fixtures: []string{"testdata/fixtures/sad.yaml"},
vulnID: "CVE-2020-1234",
want: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_ = dbtest.InitDB(t, tc.fixtures)
defer db.Close()
v := vulnerability.New(db.Config{})
got := v.GetDetails(tc.vulnID)
assert.Equal(t, tc.want, got)
})
}
}
func TestIsRejected(t *testing.T) {
testCases := []struct {
name string
details map[types.SourceID]types.VulnerabilityDetail
want bool
}{
{
name: "happy path",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.NVD: {
ID: "CVE-2020-1234",
CvssScore: 9.1,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
VulnStatus: "Modified",
},
vulnerability.RedHat: {
ID: "CVE-2020-1234",
CvssScoreV3: 5.6,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
},
want: false,
},
{
name: "happy path, when vulnerability from redhat and ubuntu is rejected by Nvd",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.RedHat: {
ID: "CVE-2020-1234",
CvssScoreV3: 5.6,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
vulnerability.Ubuntu: {
ID: "CVE-2020-1234",
CvssScore: 1.2,
CvssScoreV3: 3.4,
Severity: types.SeverityLow,
SeverityV3: types.SeverityMedium,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
vulnerability.NVD: {
ID: "CVE-2020-1234",
CvssScore: 9.1,
Title: "test vulnerability",
Description: "** REJECT ** a test vulnerability where vendor rates it lower than NVD",
VulnStatus: "Rejected",
},
},
want: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := vulnerability.New(nil).IsRejected(tc.details)
assert.Equal(t, tc.want, got)
})
}
}
func TestNormalize(t *testing.T) {
testCases := []struct {
name string
vulnID string
details map[types.SourceID]types.VulnerabilityDetail
want types.Vulnerability
}{
{
name: "happy path",
vulnID: "CVE-2020-1234",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.NVD: {
CvssScore: 4.2,
CvssVector: "AV:N/AC:M/Au:N/C:N/I:P/A:N",
CvssScoreV3: 5.6,
CvssVectorV3: "CVSS:3.0/AV:A/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
SeverityV3: types.SeverityMedium,
CweIDs: []string{"CWE-125", "CWE-200"},
LastModifiedDate: utils.MustTimeParse("2020-01-01T01:02:03Z"),
PublishedDate: utils.MustTimeParse("2001-01-01T01:02:03Z"),
},
vulnerability.RedHat: {
CvssScoreV3: 6.7,
CvssVectorV3: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
SeverityV3: types.SeverityHigh,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
References: []string{"http://foo-bar.com/baz"},
},
},
want: types.Vulnerability{
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
Severity: types.SeverityMedium.String(),
VendorSeverity: types.VendorSeverity{
vulnerability.NVD: types.SeverityMedium,
vulnerability.RedHat: types.SeverityHigh,
},
CVSS: types.VendorCVSS{
vulnerability.NVD: types.CVSS{
V2Vector: "AV:N/AC:M/Au:N/C:N/I:P/A:N",
V3Vector: "CVSS:3.0/AV:A/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
V2Score: 4.2,
V3Score: 5.6,
},
vulnerability.RedHat: types.CVSS{
V2Vector: "",
V3Vector: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
V2Score: 0,
V3Score: 6.7,
},
},
CweIDs: []string{"CWE-125", "CWE-200"},
References: []string{"http://foo-bar.com/baz"},
LastModifiedDate: utils.MustTimeParse("2020-01-01T01:02:03Z"),
PublishedDate: utils.MustTimeParse("2001-01-01T01:02:03Z"),
},
},
{
name: "happy path, classifications for redhat, ubuntu and nodejs with variety of scores and vectors",
vulnID: "CVE-2020-1234",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.RedHat: {
ID: "CVE-2020-1234",
CvssScore: 4.2,
CvssVector: "AV:N/AC:M/Au:N/C:N/I:P/A:N",
CvssScoreV3: 5.6,
CvssVectorV3: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
SeverityV3: types.SeverityCritical,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
References: []string{"http://foo-bar.com/baz"},
},
vulnerability.Ubuntu: {
ID: "CVE-2020-1234",
CvssScoreV3: 3.4,
CvssVectorV3: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
Severity: types.SeverityLow,
SeverityV3: types.SeverityMedium,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
vulnerability.NodejsSecurityWg: {
ID: "CVE-2020-1234",
CvssScore: -1,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
},
want: types.Vulnerability{
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
Severity: types.SeverityMedium.String(), // from Red Hat
VendorSeverity: types.VendorSeverity{
vulnerability.RedHat: types.SeverityCritical,
vulnerability.Ubuntu: types.SeverityMedium,
},
CVSS: types.VendorCVSS{
vulnerability.RedHat: types.CVSS{
V2Vector: "AV:N/AC:M/Au:N/C:N/I:P/A:N",
V3Vector: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
V2Score: 4.2,
V3Score: 5.6,
},
vulnerability.Ubuntu: types.CVSS{
V3Vector: "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
V3Score: 3.4,
},
},
References: []string{"http://foo-bar.com/baz"},
},
},
{
name: "happy path, classifications for ubuntu and nodejs with variety vectors but no scores",
vulnID: "CVE-2020-1234",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.Ubuntu: {
ID: "CVE-2020-1234",
Severity: types.SeverityLow,
SeverityV3: types.SeverityMedium,
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
vulnerability.NodejsSecurityWg: {
ID: "CVE-2020-1234",
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
},
want: types.Vulnerability{
Severity: types.SeverityMedium.String(),
VendorSeverity: types.VendorSeverity{
vulnerability.Ubuntu: types.SeverityMedium,
},
CVSS: types.VendorCVSS{},
Title: "test vulnerability",
Description: "a test vulnerability where vendor rates it lower than NVD",
},
},
{
name: "happy path, nvd CVSS v4.0",
vulnID: "CVE-2020-1234",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.NVD: {
Description: "a test vulnerability for NVD CVSS v4.0",
CvssScoreV3: 9.8,
CvssVectorV3: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
CvssScoreV40: 6.9,
CvssVectorV40: "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:L/VI:L/VA:L/SC:N/SI:N/SA:N/E:X/CR:X/IR:X/AR:X/MAV:X/MAC:X/MAT:X/MPR:X/MUI:X/MVC:X/MVI:X/MVA:X/MSC:X/MSI:X/MSA:X/S:X/AU:X/R:X/V:X/RE:X/U:X",
SeverityV3: types.SeverityCritical,
SeverityV40: types.SeverityMedium,
CweIDs: []string{"CWE-287"},
References: []string{
"https://github.com/GTA12138/vul/blob/main/clash%20for%20windows.md",
"https://vuldb.com/?ctiid.267406", "https://vuldb.com/?id.267406",
"https://vuldb.com/?submit.345469",
},
LastModifiedDate: utils.MustTimeParse("2024-06-11T17:57:13.767Z"),
PublishedDate: utils.MustTimeParse("2024-06-07T10:15:12.293Z"),
},
},
want: types.Vulnerability{
Description: "a test vulnerability for NVD CVSS v4.0",
Severity: types.SeverityMedium.String(),
VendorSeverity: types.VendorSeverity{
vulnerability.NVD: types.SeverityMedium,
},
CVSS: types.VendorCVSS{
vulnerability.NVD: types.CVSS{
V3Vector: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
V40Vector: "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:L/VI:L/VA:L/SC:N/SI:N/SA:N/E:X/CR:X/IR:X/AR:X/MAV:X/MAC:X/MAT:X/MPR:X/MUI:X/MVC:X/MVI:X/MVA:X/MSC:X/MSI:X/MSA:X/S:X/AU:X/R:X/V:X/RE:X/U:X",
V3Score: 9.8,
V40Score: 6.9,
},
},
CweIDs: []string{"CWE-287"},
References: []string{
"https://github.com/GTA12138/vul/blob/main/clash%20for%20windows.md",
"https://vuldb.com/?ctiid.267406", "https://vuldb.com/?id.267406",
"https://vuldb.com/?submit.345469",
},
LastModifiedDate: utils.MustTimeParse("2024-06-11T17:57:13.767Z"),
PublishedDate: utils.MustTimeParse("2024-06-07T10:15:12.293Z"),
},
},
{
name: "happy path, both nvd and ghsa have published & modified date information and nvd is preferred",
vulnID: "CVE-2020-1234",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.NVD: {
LastModifiedDate: utils.MustTimeParse("2024-06-11T17:57:13.767Z"),
PublishedDate: utils.MustTimeParse("2024-06-07T10:15:12.293Z"),
},
vulnerability.GHSA: {
LastModifiedDate: utils.MustTimeParse("2023-06-11T17:57:13.767Z"),
PublishedDate: utils.MustTimeParse("2023-06-07T10:15:12.293Z"),
},
},
want: types.Vulnerability{
LastModifiedDate: utils.MustTimeParse("2024-06-11T17:57:13.767Z"),
PublishedDate: utils.MustTimeParse("2024-06-07T10:15:12.293Z"),
Severity: types.SeverityUnknown.String(),
VendorSeverity: types.VendorSeverity{},
CVSS: types.VendorCVSS{},
},
},
{
name: "happy path, only ghsa has published & modified date information and ghsa is preferred",
vulnID: "GHSA-1234-5678-0000",
details: map[types.SourceID]types.VulnerabilityDetail{
vulnerability.NVD: {},
vulnerability.GHSA: {
LastModifiedDate: utils.MustTimeParse("2023-06-11T17:57:13.767Z"),
PublishedDate: utils.MustTimeParse("2023-06-07T10:15:12.293Z"),
},
},
want: types.Vulnerability{
LastModifiedDate: utils.MustTimeParse("2023-06-11T17:57:13.767Z"),
PublishedDate: utils.MustTimeParse("2023-06-07T10:15:12.293Z"),
Severity: types.SeverityUnknown.String(),
VendorSeverity: types.VendorSeverity{},
CVSS: types.VendorCVSS{},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := vulnerability.New(nil).Normalize(tc.vulnID, tc.details)
assert.Equal(t, tc.want, got)
})
}
}