From ee5b57f4aedab91c1442cf0726a7b50d10145f6f Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 16 Aug 2026 06:26:33 +0000 Subject: [PATCH] audit fix: never downgrade a package out of its installed release line A downgrade candidate must share the installed version's major (minor below 1.0.0). When an advisory covers every release of the installed line, the package is reported under "no published version fixes" instead of being moved to an older major, with or without --latest. A downgrade that rewrites a declared range also pins the lockfile edge, since the rewritten range would otherwise re-resolve to the newest release it allows, which is the vulnerable one. Fixes #39309 --- docs/pm/cli/audit.mdx | 4 +- src/install/audit_fix.rs | 16 +- test/cli/install/bun-audit.test.ts | 199 +++++++++++++++++- .../packages/create-zero-major-packages.ts | 48 +++++ .../registry/packages/zero-major/package.json | 49 +++++ .../packages/zero-major/zero-major-0.4.0.tgz | Bin 0 -> 159 bytes .../packages/zero-major/zero-major-0.4.1.tgz | Bin 0 -> 160 bytes .../packages/zero-major/zero-major-0.5.0.tgz | Bin 0 -> 160 bytes .../packages/zero-major/zero-major-0.5.1.tgz | Bin 0 -> 160 bytes 9 files changed, 303 insertions(+), 13 deletions(-) create mode 100644 test/cli/install/registry/packages/create-zero-major-packages.ts create mode 100644 test/cli/install/registry/packages/zero-major/package.json create mode 100644 test/cli/install/registry/packages/zero-major/zero-major-0.4.0.tgz create mode 100644 test/cli/install/registry/packages/zero-major/zero-major-0.4.1.tgz create mode 100644 test/cli/install/registry/packages/zero-major/zero-major-0.5.0.tgz create mode 100644 test/cli/install/registry/packages/zero-major/zero-major-0.5.1.tgz diff --git a/docs/pm/cli/audit.mdx b/docs/pm/cli/audit.mdx index 8fa35e1734fb..6285756ffc30 100644 --- a/docs/pm/cli/audit.mdx +++ b/docs/pm/cli/audit.mdx @@ -96,8 +96,8 @@ Fixed 2 vulnerabilities in 2 packages ``` - **blocked by a dependent's range** — no safe version fits a dependent's declared range. If the range is in your own `package.json` or catalog, `bun audit fix --latest` gets past it. Otherwise, update the dependent or add an [`overrides`](/pm/overrides) entry. -- **no published version fixes** — every published version is vulnerable. Replace the package, or silence the advisory with the printed `--ignore` command. -- If no newer version is safe but an older one is, Bun downgrades and marks the row `(downgrade)`. +- **no published version fixes** — every version Bun would install is vulnerable. Replace the package, or silence the advisory with the printed `--ignore` command. +- If no newer version is safe but an older one is, Bun downgrades and marks the row `(downgrade)`. Bun only downgrades within the installed major version (within the installed minor version below 1.0.0), with or without `--latest`. When an advisory covers every `2.x` release, Bun lists the package under **no published version fixes** instead of installing a `1.x` release. - Bun still installs a safe version newer than `--minimum-release-age` and marks the row `(newer than --minimum-release-age)`. - Bun upgrades patched dependencies (`patchedDependencies`) like any other package. Re-create the patch afterwards with `bun patch`. - After installing, Bun re-audits the new lockfile. The `remaining` count and exit code reflect that second audit, so they match what a follow-up `bun audit` would report. diff --git a/src/install/audit_fix.rs b/src/install/audit_fix.rs index 505bd97a225c..c1d01cd6d5ca 100644 --- a/src/install/audit_fix.rs +++ b/src/install/audit_fix.rs @@ -214,6 +214,11 @@ struct Candidate { downgrade: bool, } +/// Downgrades stay in the installed major (minor below 1.0.0): an older line is a different API, and the bulk response only carries advisories matching the installed versions, so its releases merely look safe (#39309). +fn same_release_line(a: Semver::Version, b: Semver::Version) -> bool { + a.major == b.major && (a.major != 0 || a.minor == b.minor) +} + fn fmt_version(version: Semver::Version, buf: &[u8]) -> Box<[u8]> { let mut out: Vec = Vec::new(); let _ = write!(out, "{}", version.fmt(buf)); @@ -691,7 +696,13 @@ pub fn plan_fixes(manager: &mut PackageManager, advisories: &[Advisory]) -> crat } let upgrade_count = candidates.len(); for (i, &v) in releases.iter().enumerate().rev() { - if v.order(inst.current, manifest_buf, buf) == Ordering::Less && is_safe(v) { + if v.order(inst.current, manifest_buf, buf) != Ordering::Less { + continue; + } + if !same_release_line(v, inst.current) { + break; + } + if is_safe(v) { candidates.push(Candidate { version: v, index: i, @@ -819,7 +830,8 @@ pub fn plan_fixes(manager: &mut PackageManager, advisories: &[Advisory]) -> crat edits.push(edit); } // A rewritten peer row is deferred by the differ and rebinds to the old package unless the edge is pinned too. - if edge.peer { + // A rewritten range re-resolves to the newest release it allows, which for a downgrade is the vulnerable one again (`^1.0.1` still takes 1.1.0). + if edge.peer || candidate.downgrade { edges.push(PlannedEdge { dep_id: edge.dep_id, parent: edge.parent, diff --git a/test/cli/install/bun-audit.test.ts b/test/cli/install/bun-audit.test.ts index b0197c013fb3..74329f0b426d 100644 --- a/test/cli/install/bun-audit.test.ts +++ b/test/cli/install/bun-audit.test.ts @@ -1526,25 +1526,112 @@ describe("`bun audit fix`", () => { }); test.concurrent("a range that rejects every safe release is blocked on the highest safe downgrade", async () => { + await using server = startRegistry({ "no-deps": [adv(">=1.1.0")] }); + using dir = await setup(server, { name: "foo", dependencies: { "no-deps": "^1.1.0" } }); + const lockBefore = await lock(dir); + expect(lockBefore).toContain('"no-deps@1.1.0"'); + + const { stdout, exitCode } = await auditFix(dir); + expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(` + "bun audit fix () + + blocked by a dependent's range: + v no-deps 1.1.0 -> 1.0.1 (downgrade) + package.json depends on no-deps@^1.1.0 + bun audit fix --latest + + Fixed 0 of 1 vulnerability (checked 1) + 1 vulnerability remaining" + `); + expect(exitCode).toBe(1); + expect(await lock(dir)).toBe(lockBefore); + }); + + test.concurrent("a safe release in an older major is not a downgrade candidate", async () => { await using server = startRegistry({ "no-deps": [adv(">=2.0.0")] }); using dir = await setup(server, { name: "foo", dependencies: { "no-deps": "^2.0.0" } }); const lockBefore = await lock(dir); expect(lockBefore).toContain('"no-deps@2.0.0"'); + const { stdout, stderr, exitCode } = await auditFix(dir); + expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(` + "bun audit fix () + + no published version fixes: + no-deps@2.0.0 1 + bun audit fix --ignore 1 + + Fixed 0 of 1 vulnerability (checked 1) + 1 vulnerability remaining" + `); + expect(stderr).not.toContain("Saved lockfile"); + expect(exitCode).toBe(1); + expect(await lock(dir)).toBe(lockBefore); + }); + + // peer-deps@1.0.0 declares no-deps@* as a peer, so every older release is inside the range; the installed major is still the floor. + test.concurrent("a dependent's wide range does not let a fix downgrade into an older major", async () => { + await using server = startRegistry({ "no-deps": [adv(">=2.0.0")] }); + using dir = await setup(server, { name: "foo", dependencies: { "peer-deps": "1.0.0" } }); + const lockBefore = await lock(dir); + expect(lockBefore).toContain('"no-deps@2.0.0"'); + const { stdout, exitCode } = await auditFix(dir); expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(` "bun audit fix () - blocked by a dependent's range: - v no-deps 2.0.0 -> 1.1.0 (downgrade) - package.json depends on no-deps@^2.0.0 - bun audit fix --latest + no published version fixes: + no-deps@2.0.0 1 + bun audit fix --ignore 1 + + Fixed 0 of 1 vulnerability (checked 2) + 1 vulnerability remaining" + `); + expect(exitCode).toBe(1); + expect(await lock(dir)).toBe(lockBefore); + expect(await installedVersion(dir, "no-deps")).toBe("2.0.0"); + }); + + // zero-major publishes 0.4.0, 0.4.1, 0.5.0 and 0.5.1; below 1.0.0 the minor is the release line. + test.concurrent("a 0.x package downgrades within its minor line", async () => { + await using server = startRegistry({ "zero-major": [adv(">=0.5.1")] }); + using dir = await setup(server, { name: "foo", dependencies: { "zero-major": "*" } }); + expect(await lock(dir)).toContain('"zero-major@0.5.1"'); + + const { stdout, exitCode } = await auditFix(dir); + expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(` + "bun audit fix () + + fixing: + v zero-major 0.5.1 -> 0.5.0 (downgrade) + + Fixed 1 vulnerability in 1 package (checked 1)" + `); + expect(exitCode).toBe(0); + expect(await lock(dir)).toContain('"zero-major@0.5.0"'); + expect(await installedVersion(dir, "zero-major")).toBe("0.5.0"); + }); + + test.concurrent("a 0.x package is not downgraded into an older minor line", async () => { + await using server = startRegistry({ "zero-major": [adv(">=0.5.0")] }); + using dir = await setup(server, { name: "foo", dependencies: { "zero-major": "*" } }); + const lockBefore = await lock(dir); + expect(lockBefore).toContain('"zero-major@0.5.1"'); + + const { stdout, exitCode } = await auditFix(dir); + expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(` + "bun audit fix () + + no published version fixes: + zero-major@0.5.1 1 + bun audit fix --ignore 1 Fixed 0 of 1 vulnerability (checked 1) 1 vulnerability remaining" `); expect(exitCode).toBe(1); expect(await lock(dir)).toBe(lockBefore); + expect(await installedVersion(dir, "zero-major")).toBe("0.5.1"); }); test.concurrent("no vulnerabilities", async () => { @@ -3883,8 +3970,8 @@ describe("`bun audit fix`", () => { }); test.concurrent("--json marks a blocked fix that would be a downgrade", async () => { - await using server = startRegistry({ "no-deps": [adv(">=2.0.0")] }); - using dir = await setup(server, { name: "foo", dependencies: { "no-deps": "^2.0.0" } }); + await using server = startRegistry({ "no-deps": [adv(">=1.1.0")] }); + using dir = await setup(server, { name: "foo", dependencies: { "no-deps": "^1.1.0" } }); const lockBefore = await lock(dir); const { stdout, exitCode } = await auditFix(dir, "--json"); @@ -3892,11 +3979,11 @@ describe("`bun audit fix`", () => { expect(doc.blocked).toStrictEqual([ { name: "no-deps", - from: "2.0.0", - to: "1.1.0", + from: "1.1.0", + to: "1.0.1", downgrade: true, latestFixes: true, - blockers: [{ dependent: "package.json", range: "^2.0.0", bundled: false }], + blockers: [{ dependent: "package.json", range: "^1.1.0", bundled: false }], }, ]); expect(doc).toMatchObject({ dryRun: false, fixed: 0, remaining: 1, fixes: [] }); @@ -4264,6 +4351,100 @@ describe("`bun audit fix --latest`", () => { expect(await lock(dir)).toContain('"no-deps@2.0.0"'); }); + // Every no-deps 2.x release is vulnerable. The safe 1.x releases are a different major, so there is nothing to rewrite the range to. + test.concurrent("never rewrites a range to downgrade into an older major", async () => { + await using server = startRegistry({ "no-deps": [adv(">=2.0.0")] }); + using dir = await setup(server, { name: "foo", dependencies: { "no-deps": "^2.0.0" } }); + const lockBefore = await lock(dir); + expect(lockBefore).toContain('"no-deps@2.0.0"'); + const pkgJsonBefore = await pkgJsonText(dir); + + const { stdout, stderr, exitCode } = await auditFix(dir, "--latest"); + expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(` + "bun audit fix () + + no published version fixes: + no-deps@2.0.0 1 + bun audit fix --ignore 1 + + Fixed 0 of 1 vulnerability (checked 1) + 1 vulnerability remaining" + `); + expect(stderr).not.toContain("Saved lockfile"); + expect(exitCode).toBe(1); + expect(await pkgJsonText(dir)).toBe(pkgJsonBefore); + expect(await lock(dir)).toBe(lockBefore); + expect(await installedVersion(dir, "no-deps")).toBe("2.0.0"); + + const json = await auditFix(dir, "--latest", "--json"); + expect(JSON.parse(json.stdout)).toMatchObject({ + fixed: 0, + remaining: 1, + fixes: [], + blocked: [], + unfixable: [{ name: "no-deps", from: "2.0.0", advisories: ["1"] }], + }); + expect(json.exitCode).toBe(1); + expect(await pkgJsonText(dir)).toBe(pkgJsonBefore); + expect(await lock(dir)).toBe(lockBefore); + }); + + test.concurrent("rewrites a range for a downgrade that stays in the installed major", async () => { + await using server = startRegistry({ "no-deps": [adv(">=1.1.0")] }); + using dir = await setup(server, { name: "foo", dependencies: { "no-deps": "^1.1.0" } }); + expect(await lock(dir)).toContain('"no-deps@1.1.0"'); + + const { stdout, exitCode } = await auditFix(dir, "--latest"); + expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(` + "bun audit fix () + + fixing: + v no-deps 1.1.0 -> 1.0.1 (downgrade) + package.json: ^1.1.0 -> ^1.0.1 + + Fixed 1 vulnerability in 1 package (checked 1)" + `); + expect(exitCode).toBe(0); + expect((await pkgJson(dir)).dependencies).toStrictEqual({ "no-deps": "^1.0.1" }); + const lockfile = await lock(dir); + expect(lockfile).toContain('"no-deps@1.0.1"'); + expect(lockfile).not.toContain('"no-deps@1.1.0"'); + expect(await installedVersion(dir, "no-deps")).toBe("1.0.1"); + + const recheck = await audit(dir); + expectClean(recheck, 1); + + await runBunInstall(installEnv(dir), dir, { frozenLockfile: true }); + }); + + test.concurrent("rewrites a catalog entry for a downgrade that stays in the installed major", async () => { + await using server = startRegistry({ "no-deps": [adv(">=1.1.0")] }); + const member = JSON.stringify({ name: "a", dependencies: { "no-deps": "catalog:" } }); + using dir = await setup( + server, + { name: "root", workspaces: ["packages/*"], catalog: { "no-deps": "^1.1.0" } }, + { "packages/a/package.json": member }, + ); + expect(await lock(dir)).toContain('"no-deps@1.1.0"'); + + const { stdout, exitCode } = await auditFix(dir, "--latest"); + expect(stdout).toContain(" v no-deps 1.1.0 -> 1.0.1 (downgrade)\n package.json (catalog): ^1.1.0 -> ^1.0.1\n"); + expect(stdout).toContain("Fixed 1 vulnerability in 1 package"); + expect(exitCode).toBe(0); + + expect((await pkgJson(dir)).catalog).toStrictEqual({ "no-deps": "^1.0.1" }); + expect(await pkgJsonText(dir, "packages", "a")).toBe(member); + const lockfile = await lock(dir); + expect(lockfile).toContain('"no-deps@1.0.1"'); + expect(lockfile).not.toContain('"no-deps@1.1.0"'); + expect((await pkgJson(dir, "packages", "a", "node_modules", "no-deps")).version).toBe("1.0.1"); + + const recheck = await audit(dir); + expectClean(recheck, 1); + + await runBunInstall(installEnv(dir), dir, { frozenLockfile: true }); + }); + test.concurrent("a package blocked only by a transitive dependent stays blocked", async () => { await using server = startRegistry({ "no-deps": [adv("<1.1.0")] }); using dir = await setup(server, { name: "foo", dependencies: { "one-dep": "1.0.0" } }); diff --git a/test/cli/install/registry/packages/create-zero-major-packages.ts b/test/cli/install/registry/packages/create-zero-major-packages.ts new file mode 100644 index 000000000000..ad76055a878f --- /dev/null +++ b/test/cli/install/registry/packages/create-zero-major-packages.ts @@ -0,0 +1,48 @@ +#!/usr/bin/env bun +// Generates the `zero-major` fixture (0.4.x and 0.5.x releases, nothing else) used by bun-audit.test.ts to check that +// `bun audit fix` treats 0.x minors as separate release lines. + +import { mkdir, writeFile } from "fs/promises"; +import { join } from "path"; + +const packagesDir = import.meta.dir; + +const packages: Record = { + "zero-major": ["0.4.0", "0.4.1", "0.5.0", "0.5.1"], +}; + +for (const [name, versionList] of Object.entries(packages)) { + const dir = join(packagesDir, name); + await mkdir(dir, { recursive: true }); + + const versions: Record = {}; + let latest = ""; + for (const version of versionList) { + const pkgJson = { name, version }; + const tarball = join(dir, `${name}-${version}.tgz`); + await Bun.Archive.write( + tarball, + { "package/package.json": JSON.stringify(pkgJson, null, 2) }, + { compress: "gzip" }, + ); + + const bytes = await Bun.file(tarball).bytes(); + versions[version] = { + ...pkgJson, + _id: `${name}@${version}`, + dist: { + integrity: `sha512-${Buffer.from(new Bun.CryptoHasher("sha512").update(bytes).digest()).toString("base64")}`, + shasum: new Bun.CryptoHasher("sha1").update(bytes).digest("hex"), + tarball: `http://localhost:4873/${name}/-/${name}-${version}.tgz`, + }, + }; + latest = version; + } + + await writeFile( + join(dir, "package.json"), + JSON.stringify({ _id: name, name, "dist-tags": { latest }, versions }, null, 2), + ); +} + +console.log("Created zero-major test package"); diff --git a/test/cli/install/registry/packages/zero-major/package.json b/test/cli/install/registry/packages/zero-major/package.json new file mode 100644 index 000000000000..134fc8dfefb1 --- /dev/null +++ b/test/cli/install/registry/packages/zero-major/package.json @@ -0,0 +1,49 @@ +{ + "_id": "zero-major", + "name": "zero-major", + "dist-tags": { + "latest": "0.5.1" + }, + "versions": { + "0.4.0": { + "name": "zero-major", + "version": "0.4.0", + "_id": "zero-major@0.4.0", + "dist": { + "integrity": "sha512-xY4I6VAgjzMCnNTpToxaeCeV1wtNlO92G/rB/V8hUIlhXnNSfiOwoWKr38ElkI9ro09fwPIaM34s7INVa2uCMw==", + "shasum": "60ec5ccba9d9ab89bebaa5a0fd117e50a3181e3f", + "tarball": "http://localhost:4873/zero-major/-/zero-major-0.4.0.tgz" + } + }, + "0.4.1": { + "name": "zero-major", + "version": "0.4.1", + "_id": "zero-major@0.4.1", + "dist": { + "integrity": "sha512-m9SPZLJj+qAzlWZKR+s0MA8V+nTc9iJiMCM327hhW8ib2cHgj4sC33R61Mffid5uohvhs8VKmTEkLIX+wAGcdw==", + "shasum": "ec28d936c91a26ed55ff0ccb57229b9199214d9a", + "tarball": "http://localhost:4873/zero-major/-/zero-major-0.4.1.tgz" + } + }, + "0.5.0": { + "name": "zero-major", + "version": "0.5.0", + "_id": "zero-major@0.5.0", + "dist": { + "integrity": "sha512-XXcn5EnWRgNs1wTxAyNamlP5WwzpwinVz29ki/ph99Utwq5NaTa4jTM2d0W8DH1+5SRK4pSD6rE03TFYypOPfQ==", + "shasum": "e678e28002fe6c54c5a81ea5353172890d97f07e", + "tarball": "http://localhost:4873/zero-major/-/zero-major-0.5.0.tgz" + } + }, + "0.5.1": { + "name": "zero-major", + "version": "0.5.1", + "_id": "zero-major@0.5.1", + "dist": { + "integrity": "sha512-bNixOAGH2uJiY8pqjQq9XERxmxCwl1GsgdrUWrwZDfzn+j8rC97F/uJAPjvRJLL+jfhUZRlN3C/KjgNlWdah+A==", + "shasum": "5873c91085ace4ae7c3162b9467848d0db4203fe", + "tarball": "http://localhost:4873/zero-major/-/zero-major-0.5.1.tgz" + } + } + } +} \ No newline at end of file diff --git a/test/cli/install/registry/packages/zero-major/zero-major-0.4.0.tgz b/test/cli/install/registry/packages/zero-major/zero-major-0.4.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..5244bac82e92912db4433150cfb920638098c128 GIT binary patch literal 159 zcmb2|=3oGW|8LJ9Y$i?+eQEx;1|6KfB*% zj^>-b#QV9+!avvbE&gp_yn3&Ay8r1}k-0%3OSf;@wCY3B(QC7gb?(%+IKO&bcaQjD|I@3o7C(A*Dmyg$?Vc64vu;ZTerA3CQ)JV{?c1k4 z|951qz{X^WMQ@M9CHuWr{Gxd~R53~4@O-<;Ex$F>_LXegAq{Z@lsw>H!Qc`(laWD# GfdK$Sc}6e* literal 0 HcmV?d00001 diff --git a/test/cli/install/registry/packages/zero-major/zero-major-0.5.0.tgz b/test/cli/install/registry/packages/zero-major/zero-major-0.5.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..26ed88f9a52396c445a7dd6bc5852a16fd4eff01 GIT binary patch literal 160 zcmb2|=3oGW|8LK4_Ju^>;f1&XN_N~b&G>5nWN;b^@z!0hZLyJL! GfdK$mUq;vf literal 0 HcmV?d00001