Skip to content

Commit 690bf17

Browse files
authored
fix(arborist): fix audit-report determinism due to dropped via links (#9603)
**Description:** Fixes #8989 **Bug:** `npm audit` was occasionally generating non-deterministic output with missing vulnerabilities when metavulns from identical-range dependencies caused a collision. **Root Cause:** In `AuditReport#init`, the deduplication logic (`if (!seen.has(k))`) was aggressively dropping identically-computed metavuln advisory ranges across different dependency paths. Because `vuln.addVia()` was invoked *inside* this block, a duplicated range collision would simply skip the execution and permanently drop the `via` link (and its corresponding `effects` link). Consequently, depending on the random `Promise.all` resolution order of the metavuln calculator across network calls, different `via` branches were being non-deterministically truncated. **Fix:** This PR extracts the `addVia` operation from the `seen` deduplication block and implements a comprehensive post-loop reconciliation pass. We ensure that: 1. `via` and `effects` links are deterministically established *after* all dependencies and metavulns are cleanly instantiated. 2. The reconciliation ignores already deleted advisories, guaranteeing that the cleanup lifecycle (`deleteAdvisory`) cannot be accidentally resurrected. 3. Because we aren't repeatedly calling the `fixAvailable` setter during the primary loop iterations, performance overhead remains minimized. **Testing:** - Added a new strict programmatic determinism test in `audit-report.js` covering the identical range metavuln fallback (`*`) condition. - Confirmed the new test correctly reproduces the regression (fails deterministically on unfixed code by dropping the `via` path). - All 27 existing suite tests pass flawlessly.
1 parent 0c4dd41 commit 690bf17

2 files changed

Lines changed: 80 additions & 5 deletions

File tree

‎workspaces/arborist/lib/audit-report.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,6 @@ class AuditReport extends Map {
152152
continue
153153
}
154154

155-
// we will have loaded the source already if this is a metavuln
156-
if (advisory.type === 'metavuln') {
157-
vuln.addVia(this.get(advisory.dependency))
158-
}
159-
160155
// already marked this one, no need to do it again
161156
if (vuln.nodes.has(node)) {
162157
continue
@@ -219,6 +214,17 @@ class AuditReport extends Map {
219214
}
220215
}
221216

217+
// post-loop reconciliation: establish all via links
218+
// done here to ensure all vulns exist, avoid redundant setter triggers,
219+
// and correctly handle multiple paths and omission cleanups.
220+
for (const vuln of this.values()) {
221+
for (const advisory of vuln.advisories) {
222+
if (advisory.type === 'metavuln') {
223+
vuln.addVia(this.get(advisory.dependency))
224+
}
225+
}
226+
}
227+
222228
timeEnd()
223229
}
224230

‎workspaces/arborist/test/audit-report.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,72 @@ t.test('audit with filterSet limiting to only mkdirp and minimist', async t => {
567567
t.equal(report.get('nyc'), undefined, 'no nyc vuln reported')
568568
t.equal(report.get('mkdirp').simpleRange, '0.4.1 - 0.5.1', 'mkdirp vuln reported')
569569
})
570+
571+
t.test('determinism: multiple metavulns with identical range but different dependencies', async t => {
572+
const registry = createRegistry(t)
573+
574+
// Create a tree where A depends on both B and C. Both B and C are vulnerable.
575+
const path = t.testdir()
576+
const tree = new Node({
577+
path,
578+
pkg: {
579+
name: 'root',
580+
dependencies: {
581+
A: '1.0.0',
582+
},
583+
},
584+
children: [
585+
{
586+
pkg: { name: 'A', version: '1.0.0', dependencies: { B: '1.0.0', C: '1.0.0' } },
587+
},
588+
{
589+
pkg: { name: 'B', version: '1.0.0' },
590+
},
591+
{
592+
pkg: { name: 'C', version: '1.0.0' },
593+
},
594+
],
595+
})
596+
597+
registry.audit({
598+
times: 5,
599+
results: {
600+
B: [{ id: 1, url: 'https://B', title: 'B vuln', severity: 'high', vulnerable_versions: '*' }],
601+
C: [{ id: 2, url: 'https://C', title: 'C vuln', severity: 'high', vulnerable_versions: '*' }],
602+
},
603+
})
604+
605+
// We intentionally do not mock the packuments for A, B, and C.
606+
// By using the fixtures directory, unmatched GET requests will receive a 404.
607+
// This triggers the metavuln calculator fallback which defaults the effective range to `*`.
608+
// As a result, both B and C trigger metavulns on A with identical ranges (`*`),
609+
// producing the identical collision key `A@*` required to reproduce the determinism bug.
610+
registry.mocks({ dir: join(__dirname, 'fixtures') })
611+
612+
// We loop 5 times just to show it is deterministic.
613+
const results = []
614+
let lastReport
615+
for (let i = 0; i < 5; i++) {
616+
lastReport = await AuditReport.load(tree, { registry: 'https://registry.npmjs.org' })
617+
results.push(JSON.stringify(lastReport.toJSON()))
618+
}
619+
620+
const uniqueResults = new Set(results)
621+
t.equal(uniqueResults.size, 1, 'output is identical across runs')
622+
623+
// The key assertion is that BOTH B and C are correctly included in A's via list,
624+
// which formally proves the determinism bug dropping a via path is fixed.
625+
const A = lastReport.get('A')
626+
const B = lastReport.get('B')
627+
const C = lastReport.get('C')
628+
629+
t.ok(A, 'A is vulnerable')
630+
const viaNames = [...A.via].map(v => v.name)
631+
const BEffects = [...B.effects].map(v => v.name)
632+
const CEffects = [...C.effects].map(v => v.name)
633+
634+
t.ok(viaNames.includes('B'), 'A via list includes B')
635+
t.ok(viaNames.includes('C'), 'A via list includes C')
636+
t.ok(BEffects.includes('A'), 'B effects includes A')
637+
t.ok(CEffects.includes('A'), 'C effects includes A')
638+
})

0 commit comments

Comments
 (0)