diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 84e14c6526..b3a4609c8a 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -49,9 +49,57 @@ jobs: path: out/hf-media-linux-aarch64.tar.gz if-no-files-found: error + lambda-handler-zip: + name: Lambda handler zip + runs-on: ubuntu-latest + timeout-minutes: 30 + # Runs apart from the publish job: read-only token, no secrets, no stored git credentials. + permissions: + contents: read + env: + EXPECTED_RELEASE_SHA: >- + ${{ github.event_name == 'pull_request' + && github.event.pull_request.merge_commit_sha + || github.sha }} + if: >- + github.event_name == 'push' || + (github.event.pull_request.merged == true && + startsWith(github.event.pull_request.head.ref, 'release/v')) + outputs: + sha256: ${{ steps.zip.outputs.sha256 }} + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + ref: ${{ env.EXPECTED_RELEASE_SHA }} + persist-credentials: false + - name: Verify immutable release checkout + run: | + ACTUAL_SHA="$(git rev-parse HEAD)" + EXPECTED_COMMIT_SHA="$(git rev-parse "${EXPECTED_RELEASE_SHA}^{commit}")" + if [ "$ACTUAL_SHA" != "$EXPECTED_COMMIT_SHA" ]; then + echo "::error::Expected release commit $EXPECTED_COMMIT_SHA, checked out $ACTUAL_SHA" + exit 1 + fi + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: 24 + - run: bun install --frozen-lockfile + - run: bun run build + - name: Build Lambda handler zip + id: zip + run: | + bun run --cwd packages/aws-lambda build:zip + echo "sha256=$(sha256sum packages/aws-lambda/dist/handler.zip | cut -d' ' -f1)" >> "$GITHUB_OUTPUT" + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: hyperframes-lambda-handler-zip + path: packages/aws-lambda/dist/handler.zip + if-no-files-found: error + publish: name: Publish - needs: lambda-arm64-ffmpeg + needs: [lambda-arm64-ffmpeg, lambda-handler-zip] runs-on: ubuntu-latest timeout-minutes: 30 environment: npm-publish @@ -158,20 +206,32 @@ jobs: - run: bun install --frozen-lockfile - run: bun run build + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: hyperframes-lambda-handler-zip + path: dist/lambda-handler-build + # Before `npm publish`, so @hyperframes/aws-lambda pins this exact zip. - - name: Build Lambda handler zip + - name: Pin Lambda handler zip id: lambda_zip env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} VERSION: ${{ steps.version.outputs.version }} REPO: ${{ github.repository }} + EXPECTED_SHA256: ${{ needs.lambda-handler-zip.outputs.sha256 }} run: | # A retried run reuses the zip already on the release, so the pin still matches it. if gh release download "v${VERSION}" --repo "$REPO" --pattern hyperframes-lambda-handler.zip --dir dist; then echo "attached=true" >> "$GITHUB_OUTPUT" else - bun run --cwd packages/aws-lambda build:zip + ACTUAL_SHA256="$(sha256sum dist/lambda-handler-build/handler.zip | cut -d' ' -f1)" + if [ -z "$EXPECTED_SHA256" ] || [ "$ACTUAL_SHA256" != "$EXPECTED_SHA256" ]; then + echo "::error::Lambda handler zip sha256 $ACTUAL_SHA256 does not match the build job's $EXPECTED_SHA256" + exit 1 + fi + mv dist/lambda-handler-build/handler.zip dist/hyperframes-lambda-handler.zip fi + rm -rf dist/lambda-handler-build bun packages/aws-lambda/scripts/pin-handler-zip.ts - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 diff --git a/scripts/publish-workflow.test.mjs b/scripts/publish-workflow.test.mjs index 252d1955c3..111ce175dc 100644 --- a/scripts/publish-workflow.test.mjs +++ b/scripts/publish-workflow.test.mjs @@ -1,5 +1,6 @@ import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -132,13 +133,23 @@ test("stable release tag creation survives retries and rejects a mismatched comm } }); +test("the Lambda handler zip is built apart from the release credentials", () => { + const build = config.jobs["lambda-handler-zip"]; + assert.deepEqual(build.permissions, { contents: "read" }); + assert.equal(build.environment, undefined); + assert.doesNotMatch(JSON.stringify(build), /secrets\./); + const buildCheckout = build.steps.find((step) => step.uses?.startsWith("actions/checkout@")); + assert.equal(buildCheckout.with["persist-credentials"], false); + assert.ok(publish.needs.includes("lambda-handler-zip")); +}); + test("the Lambda handler zip is pinned and attached to the release before npm publish", () => { const names = publish.steps.map((step) => step.name); const at = (name) => { assert.ok(names.includes(name), `missing step ${name}`); return names.indexOf(name); }; - assert.ok(at("Build Lambda handler zip") < at("Create GitHub Release")); + assert.ok(at("Pin Lambda handler zip") < at("Create GitHub Release")); assert.ok(at("Create GitHub Release") < at("Attach Lambda handler zip to release")); assert.ok(at("Attach Lambda handler zip to release") < at("Publish packages")); const attach = publish.steps[at("Attach Lambda handler zip to release")]; @@ -146,14 +157,19 @@ test("the Lambda handler zip is pinned and attached to the release before npm pu assert.doesNotMatch(attach.run, /--clobber/); }); -test("a retried release reuses the attached handler zip instead of building a new one", () => { - const step = publish.steps.find((s) => s.name === "Build Lambda handler zip"); - const run = (ghExit) => { +test("the pin step reuses an attached zip and otherwise takes only the built zip's hash", () => { + const step = publish.steps.find((s) => s.name === "Pin Lambda handler zip"); + const builtZip = "built handler zip"; + const builtSha256 = createHash("sha256").update(builtZip).digest("hex"); + const run = ({ ghExit, 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, ""); for (const [name, exit] of [ ["gh", ghExit], ["bun", 0], @@ -172,27 +188,47 @@ test("a retried release reuses the attached handler zip instead of building a ne GITHUB_OUTPUT: output, VERSION: "9.8.7", REPO: "o/r", + EXPECTED_SHA256: expectedSha256, }, timeout: 5_000, }); - assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`); - return { calls: readFileSync(log, "utf8"), output: readFileSync(output, "utf8") }; + 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"), + output: readFileSync(output, "utf8"), + pinned, + }; } finally { rmSync(root, { recursive: true, force: true }); } }; - const retried = run(0); + const retried = run({ ghExit: 0, expectedSha256: builtSha256 }); + assert.equal(retried.status, 0, retried.stdout); assert.match( retried.calls, /^gh release download v9\.8\.7 --repo o\/r --pattern hyperframes-lambda-handler\.zip --dir dist$/m, ); - assert.doesNotMatch(retried.calls, /build:zip/); assert.match(retried.calls, /^bun packages\/aws-lambda\/scripts\/pin-handler-zip\.ts$/m); assert.equal(retried.output, "attached=true\n"); - const fresh = run(1); - assert.match(fresh.calls, /^bun run --cwd packages\/aws-lambda build:zip$/m); + const fresh = run({ ghExit: 1, expectedSha256: builtSha256 }); + assert.equal(fresh.status, 0, fresh.stdout); + assert.doesNotMatch(fresh.calls, /build:zip/); assert.match(fresh.calls, /^bun packages\/aws-lambda\/scripts\/pin-handler-zip\.ts$/m); assert.equal(fresh.output, ""); + assert.equal(fresh.pinned, builtZip); + + for (const expectedSha256 of ["0".repeat(64), ""]) { + const rejected = run({ ghExit: 1, 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); + } });