Skip to content

fix: adapt audit client to npmjs /advisories/bulk endpoint - #11268

Merged
zkochan merged 43 commits into
pnpm:mainfrom
wamasimba:fix-npmjs-audit-endpoint
Apr 15, 2026
Merged

zkochan merged 43 commits into
pnpm:mainfrom
wamasimba:fix-npmjs-audit-endpoint

Conversation

@wamasimba

@wamasimba wamasimba commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

The legacy /-/npm/v1/security/audits{,/quick} endpoints have been retired by npmjs.org. This PR rewires the audit client to the replacement /-/npm/v1/security/advisories/bulk endpoint.

The new endpoint is not a drop-in rename — the request and response contracts are both different:

  • Request: a flat { pkgName: [versions] } map. lockfileToAuditRequest walks the lockfile once and builds the POST body directly; there is no more nested AuditTree.
  • Response: only id, url, title, severity, vulnerable_versions, and cwe per advisory. Everything else the old endpoint returned is computed locally:
    • findings[].paths are walked from the lockfile (skipped entirely when the response is empty; the second walk intentionally avoids @pnpm/lockfile.walker's global dedup so alternate install chains to the same shared dep aren't dropped).
    • metadata.vulnerabilities counts advisories per severity.
    • metadata.dependencies / devDependencies / optionalDependencies / totalDependencies come from a classified lockfile walk; the classifier respects --prod/--dev include flags when deciding whether a subgraph is reachable non-optionally.
    • patched_versions is inferred from the vulnerable range for common <X.Y.Z / <=X.Y.Z shapes so audit --fix can still produce usable overrides; left undefined when inference fails.
    • github_advisory_id is parsed from the advisory URL and canonicalized to the github.com form (uppercase GHSA- prefix, lowercase suffix).
    • info severity is now supported end-to-end (severity type, --audit-level, filters, colors).

Breaking changes (v11)

  • Private registries that do not implement /advisories/bulk now fail with AuditEndpointNotExistsError.
  • CVE-based filtering is replaced with GHSA-based filtering, since the bulk endpoint does not return CVE identifiers:
    • auditConfig.ignoreCves → auditConfig.ignoreGhsas (the old key is no longer recognized).
    • pnpm audit --ignore <id> and --ignore-unfixable now read and write GHSAs.
    • Migration: replace each CVE-YYYY-NNNNN in auditConfig.ignoreCves with the matching GHSA-xxxx-xxxx-xxxx (visible in the More info column of pnpm audit output) under auditConfig.ignoreGhsas.
  • --ignore-unfixable now only targets advisories whose patched range couldn't be inferred — the only "no fix available" signal the bulk endpoint provides.
  • AuditReport and AuditAdvisory are trimmed to just the fields the audit client actually populates:
    • AuditReport: advisories + metadata only (actions and muted removed).
    • AuditAdvisory: findings, id, title, module_name, vulnerable_versions, patched_versions?, severity, cwe, github_advisory_id, url. Dropped: cves, created, updated, deleted, access, overview, recommendation, references, found_by, reported_by, metadata.
    • AuditAction, AuditResolution, AuditActionRecommendation removed (no consumers).

Hardening

  • Response body validated: non-object / malformed JSON / non-array package buckets all surface as ERR_PNPM_AUDIT_BAD_RESPONSE with a body excerpt. Advisory id must be a finite number and severity must be a known value before being indexed.
  • Name-keyed records use Object.create(null) so a hostile/unusual package name can't trigger prototype pollution.
  • GHSA ids canonicalized on both read and write so casing drift between config and registry doesn't mask ignores.
  • findings[].paths are deduped and capped per (name, version) to keep pathologically shared graphs from blowing up memory.

Internals

  • AuditTree / AuditNode / lockfileToAuditTree removed. lockfileToAuditIndex.ts exports lockfileToAuditRequest (flat POST body + counts) and buildAuditPathIndex (only invoked when the response has advisories).
  • AuditAdvisory.findings is now AuditFinding[] (was an unintended 1-tuple).
  • Top-level test fixtures regenerated from real registry.npmjs.org responses; synthetic update-* fixtures converted in place to bulk shape.

Comment thread deps/compliance/audit/src/index.ts Outdated
const quickAuditUrl = `${registry}-/npm/v1/security/audits/quick`
const auditUrl = `${registry}-/npm/v1/security/advisories/bulk`
const authHeaderValue = getAuthHeader(registry)
const requestBody = JSON.stringify(auditTree)

@carlos-menezes carlos-menezes Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes are less trivial than just changing the endpoint. The expected body of the POST request is completely different to what is generated by lockfileToAuditTree and sent as body.

Expected:

{
  "@npmcli/arborist": [
    "1.0.0",
    "2.0.1"
  ],
  "@npmcli/config": [
    "10.4.2"
  ]
}

The type of auditTree is:

export interface AuditNode {
  version?: string
  integrity?: string
  requires?: Record<string, string>
  dependencies?: { [name: string]: AuditNode }
  dev: boolean
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More complex you mean?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More complex you mean?

yikes 😬 I meant less trivial!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before putting together this PR I hacked into my local pnpm script files, to change the URL, and it seemed to work. But if formats can differ, we should cope...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retracing my steps; it seems you're right: even the message body does not conform to the one required by the new endpoint:

 ERR_PNPM_AUDIT_BAD_RESPONSE  The audit endpoint (at https://registry.npmjs.org/-/npm/v1/security/advisories/bulk) responded with 400: {"statusCode":400,"error":"Bad Request","message":"Invalid request payload input"}

@Rulqu

Rulqu commented Apr 15, 2026

Copy link
Copy Markdown

What about the people using private registries?
Their endpoints are ${registry}-/npm/v1/security/audits

@wamasimba

Copy link
Copy Markdown
Contributor Author

What about the people using private registries? Their endpoints are ${registry}-/npm/v1/security/audits

Good point; we could re-instate the fallback mechanism, with that endpoint fallback. Any opinions?

The new endpoint expects a flat {name: [versions]} request body and
returns advisories keyed by package name, so simply renaming the URL is
not enough. Flatten the audit tree for the request and map the bulk
response into the existing AuditReport shape, recomputing findings
paths from the lockfile and severity counts locally, since the new
endpoint no longer returns them.
@zkochan zkochan changed the title fix: use new npmjs.org audit endpoint fix: adapt audit client to npmjs /advisories/bulk endpoint Apr 15, 2026
@zkochan
zkochan requested a review from Copilot April 15, 2026 13:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates pnpm’s audit implementation to use npmjs.org’s replacement /-/npm/v1/security/advisories/bulk endpoint now that the legacy /-/npm/v1/security/audits{,/quick} endpoints are retired, while keeping downstream consumers working by mapping the new response back into AuditReport.

Changes:

  • Switch audit client networking from /audits{,/quick} to /advisories/bulk, including new request/response adaptation logic.
  • Update audit-related tests/mocks and regenerate audit --json snapshot for the new report shape (actions: [], locally computed counts/paths).
  • Fix AuditAdvisory.findings typing from a tuple to an array and add semver to support local vulnerability matching.

Reviewed changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
deps/compliance/audit/src/index.ts Implements /advisories/bulk request/response mapping and local findings/count computation.
deps/compliance/audit/src/types.ts Fixes findings typing by introducing AuditFinding and using AuditFinding[].
deps/compliance/audit/test/index.ts Updates mock endpoint paths/responses for the new bulk endpoint behavior.
deps/compliance/audit/package.json Adds semver (+ types) required for local version range matching.
deps/compliance/commands/test/audit/utils/responses/index.ts Converts legacy captured fixtures into bulk response shape at load time.
deps/compliance/commands/test/audit/index.ts Updates audit command integration tests to intercept the new bulk endpoint and adjusts expectations.
deps/compliance/commands/test/audit/fix.ts Updates audit fix tests to intercept the new bulk endpoint.
deps/compliance/commands/test/audit/ignore.ts Updates ignore tests to intercept the new bulk endpoint.
deps/compliance/commands/test/audit/fixWithUpdate.ts Converts legacy on-disk fixture responses to bulk shape before replying.
deps/compliance/commands/test/audit/preserveReferenceOverrides.ts Updates endpoint interception to bulk.
deps/compliance/commands/test/audit/__snapshots__/index.ts.snap Regenerates snapshot for the new JSON output shape (no actions, new counts).
pnpm-lock.yaml Records semver and @types/semver additions.
.changeset/fix-npmjs-audit-endpoint.md Declares release impacts (major/minor/patch) for the endpoint migration.
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deps/compliance/audit/src/index.ts Resolved
Comment thread deps/compliance/audit/src/index.ts Resolved
Comment thread deps/compliance/audit/src/index.ts Resolved
Comment thread deps/compliance/audit/src/index.ts Resolved
zkochan added 3 commits April 15, 2026 15:56
…ures

The /advisories/bulk endpoint does not return CVE identifiers or most of
the metadata the old /audits endpoint provided. Rename
auditConfig.ignoreCves to auditConfig.ignoreGhsas, make --ignore and
--ignore-unfixable operate on GitHub advisory IDs, and derive the GHSA
from each advisory URL. patched_versions is inferred from vulnerable_versions
for simple < and <= ranges so audit --fix still produces useful overrides.

Regenerate the top-level fixtures with real bulk responses captured from
registry.npmjs.org and convert the synthetic update-* fixtures to the
bulk shape. Drop the runtime fixture-shape conversion from the tests.
- Skip importer wrapper nodes in buildBulkRequestBody so only real
  package names appear in the /advisories/bulk payload.
- Compute metadata.dependencies as the non-dev portion so the counts
  in AuditMetadata stay internally consistent.
- Explicitly populate every required AuditAdvisory field in
  normalizeAdvisory instead of relying on a force cast, so downstream
  code never sees undefined in required fields.
- Add a unit test that drives the locally-computed findings path with
  a bare bulk response matching what registry.npmjs.org returns today.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 29 out of 32 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deps/compliance/audit/src/index.ts Resolved
zkochan added 4 commits April 15, 2026 16:17
- buildFindings no longer emits a placeholder finding when no installed
  version satisfies the vulnerable range; bulkResponseToAuditReport now
  skips advisories that end up with zero findings so the CLI does not
  report false positives for packages the lockfile doesn't use.
- Rename advisoryWthNoResolutions to advisoryWithNoResolutions.
- Update info-vulnerability fixture to key on a package the lockfile
  actually uses (axios) so the new skip-empty-findings logic exercises
  the info-severity path.
- Backfill the preserve-reference-overrides lockfile with a snapshots
  block so lockfileToAuditTree walks its dependencies; without it, the
  fixture had only importer-level deps and no advisories could match.
The nested AuditTree shape existed to match the legacy /audits request
body. The new /advisories/bulk endpoint needs only a flat
{name: [versions]} map, and finding paths are computed locally from the
lockfile. Walking the lockfile into a deep tree and then traversing it
twice more to flatten it and index paths was wasted work.

Replace lockfileToAuditTree with lockfileToAuditIndex, which walks the
lockfile once and returns both the POST body and a name→version→paths
index. Drop AuditTree/AuditNode, buildBulkRequestBody, buildPathIndex,
and the manifest-read/ramda/workspace.project-manifest-reader deps they
needed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 38 out of 42 changed files in this pull request and generated 1 comment.

Files not reviewed (2)
  • deps/compliance/commands/test/audit/fixtures/preserve-reference-overrides/pnpm-lock.yaml: Language not supported
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deps/compliance/audit/src/index.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 38 out of 42 changed files in this pull request and generated 3 comments.

Files not reviewed (2)
  • deps/compliance/commands/test/audit/fixtures/preserve-reference-overrides/pnpm-lock.yaml: Language not supported
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deps/compliance/audit/src/index.ts Resolved
Comment thread deps/compliance/audit/src/index.ts Resolved
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.

6 participants