diff --git a/pkgs/github-ci/worker/src/index.ts b/pkgs/github-ci/worker/src/index.ts index 9545ab207f..bc62581766 100644 --- a/pkgs/github-ci/worker/src/index.ts +++ b/pkgs/github-ci/worker/src/index.ts @@ -85,6 +85,14 @@ export default { } catch { return new Response("bad json", { status: 400 }); } + // #6946: JSON.parse also succeeds for null/array/scalar bodies, and + // dereferencing payload.repository on those threw out of fetch + // (outside the receiver-failed envelope below). A webhook payload + // is an object by contract; anything else is a deliberate 400 for + // every event type, before any dereference. + if (typeof payload !== "object" || payload === null || Array.isArray(payload)) { + return new Response("bad payload", { status: 400 }); + } const eventType = req.headers.get("x-github-event") ?? "workflow_run"; if (eventType === "ping") return new Response("pong", { status: 200 }); diff --git a/pkgs/github-ci/worker/test/worker.test.ts b/pkgs/github-ci/worker/test/worker.test.ts index a99cd1ee9a..385bf5a9f0 100644 --- a/pkgs/github-ci/worker/test/worker.test.ts +++ b/pkgs/github-ci/worker/test/worker.test.ts @@ -70,6 +70,30 @@ describe("github-ci worker", () => { expect(res.status).toBe(401); }); + it("rejects a signed null body with 400 for workflow_run (#6946)", async () => { + const body = "null"; + const res = await post(body, "workflow_run", await sign(body)); + expect(res.status).toBe(400); + }); + + it("rejects a signed null body with 400 for push (#6946)", async () => { + const body = "null"; + const res = await post(body, "push", await sign(body)); + expect(res.status).toBe(400); + }); + + it("rejects a signed array body with 400 (#6946)", async () => { + const body = "[]"; + const res = await post(body, "workflow_run", await sign(body)); + expect(res.status).toBe(400); + }); + + it("rejects a signed scalar body with 400 (#6946)", async () => { + const body = "\"hello\""; + const res = await post(body, "push", await sign(body)); + expect(res.status).toBe(400); + }); + it("answers ping", async () => { const body = JSON.stringify({ zen: "ok" }); const res = await post(body, "ping", await sign(body));