mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
ci(publish): bind Lambda release retries to immutable run artifacts (#7920)
* ci(publish): reuse Lambda assets only from a release this run created (SECOPS-1217) The pack job downloaded any Lambda handler zip or video image already on the release and pinned it into @hyperframes/aws-lambda without checking where it came from, so anyone with contents write could pre-stage both before a stable release. A new step fails the run when the release exists but github-actions[bot] did not create it during an earlier attempt of this run, or when a Lambda asset on it was uploaded by anyone else. The pin steps reuse an asset only when that step allows it. * fix: bind release retries to immutable run artifacts * fix: fail release digest pipelines before side effects
This commit is contained in:
@@ -191,208 +191,16 @@ test("the Lambda handler zip is pinned before packing and attached before npm pu
|
||||
assert.ok(
|
||||
indexIn(publish, "Attach Lambda assets to release") < indexIn(publish, "Publish packages"),
|
||||
);
|
||||
const attach = publish.steps[indexIn(publish, "Attach Lambda assets to release")];
|
||||
assert.doesNotMatch(attach.run, /--clobber/);
|
||||
});
|
||||
|
||||
test("the pack job pins only the Lambda assets this run built", () => {
|
||||
for (const step of pack.steps) {
|
||||
assert.doesNotMatch(step.run ?? "", /\bgh release\b/, step.name);
|
||||
assert.ok(!JSON.stringify(step.env ?? {}).includes("secrets."), step.name);
|
||||
}
|
||||
test("packing pins only the freshly built zip and never downloads release assets", () => {
|
||||
const step = pack.steps.find((s) => s.name === "Pin Lambda handler zip");
|
||||
const builtZip = "built handler zip";
|
||||
const builtSha256 = createHash("sha256").update(builtZip).digest("hex");
|
||||
const run = (expectedSha256) => {
|
||||
const root = mkdtempSync(join(tmpdir(), "hyperframes-lambda-zip-step-"));
|
||||
try {
|
||||
const bin = join(root, "bin");
|
||||
mkdirSync(bin);
|
||||
mkdirSync(join(root, "dist", "lambda-handler-build"), { recursive: true });
|
||||
writeFileSync(join(root, "dist", "lambda-handler-build", "handler.zip"), builtZip);
|
||||
const log = join(root, "calls.log");
|
||||
writeFileSync(log, "");
|
||||
writeFileSync(join(bin, "bun"), `#!/bin/sh\necho "bun $*" >> "${log}"\n`);
|
||||
chmodSync(join(bin, "bun"), 0o755);
|
||||
const result = spawnSync("bash", ["-euo", "pipefail", "-c", step.run], {
|
||||
cwd: root,
|
||||
encoding: "utf8",
|
||||
env: {
|
||||
...process.env,
|
||||
PATH: `${bin}:${process.env.PATH}`,
|
||||
EXPECTED_SHA256: expectedSha256,
|
||||
},
|
||||
timeout: 5_000,
|
||||
});
|
||||
let pinned = null;
|
||||
try {
|
||||
pinned = readFileSync(join(root, "dist", "hyperframes-lambda-handler.zip"), "utf8");
|
||||
} catch {}
|
||||
return {
|
||||
status: result.status,
|
||||
stdout: result.stdout,
|
||||
calls: readFileSync(log, "utf8"),
|
||||
pinned,
|
||||
};
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
};
|
||||
|
||||
const built = run(builtSha256);
|
||||
assert.equal(built.status, 0, built.stdout);
|
||||
assert.match(built.calls, /^bun packages\/aws-lambda\/scripts\/pin-handler-zip\.ts$/m);
|
||||
assert.equal(built.pinned, builtZip);
|
||||
|
||||
for (const expectedSha256 of ["0".repeat(64), ""]) {
|
||||
const rejected = run(expectedSha256);
|
||||
assert.equal(rejected.status, 1, rejected.stdout);
|
||||
assert.match(rejected.stdout, /::error::Lambda handler zip sha256/);
|
||||
assert.doesNotMatch(rejected.calls, /pin-handler-zip/);
|
||||
assert.equal(rejected.pinned, null);
|
||||
}
|
||||
});
|
||||
|
||||
test("the release keeps only Lambda assets that npm pins or this run produced", () => {
|
||||
const step = publish.steps.find((s) => s.name === "Attach Lambda assets to release");
|
||||
assert.ok(step);
|
||||
const sha = (text) => createHash("sha256").update(text).digest("hex");
|
||||
const assets = {
|
||||
"hyperframes-lambda-handler.zip": ["hyperframes-lambda-handler.sha256", "handler-zip.json"],
|
||||
"hyperframes-lambda-video-arm64.oci.tar": [
|
||||
"hyperframes-lambda-video-arm64.sha256",
|
||||
"video-image.json",
|
||||
],
|
||||
};
|
||||
const run = ({ npm = 404, pins, release = {}, attempt = 1, badIntegrity = false }) => {
|
||||
const temp = mkdtempSync(join(tmpdir(), "hyperframes-attach-step-"));
|
||||
try {
|
||||
const bin = join(temp, "bin");
|
||||
const bundle = join(temp, "release-bundle", "assets");
|
||||
mkdirSync(bin);
|
||||
mkdirSync(bundle, { recursive: true });
|
||||
for (const [asset, [sidecar]] of Object.entries(assets)) {
|
||||
writeFileSync(join(bundle, asset), `bundle ${asset}`);
|
||||
writeFileSync(join(bundle, sidecar), `${sha(`bundle ${asset}`)} ${asset}\n`);
|
||||
}
|
||||
const fixture = join(temp, "fixture");
|
||||
mkdirSync(join(fixture, "package", "prebuilt"), { recursive: true });
|
||||
for (const [asset, [, manifest]] of Object.entries(assets)) {
|
||||
const pinned = pins?.[asset] ?? `pinned ${asset}`;
|
||||
writeFileSync(
|
||||
join(fixture, "package", "prebuilt", manifest),
|
||||
JSON.stringify({ sha256: sha(pinned) }),
|
||||
);
|
||||
}
|
||||
const tarball = join(temp, "aws-lambda.tgz");
|
||||
execFileSync("tar", ["-czf", tarball, "-C", fixture, "package"], { stdio: "pipe" });
|
||||
const integrity = `sha512-${createHash("sha512").update(readFileSync(tarball)).digest("base64")}`;
|
||||
writeFileSync(
|
||||
join(temp, "meta.json"),
|
||||
JSON.stringify({
|
||||
dist: {
|
||||
tarball: "https://registry.invalid/aws-lambda.tgz",
|
||||
integrity: badIntegrity ? "sha512-other" : integrity,
|
||||
},
|
||||
}),
|
||||
);
|
||||
writeFileSync(
|
||||
join(temp, "release.tsv"),
|
||||
Object.entries(release)
|
||||
.map(
|
||||
([asset, contents]) =>
|
||||
`${asset}\t${contents === null ? "" : `sha256:${sha(contents)}`}\n`,
|
||||
)
|
||||
.join(""),
|
||||
);
|
||||
const log = join(temp, "calls.log");
|
||||
writeFileSync(log, "");
|
||||
writeFileSync(
|
||||
join(bin, "curl"),
|
||||
[
|
||||
"#!/bin/bash",
|
||||
`echo "curl $*" >> "${log}"`,
|
||||
'while [ $# -gt 0 ]; do case "$1" in -o) out="$2"; shift ;; -w) status=1 ;; esac; shift; done',
|
||||
`if [ -n "$status" ]; then cp "${join(temp, "meta.json")}" "$out"; printf '%s' "${npm}"; else cp "${tarball}" "$out"; fi`,
|
||||
].join("\n"),
|
||||
);
|
||||
writeFileSync(
|
||||
join(bin, "gh"),
|
||||
`#!/bin/sh\necho "gh $*" >> "${log}"\nif [ "$1" = api ]; then cat "${join(temp, "release.tsv")}"; fi\n`,
|
||||
);
|
||||
chmodSync(join(bin, "curl"), 0o755);
|
||||
chmodSync(join(bin, "gh"), 0o755);
|
||||
const result = runStep(step, {
|
||||
cwd: temp,
|
||||
path: `${bin}:${process.env.PATH}`,
|
||||
env: {
|
||||
RUNNER_TEMP: temp,
|
||||
VERSION: "1.2.3",
|
||||
REPO: "o/r",
|
||||
GH_TOKEN: "test-token",
|
||||
RUN_ATTEMPT: String(attempt),
|
||||
},
|
||||
});
|
||||
const uploads = readFileSync(log, "utf8")
|
||||
.split("\n")
|
||||
.filter((line) => line.startsWith("gh release upload"));
|
||||
return { ...result, uploads };
|
||||
} finally {
|
||||
rmSync(temp, { recursive: true, force: true });
|
||||
}
|
||||
};
|
||||
const names = Object.keys(assets);
|
||||
const bundleRelease = Object.fromEntries(names.map((name) => [name, `bundle ${name}`]));
|
||||
const foreignRelease = Object.fromEntries(names.map((name) => [name, `other ${name}`]));
|
||||
const pinnedRelease = Object.fromEntries(names.map((name) => [name, `pinned ${name}`]));
|
||||
|
||||
const fresh = run({});
|
||||
assert.equal(fresh.status, 0, fresh.stdout + fresh.stderr);
|
||||
assert.equal(fresh.uploads.length, 2);
|
||||
for (const line of fresh.uploads) {
|
||||
assert.match(line, /^gh release upload v1\.2\.3 \S+ \S+\.sha256 --repo o\/r$/);
|
||||
}
|
||||
|
||||
const rerunPublish = run({ release: bundleRelease });
|
||||
assert.equal(rerunPublish.status, 0, rerunPublish.stdout + rerunPublish.stderr);
|
||||
assert.deepEqual(rerunPublish.uploads, []);
|
||||
|
||||
const foreign = run({ release: foreignRelease });
|
||||
assert.equal(foreign.status, 1);
|
||||
assert.match(
|
||||
foreign.stdout,
|
||||
/::error::Release v1\.2\.3 already has .* this run did not attach it/,
|
||||
);
|
||||
assert.deepEqual(foreign.uploads, []);
|
||||
|
||||
const rerunReplaces = run({ release: foreignRelease, attempt: 2 });
|
||||
assert.equal(rerunReplaces.status, 0, rerunReplaces.stdout + rerunReplaces.stderr);
|
||||
assert.equal(rerunReplaces.uploads.length, 2);
|
||||
for (const line of rerunReplaces.uploads) assert.match(line, / --clobber$/);
|
||||
assert.match(rerunReplaces.stdout, /::warning::Replacing/);
|
||||
|
||||
const keepsPinned = run({ npm: 200, release: pinnedRelease, attempt: 2 });
|
||||
assert.equal(keepsPinned.status, 0, keepsPinned.stdout + keepsPinned.stderr);
|
||||
assert.deepEqual(keepsPinned.uploads, []);
|
||||
|
||||
for (const release of [foreignRelease, bundleRelease, {}]) {
|
||||
const unpinned = run({ npm: 200, release, attempt: 2 });
|
||||
assert.equal(unpinned.status, 1, unpinned.stdout);
|
||||
assert.match(unpinned.stdout, /::error::@hyperframes\/aws-lambda@1\.2\.3 on npm pins/);
|
||||
assert.deepEqual(unpinned.uploads, []);
|
||||
}
|
||||
|
||||
const tamperedNpm = run({ npm: 200, release: pinnedRelease, badIntegrity: true });
|
||||
assert.equal(tamperedNpm.status, 1);
|
||||
assert.match(tamperedNpm.stdout, /does not match its npm integrity/);
|
||||
|
||||
const registryDown = run({ npm: 503 });
|
||||
assert.equal(registryDown.status, 1);
|
||||
assert.match(registryDown.stdout, /npm registry answered 503/);
|
||||
assert.deepEqual(registryDown.uploads, []);
|
||||
|
||||
const noDigest = run({ release: { [names[0]]: null } });
|
||||
assert.equal(noDigest.status, 1);
|
||||
assert.match(noDigest.stdout, /without a digest/);
|
||||
assert.deepEqual(noDigest.uploads, []);
|
||||
assert.match(step.run, /ACTUAL_SHA256.*sha256sum/);
|
||||
assert.match(step.run, /ACTUAL_SHA256.*!=.*EXPECTED_SHA256/);
|
||||
assert.doesNotMatch(workflow, /gh release download/);
|
||||
assert.equal(step.env.EXPECTED_SHA256, "${{ needs.lambda-handler-zip.outputs.sha256 }}");
|
||||
});
|
||||
|
||||
const stepOf = (job, name) => {
|
||||
@@ -436,7 +244,7 @@ test("the workflow publishes exactly the public workspace packages", () => {
|
||||
});
|
||||
|
||||
test("release-commit code runs only in a job without secrets", () => {
|
||||
assert.deepEqual(pack.permissions, { contents: "read" });
|
||||
assert.deepEqual(pack.permissions, { contents: "read", actions: "read" });
|
||||
assert.equal(pack.environment, undefined);
|
||||
const packCheckout = pack.steps.find((step) => step.uses?.startsWith("actions/checkout@"));
|
||||
assert.equal(packCheckout.with["persist-credentials"], false);
|
||||
@@ -602,8 +410,8 @@ test("publishing skips only identical uploads and fails on a version it did not
|
||||
assert.doesNotMatch(squatted.calls, /npm publish/);
|
||||
|
||||
const rerun = run({ viewResult: "sha512-other", attempt: 2 });
|
||||
assert.equal(rerun.status, 0, rerun.stdout);
|
||||
assert.match(rerun.stdout, /::warning::/);
|
||||
assert.equal(rerun.status, 1, rerun.stdout);
|
||||
assert.match(rerun.stdout, /this run did not publish it/);
|
||||
assert.doesNotMatch(rerun.calls, /npm publish/);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,342 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { parse } from "yaml";
|
||||
|
||||
const workflow = parse(
|
||||
readFileSync(new URL("../.github/workflows/publish.yml", import.meta.url), "utf8"),
|
||||
);
|
||||
const names = [
|
||||
"hyperframes-lambda-handler.zip",
|
||||
"hyperframes-lambda-handler.sha256",
|
||||
"hyperframes-lambda-video-arm64.oci.tar",
|
||||
"hyperframes-lambda-video-arm64.sha256",
|
||||
];
|
||||
const hash = (value) => createHash("sha256").update(value).digest("hex");
|
||||
const step = (name, job = "publish") => workflow.jobs[job].steps.find((s) => s.name === name);
|
||||
|
||||
function fixture(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "hf-release-receipt-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
for (const dir of [
|
||||
"bin",
|
||||
"release-bundle/assets",
|
||||
"release-receipt",
|
||||
"dist/lambda-handler-build",
|
||||
])
|
||||
mkdirSync(join(root, dir), { recursive: true });
|
||||
for (const name of names)
|
||||
writeFileSync(join(root, "release-bundle/assets", name), `built ${name}`);
|
||||
writeFileSync(join(root, "dist/lambda-handler-build/handler.zip"), "built zip");
|
||||
const statePath = join(root, "state.json");
|
||||
const initial = { release: null, assets: [], calls: [], artifacts: [], failUpload: null };
|
||||
writeFileSync(statePath, JSON.stringify(initial));
|
||||
// Only this offline stub can service gh calls. No credentials or real network calls.
|
||||
writeFileSync(
|
||||
join(root, "bin/gh"),
|
||||
`#!${process.execPath}
|
||||
const fs = require('node:fs'); const crypto = require('node:crypto');
|
||||
const state = JSON.parse(fs.readFileSync(process.env.STATE_FILE, 'utf8'));
|
||||
const args = process.argv.slice(2); state.calls.push(args);
|
||||
let response; let status = 0;
|
||||
const route = args.find(a => a.startsWith('repos/') || a.startsWith('https://uploads.github.com/'));
|
||||
if (route?.includes('/actions/runs/')) response = {artifacts:state.artifacts};
|
||||
else if (route?.endsWith('/generate-notes')) response = 'generated notes';
|
||||
else if (route === 'repos/test/repo/releases' && args.includes('POST')) {
|
||||
if (state.release) status = 1;
|
||||
else { state.release = {id:123, tag_name:'v9.8.7'}; response = state.release; }
|
||||
} else if (route?.includes('/releases/tags/')) {
|
||||
if (!state.release) status = 1; else response = state.release;
|
||||
} else if (route?.startsWith('https://uploads.github.com/')) {
|
||||
const id = Number(route.split('/releases/')[1].split('/')[0]);
|
||||
const name = new URL(route).searchParams.get('name');
|
||||
if (state.release?.id !== id || state.failUpload === name) status = 1;
|
||||
else {
|
||||
const file = args[args.indexOf('--input') + 1];
|
||||
response = {id:state.assets.length+1,name,state:'uploaded',digest:'sha256:'+crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex')};
|
||||
state.assets.push(response);
|
||||
}
|
||||
} else if (route?.includes('/releases/123/assets')) response = state.assets;
|
||||
else status = 1;
|
||||
fs.writeFileSync(process.env.STATE_FILE, JSON.stringify(state));
|
||||
if (response !== undefined) process.stdout.write(typeof response === 'string' ? response : JSON.stringify(response));
|
||||
process.exit(status);
|
||||
`,
|
||||
{ mode: 0o755 },
|
||||
);
|
||||
writeFileSync(
|
||||
join(root, "bin/bun"),
|
||||
"#!/bin/sh\ncp dist/hyperframes-lambda-handler.zip pinned.zip\n",
|
||||
{ mode: 0o755 },
|
||||
);
|
||||
// Registry responses and package bytes are local fixtures; curl never accesses the network.
|
||||
writeFileSync(
|
||||
join(root, "bin/curl"),
|
||||
`#!${process.execPath}
|
||||
const fs = require('node:fs'); const path = require('node:path');
|
||||
const args = process.argv.slice(2); const root = process.env.RUNNER_TEMP;
|
||||
if (process.env.NPM_STATUS === 'network-error') process.exit(7);
|
||||
const out = args[args.indexOf('-o') + 1];
|
||||
if (args.includes('-w')) {
|
||||
fs.copyFileSync(path.join(root, 'npm-meta.json'), out);
|
||||
process.stdout.write(process.env.NPM_STATUS || '404');
|
||||
} else fs.copyFileSync(path.join(root, 'npm-package.tgz'), out);
|
||||
`,
|
||||
{ mode: 0o755 },
|
||||
);
|
||||
const published = (pins = {}) => {
|
||||
const dir = join(root, "npm-fixture/package/prebuilt");
|
||||
mkdirSync(dir, { recursive: true });
|
||||
for (const [manifest, asset] of [
|
||||
["handler-zip.json", names[0]],
|
||||
["video-image.json", names[2]],
|
||||
])
|
||||
writeFileSync(
|
||||
join(dir, manifest),
|
||||
JSON.stringify({ sha256: pins[manifest] ?? hash(`built ${asset}`) }),
|
||||
);
|
||||
execFileSync("tar", [
|
||||
"-czf",
|
||||
join(root, "npm-package.tgz"),
|
||||
"-C",
|
||||
join(root, "npm-fixture"),
|
||||
"package",
|
||||
]);
|
||||
const integrity =
|
||||
"sha512-" +
|
||||
createHash("sha512")
|
||||
.update(readFileSync(join(root, "npm-package.tgz")))
|
||||
.digest("base64");
|
||||
writeFileSync(
|
||||
join(root, "npm-meta.json"),
|
||||
JSON.stringify({ dist: { tarball: "https://fixture.invalid/package.tgz", integrity } }),
|
||||
);
|
||||
};
|
||||
published();
|
||||
const state = () => JSON.parse(readFileSync(statePath, "utf8"));
|
||||
const update = (changes) => writeFileSync(statePath, JSON.stringify({ ...state(), ...changes }));
|
||||
const receiptPath = join(root, "release-receipt/receipt.json");
|
||||
const receipt = () => JSON.parse(readFileSync(receiptPath, "utf8"));
|
||||
const run = (name, env = {}, job = "publish") => {
|
||||
const command = step(name, job);
|
||||
assert.ok(command, `Missing workflow step: ${name}`);
|
||||
return spawnSync(
|
||||
"bash",
|
||||
[
|
||||
"-e",
|
||||
...(workflow.defaults?.run?.shell === "bash" ? ["-o", "pipefail"] : []),
|
||||
"-c",
|
||||
command.run,
|
||||
],
|
||||
{
|
||||
cwd: root,
|
||||
encoding: "utf8",
|
||||
timeout: 5000,
|
||||
env: {
|
||||
PATH: `${join(root, "bin")}:${process.env.PATH}`,
|
||||
STATE_FILE: statePath,
|
||||
RUNNER_TEMP: root,
|
||||
GITHUB_OUTPUT: join(root, "outputs"),
|
||||
REPO: "test/repo",
|
||||
VERSION: "9.8.7",
|
||||
RUN_ID: "55",
|
||||
RUN_NUMBER: "55",
|
||||
EXPECTED_RELEASE_SHA: "a".repeat(40),
|
||||
PRERELEASE: "false",
|
||||
RELEASE_ID: "123",
|
||||
...env,
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
return { root, run, state, update, receipt, receiptPath, published };
|
||||
}
|
||||
const succeeds = (result) => assert.equal(result.status, 0, result.stdout + result.stderr);
|
||||
const fails = (result) => assert.notEqual(result.status, 0, result.stdout + result.stderr);
|
||||
|
||||
test("a first publish creates a durable receipt before uploads; same-run retry preserves bytes", (t) => {
|
||||
const f = fixture(t);
|
||||
succeeds(f.run("Create GitHub Release"));
|
||||
assert.equal(f.receipt().release_id, 123);
|
||||
assert.equal(f.receipt().run, "55");
|
||||
assert.equal(f.receipt().assets[names[0]], hash(`built ${names[0]}`));
|
||||
succeeds(f.run("Attach Lambda assets to release"));
|
||||
succeeds(f.run("Verify Lambda release before npm publication"));
|
||||
succeeds(f.run("Create GitHub Release"));
|
||||
succeeds(f.run("Attach Lambda assets to release"));
|
||||
assert.equal(f.state().assets.length, 4);
|
||||
assert.equal(
|
||||
f.state().calls.filter((args) => args.includes("repos/test/repo/releases")).length,
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
test("an existing foreign release is rejected, including a bot-authored creation race", (t) => {
|
||||
const f = fixture(t);
|
||||
f.update({ release: { id: 999, author: { login: "github-actions[bot]" } } });
|
||||
fails(f.run("Create GitHub Release"));
|
||||
assert.equal(f.state().assets.length, 0);
|
||||
});
|
||||
|
||||
test("receipt scope, release identity and original digests fail closed", (t) => {
|
||||
const f = fixture(t);
|
||||
succeeds(f.run("Create GitHub Release"));
|
||||
const original = f.receipt();
|
||||
for (const changed of [
|
||||
{ run: "other" },
|
||||
{ repo: "other/repo" },
|
||||
{ sha: "b".repeat(40) },
|
||||
{ tag: "v0.0.0" },
|
||||
{ release_id: null },
|
||||
{ release_id: 1.5 },
|
||||
{ release_id: 999 },
|
||||
{ assets: {} },
|
||||
]) {
|
||||
writeFileSync(f.receiptPath, JSON.stringify({ ...original, ...changed }));
|
||||
fails(f.run("Create GitHub Release"));
|
||||
}
|
||||
writeFileSync(f.receiptPath, JSON.stringify(original));
|
||||
f.update({ release: { id: 999 } });
|
||||
fails(f.run("Create GitHub Release"));
|
||||
f.update({ release: { id: 123 } });
|
||||
writeFileSync(join(f.root, "release-bundle/assets", names[0]), "replacement bundle");
|
||||
fails(f.run("Create GitHub Release"));
|
||||
});
|
||||
|
||||
test("partial pair upload is recoverable from the exact original bundle", (t) => {
|
||||
const f = fixture(t);
|
||||
succeeds(f.run("Create GitHub Release"));
|
||||
f.update({ failUpload: names[1] });
|
||||
fails(f.run("Attach Lambda assets to release"));
|
||||
assert.equal(f.state().assets.length, 1);
|
||||
f.update({ failUpload: null });
|
||||
succeeds(f.run("Create GitHub Release"));
|
||||
succeeds(f.run("Attach Lambda assets to release"));
|
||||
succeeds(f.run("Verify Lambda release before npm publication"));
|
||||
assert.equal(f.state().assets.length, 4);
|
||||
});
|
||||
|
||||
test("missing, altered or ambiguous release assets never pass publication checks", (t) => {
|
||||
const f = fixture(t);
|
||||
succeeds(f.run("Create GitHub Release"));
|
||||
succeeds(f.run("Attach Lambda assets to release"));
|
||||
const original = f.state().assets;
|
||||
for (const assets of [
|
||||
original.slice(1),
|
||||
[...original, original[0]],
|
||||
original.map((a, i) => (i ? a : { ...a, digest: undefined })),
|
||||
original.map((a, i) => (i ? a : { ...a, digest: "sha256:" + "0".repeat(64) })),
|
||||
]) {
|
||||
f.update({ assets });
|
||||
fails(f.run("Verify Lambda release before npm publication"));
|
||||
}
|
||||
f.update({ assets: original, release: { id: 999 } });
|
||||
fails(f.run("Verify Lambda release before npm publication"));
|
||||
});
|
||||
|
||||
test("a substituted asset is rejected on attachment retry instead of being trusted or clobbered", (t) => {
|
||||
const f = fixture(t);
|
||||
succeeds(f.run("Create GitHub Release"));
|
||||
f.update({
|
||||
assets: [{ id: 42, name: names[0], state: "uploaded", digest: "sha256:" + hash("attacker") }],
|
||||
});
|
||||
fails(f.run("Attach Lambda assets to release"));
|
||||
assert.equal(f.state().assets.length, 1);
|
||||
});
|
||||
|
||||
test("artifact lookup is run-scoped and refuses expired or ambiguous receipts and bundles", (t) => {
|
||||
const f = fixture(t);
|
||||
for (const [name, job, artifactName] of [
|
||||
["Find previous release bundle", "pack", "release-bundle"],
|
||||
["Find previous release receipt", "publish", "lambda-release-receipt"],
|
||||
]) {
|
||||
for (const artifacts of [[], [{ id: 7, name: artifactName, expired: false }]]) {
|
||||
f.update({ artifacts });
|
||||
succeeds(f.run(name, {}, job));
|
||||
}
|
||||
for (const artifacts of [
|
||||
[{ id: 7, name: artifactName, expired: true }],
|
||||
[
|
||||
{ id: 7, name: artifactName, expired: false },
|
||||
{ id: 8, name: artifactName, expired: false },
|
||||
],
|
||||
]) {
|
||||
f.update({ artifacts });
|
||||
fails(f.run(name, {}, job));
|
||||
}
|
||||
}
|
||||
assert.ok(
|
||||
f.state().calls.every((args) => args.some((arg) => arg.includes("/actions/runs/55/artifacts"))),
|
||||
);
|
||||
});
|
||||
|
||||
test("fresh pack pins only the independent build hash and rejects mismatches", (t) => {
|
||||
const f = fixture(t);
|
||||
for (const expected of ["", "0".repeat(64)])
|
||||
fails(f.run("Pin Lambda handler zip", { EXPECTED_SHA256: expected }, "pack"));
|
||||
succeeds(f.run("Pin Lambda handler zip", { EXPECTED_SHA256: hash("built zip") }, "pack"));
|
||||
assert.equal(readFileSync(join(f.root, "pinned.zip"), "utf8"), "built zip");
|
||||
assert.equal(f.state().calls.length, 0);
|
||||
});
|
||||
|
||||
test("receipt and bundle persistence precede side effects and do not overwrite provenance", () => {
|
||||
const pack = workflow.jobs.pack;
|
||||
const upload = pack.steps.find((s) => s.id === "bundle");
|
||||
assert.equal(upload.with.overwrite, false);
|
||||
const resolve = pack.steps.findIndex((s) => s.name === "Resolve version");
|
||||
for (const s of pack.steps.slice(resolve))
|
||||
assert.equal(s.if, "steps.previous_bundle.outputs.artifact == ''");
|
||||
const publish = workflow.jobs.publish.steps;
|
||||
assert.ok(
|
||||
publish.findIndex((s) => s.name === "Save release receipt") <
|
||||
publish.findIndex((s) => s.name === "Attach Lambda assets to release"),
|
||||
);
|
||||
assert.equal(step("Save release receipt").with.overwrite, false);
|
||||
assert.ok(
|
||||
publish.findIndex((s) => s.name === "Verify Lambda release before npm publication") <
|
||||
publish.findIndex((s) => s.name === "Publish packages"),
|
||||
);
|
||||
const download = publish.find(
|
||||
(s) =>
|
||||
s.uses?.startsWith("actions/download-artifact@") &&
|
||||
s.with.path === "${{ runner.temp }}/release-bundle",
|
||||
);
|
||||
assert.equal(download.with["artifact-ids"], "${{ needs.pack.outputs.bundle_id }}");
|
||||
assert.equal(workflow.concurrency["cancel-in-progress"], false);
|
||||
});
|
||||
|
||||
// GitHub's implicit bash shell does not enable pipefail; explicit shell: bash does.
|
||||
test("an incomplete bundle fails before creating a release or receipt", (t) => {
|
||||
const f = fixture(t);
|
||||
rmSync(join(f.root, "release-bundle/assets", names[0]));
|
||||
fails(f.run("Create GitHub Release"));
|
||||
assert.equal(f.state().release, null);
|
||||
});
|
||||
|
||||
test("published npm pins accept only the original bundle and an integrity-verified package", (t) => {
|
||||
const f = fixture(t);
|
||||
succeeds(f.run("Check published Lambda pins"));
|
||||
succeeds(f.run("Check published Lambda pins", { NPM_STATUS: "200" }));
|
||||
for (const manifest of ["handler-zip.json", "video-image.json"]) {
|
||||
f.published({ [manifest]: hash("different published bytes") });
|
||||
fails(f.run("Check published Lambda pins", { NPM_STATUS: "200" }));
|
||||
}
|
||||
f.published();
|
||||
writeFileSync(join(f.root, "npm-package.tgz"), "corrupt package");
|
||||
fails(f.run("Check published Lambda pins", { NPM_STATUS: "200" }));
|
||||
assert.equal(f.state().calls.length, 0);
|
||||
});
|
||||
|
||||
test("npm registry uncertainty fails before release mutation", (t) => {
|
||||
const f = fixture(t);
|
||||
for (const status of ["500", "403", "network-error"])
|
||||
fails(f.run("Check published Lambda pins", { NPM_STATUS: status }));
|
||||
const steps = workflow.jobs.publish.steps;
|
||||
const check = steps.findIndex((s) => s.name === "Check published Lambda pins");
|
||||
assert.ok(check >= 0 && check < steps.findIndex((s) => s.name === "Create GitHub Release"));
|
||||
assert.equal(f.state().calls.length, 0);
|
||||
});
|
||||
Reference in New Issue
Block a user