Skip to content

[BUG] npm audit nondeterministically includes a top-level metavuln (@types/jest) with an unchanged lockfile #10313

Open

Description

@gfernandescs
  • I have searched the existing issues
  • I am using the latest npm (also reproduced on npm 12.2.2 via npx)
  • This is not solely a request to bump a dependency for a CVE — this is about npm audit's report being nondeterministic for an unchanged tree, independent of which advisory is involved.

Current Behavior

With a lockfile that does not change, consecutive npm audit --json runs disagree about whether a top-level package is reported as a (meta)vulnerability. In a minimal tree whose only direct dependency is @types/jest@30.0.0, the entry vulnerabilities["@types/jest"] (a high metavuln reached via expect → jest-message-util/jest-util → picomatch, GHSA-fc5q-39xc-vjpf) is present on some runs and absent on others, with no change to package.json, package-lock.json, or node_modules. The deeper packages (expect, jest-message-util, jest-util, jest-mock, picomatch) are reported on every run — only the top-level metavuln flickers, which also makes the high/total counts flip between 5 and 6.

This breaks any tooling that ratchets the audit output against a baseline (the same lockfile intermittently crosses the threshold).

run 1: absent  | high=5 | total=5
run 2: PRESENT | high=6 | total=6
run 3: PRESENT | high=6 | total=6
run 4: absent  | high=5 | total=5
...   (2 of 12 runs PRESENT on npm 11.17.0; 2 of 12 on npm 12.2.2)

Expected Behavior

The same tree plus the same advisory data should produce the same report every time. npm audit should be deterministic for an unchanged lockfile.

Steps To Reproduce

  1. Empty folder with this package.json:
    {
      "name": "audit-flake-repro",
      "version": "1.0.0",
      "private": true,
      "devDependencies": { "@types/jest": "30.0.0" }
    }
  2. npm install --ignore-scripts
  3. Run this a dozen times and watch the top-level entry appear and disappear:
    for i in $(seq 1 12); do
      npm audit --json 2>/dev/null \
        | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const j=JSON.parse(s);const v=j.vulnerabilities||{};console.log((v["@types/jest"]?"PRESENT":"absent ")+" high="+Object.values(v).filter(x=>x.severity==="high").length)})'
    done

Root cause (investigated)

The nondeterminism is entirely client-side, in AuditReport#init — not in the server response:

  • Running with --loglevel silly, a PRESENT run and an absent run have an identical bulk request body and an identical raw server report (the object logged by log.silly('audit report', this.report) at workspaces/arborist/lib/audit-report.js#L102, before #init runs). Same input, different output.
  • Diffing the two silly logs, the only difference is the order in which the metavuln packuments resolve from cache (expect, jest-message-util, jest-util are fetched in a different order between runs).

The mechanism is an iterate-while-mutating race on the advisories Set in #init:

  • const advisories = new Set(await Promise.all(promises)) (L128) is then iterated with for (const advisory of advisories) (L130).
  • Inside the loop, metavulns for dependents are computed asynchronously and pushed back into that same Set from promise callbacks: p.push(calc.then(meta => { if (...) advisories.add(meta) })) (L182–L185), awaited per-advisory at await Promise.all(p) (L191).
  • The top-level @types/jest metavuln is propagated up the dependent chain picomatch → jest-message-util/jest-util → expect → @types/jest, each level added to the Set during the triggering advisory's await Promise.all(p). When expect's metavuln resolves and is added before the Set iterator drains, the @types/jest metavuln it produces is visited and becomes a top-level entry (PRESENT). When it resolves after the iterator is already done, @types/jest is never visited (absent).

Since the order in which these cached packument lookups resolve is timing-dependent, the visited set of advisories — and therefore the final report — varies run to run.

The same code shape is present on latest (arborist 10.0.4), so the issue is not version-specific.

A fix would make #init drain the growing set deterministically (e.g. a worklist loop that keeps processing until no new advisories remain, instead of a single for...of pass over a Set that is being mutated by async callbacks), with a test that controls the calculator resolution order. Happy to open a PR if the maintainers agree on the approach.

Environment

  • npm: 11.17.0 (also reproduced on 12.2.2 via npx npm@12.2.2)
  • Node.js: 24.19.0
  • OS: Ubuntu on WSL2 (Linux 6.x)
  • npm config: defaults, public registry, no user .npmrc
  • Bundled: @npmcli/arborist 9.8.0, @npmcli/metavuln-calculator 9.0.3 (as shipped with npm 11.17.0)

Activity

  1. changed the title [-][BUG] npm audit nondeterministically includes a top-level metavuln (@types/jest) with an unchanged lockfile[/-] [+][BUG] `npm audit` nondeterministically includes a top-level metavuln (@types/jest) with an unchanged lockfile[/+] on Nov 24, 2026
  2. added
    cmd:audit
    Bug
    on Nov 24, 2026
  3. added
    Priority 2
    area:registry
    on Nov 24, 2026
  4. gfernandescs commented on Nov 27, 2026

    @gfernandescs
    Author

    I have to correct the root cause I gave above.
    The for...of over advisories does see every metavuln, because each iteration awaits its calculations before the iterator moves on.
    The real problem is the vuln.nodes.has(node) short-circuit in #init.
    Once one advisory marks expect, later advisories for expect are never passed on to @types/jest, and expect's top-level edges are not checked again.
    Only the metavuln through jest-util makes @types/jest@30.0.0 vulnerable, so the result depended on which of the jest-util and jest-message-util paths settled first.
    A worklist drain would not change anything.

    GHSA-fc5q-39xc-vjpf was withdrawn on 2026-11-18, so the steps above no longer reproduce against the public registry.
    Serving that advisory from a local stub of the bulk endpoint still reproduces it on latest (present on 9 of 15 runs).
    The same happens with live advisories: on a 771-package lockfile with jest@24.0.0 and request@2.81.0, latest gave two different reports in 5 runs.
    The top-level jest entry and the fixAvailable of many entries changed between them.

    I have a fix with tests that control the settle order:

    • already-marked nodes still pass each advisory on, and their top-level edges are checked again;
    • an advisory whose range is a subset of one already passed on with the same severity is skipped, to limit the extra work;
    • metavulns are added in request order;
    • via names are deduped.

    On the #10313 tree I get 15 of 15 identical reports with @types/jest present, and one report across the 771-package runs.
    On that larger tree, calculate calls go from 173 to 322 and the report takes about 900 ms instead of 750 ms.
    Some severities go up, because advisories that were dropped before now reach the dependents.

    I'll open a PR unless you'd prefer a different approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPriority 2Triage Requestedarea:registrycmd:audit

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions