-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathvulnerabilities.test.mjs
More file actions
152 lines (121 loc) · 4.09 KB
/
vulnerabilities.test.mjs
File metadata and controls
152 lines (121 loc) · 4.09 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
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import generateVulnerabilityData from '#site/next-data/generators/vulnerabilities.mjs';
const MOCK_VULNERABILITIES = {
1: {
cve: ['CVE-2017-1000381'],
vulnerable: '8.x || 7.x || 4.x || 6.x || 5.x',
},
8: {
cve: ['CVE-2016-5180'],
vulnerable: '0.10.x || 0.12.x || 4.x',
},
11: {
cve: [],
vulnerable: '6.x',
},
24: {
cve: ['CVE-2016-1669'],
vulnerable: '>=6.0.0 <6.2.0 || 5.x || 4.x',
},
54: {
cve: ['CVE-2018-12115'],
vulnerable: '<= 10',
},
};
// Note: We mock fetch to return this object shape in tests
describe('generateVulnerabilityData', () => {
it('returns an empty object when source JSON is empty', async () => {
globalThis.fetch = async () => ({
json: async () => ({}),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(grouped, {});
});
it('ignores non-numeric values in the "vulnerable" string', async () => {
globalThis.fetch = async () => ({
json: async () => ({
a: { cve: ['CVE-2021-1234'], vulnerable: 'foo || bar || 12.x' },
b: { cve: ['CVE-2021-5678'], vulnerable: 'baz || 13.x' },
}),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), ['12', '13']);
});
it('can group a single version', async () => {
globalThis.fetch = async () => ({
json: async () => ({ a: { cve: ['CVE-2021-1234'], vulnerable: '12.x' } }),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), ['12']);
});
it('can group a 0.x version', async () => {
globalThis.fetch = async () => ({
json: async () => ({
a: { cve: ['CVE-2021-1234'], vulnerable: '0.10.x' },
}),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), ['0']);
});
it('can group two versions', async () => {
globalThis.fetch = async () => ({
json: async () => ({
a: { cve: ['CVE-2021-1234'], vulnerable: '12.x || 13.x' },
}),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), ['12', '13']);
});
it('returns the major when given a greater-than range', async () => {
globalThis.fetch = async () => ({
json: async () => ({
a: { cve: ['CVE-2021-5678'], vulnerable: '>=6.0.0 <6.2.0' },
}),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), ['6']);
});
it('returns a descending list of major versions when given a less-than range', async () => {
globalThis.fetch = async () => ({
json: async () => ({ a: { cve: ['CVE-2021-5678'], vulnerable: '< 5' } }),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), [
'0',
'1',
'2',
'3',
'4',
]);
});
it('treats <= as inclusive of the specified major only (based on current implementation)', async () => {
globalThis.fetch = async () => ({
json: async () => ({ a: { cve: ['CVE-2021-5678'], vulnerable: '<= 5' } }),
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), ['5']);
});
it('groups vulnerabilities by major version extracted from "vulnerable" string', async () => {
globalThis.fetch = async () => ({
json: async () => MOCK_VULNERABILITIES,
});
const grouped = await generateVulnerabilityData();
assert.deepEqual(Object.keys(grouped).sort(Number), [
'0',
'4',
'5',
'6',
'7',
'8',
'10',
]);
assert.strictEqual(grouped['0'].length, 2);
assert.strictEqual(grouped['4'].length, 3);
assert.strictEqual(grouped['5'].length, 2);
assert.strictEqual(grouped['6'].length, 3);
assert.strictEqual(grouped['7'].length, 1);
assert.strictEqual(grouped['8'].length, 1);
assert.strictEqual(grouped['10'].length, 1);
});
});