Skip to content

fix(arborist): fix audit-report determinism due to dropped via links#9603

Merged
owlstronaut merged 3 commits into
npm:latestfrom
arjun-vegeta:fix-audit-determinism
Jun 24, 2026
Merged

fix(arborist): fix audit-report determinism due to dropped via links#9603
owlstronaut merged 3 commits into
npm:latestfrom
arjun-vegeta:fix-audit-determinism

Conversation

@arjun-vegeta

Copy link
Copy Markdown
Contributor

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.

In `AuditReport#init`, the deduplication logic (`if (!seen.has(k))`) prevents evaluating the exact same advisory multiple times. However, if a package has multiple identical-range dependencies that trigger metavulns, those metavulns share the same computed range and produce an identical collision key `k`.

Previously, `vuln.addVia()` was called inside the `if (!seen.has(k))` block. When a collision occurred, the duplicate key skipped the block, causing the `via` link (and its reciprocal `effects` link) for the second dependency to be silently dropped. This resulted in an incomplete dependency graph and non-deterministic serialization depending on the resolution order of `Promise.all()` in the real world.

This commit removes the inline `addVia` and adds a post-loop reconciliation pass at the end of `#init`. By waiting until all advisories are instantiated and cleaned up, we mathematically guarantee that all valid metavulns establish their `via` links deterministically. This also structurally avoids triggering the expensive `#fixAvailable` setter multiple times during the loop iteration.

A comprehensive programmatic test has been added to reproduce and assert the dropped-via bug and determinism.
@arjun-vegeta arjun-vegeta requested review from a team as code owners June 23, 2026 15:56
Comment thread workspaces/arborist/lib/audit-report.js Outdated
@owlstronaut owlstronaut merged commit 690bf17 into npm:latest Jun 24, 2026
24 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Backport to release/v11 created: #9638

owlstronaut pushed a commit that referenced this pull request Jun 24, 2026
…9638)

Backport of #9603 to `release/v11`.

Co-authored-by: Arjun R <149392539+arjun-vegeta@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Inconsistent output of 'npm audit'

2 participants