| 1 |
const t = require('tap') |
| 2 |
const localeCompare = require('@isaacs/string-locale-compare')('en') |
| 3 |
const AuditReport = require('../lib/audit-report.js') |
| 4 |
const Node = require('../lib/node.js') |
| 5 |
const Arborist = require('../') |
| 6 |
const MockRegistry = require('@npmcli/mock-registry') |
| 7 |
|
| 8 |
const { join, resolve } = require('node:path') |
| 9 |
const fixtures = resolve(__dirname, 'fixtures') |
| 10 |
|
| 11 |
const newArb = (path, opts = {}) => new Arborist({ path, ...opts }) |
| 12 |
|
| 13 |
const sortReport = report => { |
| 14 |
const entries = Object.entries(report.vulnerabilities) |
| 15 |
const vulns = entries.sort(([a], [b]) => localeCompare(a, b)) |
| 16 |
.map(([name, vuln]) => [ |
| 17 |
name, |
| 18 |
{ |
| 19 |
...vuln, |
| 20 |
via: (vuln.via || []).sort((a, b) => |
| 21 |
localeCompare(String(a.source || a), String(b.source || b))), |
| 22 |
effects: (vuln.effects || []).sort(localeCompare), |
| 23 |
}, |
| 24 |
]) |
| 25 |
report.vulnerabilities = vulns.reduce((set, [k, v]) => { |
| 26 |
set[k] = v |
| 27 |
return set |
| 28 |
}, {}) |
| 29 |
} |
| 30 |
|
| 31 |
const createRegistry = (t) => { |
| 32 |
const registry = new MockRegistry({ |
| 33 |
strict: true, |
| 34 |
tap: t, |
| 35 |
registry: 'https://registry.npmjs.org', |
| 36 |
}) |
| 37 |
return registry |
| 38 |
} |
| 39 |
|
| 40 |
t.test('all severity levels', async t => { |
| 41 |
const path = resolve(fixtures, 'audit-all-severities') |
| 42 |
const registry = createRegistry(t) |
| 43 |
registry.audit({ convert: true, results: require(resolve(path, 'audit.json')) }) |
| 44 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 45 |
const cache = t.testdir() |
| 46 |
const arb = newArb(path, { cache }) |
| 47 |
|
| 48 |
const tree = await arb.loadVirtual() |
| 49 |
const report = await AuditReport.load(tree, arb.options) |
| 50 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 51 |
t.equal(report.topVulns.size, 2) |
| 52 |
}) |
| 53 |
|
| 54 |
t.test('vulnerable dep not from registry', async t => { |
| 55 |
const path = resolve(fixtures, 'minimist-git-dep') |
| 56 |
const registry = createRegistry(t) |
| 57 |
registry.audit({ convert: true, results: require(resolve(path, 'audit.json')) }) |
| 58 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 59 |
const cache = t.testdir() |
| 60 |
const arb = newArb(path, { cache }) |
| 61 |
|
| 62 |
const tree = await arb.loadVirtual() |
| 63 |
const report = await AuditReport.load(tree, arb.options) |
| 64 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 65 |
t.equal(report.has('minimist'), true) |
| 66 |
t.equal(report.topVulns.has('minimist'), true) |
| 67 |
t.equal(report.isVulnerable(tree.children.get('minimist')), true) |
| 68 |
}) |
| 69 |
|
| 70 |
t.test('metavuln where dep is not a registry dep', async t => { |
| 71 |
const path = resolve(fixtures, 'minimist-git-metadep') |
| 72 |
const registry = createRegistry(t) |
| 73 |
registry.audit({ convert: true, results: require(resolve(path, 'audit.json')) }) |
| 74 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 75 |
const cache = t.testdir() |
| 76 |
const arb = newArb(path, { cache }) |
| 77 |
|
| 78 |
const tree = await arb.loadVirtual() |
| 79 |
const report = await AuditReport.load(tree, arb.options) |
| 80 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 81 |
|
| 82 |
t.equal(report.has('@isaacs/minimist-git-dep'), true) |
| 83 |
t.equal(report.has('minimist'), true) |
| 84 |
t.equal(report.topVulns.has('@isaacs/minimist-git-dep'), true) |
| 85 |
}) |
| 86 |
|
| 87 |
t.test('metavuln where a dep is not on the registry at all', async t => { |
| 88 |
const path = resolve(fixtures, 'audit-missing-packument') |
| 89 |
const registry = createRegistry(t) |
| 90 |
registry.audit({ convert: true, results: require(resolve(path, 'audit.json')) }) |
| 91 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 92 |
const cache = t.testdir() |
| 93 |
const arb = newArb(path, { cache }) |
| 94 |
|
| 95 |
const tree = await arb.loadVirtual() |
| 96 |
const report = await AuditReport.load(tree, arb.options) |
| 97 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 98 |
t.equal(report.topVulns.size, 1) |
| 99 |
}) |
| 100 |
|
| 101 |
t.test('get advisory about node not in tree', async t => { |
| 102 |
// this should never happen, but if it does, we're prepared for it |
| 103 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 104 |
const registry = createRegistry(t) |
| 105 |
registry.audit({ convert: true, results: require(resolve(path, 'audit.json')) }) |
| 106 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 107 |
const cache = t.testdir() |
| 108 |
const arb = newArb(path, { cache }) |
| 109 |
|
| 110 |
const tree = await arb.loadVirtual() |
| 111 |
tree.children.get('mkdirp').parent = null |
| 112 |
tree.children.get('nyc').parent = null |
| 113 |
tree.children.get('minimist').parent = null |
| 114 |
new Node({ |
| 115 |
parent: tree, |
| 116 |
path: resolve(path, 'node_modules/fooo'), |
| 117 |
pkg: { name: 'fooo', version: '1.2.3' }, |
| 118 |
}) |
| 119 |
tree.package = { dependencies: { |
| 120 |
fooo: '', |
| 121 |
} } |
| 122 |
|
| 123 |
const report = await AuditReport.load(tree, arb.options) |
| 124 |
t.equal(report.topVulns.size, 0, 'one top node found vulnerable') |
| 125 |
t.equal(report.size, 0, 'no vulns that were relevant') |
| 126 |
t.equal(report.get('nyc'), undefined) |
| 127 |
t.equal(report.get('mkdirp'), undefined) |
| 128 |
}) |
| 129 |
|
| 130 |
t.test('unfixable, but not a semver major forced fix', async t => { |
| 131 |
const path = resolve(fixtures, 'mkdirp-pinned') |
| 132 |
const registry = createRegistry(t) |
| 133 |
registry.audit({ convert: true, results: require(resolve(fixtures, 'audit-nyc-mkdirp', 'audit.json')) }) |
| 134 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 135 |
const cache = t.testdir() |
| 136 |
const arb = newArb(path, { cache }) |
| 137 |
|
| 138 |
const tree = await arb.loadVirtual() |
| 139 |
const report = await AuditReport.load(tree, arb.options) |
| 140 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 141 |
|
| 142 |
t.equal(report.topVulns.size, 1) |
| 143 |
}) |
| 144 |
|
| 145 |
t.test('audit outdated nyc and mkdirp', async t => { |
| 146 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 147 |
const registry = createRegistry(t) |
| 148 |
registry.audit({ results: require(resolve(path, 'advisory-bulk.json')) }) |
| 149 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 150 |
const cache = t.testdir() |
| 151 |
const arb = newArb(path, { cache }) |
| 152 |
|
| 153 |
const tree = await arb.loadVirtual() |
| 154 |
const report = await AuditReport.load(tree, arb.options) |
| 155 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 156 |
|
| 157 |
t.throws(() => report.set('foo', 'bar'), { |
| 158 |
message: 'do not call AuditReport.set() directly', |
| 159 |
}) |
| 160 |
|
| 161 |
t.equal(report.topVulns.size, 1, 'one top node found vulnerable') |
| 162 |
t.equal(report.get('nyc').simpleRange, '6.2.0-alpha - 13.1.0') |
| 163 |
t.equal(report.get('mkdirp').simpleRange, '0.4.1 - 0.5.1') |
| 164 |
}) |
| 165 |
|
| 166 |
t.test('audit outdated nyc and mkdirp with before: option', async t => { |
| 167 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 168 |
const registry = createRegistry(t) |
| 169 |
registry.audit({ results: require(resolve(path, 'advisory-bulk.json')) }) |
| 170 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 171 |
|
| 172 |
const cache = t.testdir() |
| 173 |
const arb = newArb(path, { before: new Date('2020-01-01'), cache }) |
| 174 |
|
| 175 |
const tree = await arb.loadVirtual() |
| 176 |
const report = await AuditReport.load(tree, arb.options) |
| 177 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 178 |
|
| 179 |
t.equal(report.topVulns.size, 1, 'one top node found vulnerable') |
| 180 |
t.equal(report.get('nyc').simpleRange, '6.2.0-alpha - 13.1.0') |
| 181 |
t.equal(report.get('mkdirp').simpleRange, '0.4.1 - 0.5.1') |
| 182 |
}) |
| 183 |
|
| 184 |
t.test('audit returns an error', async t => { |
| 185 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 186 |
const registry = createRegistry(t) |
| 187 |
registry.audit({ responseCode: 503, results: 'no audit for you' }) |
| 188 |
|
| 189 |
const logs = [] |
| 190 |
const onlog = (...msg) => { |
| 191 |
if (msg[0] === 'http') { |
| 192 |
return |
| 193 |
} |
| 194 |
logs.push(msg) |
| 195 |
} |
| 196 |
process.on('log', onlog) |
| 197 |
t.teardown(() => process.removeListener('log', onlog)) |
| 198 |
|
| 199 |
const cache = t.testdir() |
| 200 |
const arb = newArb(path, { cache }) |
| 201 |
|
| 202 |
const tree = await arb.loadVirtual() |
| 203 |
const report = await AuditReport.load(tree, arb.options) |
| 204 |
t.equal(report.report, null, 'did not get audit response') |
| 205 |
t.equal(report.size, 0, 'did not find any vulnerabilities') |
| 206 |
t.match(logs.filter(l => l[1].includes('audit')), [ |
| 207 |
[ |
| 208 |
'silly', |
| 209 |
'audit', |
| 210 |
'bulk request', |
| 211 |
], |
| 212 |
[ |
| 213 |
'verbose', |
| 214 |
'audit error', |
| 215 |
report.error, |
| 216 |
], |
| 217 |
['silly', 'audit error', 'no audit for you'], |
| 218 |
], 'logged audit failure') |
| 219 |
t.match(report.error, Error) |
| 220 |
}) |
| 221 |
|
| 222 |
t.test('audit disabled by config', async t => { |
| 223 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 224 |
createRegistry(t) |
| 225 |
|
| 226 |
const logs = [] |
| 227 |
const onlog = (...msg) => logs.push(msg) |
| 228 |
process.on('log', onlog) |
| 229 |
t.teardown(() => process.removeListener('log', onlog)) |
| 230 |
|
| 231 |
const cache = t.testdir() |
| 232 |
const arb = newArb(path, { audit: false, cache }) |
| 233 |
|
| 234 |
const tree = await arb.loadVirtual() |
| 235 |
const report = await AuditReport.load(tree, arb.options) |
| 236 |
t.equal(report.report, null, 'did not get audit response') |
| 237 |
t.equal(report.size, 0, 'did not find any vulnerabilities') |
| 238 |
t.match(logs, [], 'no logs of error') |
| 239 |
t.equal(report.error, null, 'no error encountered') |
| 240 |
}) |
| 241 |
|
| 242 |
t.test('audit disabled by offline mode', async t => { |
| 243 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 244 |
createRegistry(t) |
| 245 |
|
| 246 |
const logs = [] |
| 247 |
const onlog = (...msg) => logs.push(msg) |
| 248 |
process.on('log', onlog) |
| 249 |
t.teardown(() => process.removeListener('log', onlog)) |
| 250 |
|
| 251 |
const cache = t.testdir() |
| 252 |
const arb = newArb(path, { offline: true, cache }) |
| 253 |
|
| 254 |
const tree = await arb.loadVirtual() |
| 255 |
const report = await AuditReport.load(tree, arb.options) |
| 256 |
t.equal(report.report, null, 'did not get audit response') |
| 257 |
t.equal(report.size, 0, 'did not find any vulnerabilities') |
| 258 |
t.match(logs, [], 'no logs of error') |
| 259 |
t.equal(report.error, null, 'no error encountered') |
| 260 |
}) |
| 261 |
|
| 262 |
t.test('one vulnerability', async t => { |
| 263 |
const path = resolve(fixtures, 'audit-one-vuln') |
| 264 |
const registry = createRegistry(t) |
| 265 |
registry.audit({ convert: true, results: require(resolve(path, 'audit.json')) }) |
| 266 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 267 |
const cache = t.testdir() |
| 268 |
const arb = newArb(path, { cache }) |
| 269 |
|
| 270 |
const tree = await arb.loadVirtual() |
| 271 |
const report = await AuditReport.load(tree, arb.options) |
| 272 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 273 |
|
| 274 |
t.equal(report.topVulns.size, 0) |
| 275 |
}) |
| 276 |
|
| 277 |
t.test('a dep vuln that also has its own advisory against it', async t => { |
| 278 |
const path = resolve(fixtures, 'audit-dep-vuln-with-own-advisory') |
| 279 |
const registry = createRegistry(t) |
| 280 |
registry.audit({ convert: true, results: require(resolve(path, 'audit.json')) }) |
| 281 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 282 |
const cache = t.testdir() |
| 283 |
const arb = newArb(path, { cache }) |
| 284 |
|
| 285 |
const tree = await arb.loadVirtual() |
| 286 |
const report = await AuditReport.load(tree, arb.options) |
| 287 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 288 |
|
| 289 |
t.equal(report.topVulns.size, 0) |
| 290 |
}) |
| 291 |
|
| 292 |
t.test('get default opts when loaded without opts', async t => { |
| 293 |
const ar = new AuditReport() |
| 294 |
t.equal(ar.tree, undefined) |
| 295 |
t.strictSame(ar.options, {}) |
| 296 |
}) |
| 297 |
|
| 298 |
t.test('audit report with a lying v5 lockfile', async t => { |
| 299 |
// npm v5 stored the resolved dependency version in the `requires` |
| 300 |
// set, rather than the spec that is actually required. As a result, |
| 301 |
// a dep may _appear_ to be a metavuln, but when we scan the |
| 302 |
// packument, it turns out that it matches no nodes, and gets deleted. |
| 303 |
const path = resolve(fixtures, 'eslintme') |
| 304 |
const registry = createRegistry(t) |
| 305 |
registry.audit({ results: require(resolve(path, 'audit.json')) }) |
| 306 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 307 |
const cache = t.testdir() |
| 308 |
const arb = newArb(path, { cache }) |
| 309 |
const tree = await arb.loadVirtual() |
| 310 |
const report = await AuditReport.load(tree, arb.options) |
| 311 |
// also try to delete something that just very much is not present |
| 312 |
report.delete('eslint') |
| 313 |
report.delete('eslint') |
| 314 |
t.matchSnapshot(report.toJSON()) |
| 315 |
}) |
| 316 |
|
| 317 |
t.test('omit options', async t => { |
| 318 |
const path = resolve(fixtures, 'audit-omit') |
| 319 |
const omits = [ |
| 320 |
[], |
| 321 |
['dev'], |
| 322 |
['optional'], |
| 323 |
['dev', 'optional'], |
| 324 |
['peer'], |
| 325 |
['peer', 'dev'], |
| 326 |
['peer', 'dev', 'optional'], // empty |
| 327 |
] |
| 328 |
for (const omit of omits) { |
| 329 |
await t.test(`omit=[${omit.join(',')}]`, async t => { |
| 330 |
const cache = t.testdir() |
| 331 |
const arb = newArb(path, { cache }) |
| 332 |
const tree = await arb.loadVirtual() |
| 333 |
const registry = createRegistry(t) |
| 334 |
const s = omit.map(o => `-omit${o}`).join('') |
| 335 |
const bulkResults = require(resolve(path, `bulk${s}.json`)) |
| 336 |
if (Object.keys(bulkResults).length) { /// peer, dev, optional is empty |
| 337 |
registry.audit({ convert: false, results: bulkResults }) |
| 338 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 339 |
} |
| 340 |
const r1 = (await AuditReport.load(tree, { ...arb.options, omit })).toJSON() |
| 341 |
sortReport(r1) |
| 342 |
t.matchSnapshot(r1, 'bulk') |
| 343 |
const r2 = (await AuditReport.load(tree, { ...arb.options, omit })).toJSON() |
| 344 |
sortReport(r2) |
| 345 |
t.strictSame(r1, r2, 'same results') |
| 346 |
}) |
| 347 |
} |
| 348 |
}) |
| 349 |
|
| 350 |
t.test('audit when tree is empty', async t => { |
| 351 |
createRegistry(t) |
| 352 |
const tree = new Node({ |
| 353 |
path: '/path/to/tree', |
| 354 |
}) |
| 355 |
const auditReport = new AuditReport(tree) |
| 356 |
const { report } = await auditReport.run() |
| 357 |
t.strictSame(report, null) |
| 358 |
}) |
| 359 |
|
| 360 |
t.test('audit when bulk report does not have anything in it', async t => { |
| 361 |
createRegistry(t) |
| 362 |
const tree = new Node({ |
| 363 |
path: '/path/to/tree', |
| 364 |
pkg: { |
| 365 |
name: 'tree', |
| 366 |
version: '1.2.3', |
| 367 |
devDependencies: { something: '1.2.3' }, |
| 368 |
}, |
| 369 |
children: [ |
| 370 |
{ pkg: { name: 'something', version: '1.2.3' } }, |
| 371 |
], |
| 372 |
}) |
| 373 |
const auditReport = new AuditReport(tree, { omit: ['dev'] }) |
| 374 |
const { report } = await auditReport.run() |
| 375 |
t.strictSame(report, null) |
| 376 |
}) |
| 377 |
|
| 378 |
t.test('audit supports alias deps', async t => { |
| 379 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 380 |
const registry = createRegistry(t) |
| 381 |
registry.audit({ results: require(resolve(path, 'advisory-bulk.json')) }) |
| 382 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 383 |
const cache = t.testdir() |
| 384 |
const arb = newArb(path, { cache }) |
| 385 |
const tree = new Node({ |
| 386 |
path, |
| 387 |
pkg: { |
| 388 |
name: 'mkdirp', |
| 389 |
version: '0.5.0', |
| 390 |
dependencies: { |
| 391 |
novulnshereiswear: 'npm:mkdirp@*', |
| 392 |
mkdirp: 'npm:[email protected]', |
| 393 |
}, |
| 394 |
}, |
| 395 |
children: [ |
| 396 |
{ |
| 397 |
name: 'novulnshereiswear', |
| 398 |
pkg: { |
| 399 |
name: 'mkdirp', |
| 400 |
version: '0.5.1', |
| 401 |
dependencies: { |
| 402 |
minimist: '0.0.8', |
| 403 |
}, |
| 404 |
}, |
| 405 |
}, |
| 406 |
{ |
| 407 |
pkg: { |
| 408 |
name: 'mkdirp', |
| 409 |
version: '0.5.1', |
| 410 |
dependencies: { |
| 411 |
minimist: '0.0.8', |
| 412 |
}, |
| 413 |
}, |
| 414 |
}, |
| 415 |
{ pkg: { name: 'minimist', version: '0.0.8' } }, |
| 416 |
], |
| 417 |
}) |
| 418 |
|
| 419 |
const report = await AuditReport.load(tree, arb.options) |
| 420 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 421 |
t.equal(report.get('mkdirp').simpleRange, '0.4.1 - 0.5.1') |
| 422 |
}) |
| 423 |
|
| 424 |
t.test('linked local package should not be audited against the registry', async t => { |
| 425 |
const path = resolve(fixtures, 'audit-linked-package') |
| 426 |
// No registry.audit() mock needed — no request should be made |
| 427 |
// because linked packages must be excluded from the bulk payload |
| 428 |
createRegistry(t) |
| 429 |
const cache = t.testdir() |
| 430 |
const arb = newArb(path, { cache }) |
| 431 |
|
| 432 |
const tree = await arb.loadVirtual() |
| 433 |
const report = await AuditReport.load(tree, arb.options) |
| 434 |
|
| 435 |
t.equal( |
| 436 |
report.has('electron-test-app'), |
| 437 |
false, |
| 438 |
'linked local package should not appear in audit report' |
| 439 |
) |
| 440 |
t.equal( |
| 441 |
report.size, |
| 442 |
0, |
| 443 |
'audit report should be empty when all dependencies are local links' |
| 444 |
) |
| 445 |
}) |
| 446 |
|
| 447 |
t.test('audit with filterSet limiting to only mkdirp and minimist', async t => { |
| 448 |
const path = resolve(fixtures, 'audit-nyc-mkdirp') |
| 449 |
const registry = createRegistry(t) |
| 450 |
registry.audit({ results: require(resolve(path, 'advisory-bulk.json')) }) |
| 451 |
registry.mocks({ dir: join(__dirname, 'fixtures') }) |
| 452 |
const cache = t.testdir() |
| 453 |
const arb = newArb(path, { cache }) |
| 454 |
|
| 455 |
const tree = await arb.loadVirtual() |
| 456 |
const filterSet = new Set([ |
| 457 |
tree.children.get('mkdirp'), |
| 458 |
tree.children.get('minimist'), |
| 459 |
]) |
| 460 |
const options = { ...arb.options, filterSet } |
| 461 |
const report = await AuditReport.load(tree, options) |
| 462 |
t.matchSnapshot(JSON.stringify(report, 0, 2), 'json version') |
| 463 |
|
| 464 |
t.equal(report.topVulns.size, 0, 'no top nodes reported') |
| 465 |
t.equal(report.get('nyc'), undefined, 'no nyc vuln reported') |
| 466 |
t.equal(report.get('mkdirp').simpleRange, '0.4.1 - 0.5.1', 'mkdirp vuln reported') |
| 467 |
}) |