Skip to content

fix(yarn): support Yarn 4 tree-report audit#355

Open
Bafff wants to merge 1 commit intoIBM:mainfrom
Bafff:fix/yarn-4-tree-report
Open

fix(yarn): support Yarn 4 tree-report audit#355
Bafff wants to merge 1 commit intoIBM:mainfrom
Bafff:fix/yarn-4-tree-report

Conversation

@Bafff
Copy link
Copy Markdown

@Bafff Bafff commented Apr 15, 2026

Closes #332.

Summary

Yarn 4 changed yarn npm audit --json to an NDJSON tree-report stream (one advisory per line, shape {value, children: {ID, URL, Severity, ...}}), which is incompatible with the Yarn 2/3 aggregated report format. With v7.1.0 the parser silently emits zero findings on any Yarn 4 project.

This PR adds a dedicated Yarn 4 branch that:

  • Adds yarnUsesBerryTreeReport (>=4.0.0) and parses each NDJSON line into the existing ProcessedAdvisory shape, so allowlist matching, fail levels, exit codes, and audit-ci.jsonc config behave exactly as on Yarn classic / 2 / 3.
  • Synthesises a per-severity summary from the stream (Yarn 4 no longer emits a summary line) and prints it under --report-type=important|summary.
  • Skips deprecation-only entries (lines without a https://github.com/advisories/... URL) to match npm audit / Yarn 2/3 parity.
  • Derives allowlist paths from Dependents[], collapsing the root workspace (workspace:.) to a bare module name and prefixing non-root workspaces with their workspace name (so allowlist entries like GHSA-…|my-workspace>qs keep working).
  • Updates README Requirements (drops the <4.0.0 cap) and Limitations (replaces the "not supported" note with a description of the new behaviour).

The npm and pnpm code paths are not touched — the change is gated entirely behind isYarnBerryV4.

Test plan

  • New test/yarn-4-auditor.spec.ts (10 tests) covers: severity gating, allowlist by id, allowlist by dependents path, synthesised summary for summary and important report types, deprecation-line filtering for both summary and important output. All pass against bundled Yarn 4.2.2.
  • New fixture test/yarn-4-deprecation-summary/ exercises a real lockfile where Yarn 4 emits both deprecation lines (inflight) and a critical advisory (open) — only the advisory ends up in findings/summary.
  • End-to-end verified against a real Yarn 4 project: 20/20 advisories match yarn npm audit --json baseline, 6 deprecation lines correctly filtered, allowlist subtraction works (3 allowlisted → 17 failed; all 20 allowlisted → exit 0 "Passed yarn security audit").
  • End-to-end verified against a real npm project: identical result vs. main (76 advisories on both), confirming the npm path is unaffected.
  • tsc --noEmit clean.

Yarn 4 changed `yarn npm audit --json` to an NDJSON tree-report stream
(one advisory per line, shape `{value, children: {ID, URL, Severity, ...}}`),
which is incompatible with the Yarn 2/3 aggregated report format.

This change:

- Adds a `yarnUsesBerryTreeReport` version gate (>=4.0.0) and parses the
  per-line advisory format into the existing `ProcessedAdvisory` shape so
  Model.process(), allowlist matching, fail-level filtering, and exit-code
  semantics all behave the same as on Yarn classic / 2 / 3.
- Synthesises a per-severity summary from the stream (Yarn 4 no longer
  emits a summary line) and prints it under `--report-type=important|summary`.
- Skips deprecation-only entries (lines without a GitHub advisory URL).
- Derives allowlist paths from `Dependents[]`, collapsing the root workspace
  to a bare module name and prefixing non-root workspaces with their name.
- Updates README Requirements and Limitations to drop the Yarn <4 cap and
  document the new behaviour.

Verified end-to-end against real Yarn 4 and npm projects: 20/20 advisories
detected on Yarn 4 (matching `yarn npm audit` baseline), 6 deprecation
lines correctly filtered out, allowlist subtraction works, npm/pnpm paths
unchanged from main.

Signed-off-by: Baf <26187677+Bafff@users.noreply.github.com>
@Bafff Bafff force-pushed the fix/yarn-4-tree-report branch from 826b4b3 to 1b554a8 Compare April 16, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Yarn v4

1 participant