diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 841fae4..1ed0666 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,10 +23,14 @@ jobs: - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 with: - node-version: 24 + node-version: "24.21.0" cache: npm registry-url: https://registry.npmjs.org + - name: Pin release npm + # Older CLIs can silently ignore --include-attestations. + run: npm install --global npm@11.19.0 --ignore-scripts + - name: Require tag to match package version env: RELEASE_TAG: ${{ github.ref_name }} @@ -74,10 +78,16 @@ jobs: - name: Clock ordering matrix run: npm run test:clock-matrix - - name: Publish to npm with provenance - run: npm publish --provenance --access public - - - name: Create GitHub release + - name: Verify identity and recover release env: GH_TOKEN: ${{ github.token }} - run: gh release create "$GITHUB_REF_NAME" --verify-tag --generate-notes --title "$GITHUB_REF_NAME" + run: node scripts/release-retry.mjs + + - name: Retain exact release tarballs + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: release-tarballs-${{ github.run_attempt }} + path: ${{ runner.temp }}/cmp-release-*/*.tgz + if-no-files-found: ignore + retention-days: 7 diff --git a/scripts/release-retry.mjs b/scripts/release-retry.mjs new file mode 100644 index 0000000..a312ebd --- /dev/null +++ b/scripts/release-retry.mjs @@ -0,0 +1,183 @@ +#!/usr/bin/env node + +// Called only after the workflow's full gates. npm owns Sigstore verification; +// decoded claims are usable only in its successful, matching verified record. +import { spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { basename, join } from "node:path"; + +const registry = "https://registry.npmjs.org/"; +const repository = "Comfy-Org/comfy-multi-player"; +const repositoryUrl = `https://github.com/${repository}`; +const workflowPath = ".github/workflows/release.yml"; +const provenanceType = "https://slsa.dev/provenance/v1"; + +function requireThat(condition, message) { + if (!condition) throw new Error(message); +} + +function command(program, args, cwd = process.cwd(), allowFailure = false) { + const result = spawnSync(program, args, { cwd, encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }); + requireThat(!result.error && result.status !== null, `${program} could not complete`); + requireThat(allowFailure || result.status === 0, `${program} ${args[0]} exited ${result.status}`); + return result; +} + +function readJson(path) { + return JSON.parse(readFileSync(path, "utf8")); +} + +function requireIdentity(value, expected, label) { + requireThat(value?.name === expected.name && value?.version === expected.version, `${label}: package name/version conflict`); +} + +function requireRemoteTag(ref, sha) { + const raw = command("git", ["ls-remote", "--exit-code", `${repositoryUrl}.git`, ref, `${ref}^{}`]).stdout.trim(); + const refs = new Map(); + for (const line of raw.split("\n")) { + const [commit, name, extra] = line.split("\t"); + requireThat(/^[a-f0-9]{40}$/.test(commit) && [ref, `${ref}^{}`].includes(name) && !extra && !refs.has(name), "malformed remote tag response"); + refs.set(name, commit); + } + requireThat(refs.has(ref) && (refs.get(`${ref}^{}`) ?? refs.get(ref)) === sha, "remote tag does not resolve to GITHUB_SHA"); +} + +function releaseExists(tag) { + const result = command("gh", ["api", "--hostname", "github.com", "--include", `repos/${repository}/releases/tags/${encodeURIComponent(tag)}`], process.cwd(), true); + const response = /^HTTP\/\S+ (\d{3})[^\n]*\r?\n[\s\S]*?\r?\n\r?\n([\s\S]*)$/.exec(result.stdout); + requireThat(response, "missing GitHub release HTTP response"); + const value = JSON.parse(response[2]); + if (result.status === 1 && response[1] === "404" && value?.message === "Not Found") return false; + requireThat(result.status === 0 && response[1] === "200", "GitHub release lookup failed (not a definitive 404)"); + requireThat(value?.tag_name === tag && value?.draft === false, "existing GitHub release has a different tag or is not published"); + return true; +} + +function download(url, path) { + // No redirects: registry metadata cannot redirect this probe to another host. + return command("curl", ["--silent", "--show-error", "--proto", "=https", "--connect-timeout", "30", "--max-time", "180", "--output", path, "--write-out", "%{http_code}", url]).stdout; +} + +function tarIdentity(path, expected) { + requireIdentity(JSON.parse(command("tar", ["-xOzf", path, "package/package.json"]).stdout), expected, "tarball"); +} + +function sha512(path) { + return createHash("sha512").update(readFileSync(path)).digest(); +} + +function requireProvenance(audit, expected, digest, ref, sha) { + requireThat(Array.isArray(audit?.invalid) && audit.invalid.length === 0 && + Array.isArray(audit?.missing) && audit.missing.length === 0 && Array.isArray(audit?.verified), + "audit did not return clean, verified attestation output"); + const records = audit.verified.filter((record) => record?.name === expected.name && record?.version === expected.version && + record?.registry === registry && record?.location === `node_modules/${expected.name}`); + requireThat(records.length === 1, "missing unique verified package record"); + const bundles = records[0].attestationBundles; + requireThat(Array.isArray(bundles), "missing verified attestation bundles"); + const provenance = bundles.filter((entry) => entry?.predicateType === provenanceType); + requireThat(provenance.length === 1, "missing unique verified SLSA provenance bundle"); + const envelope = provenance[0].bundle?.dsseEnvelope; + requireThat(envelope?.payloadType === "application/vnd.in-toto+json" && typeof envelope.payload === "string", "malformed verified DSSE envelope"); + const statement = JSON.parse(Buffer.from(envelope.payload, "base64").toString("utf8")); + requireThat(statement?._type === "https://in-toto.io/Statement/v1" && statement?.predicateType === provenanceType, "unexpected provenance statement type"); + // npm's purl encoding preserves the namespace slash, but escapes @. + const subjectName = `pkg:npm/${expected.name.replaceAll("@", "%40")}@${expected.version}`; + requireThat(Array.isArray(statement.subject) && statement.subject.length === 1 && + statement.subject[0]?.name === subjectName && statement.subject[0]?.digest?.sha512 === digest.toString("hex"), + "provenance subject name/digest conflict"); + const definition = statement.predicate?.buildDefinition; + const workflow = definition?.externalParameters?.workflow; + requireThat(definition?.buildType === "https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1" && + workflow?.repository === repositoryUrl && workflow?.path === workflowPath && workflow?.ref === ref, + "provenance repository/workflow/ref conflict"); + const sources = definition.resolvedDependencies?.filter((source) => source?.uri === `git+${repositoryUrl}@${ref}`); + requireThat(Array.isArray(sources) && sources.length === 1 && sources[0]?.digest?.gitCommit === sha, + "provenance source URI/commit conflict"); +} + +function verifyExisting(metadata, expected, digest, dir, ref, sha) { + requireIdentity(metadata, expected, "registry"); + const integrity = `sha512-${digest.toString("base64")}`; + requireThat(metadata.dist?.integrity === integrity, "registry/local SHA512 SRI conflict"); + const url = new URL(metadata.dist.tarball); + requireThat(url.origin === "https://registry.npmjs.org" && !url.username && !url.password && !url.hash && !url.search, + "unexpected registry tarball URL"); + const downloaded = join(dir, "registry.tgz"); + requireThat(download(url.href, downloaded) === "200", "registry tarball download failed"); + requireThat(sha512(downloaded).equals(digest), "downloaded/local SHA512 conflict"); + tarIdentity(downloaded, expected); + + // A disposable exact dependency lets the supported npm CLI verify registry + // signatures and Sigstore attestations. No dependency lifecycle scripts run. + const auditDir = join(dir, "audit"); + mkdirSync(auditDir); + writeFileSync(join(auditDir, "package.json"), JSON.stringify({ private: true })); + try { + command("npm", ["install", "--ignore-scripts", "--no-audit", "--no-fund", "--save-exact", `--registry=${registry}`, `${expected.name}@${expected.version}`], auditDir); + const location = `node_modules/${expected.name}`; + const locked = readJson(join(auditDir, "package-lock.json"))?.packages?.[location]; + requireThat(locked?.integrity === integrity && locked?.resolved === url.href, "audited dependency artifact conflict"); + requireIdentity(readJson(join(auditDir, location, "package.json")), expected, "audited dependency"); + const audit = JSON.parse(command("npm", ["audit", "signatures", "--json", "--include-attestations", "--ignore-scripts", `--registry=${registry}`], auditDir).stdout); + requireProvenance(audit, expected, digest, ref, sha); + } finally { + rmSync(auditDir, { recursive: true, force: true }); + } +} + +function release() { + const expected = readJson("package.json"); + const { GITHUB_REPOSITORY, GITHUB_REF: ref, GITHUB_REF_NAME: tag, GITHUB_SHA: sha, GITHUB_WORKFLOW_REF } = process.env; + requireThat(GITHUB_REPOSITORY === repository && expected.name === "@comfyorg/comfy-multi-player" && + typeof expected.version === "string" && tag === `v${expected.version}` && ref === `refs/tags/${tag}` && + GITHUB_WORKFLOW_REF === `${repository}/${workflowPath}@${ref}` && /^[a-f0-9]{40}$/.test(sha ?? ""), + "release context does not match package/repository/workflow/tag"); + requireThat(command("npm", ["--version"]).stdout.trim() === "11.19.0", "release requires npm 11.19.0 (verified attestation bundles)"); + requireThat(command("git", ["rev-parse", "HEAD"]).stdout.trim() === sha, "checkout is not GITHUB_SHA"); + requireRemoteTag(ref, sha); + + const dir = mkdtempSync(join(process.env.RUNNER_TEMP || tmpdir(), "cmp-release-")); + console.log(`Release diagnostics retained in ${dir}`); + const pack = JSON.parse(command("npm", ["pack", "--ignore-scripts", "--json", "--pack-destination", dir]).stdout); + requireThat(Array.isArray(pack) && pack.length === 1, "expected one packed artifact"); + requireIdentity(pack[0], expected, "pack manifest"); + const filename = pack[0].filename; + requireThat(typeof filename === "string" && basename(filename) === filename && filename.endsWith(".tgz"), "invalid packed filename"); + const tarball = join(dir, filename); + tarIdentity(tarball, expected); + const digest = sha512(tarball); + console.log(`Packed ${expected.name}@${expected.version}: sha512-${digest.toString("base64")}`); + + // Inspect both services before any write. A failed lookup is never absence. + const hasRelease = releaseExists(tag); + const metadataPath = join(dir, "registry.json"); + const status = download(`${registry}${encodeURIComponent(expected.name)}/${encodeURIComponent(expected.version)}`, metadataPath); + const metadata = readJson(metadataPath); + const absent = status === "404" && metadata === `version not found: ${expected.version}`; + if (!absent) { + requireThat(status === "200", "registry version lookup failed (not a definitive version 404)"); + verifyExisting(metadata, expected, digest, dir, ref, sha); + console.log("Existing npm artifact and verified provenance match; skipping npm publish"); + } + + // Recheck after potentially slow download/verification, before either write. + // Remote tag protection remains necessary: no client can atomically lock a tag + // across the independent npm and GitHub services. + requireRemoteTag(ref, sha); + if (absent) command("npm", ["publish", tarball, "--provenance", "--access", "public", "--ignore-scripts", `--registry=${registry}`]); + requireRemoteTag(ref, sha); + if (!hasRelease) { + command("gh", ["release", "create", tag, "--repo", repository, "--verify-tag", "--generate-notes", "--title", tag]); + } + console.log("Release recovery complete"); +} + +try { + release(); +} catch (error) { + console.error(`release recovery failed: ${error instanceof Error ? error.message : String(error)}`); + process.exitCode = 1; +} diff --git a/test/ci-release-contract.test.ts b/test/ci-release-contract.test.ts index 23c91aa..ffbd73b 100644 --- a/test/ci-release-contract.test.ts +++ b/test/ci-release-contract.test.ts @@ -25,6 +25,9 @@ const REQUIRED_STEPS = { interface Step { name?: string; run?: string; + uses?: string; + with?: Record; + env?: Record; if?: unknown; shell?: unknown; "continue-on-error"?: unknown; @@ -66,6 +69,34 @@ function requireWorkflow(document: unknown, jobId: string): void { const buildIndex = steps.findIndex((step) => step.name === "Build"); const packIndex = steps.findIndex((step) => step.name === "Verify package contents"); if (packIndex <= buildIndex) throw new Error("Verify package contents must run after Build"); + if (jobId === "publish") requireRelease(steps); +} + +function requireRelease(steps: Step[]): void { + const recovery = steps.filter((step) => step.name === "Verify identity and recover release"); + if (recovery.length !== 1 || !unconditionalGate(recovery[0]!) || + recovery[0]!.run !== "node scripts/release-retry.mjs" || recovery[0]!.env?.GH_TOKEN !== "${{ github.token }}") { + throw new Error("missing failure-propagating release identity/recovery gate"); + } + const recoveryIndex = steps.indexOf(recovery[0]!); + for (const name of Object.keys(REQUIRED_STEPS)) { + if (steps.findIndex((step) => step.name === name) >= recoveryIndex) { + throw new Error(`release recovery must follow ${name}`); + } + } + const toolchain = steps.filter((step) => step.name === "Pin release npm"); + if (toolchain.length !== 1 || !unconditionalGate(toolchain[0]!) || + toolchain[0]!.run !== "npm install --global npm@11.19.0 --ignore-scripts" || + steps.indexOf(toolchain[0]!) >= steps.findIndex((step) => step.name === "Install")) { + throw new Error("missing supported pinned npm before Install"); + } + const node = steps.find((step) => step.uses?.startsWith("actions/setup-node@")); + if (!node || !unconditionalGate(node) || node.with?.["node-version"] !== "24.21.0") { + throw new Error("missing pinned release Node"); + } + if (steps.some((step) => /npm publish|gh release create/.test(step.run ?? ""))) { + throw new Error("release writes must go through identity/recovery gate"); + } } const loadYaml = (relative: string) => parse(readFileSync(join(root, relative), "utf8")) as unknown; @@ -76,6 +107,28 @@ describe("parsed CI and release contracts", () => { (file, job) => requireWorkflow(loadYaml(`.github/workflows/${file}.yml`), job), ); + it.each(Object.keys(REQUIRED_STEPS))("rejects release recovery before %s", (name) => { + const document = loadYaml(".github/workflows/release.yml") as { jobs: { publish: { steps: Step[] } } }; + const steps = document.jobs.publish.steps; + const index = steps.findIndex((step) => step.name === "Verify identity and recover release"); + const recovery = steps.splice(index, 1)[0]!; + steps.splice(steps.findIndex((step) => step.name === name), 0, recovery); + expect(() => requireWorkflow(document, "publish")).toThrow("release recovery must follow"); + }); + + it.each([ + ["conditional recovery", (steps: Step[]) => { steps.find((step) => step.name === "Verify identity and recover release")!.if = "${{ success() }}"; }], + ["non-fatal recovery", (steps: Step[]) => { steps.find((step) => step.name === "Verify identity and recover release")!["continue-on-error"] = true; }], + ["changed helper", (steps: Step[]) => { steps.find((step) => step.name === "Verify identity and recover release")!.run = "echo skipped"; }], + ["old npm", (steps: Step[]) => { steps.find((step) => step.name === "Pin release npm")!.run = "npm install --global npm@10.9.7"; }], + ["moving Node", (steps: Step[]) => { steps.find((step) => step.uses?.startsWith("actions/setup-node@"))!.with!["node-version"] = 24; }], + ["unguarded publication", (steps: Step[]) => { steps.push({ run: "npm publish --provenance --access public" }); }], + ] as const)("rejects %s", (_name, change) => { + const document = loadYaml(".github/workflows/release.yml") as { jobs: { publish: { steps: Step[] } } }; + change(document.jobs.publish.steps); + expect(() => requireWorkflow(document, "publish")).toThrow(); + }); + it("CodeRabbit protects the workflow verifier by structured rule fields", () => { const config = loadYaml(".coderabbit.yaml") as { reviews?: { path_instructions?: Array<{ path?: string; instructions?: string }> }; diff --git a/test/release-retry.test.ts b/test/release-retry.test.ts new file mode 100644 index 0000000..f9616c9 --- /dev/null +++ b/test/release-retry.test.ts @@ -0,0 +1,307 @@ +import { createHash } from "node:crypto"; +import { spawnSync } from "node:child_process"; +import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; + +const root = dirname(dirname(fileURLToPath(import.meta.url))); +const script = join(root, "scripts/release-retry.mjs"); +const name = "@comfyorg/comfy-multi-player"; +const version = "1.2.3"; +const repo = "Comfy-Org/comfy-multi-player"; +const sha = "1234567890abcdef1234567890abcdef12345678"; +const otherSha = "abcdef1234567890abcdef1234567890abcdef12"; +const ref = `refs/tags/v${version}`; +const registry = "https://registry.npmjs.org/"; +const predicateType = "https://slsa.dev/provenance/v1"; + +// Only external processes are doubled. The release helper runs unchanged in a +// fresh process; real tar/gzip bytes, hashing, parsing and decisions run locally. +// These fixtures do NOT test Sigstore cryptography: npm owns that verification. +const commandFixture = String.raw`#!/usr/bin/env node +const fs = require('node:fs'); +const path = require('node:path'); +const f = JSON.parse(fs.readFileSync(process.env.RELEASE_FIXTURE)); +const cmd = path.basename(process.argv[1]); +const args = process.argv.slice(2); +fs.appendFileSync(f.log, JSON.stringify({cmd,args,cwd:process.cwd()})+'\n'); +const out = value => process.stdout.write(typeof value === 'string' ? value : JSON.stringify(value)); +if (cmd === 'git') { + if (args[0] === 'rev-parse') out(f.head); + else { + const probes = fs.readFileSync(f.log,'utf8').trim().split('\n').map(JSON.parse).filter(c => c.args[0] === 'ls-remote').length; + out(probes >= f.tagChangesAt ? f.changedTag : f.tag); + process.exitCode = f.tagStatus; + } +} else if (cmd === 'curl') { + const target = args[args.indexOf('--output')+1]; + const url = args.at(-1); + if (url.endsWith('.tgz')) { + fs.copyFileSync(f.download, target); out(f.downloadStatus); + } else { + fs.writeFileSync(target, typeof f.metadata === 'string' ? f.metadata : JSON.stringify(f.metadata)); + out(f.registryStatus); + } + process.exitCode = f.curlStatus; +} else if (cmd === 'gh') { + if (args[0] === 'api') { out(f.release); process.exitCode = f.releaseStatus; } + else { out('created'); process.exitCode = f.createStatus; } +} else if (cmd === 'npm') { + if (args[0] === '--version') out(f.npmVersion); + else if (args[0] === 'pack') { + const dir = args[args.indexOf('--pack-destination')+1]; + fs.copyFileSync(f.local, path.join(dir,'release.tgz')); + out([{filename:'release.tgz',name:f.packName,version:f.packVersion}]); + process.exitCode = f.packStatus; + } else if (args[0] === 'install') { + const location = 'node_modules/'+f.name; + fs.mkdirSync(location,{recursive:true}); + fs.writeFileSync(location+'/package.json',JSON.stringify({name:f.name,version:f.version})); + fs.writeFileSync('package-lock.json',JSON.stringify({packages:{[location]:{integrity:f.lockIntegrity,resolved:f.metadata.dist?.tarball}}})); + process.exitCode = f.installStatus; + } else if (args[0] === 'audit') { out(f.audit); process.exitCode = f.auditStatus; } + else if (args[0] === 'publish') { out('published'); process.exitCode = f.publishStatus; } + else throw Error('unexpected npm command '+args); +} else throw Error('unexpected command '+cmd); +`; + +function fixture() { + const dir = mkdtempSync(join(tmpdir(), "release-retry-test-")); + mkdirSync(join(dir, "bin")); + mkdirSync(join(dir, "package")); + writeFileSync(join(dir, "package.json"), JSON.stringify({ name, version })); + writeFileSync(join(dir, "package/package.json"), JSON.stringify({ name, version })); + const local = join(dir, "local.tgz"); + const tar = spawnSync("tar", ["-czf", local, "package/package.json"], { cwd: dir }); + if (tar.status !== 0) throw new Error("could not create real tar fixture"); + const digest = createHash("sha512").update(readFileSync(local)).digest(); + const integrity = `sha512-${digest.toString("base64")}`; + const statement = { + _type: "https://in-toto.io/Statement/v1", + predicateType, + subject: [{ name: `pkg:npm/%40comfyorg/comfy-multi-player@${version}`, digest: { sha512: digest.toString("hex") } }], + predicate: { buildDefinition: { + buildType: "https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1", + externalParameters: { workflow: { repository: `https://github.com/${repo}`, path: ".github/workflows/release.yml", ref } }, + resolvedDependencies: [{ uri: `git+https://github.com/${repo}@${ref}`, digest: { gitCommit: sha } }], + } }, + }; + const record = { + name, version, location: `node_modules/${name}`, registry, + attestations: { provenance: { predicateType } }, + attestationBundles: [{ predicateType, bundle: { dsseEnvelope: { payloadType: "application/vnd.in-toto+json", payload: "" } } }], + }; + const data = { + name, version, log: join(dir, "commands.jsonl"), local, download: local, + head: sha, tag: `${sha}\t${ref}\n`, tagStatus: 0, + tagChangesAt: 99, changedTag: `${otherSha}\t${ref}\n`, env: {} as Record, + npmVersion: "11.19.0", packName: name, packVersion: version, packStatus: 0, + metadata: { name, version, dist: { integrity, tarball: `${registry}@comfyorg/comfy-multi-player/-/comfy-multi-player-${version}.tgz` } } as unknown, + registryStatus: "200", curlStatus: 0, downloadStatus: "200", lockIntegrity: integrity, + release: `HTTP/2.0 404 Not Found\r\nContent-Type: application/json\r\n\r\n${JSON.stringify({ message: "Not Found" })}`, + releaseStatus: 1, createStatus: 0, publishStatus: 0, installStatus: 0, auditStatus: 0, + audit: { invalid: [], missing: [], verified: [record] } as unknown, + }; + const executable = join(dir, "bin/command.cjs"); + writeFileSync(executable, commandFixture); + chmodSync(executable, 0o755); + for (const cmd of ["npm", "git", "curl", "gh"]) symlinkSync(executable, join(dir, "bin", cmd)); + return { dir, data, record, statement }; +} + +type Fixture = ReturnType; +type Command = { cmd: string; args: string[]; cwd: string }; +function run(change: (f: Fixture) => void = () => {}) { + const f = fixture(); + try { + change(f); + const envelope = f.record.attestationBundles[0]?.bundle.dsseEnvelope; + if (envelope && !envelope.payload) envelope.payload = Buffer.from(JSON.stringify(f.statement)).toString("base64"); + writeFileSync(join(f.dir, "fixture.json"), JSON.stringify(f.data)); + const result = spawnSync(process.execPath, [script], { + cwd: f.dir, encoding: "utf8", + env: { ...process.env, PATH: `${join(f.dir, "bin")}:${process.env.PATH}`, RELEASE_FIXTURE: join(f.dir, "fixture.json"), + RUNNER_TEMP: f.dir, GITHUB_REPOSITORY: repo, GITHUB_SHA: sha, GITHUB_REF: ref, + GITHUB_REF_NAME: `v${version}`, GITHUB_WORKFLOW_REF: `${repo}/.github/workflows/release.yml@${ref}`, ...f.data.env }, + }); + const commands: Command[] = existsSync(f.data.log) ? readFileSync(f.data.log, "utf8").trim().split("\n").map((line) => JSON.parse(line) as Command) : []; + return { ...result, commands, writes: commands.filter(({ cmd, args }) => + (cmd === "npm" && args[0] === "publish") || (cmd === "gh" && args[0] === "release")) }; + } finally { + rmSync(f.dir, { recursive: true, force: true }); + } +} + +function absent(f: Fixture) { + f.data.registryStatus = "404"; + f.data.metadata = JSON.stringify(`version not found: ${version}`); +} + +function existingRelease(f: Fixture) { + f.data.releaseStatus = 0; + f.data.release = `HTTP/2.0 200 OK\r\nContent-Type: application/json\r\n\r\n${JSON.stringify({ tag_name: `v${version}`, draft: false })}`; +} + +describe("release retry subprocess orchestration (offline)", () => { + it.each([false, true])("publishes the one packed tarball after definitive version 404 (existing release: %s)", (hasRelease) => { + const result = run((f) => { + absent(f); + if (hasRelease) existingRelease(f); + }); + expect(result.status, result.stderr).toBe(0); + expect(result.writes.map(({ cmd, args }) => [cmd, ...args.slice(0, 2)])).toEqual([ + ["npm", "publish", expect.stringMatching(/release\.tgz$/)], + ...(hasRelease ? [] : [["gh", "release", "create"]]), + ]); + expect(result.writes[0]!.args).toEqual(["publish", expect.stringMatching(/release\.tgz$/), "--provenance", "--access", "public", "--ignore-scripts", `--registry=${registry}`]); + expect(result.commands.filter(({ args }) => args[0] === "pack")).toHaveLength(1); + expect(result.commands.findIndex(({ args }) => args[0] === "ls-remote")).toBeLessThan(result.commands.indexOf(result.writes[0]!)); + expect(result.commands.findIndex(({ args }) => args[0] === "api")).toBeLessThan(result.commands.indexOf(result.writes[0]!)); + expect(result.commands.some(({ args }) => args[0] === "audit")).toBe(false); + }); + + it("skips only npm publish for exact bytes with verified provenance and creates the missing release", () => { + const result = run(); + expect(result.status, result.stderr).toBe(0); + expect(result.writes.map(({ cmd, args }) => [cmd, ...args])).toEqual([ + ["gh", "release", "create", `v${version}`, "--repo", repo, "--verify-tag", "--generate-notes", "--title", `v${version}`], + ]); + expect(result.commands.find(({ args }) => args[0] === "install")?.args).toEqual([ + "install", "--ignore-scripts", "--no-audit", "--no-fund", "--save-exact", `--registry=${registry}`, `${name}@${version}`, + ]); + expect(result.commands.find(({ args }) => args[0] === "audit")?.args).toEqual([ + "audit", "signatures", "--json", "--include-attestations", "--ignore-scripts", `--registry=${registry}`, + ]); + }); + + it("does no writes when both artifacts already match, including an annotated remote tag", () => { + const result = run((f) => { existingRelease(f); f.data.tag = `${otherSha}\t${ref}\n${sha}\t${ref}^{}\n`; }); + expect(result.status, result.stderr).toBe(0); + expect(result.writes).toEqual([]); + }); + + const conflicts: Array<[string, (f: Fixture) => void]> = [ + ["tag changes during verification", (f) => { f.data.tagChangesAt = 2; }], + ["wrong event tag", (f) => { f.data.env.GITHUB_REF_NAME = "v9.9.9"; }], + ["wrong workflow context", (f) => { f.data.env.GITHUB_WORKFLOW_REF = `${repo}/.github/workflows/other.yml@${ref}`; }], + ["wrong remote commit", (f) => { f.data.tag = `${otherSha}\t${ref}\n`; }], + ["wrong annotated peeled commit", (f) => { f.data.tag = `${sha}\t${ref}\n${otherSha}\t${ref}^{}\n`; }], + ["missing remote tag", (f) => { f.data.tag = ""; }], + ["tag transport failure", (f) => { f.data.tagStatus = 128; }], + ["malformed remote tag", (f) => { f.data.tag = "not a ref"; }], + ["wrong checkout", (f) => { f.data.head = otherSha; }], + ["npm10 silently ignores include-attestations", (f) => { f.data.npmVersion = "10.9.7"; }], + ["pack failure", (f) => { f.data.packStatus = 1; }], + ["pack manifest name", (f) => { f.data.packName = "other"; }], + ["pack manifest version", (f) => { f.data.packVersion = "9.9.9"; }], + ["registry auth", (f) => { f.data.registryStatus = "401"; }], + ["registry rate limit", (f) => { f.data.registryStatus = "429"; }], + ["registry outage", (f) => { f.data.registryStatus = "503"; }], + ["registry network", (f) => { f.data.curlStatus = 7; }], + ["registry malformed JSON", (f) => { f.data.metadata = "not-json"; }], + ["ambiguous registry 404", (f) => { f.data.registryStatus = "404"; f.data.metadata = "Not Found"; }], + ["registry name", (f) => { (f.data.metadata as { name: string }).name = "other"; }], + ["registry version", (f) => { (f.data.metadata as { version: string }).version = "9.9.9"; }], + ["registry digest", (f) => { (f.data.metadata as { dist: { integrity: string } }).dist.integrity = "sha512-wrong"; }], + ["registry tarball from another host", (f) => { (f.data.metadata as { dist: { tarball: string } }).dist.tarball = "https://example.com/package.tgz"; }], + ["download bytes", (f) => { + // Still a valid package with the expected name/version: tar parsing or + // identity checks must not accidentally stand in for byte comparison. + writeFileSync(join(f.dir, "package/extra.txt"), "different artifact"); + f.data.download = join(f.dir, "different.tgz"); + expect(spawnSync("tar", ["-czf", f.data.download, "package"], { cwd: f.dir }).status).toBe(0); + }], + ["download unavailable", (f) => { f.data.downloadStatus = "503"; }], + ["different install integrity", (f) => { f.data.lockIntegrity = "sha512-wrong"; }], + ["install failure", (f) => { f.data.installStatus = 1; }], + ["cryptographic audit failure despite matching decoded claims", (f) => { f.data.auditStatus = 1; }], + ["npm10-shaped output from successful audit", (f) => { f.data.audit = { invalid: [], missing: [] }; }], + ["invalid audit entry", (f) => { f.data.audit = { invalid: [{}], missing: [], verified: [f.record] }; }], + ["missing audit entry", (f) => { f.data.audit = { invalid: [], missing: [{}], verified: [f.record] }; }], + ["malformed audit output", (f) => { f.data.audit = "not-json"; }], + ["duplicate verified records", (f) => { f.data.audit = { invalid: [], missing: [], verified: [f.record, f.record] }; }], + ["unverified attestation marker", (f) => { f.record.attestationBundles = []; }], + ["malformed verified payload", (f) => { f.record.attestationBundles[0]!.bundle.dsseEnvelope.payload = Buffer.from("not-json").toString("base64"); }], + ["wrong verified payload type", (f) => { f.record.attestationBundles[0]!.bundle.dsseEnvelope.payloadType = "text/plain"; }], + ["wrong verified package", (f) => { f.record.name = "other"; }], + ["wrong verified version", (f) => { f.record.version = "9.9.9"; }], + ["wrong verified registry", (f) => { f.record.registry = "https://example.com/"; }], + ["wrong verified location", (f) => { f.record.location = "node_modules/other"; }], + ["wrong bundle predicate", (f) => { f.record.attestationBundles[0]!.predicateType = "other"; }], + ["wrong statement predicate", (f) => { f.statement.predicateType = "other"; }], + ["wrong subject name", (f) => { f.statement.subject[0]!.name = "pkg:npm/other@1.2.3"; }], + ["wrong subject digest", (f) => { f.statement.subject[0]!.digest.sha512 = "0".repeat(128); }], + ["wrong repository", (f) => { f.statement.predicate.buildDefinition.externalParameters.workflow.repository += "-other"; }], + ["wrong workflow", (f) => { f.statement.predicate.buildDefinition.externalParameters.workflow.path = ".github/workflows/other.yml"; }], + ["wrong ref", (f) => { f.statement.predicate.buildDefinition.externalParameters.workflow.ref = "refs/heads/main"; }], + ["wrong source commit", (f) => { f.statement.predicate.buildDefinition.resolvedDependencies[0]!.digest.gitCommit = otherSha; }], + ["matching commit on unrelated source URI", (f) => { f.statement.predicate.buildDefinition.resolvedDependencies[0]!.uri = "git+https://github.com/other/repo@" + ref; }], + ["unrelated matching dependency beside wrong source", (f) => { + f.statement.predicate.buildDefinition.resolvedDependencies[0]!.digest.gitCommit = otherSha; + f.statement.predicate.buildDefinition.resolvedDependencies.push({ uri: `git+https://github.com/other/repo@${ref}`, digest: { gitCommit: sha } }); + }], + ["release draft", (f) => { existingRelease(f); f.data.release = f.data.release.replace('"draft":false', '"draft":true'); }], + ["release wrong tag", (f) => { existingRelease(f); f.data.release = f.data.release.replace(`v${version}`, "v9.9.9"); }], + ["release auth", (f) => { f.data.release = f.data.release.replace("404", "401"); }], + ["release rate limit", (f) => { f.data.release = f.data.release.replace("404", "429"); }], + ["release outage", (f) => { f.data.release = f.data.release.replace("404", "503"); }], + ["release network", (f) => { f.data.release = ""; }], + ["release malformed JSON", (f) => { f.data.release = "HTTP/2.0 200 OK\r\n\r\nno-json"; f.data.releaseStatus = 0; }], + ["release ambiguous 404", (f) => { f.data.release = "HTTP/2.0 404 Not Found\r\n\r\n{}"; }], + ]; + + it.each(conflicts)("fails closed with no publish/create: %s", (_label, change) => { + const result = run(change); + expect(result.status).toBe(1); + expect(result.stderr).toContain("release recovery failed:"); + expect(result.writes).toEqual([]); + }); + + it("does not publish even an absent version if release inspection is uncertain", () => { + const result = run((f) => { absent(f); f.data.release = "network failure"; }); + expect(result.status).toBe(1); + expect(result.writes).toEqual([]); + }); + + it.each(["name", "version"])("rejects conflicting %s inside the actual local archive", (field) => { + const result = run((f) => { + writeFileSync(join(f.dir, "package/package.json"), JSON.stringify({ name, version, [field]: "other" })); + expect(spawnSync("tar", ["-czf", f.data.local, "package/package.json"], { cwd: f.dir }).status).toBe(0); + const digest = createHash("sha512").update(readFileSync(f.data.local)).digest(); + f.data.lockIntegrity = `sha512-${digest.toString("base64")}`; + (f.data.metadata as { dist: { integrity: string } }).dist.integrity = f.data.lockIntegrity; + f.statement.subject[0]!.digest.sha512 = digest.toString("hex"); + }); + expect(result.status).toBe(1); + expect(result.stderr).toContain("tarball: package name/version conflict"); + expect(result.writes).toEqual([]); + }); + + it.each([false, true])("refuses success if the tag moves during npm publication (existing release: %s)", (hasRelease) => { + const result = run((f) => { + absent(f); + if (hasRelease) existingRelease(f); + f.data.tagChangesAt = 3; + }); + expect(result.status).toBe(1); + expect(result.stderr).toContain("remote tag does not resolve to GITHUB_SHA"); + expect(result.writes.map(({ cmd }) => cmd)).toEqual(["npm"]); + }); + + it("propagates a publish failure without creating a release", () => { + const result = run((f) => { absent(f); f.data.publishStatus = 1; }); + expect(result.status).toBe(1); + expect(result.writes.map(({ cmd }) => cmd)).toEqual(["npm"]); + }); + + it("propagates a release creation failure; the exact-existing path recovers on retry", () => { + const first = run((f) => { absent(f); f.data.createStatus = 1; }); + expect(first.status).toBe(1); + expect(first.writes.map(({ cmd }) => cmd)).toEqual(["npm", "gh"]); + const retry = run(); + expect(retry.status, retry.stderr).toBe(0); + expect(retry.writes.map(({ cmd }) => cmd)).toEqual(["gh"]); + }); +});