diff --git a/pkgs/github-ci/worker/README.md b/pkgs/github-ci/worker/README.md index 076626c148..cae45ff2df 100644 --- a/pkgs/github-ci/worker/README.md +++ b/pkgs/github-ci/worker/README.md @@ -9,7 +9,10 @@ during the dual-run diff period. ## Shape - `src/index.ts` — HMAC-SHA256 gate (Web Crypto, same semantics as - the Edge Function), routing, workflow_run ingest. + the Edge Function), routing, workflow_run ingest. The gate is + unconditional (#6936): with `GITHUB_WEBHOOK_SECRET` unset or empty, + every POST is rejected 503 "webhook secret not configured" before + any write — the Worker fails closed, never open. - `src/policy.ts` — the v4 ingest policy as pure functions: completed runs only, conclusion != skipped, raw payload kept only for failures, dedupe_key `github::::`. diff --git a/pkgs/github-ci/worker/src/index.ts b/pkgs/github-ci/worker/src/index.ts index 5264a7fa58..ec35eba1f3 100644 --- a/pkgs/github-ci/worker/src/index.ts +++ b/pkgs/github-ci/worker/src/index.ts @@ -67,14 +67,17 @@ export default { if (req.method === "GET") return new Response("github-ci worker ok", { status: 200 }); if (req.method !== "POST") return new Response("method not allowed", { status: 405 }); - const raw = await req.text(); const secret = env.GITHUB_WEBHOOK_SECRET ?? ""; - if (secret) { - const given = (req.headers.get("x-hub-signature-256") ?? "").replace("sha256=", ""); - const expected = await hmacHex(secret, raw); - if (!timingSafeEqual(given, expected)) { - return new Response("bad signature", { status: 401 }); - } + if (!secret.trim()) { + console.error("github-ci worker rejected webhook: GITHUB_WEBHOOK_SECRET is not configured"); + return new Response("webhook secret not configured", { status: 503 }); + } + + const raw = await req.text(); + const given = (req.headers.get("x-hub-signature-256") ?? "").replace("sha256=", ""); + const expected = await hmacHex(secret, raw); + if (!timingSafeEqual(given, expected)) { + return new Response("bad signature", { status: 401 }); } let payload: Record; diff --git a/pkgs/github-ci/worker/test/worker.test.ts b/pkgs/github-ci/worker/test/worker.test.ts index 8094df19d3..2ce2949eb9 100644 --- a/pkgs/github-ci/worker/test/worker.test.ts +++ b/pkgs/github-ci/worker/test/worker.test.ts @@ -1,15 +1,15 @@ import { createExecutionContext, env, fetchMock, waitOnExecutionContext } from "cloudflare:test"; -import { describe, expect, it, beforeAll, afterEach } from "vitest"; +import { describe, expect, it, beforeAll, afterEach, vi } from "vitest"; import worker from "../src/index"; const SECRET = "test-webhook-secret"; const PLATFORM = "https://fleet-platform-dezren39.aws-us-east-1.turso.io"; const QUEUE = "https://fleet-queue-dezren39.aws-us-east-1.turso.io"; -async function sign(body: string): Promise { +async function sign(body: string, secret = SECRET): Promise { const key = await crypto.subtle.importKey( "raw", - new TextEncoder().encode(SECRET), + new TextEncoder().encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"], @@ -24,7 +24,12 @@ function tursoOk() { }); } -async function post(body: string, event: string, sig?: string): Promise { +async function post( + body: string, + event: string, + sig?: string, + webhookSecret: string | null = SECRET, +): Promise { const req = new Request("https://github-ci.test/", { method: "POST", headers: { @@ -34,7 +39,13 @@ async function post(body: string, event: string, sig?: string): Promise[1]); + const workerEnv = { ...env } as unknown as Record; + if (webhookSecret === null) { + delete workerEnv.GITHUB_WEBHOOK_SECRET; + } else { + workerEnv.GITHUB_WEBHOOK_SECRET = webhookSecret; + } + const res = await worker.fetch(req, workerEnv as unknown as Parameters[1]); await waitOnExecutionContext(ctx); return res; } @@ -62,9 +73,41 @@ beforeAll(() => { fetchMock.activate(); fetchMock.disableNetConnect(); }); -afterEach(() => fetchMock.assertNoPendingInterceptors()); +afterEach(() => { + vi.restoreAllMocks(); + fetchMock.assertNoPendingInterceptors(); +}); describe("github-ci worker", () => { + it.each([ + ["unset", null], + ["empty", ""], + ])("rejects unsigned webhooks when the secret is %s and logs the configuration error", async (_name, secret) => { + const log = vi.spyOn(console, "error").mockImplementation(() => {}); + const res = await post(runPayload, "workflow_run", undefined, secret); + + expect(res.status).toBe(503); + expect(await res.text()).toBe("webhook secret not configured"); + expect(log).toHaveBeenCalledWith( + "github-ci worker rejected webhook: GITHUB_WEBHOOK_SECRET is not configured", + ); + log.mockRestore(); + }); + + it.each([ + ["unset", null], + ["empty", ""], + ])("rejects ping when the secret is %s", async (_name, secret) => { + const res = await post("{}", "ping", undefined, secret); + expect(res.status).toBe(503); + expect(await res.text()).toBe("webhook secret not configured"); + }); + + it("rejects a signature made with the wrong secret", async () => { + const res = await post(runPayload, "workflow_run", await sign(runPayload, "wrong-secret")); + expect(res.status).toBe(401); + }); + it("rejects a bad signature with 401", async () => { const res = await post(runPayload, "workflow_run", "sha256=00"); expect(res.status).toBe(401);