From 31c6e25998800c815e45fa0d63573a24400d69a8 Mon Sep 17 00:00:00 2001 From: repo-worker-code-g Date: Sat, 7 Nov 2026 14:25:18 -0600 Subject: [PATCH] fix(github-backup): /github null body 202 + fail-closed secret guard (#6951) --- infra/github-backup/worker/src/index.js | 10 +++- .../github-backup/worker/test/index.test.mjs | 54 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/infra/github-backup/worker/src/index.js b/infra/github-backup/worker/src/index.js index 3b4c615536..9d9e1ad4fd 100644 --- a/infra/github-backup/worker/src/index.js +++ b/infra/github-backup/worker/src/index.js @@ -35,6 +35,12 @@ export default { const url = new URL(req.url); if (url.pathname === "/health") return new Response("ok"); if (url.pathname === "/github" && req.method === "POST") { + // Fail closed on a misconfigured deployment (#6951): without a + // secret the HMAC key is zero-length and importKey throws for + // every delivery, leaving an opaque 500 that names nothing. + if (!env.GITHUB_WEBHOOK_SECRET) { + return new Response("webhook secret not configured", { status: 500 }); + } const body = await req.text(); const given = (req.headers.get("X-Hub-Signature-256") || "").replace("sha256=", ""); const expect = await hmacHex(env.GITHUB_WEBHOOK_SECRET, body); @@ -46,7 +52,9 @@ export default { try { payload = JSON.parse(body); } catch { /* ping has form encoding sometimes */ } const event = req.headers.get("X-GitHub-Event") || "unknown"; if (event === "ping") return new Response("pong"); - const repo = payload.repository && payload.repository.full_name; + // A JSON null body parses to null (#6951): every other + // non-object shape already lands on the 202 below. + const repo = payload?.repository && payload.repository.full_name; if (!repo) return new Response("no repository in payload", { status: 202 }); const id = `${Date.now()}-${crypto.randomUUID()}`; await env.GHB_EVENTS.put("ev:" + id, JSON.stringify({ diff --git a/infra/github-backup/worker/test/index.test.mjs b/infra/github-backup/worker/test/index.test.mjs index f26510acb7..2a0d2a47f6 100644 --- a/infra/github-backup/worker/test/index.test.mjs +++ b/infra/github-backup/worker/test/index.test.mjs @@ -153,3 +153,57 @@ test("POST /queue/ack requires auth", async () => { assert.equal(response.status, 401); assert.equal(deleted, false); }); + +import { createHmac } from "node:crypto"; + +const sign = (secret, body) => + "sha256=" + createHmac("sha256", secret).update(body).digest("hex"); + +function githubRequest(secret, body, event = "push") { + const headers = { "X-GitHub-Event": event }; + if (secret !== null) headers["X-Hub-Signature-256"] = sign(secret, body); + return new Request("https://worker.test/github", { method: "POST", headers, body }); +} + +test("POST /github stores a correctly signed repository event", async () => { + const puts = []; + const env = { + GHB_EVENTS: { async put(k, v) { puts.push([k, v]); } }, + GITHUB_WEBHOOK_SECRET: "whsec", + }; + const body = JSON.stringify({ repository: { full_name: "owner/repo" }, action: "created" }); + const response = await worker.fetch(githubRequest("whsec", body), env); + assert.equal(response.status, 202); + assert.equal(puts.length, 1); + assert.equal(JSON.parse(puts[0][1]).repo, "owner/repo"); + const bad = await worker.fetch(githubRequest("wrong", body), env); + assert.equal(bad.status, 401); + assert.equal(puts.length, 1); +}); + +test("POST /github returns 202 (not a crash) for a signed JSON null body (#6951)", async () => { + let puts = 0; + const env = { + GHB_EVENTS: { async put() { puts++; } }, + GITHUB_WEBHOOK_SECRET: "whsec", + }; + const response = await worker.fetch(githubRequest("whsec", "null"), env); + assert.equal(response.status, 202); + assert.equal(await response.text(), "no repository in payload"); + assert.equal(puts, 0); +}); + +test("POST /github fails closed naming the config when the webhook secret is unset or empty (#6951)", async () => { + let puts = 0; + const kv = { async put() { puts++; } }; + const body = JSON.stringify({ repository: { full_name: "owner/repo" } }); + for (const secret of [undefined, ""]) { + const env = { GHB_EVENTS: kv }; + if (secret !== undefined) env.GITHUB_WEBHOOK_SECRET = secret; + // Even a signature computed with the empty key must not be accepted. + const response = await worker.fetch(githubRequest("", body), env); + assert.equal(response.status, 500); + assert.match(await response.text(), /webhook secret not configured/); + } + assert.equal(puts, 0); +});