Debian Codegraph @

workspaces/arborist/test/vuln.js

raw
1 const t = require('tap')
2 const Vuln = require('../lib/vuln.js')
3 const Node = require('../lib/node.js')
4 const Link = require('../lib/link.js')
5 const Edge = require('../lib/edge.js')
6 const { resolve } = require('node:path')
7
8 const semver = require('semver')
9 const semverOpt = { includePrerelease: true, loose: true }
10 class MockAdvisory {
11 constructor (obj) {
12 Object.assign(this, obj)
13 this.vulnerableVersions = this.versions.filter(v =>
14 semver.satisfies(v, this.range, semverOpt))
15 }
16
17 testVersion (v) {
18 return this.vulnerableVersions.includes(v)
19 }
20 }
21
22 t.test('basic vulnerability object tests', async t => {
23 const crit = new MockAdvisory({
24 type: 'advisory',
25 source: 420,
26 id: 'cafebad',
27 title: 'borgsafalamash',
28 name: 'name',
29 dependency: 'name',
30 severity: 'critical',
31 range: '1.x < 1.3',
32 versions: [
33 '0.0.1',
34 '0.0.2',
35 '0.0.3',
36 '0.1.0',
37 '1.0.0',
38 '1.0.1',
39 '1.1.0',
40 '1.2.0',
41 '1.2.1',
42 '2.0.0',
43 '2.0.1',
44 '2.1.0',
45 '2.2.0',
46 '2.2.0-pre.0',
47 '3.0.0-pre.0',
48 '3.0.0-pre.1',
49 '3.0.0',
50 '3.0.1',
51 '3.1.0',
52 '3.2.0',
53 ],
54 })
55
56 const low = new MockAdvisory({
57 type: 'advisory',
58 source: 69,
59 id: 'deadbeef',
60 title: 'flerbygurrf',
61 name: 'name',
62 dependency: 'name',
63 severity: 'low',
64 range: '2.x < 2.3.2 || 3.x <3.0.1',
65 versions: [
66 '0.0.1',
67 '0.0.2',
68 '0.0.3',
69 '0.1.0',
70 '1.0.0',
71 '1.0.1',
72 '1.1.0',
73 '1.2.0',
74 '1.2.1',
75 '2.0.0',
76 '2.0.1',
77 '2.1.0',
78 '2.2.0',
79 '2.2.0-pre.0',
80 '3.0.0-pre.0',
81 '3.0.0-pre.1',
82 '3.0.0',
83 '3.0.1',
84 '3.1.0',
85 '3.2.0',
86 ],
87 })
88
89 const v = new Vuln({ name: 'name', advisory: crit })
90 t.type(v, Vuln)
91 t.equal(v.testSpec('github:foo/bar'), true)
92 t.equal(v.testSpec('0.x'), false)
93 t.equal(v.testSpec('>4.x'), true)
94 t.equal(v.testSpec('npm:name@>4.x'), true)
95 t.strictSame([...v.advisories], [crit])
96 t.equal(v.severity, 'critical')
97 v.addAdvisory(low)
98 t.equal(v.testSpec('2.x'), true)
99 t.equal(v.severity, 'critical')
100 t.match(v.advisories, new Set([crit, low]))
101 v.deleteAdvisory(crit)
102 t.equal(v.severity, 'low')
103 t.match(v.advisories, new Set([low]))
104 v.addAdvisory(crit)
105 t.equal(v.severity, 'critical')
106 t.match(v.advisories, new Set([crit, low]))
107 t.matchSnapshot(JSON.stringify(v, 0, 2), 'json formatted')
108
109 const meta = new MockAdvisory({
110 type: 'metavuln',
111 source: 'deadbeef',
112 severity: 'low',
113 name: 'another',
114 dependency: 'name',
115 range: '1.x',
116 versions: [
117 '0.0.0',
118 '1.0.0',
119 '1.0.1',
120 '1.0.2',
121 '2.0.0',
122 '2.0.1',
123 '3.0.0',
124 ],
125 })
126
127 const meta2 = new MockAdvisory({
128 type: 'metavuln',
129 source: 'cafebad',
130 severity: 'critical',
131 name: 'another',
132 dependency: 'name',
133 range: '>1.0.0 <=2.0.1',
134 versions: [
135 '0.0.0',
136 '1.0.0',
137 '1.0.1',
138 '2.0.0',
139 '2.0.1',
140 '3.0.0',
141 ],
142 })
143
144 const v2 = new Vuln({ name: 'another', advisory: meta })
145 v2.addVia(v)
146 t.matchSnapshot(JSON.stringify(v), 'json after adding effect')
147 t.match(v2.advisories, new Set([meta]))
148 t.equal(v2.range, meta.range)
149
150 v2.addAdvisory(meta2)
151 t.equal(v2.range, [meta.range, meta2.range].join(' || '))
152
153 // when the simple range is loaded, it memoizes and sets range as well
154 t.equal(v2.simpleRange, '1.0.0 - 2.0.1')
155 t.equal(v2.range, v2.simpleRange)
156
157 const root = new Node({
158 path: '/path/to',
159 pkg: {
160 dependencies: { thing: '1.2.1' },
161 workspaces: [
162 'packages/foo',
163 ],
164 },
165 })
166
167 // a workspace with one direct vuln and one indirect vuln
168 const ws = new Node({
169 pkg: { name: 'foo', version: '1.2.3', dependencies: { bar: '' } },
170 path: resolve(root.path, 'packages/foo'),
171 root,
172 children: [
173 { pkg: { name: 'bar', version: '1.2.3', dependencies: { baz: '' } } },
174 { pkg: { name: 'baz', version: '1.2.3' } },
175 ],
176 })
177
178 // manually connect the workspace
179 new Link({
180 name: 'foo',
181 parent: root,
182 target: ws,
183 })
184 new Edge({
185 type: 'workspace',
186 name: 'foo',
187 spec: 'file:packages/foo',
188 from: root,
189 })
190
191 const directWsVuln = new Vuln({
192 name: 'bar',
193 advisory: new MockAdvisory({
194 name: 'bar',
195 versions: ['1.2.2', '1.2.3', '1.2.4'],
196 range: '<=1.2.3',
197 }),
198 })
199 const indirectWsVuln = new Vuln({
200 name: 'baz',
201 advisory: new MockAdvisory({
202 name: 'baz',
203 versions: ['1.2.2', '1.2.3', '1.2.4'],
204 range: '<=1.2.3',
205 }),
206 })
207 // workspace dep vulns are direct vulnerabilities as well
208 t.equal(directWsVuln.isVulnerable(ws.children.get('bar')), true)
209 t.equal(directWsVuln.isDirect, true)
210 t.equal(indirectWsVuln.isVulnerable(ws.children.get('baz')), true)
211 t.equal(indirectWsVuln.isDirect, false)
212
213 const node = new Node({
214 path: '/path/to/node_modules/thing',
215 name: 'thing',
216 pkg: {
217 name: 'name',
218 version: '1.2.1',
219 },
220 parent: root,
221 })
222
223 // check twice to hit memoizing code path
224 t.equal(v.isVulnerable(node), true)
225 t.equal(v.isVulnerable(node), true)
226 t.equal(v.isDirect, true)
227 t.match(v.nodes, new Set([node]))
228
229 // make sure we don't infinitely loop when setting it to true
230 v.addVia(v2)
231 v2.fixAvailable = true
232 v.fixAvailable = true
233 v2.fixAvailable = true
234 v.deleteVia(v2)
235
236 v2.fixAvailable = { isSemVerMajor: false }
237 t.strictSame(v.fixAvailable, { isSemVerMajor: false })
238 v2.fixAvailable = true
239 t.strictSame(v.fixAvailable, { isSemVerMajor: false })
240 v2.fixAvailable = { isSemVerMajor: true }
241 t.strictSame(v.fixAvailable, { isSemVerMajor: true })
242 v2.fixAvailable = false
243 t.strictSame(v.fixAvailable, false)
244
245 t.matchSnapshot(JSON.stringify(v, 0, 2), 'json formatted after loading')
246 t.matchSnapshot(JSON.stringify(v2, 0, 2), 'json formatted metavuln')
247
248 const ok = new Node({
249 path: '/path/to/node_modules/thing',
250 pkg: {
251 name: 'name',
252 version: '3.2.0',
253 },
254 })
255 t.match(v.isVulnerable(ok), false)
256
257 const noVersion = new Node({
258 path: '/path/to/node_modules/thing',
259 pkg: {},
260 })
261 t.match(v.isVulnerable(noVersion), false, 'node without version, no opinion')
262
263 v2.deleteAdvisory(meta2)
264 v2.deleteAdvisory(meta)
265 t.strictSame([...v2.via], [], 'removing advisories removes via')
266 t.strictSame([...v.effects], [], 'removing via removes effect')
267 })