From 563c3e23c57f8b87bb77c66b7514d174a06d20cb Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 22:27:14 +0000 Subject: [PATCH] fix(webhooks): cap github-ci and github-backup bodies before the HMAC check (#11335) Both receivers read the whole POST body with req.text() before the signature check, so any unauthenticated request could make the Worker buffer an arbitrary body. They now refuse a declared Content-Length over GitHub's 25 MB payload cap with 413 without reading, and read undeclared bodies through a capped reader that stops at the cap. The HMAC check is unchanged and still runs after the bounded read. The two receivers are separate deploy units in different packages, so each carries its own small reader. Co-Authored-By: Claude Opus 5.5 --- infra/github-backup/worker/src/index.js | 34 +++++++++++- .../github-backup/worker/test/index.test.mjs | 46 ++++++++++++++++ pkgs/github-ci/worker/src/body.ts | 38 ++++++++++++++ pkgs/github-ci/worker/src/index.ts | 6 ++- pkgs/github-ci/worker/test/body.test.ts | 52 +++++++++++++++++++ pkgs/github-ci/worker/test/worker.test.ts | 23 ++++++++ 6 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 pkgs/github-ci/worker/src/body.ts create mode 100644 pkgs/github-ci/worker/test/body.test.ts diff --git a/infra/github-backup/worker/src/index.js b/infra/github-backup/worker/src/index.js index 8e5fab7c65..15e886c246 100644 --- a/infra/github-backup/worker/src/index.js +++ b/infra/github-backup/worker/src/index.js @@ -18,6 +18,37 @@ const enc = new TextEncoder(); // parallel delete fan-out. const KV_BULK_CHUNK = 100; +// Bounded webhook body read (#11335): GitHub caps payloads at 25 MB, +// so a declared or streamed body over that is refused before it is +// buffered whole. The HMAC check below stays the authority. +const MAX_WEBHOOK_BODY_BYTES = 25 * 1024 * 1024; + +async function readBodyUpTo(req, limit) { + const declared = req.headers.get("Content-Length"); + if (declared !== null && Number(declared) > limit) return null; + if (!req.body) return ""; + const reader = req.body.getReader(); + const chunks = []; + let total = 0; + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + total += value.byteLength; + if (total > limit) { + await reader.cancel(); + return null; + } + chunks.push(value); + } + const bytes = new Uint8Array(total); + let offset = 0; + for (const chunk of chunks) { + bytes.set(chunk, offset); + offset += chunk.byteLength; + } + return new TextDecoder().decode(bytes); +} + async function hmacHex(secret, body) { const key = await crypto.subtle.importKey( "raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]); @@ -41,7 +72,8 @@ export default { if (!env.GITHUB_WEBHOOK_SECRET) { return new Response("webhook secret not configured", { status: 500 }); } - const body = await req.text(); + const body = await readBodyUpTo(req, MAX_WEBHOOK_BODY_BYTES); + if (body === null) return new Response("payload too large", { status: 413 }); const given = (req.headers.get("X-Hub-Signature-256") || "").replace("sha256=", ""); const expect = await hmacHex(env.GITHUB_WEBHOOK_SECRET, body); let diff = given.length ^ expect.length; diff --git a/infra/github-backup/worker/test/index.test.mjs b/infra/github-backup/worker/test/index.test.mjs index 621cdb6898..e892a986c0 100644 --- a/infra/github-backup/worker/test/index.test.mjs +++ b/infra/github-backup/worker/test/index.test.mjs @@ -299,3 +299,49 @@ test("POST /github fails closed naming the config when the webhook secret is uns } assert.equal(puts, 0); }); + +function countingBody(chunk, count) { + const seen = { bytes: 0 }; + let i = 0; + const body = new ReadableStream({ + pull(c) { + if (i++ >= count) return c.close(); + seen.bytes += chunk.byteLength; + c.enqueue(chunk); + }, + }, { highWaterMark: 0 }); + return { body, seen }; +} + +test("POST /github refuses a declared oversized body with 413 without reading it (#11335)", async () => { + let puts = 0; + const env = { GHB_EVENTS: { async put() { puts++; } }, GITHUB_WEBHOOK_SECRET: "whsec" }; + const { body, seen } = countingBody(new Uint8Array(64), 1); + const req = new Request("https://worker.test/github", { + method: "POST", + headers: { "X-GitHub-Event": "push", "Content-Length": String(26 * 1024 * 1024) }, + body, + duplex: "half", + }); + const response = await worker.fetch(req, env); + assert.equal(response.status, 413); + assert.equal(seen.bytes, 0); + assert.equal(puts, 0); +}); + +test("POST /github stops reading a chunked oversized body at the 25 MB cap (#11335)", async () => { + let puts = 0; + const env = { GHB_EVENTS: { async put() { puts++; } }, GITHUB_WEBHOOK_SECRET: "whsec" }; + const chunk = new Uint8Array(1024 * 1024); + const { body, seen } = countingBody(chunk, 30); + const req = new Request("https://worker.test/github", { + method: "POST", + headers: { "X-GitHub-Event": "push", "X-Hub-Signature-256": "sha256=00" }, + body, + duplex: "half", + }); + const response = await worker.fetch(req, env); + assert.equal(response.status, 413); + assert.ok(seen.bytes <= 25 * 1024 * 1024 + 2 * chunk.byteLength, `read ${seen.bytes} bytes`); + assert.equal(puts, 0); +}); diff --git a/pkgs/github-ci/worker/src/body.ts b/pkgs/github-ci/worker/src/body.ts new file mode 100644 index 0000000000..17042ec6a6 --- /dev/null +++ b/pkgs/github-ci/worker/src/body.ts @@ -0,0 +1,38 @@ +// Bounded webhook body read (#11335). The receiver must not buffer an +// arbitrary unauthenticated body before the HMAC check runs, so the +// body is read with a cap: a declared Content-Length over the cap is +// refused without reading, and an undeclared (chunked) body stops at +// the cap. The HMAC check stays the authority, after this read. + +/** GitHub caps webhook payloads at 25 MB; a real delivery never needs more. */ +export const MAX_WEBHOOK_BODY_BYTES = 25 * 1024 * 1024; + +/** The body as text, or null when it is larger than `limit` bytes. */ +export async function readBodyUpTo( + req: Pick, + limit: number = MAX_WEBHOOK_BODY_BYTES, +): Promise { + const declared = req.headers.get("content-length"); + if (declared !== null && Number(declared) > limit) return null; + if (!req.body) return ""; + const reader = req.body.getReader(); + const chunks: Uint8Array[] = []; + let total = 0; + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + total += value.byteLength; + if (total > limit) { + await reader.cancel(); + return null; + } + chunks.push(value); + } + const bytes = new Uint8Array(total); + let offset = 0; + for (const chunk of chunks) { + bytes.set(chunk, offset); + offset += chunk.byteLength; + } + return new TextDecoder().decode(bytes); +} diff --git a/pkgs/github-ci/worker/src/index.ts b/pkgs/github-ci/worker/src/index.ts index ec35eba1f3..6409701129 100644 --- a/pkgs/github-ci/worker/src/index.ts +++ b/pkgs/github-ci/worker/src/index.ts @@ -25,6 +25,7 @@ * agent-note pointer. * - ping: pong. Everything else: 202 ignored. */ +import { readBodyUpTo } from "./body"; import { Turso, iArg, tArg } from "./turso"; import { FLEET_REPOS, buildRunRow, skipReason } from "./policy"; import { advanceSnapshot, appendAgentEvent, queueNow, stampDirtyWatermark, type SinkEnv } from "./sink"; @@ -73,7 +74,10 @@ export default { return new Response("webhook secret not configured", { status: 503 }); } - const raw = await req.text(); + // #11335: bounded read, so an unauthenticated oversized body is + // refused before it is buffered; the HMAC below stays the gate. + const raw = await readBodyUpTo(req); + if (raw === null) return new Response("payload too large", { status: 413 }); const given = (req.headers.get("x-hub-signature-256") ?? "").replace("sha256=", ""); const expected = await hmacHex(secret, raw); if (!timingSafeEqual(given, expected)) { diff --git a/pkgs/github-ci/worker/test/body.test.ts b/pkgs/github-ci/worker/test/body.test.ts new file mode 100644 index 0000000000..59656508c7 --- /dev/null +++ b/pkgs/github-ci/worker/test/body.test.ts @@ -0,0 +1,52 @@ +import { describe, expect, it } from "vitest"; +import { MAX_WEBHOOK_BODY_BYTES, readBodyUpTo } from "../src/body"; + +function counting(chunks: Uint8Array[]) { + const seen = { bytes: 0 }; + let i = 0; + const body = new ReadableStream( + { + pull(c) { + if (i >= chunks.length) return c.close(); + const chunk = chunks[i++]; + seen.bytes += chunk.byteLength; + c.enqueue(chunk); + }, + }, + { highWaterMark: 0 }, + ); + return { body, seen }; +} + +describe("readBodyUpTo (#11335)", () => { + it("caps at GitHub's 25 MB webhook limit by default", () => { + expect(MAX_WEBHOOK_BODY_BYTES).toBe(25 * 1024 * 1024); + }); + + it("refuses a declared oversized body without reading it", async () => { + const { body, seen } = counting([new Uint8Array(64)]); + const out = await readBodyUpTo({ headers: new Headers({ "content-length": "64" }), body }, 10); + expect(out).toBeNull(); + expect(seen.bytes).toBe(0); + }); + + it("stops reading an undeclared oversized body at the cap plus one chunk", async () => { + const chunk = new Uint8Array(4); + const { body, seen } = counting(Array.from({ length: 1000 }, () => chunk)); + const out = await readBodyUpTo({ headers: new Headers(), body }, 10); + expect(out).toBeNull(); + expect(seen.bytes).toBeLessThanOrEqual(10 + 2 * chunk.byteLength); + }); + + it("returns a body at or under the cap unchanged, multibyte included", async () => { + const text = '{"a":"é😀"}'; + const bytes = new TextEncoder().encode(text); + const { body } = counting([bytes.slice(0, 7), bytes.slice(7)]); + const out = await readBodyUpTo({ headers: new Headers({ "content-length": String(bytes.byteLength) }), body }, bytes.byteLength); + expect(out).toBe(text); + }); + + it("treats a missing body as empty", async () => { + expect(await readBodyUpTo({ headers: new Headers(), body: null })).toBe(""); + }); +}); diff --git a/pkgs/github-ci/worker/test/worker.test.ts b/pkgs/github-ci/worker/test/worker.test.ts index 7babd49589..f3beb83583 100644 --- a/pkgs/github-ci/worker/test/worker.test.ts +++ b/pkgs/github-ci/worker/test/worker.test.ts @@ -112,6 +112,29 @@ describe("github-ci worker", () => { expect(res.status).toBe(401); }); + it("refuses an oversized unsigned body with 413 before reading it all (#11335)", async () => { + const chunk = new Uint8Array(1024 * 1024); + let pulled = 0; + const body = new ReadableStream( + { + pull(c) { + if (pulled >= 30 * chunk.byteLength) return c.close(); + pulled += chunk.byteLength; + c.enqueue(chunk); + }, + }, + { highWaterMark: 0 }, + ); + const req = new Request("https://github-ci.test/", { + method: "POST", + headers: { "x-github-event": "workflow_run", "x-hub-signature-256": "sha256=00" }, + body, + }); + const res = await worker.fetch(req, env as unknown as Parameters[1]); + expect(res.status).toBe(413); + expect(pulled).toBeLessThan(30 * chunk.byteLength); + }); + 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));