Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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
183 changes: 183 additions & 0 deletions scripts/release-retry.mjs
Original file line number Diff line number Diff line change
@@ -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)");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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;
}
53 changes: 53 additions & 0 deletions test/ci-release-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const REQUIRED_STEPS = {
interface Step {
name?: string;
run?: string;
uses?: string;
with?: Record<string, unknown>;
env?: Record<string, unknown>;
if?: unknown;
shell?: unknown;
"continue-on-error"?: unknown;
Expand Down Expand Up @@ -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;
Expand All @@ -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 }> };
Expand Down
Loading
Loading