|
| 1 | +// Regression tests for how `release.yml`'s `publish-npm` job gets its npm (#5104). |
| 2 | +// |
| 3 | +// That job holds the OIDC credential, so it installs nothing: it runs an |
| 4 | +// exactly pinned Node whose bundled npm meets the floor for OIDC trusted |
| 5 | +// publishing (11.5.1), and a step fails the job before anything is published |
| 6 | +// if that npm is ever below the floor. The step's comparator is inline shell |
| 7 | +// and JavaScript in the workflow file, which runs only after a Release is |
| 8 | +// published, so a later edit that let 11.5.0 through would otherwise surface |
| 9 | +// at release time. |
| 10 | +// |
| 11 | +// As in release-dist-tag.test.mjs, these tests read the step out of the |
| 12 | +// workflow file and run THAT, with `npm` on PATH replaced by a stub that |
| 13 | +// reports a chosen version. They also pin the job's shape: an exact Node |
| 14 | +// pin, the check ahead of the download and the publish, and no step that |
| 15 | +// installs a package. |
| 16 | +// |
| 17 | +// The step's shell is bash, so those tests are skipped where there is none. |
| 18 | +// Run via `npm run test:scripts`. |
| 19 | + |
| 20 | +import { test } from "node:test"; |
| 21 | +import assert from "node:assert/strict"; |
| 22 | +import { spawnSync } from "node:child_process"; |
| 23 | +import { |
| 24 | + chmodSync, |
| 25 | + mkdtempSync, |
| 26 | + readFileSync, |
| 27 | + rmSync, |
| 28 | + writeFileSync, |
| 29 | +} from "node:fs"; |
| 30 | +import { tmpdir } from "node:os"; |
| 31 | +import path from "node:path"; |
| 32 | +import { fileURLToPath } from "node:url"; |
| 33 | +import { parse } from "yaml"; |
| 34 | + |
| 35 | +const repoRoot = path.resolve( |
| 36 | + path.dirname(fileURLToPath(import.meta.url)), |
| 37 | + "..", |
| 38 | +); |
| 39 | + |
| 40 | +const CHECK_STEP = "Check the bundled npm supports OIDC trusted publishing"; |
| 41 | + |
| 42 | +/** The `publish-npm` job's steps, as the workflow file declares them. */ |
| 43 | +function publishSteps() { |
| 44 | + const workflow = parse( |
| 45 | + readFileSync( |
| 46 | + path.join(repoRoot, ".github", "workflows", "release.yml"), |
| 47 | + "utf8", |
| 48 | + ), |
| 49 | + ); |
| 50 | + const job = workflow.jobs["publish-npm"]; |
| 51 | + assert.ok(job, "release.yml has no publish-npm job"); |
| 52 | + return job.steps; |
| 53 | +} |
| 54 | + |
| 55 | +const hasBash = |
| 56 | + process.platform !== "win32" && |
| 57 | + spawnSync("bash", ["-c", "exit 0"]).status === 0; |
| 58 | + |
| 59 | +/** |
| 60 | + * Run the workflow's npm floor check with `npm --version` reporting `version`. |
| 61 | + * |
| 62 | + * @param {string} version what the stub npm reports |
| 63 | + * @returns {{ status: number | null, stderr: string }} |
| 64 | + */ |
| 65 | +function runCheck(version) { |
| 66 | + const step = publishSteps().find((s) => s.name === CHECK_STEP); |
| 67 | + assert.ok(step, `publish-npm has no \`${CHECK_STEP}\` step`); |
| 68 | + const dir = mkdtempSync(path.join(tmpdir(), "npm-floor-")); |
| 69 | + try { |
| 70 | + const stub = path.join(dir, "npm"); |
| 71 | + writeFileSync(stub, `#!/bin/sh\necho "${version}"\n`); |
| 72 | + chmodSync(stub, 0o755); |
| 73 | + const result = spawnSync("bash", ["-e", "-c", step.run], { |
| 74 | + encoding: "utf8", |
| 75 | + env: { |
| 76 | + ...process.env, |
| 77 | + PATH: `${dir}${path.delimiter}${process.env.PATH}`, |
| 78 | + }, |
| 79 | + }); |
| 80 | + return { status: result.status, stderr: result.stderr }; |
| 81 | + } finally { |
| 82 | + rmSync(dir, { recursive: true, force: true }); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +test( |
| 87 | + "the npm floor check accepts 11.5.1 and later", |
| 88 | + { skip: !hasBash && "needs bash" }, |
| 89 | + () => { |
| 90 | + for (const version of ["11.5.1", "11.6.0", "11.19.0", "12.0.0"]) { |
| 91 | + const { status, stderr } = runCheck(version); |
| 92 | + assert.equal(status, 0, `npm ${version} was refused: ${stderr}`); |
| 93 | + } |
| 94 | + }, |
| 95 | +); |
| 96 | + |
| 97 | +test( |
| 98 | + "the npm floor check refuses anything below 11.5.1", |
| 99 | + { skip: !hasBash && "needs bash" }, |
| 100 | + () => { |
| 101 | + for (const version of ["11.5.0", "11.4.9", "10.9.9", "9.0.0"]) { |
| 102 | + const { status, stderr } = runCheck(version); |
| 103 | + assert.notEqual(status, 0, `npm ${version} was accepted`); |
| 104 | + assert.match(stderr, /below 11\.5\.1/); |
| 105 | + } |
| 106 | + }, |
| 107 | +); |
| 108 | + |
| 109 | +test("publish-npm pins Node exactly, to a release whose npm meets the floor", () => { |
| 110 | + const setup = publishSteps().find((s) => |
| 111 | + String(s.uses ?? "").startsWith("actions/setup-node@"), |
| 112 | + ); |
| 113 | + assert.ok(setup, "publish-npm has no actions/setup-node step"); |
| 114 | + const pin = String(setup.with?.["node-version"] ?? ""); |
| 115 | + assert.match( |
| 116 | + pin, |
| 117 | + /^\d+\.\d+\.\d+$/, |
| 118 | + `node-version ${pin} is not an exact x.y.z pin`, |
| 119 | + ); |
| 120 | + // Node 24.5.0 is the first release whose bundled npm is 11.5.1. |
| 121 | + const [major, minor] = pin.split(".").map(Number); |
| 122 | + assert.ok( |
| 123 | + major > 24 || (major === 24 && minor >= 5), |
| 124 | + `Node ${pin} bundles an npm below the OIDC floor`, |
| 125 | + ); |
| 126 | +}); |
| 127 | + |
| 128 | +test("publish-npm checks npm before it downloads or publishes anything", () => { |
| 129 | + const names = publishSteps().map((s) => s.name ?? s.uses); |
| 130 | + const check = names.indexOf(CHECK_STEP); |
| 131 | + assert.notEqual(check, -1, `publish-npm has no \`${CHECK_STEP}\` step`); |
| 132 | + const download = names.findIndex((n) => |
| 133 | + String(n).startsWith("Download the artifact"), |
| 134 | + ); |
| 135 | + const publish = names.indexOf("Publish package"); |
| 136 | + assert.ok( |
| 137 | + download !== -1 && publish !== -1, |
| 138 | + "publish-npm lost its download or publish step", |
| 139 | + ); |
| 140 | + assert.ok( |
| 141 | + check < download && check < publish, |
| 142 | + `the npm check runs after: ${names.join(" > ")}`, |
| 143 | + ); |
| 144 | +}); |
| 145 | + |
| 146 | +test("publish-npm installs no package next to the OIDC credential", () => { |
| 147 | + const install = |
| 148 | + /\b(?:npm|pnpm|yarn)\s+(?:install|i|ci|add|exec|dlx)\b|\bnpx\b|\bcorepack\b|\bpip3?\s+install\b|\buvx?\b/; |
| 149 | + for (const step of publishSteps()) { |
| 150 | + if (!step.run) continue; |
| 151 | + // Shell comments may name these tools in prose; only commands count. |
| 152 | + const commands = step.run |
| 153 | + .split("\n") |
| 154 | + .filter((line) => !line.trim().startsWith("#")) |
| 155 | + .join("\n"); |
| 156 | + assert.doesNotMatch( |
| 157 | + commands, |
| 158 | + install, |
| 159 | + `publish-npm step \`${step.name}\` installs or runs a fetched package`, |
| 160 | + ); |
| 161 | + } |
| 162 | +}); |
0 commit comments