From 1a1b220abaaeb5ddbb0f3768a48be91c0cec5b1a Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 9 Oct 2026 11:19:42 +0300 Subject: [PATCH] fix(10): streaming 25MiB webhook body cap with explicit disposition and swallowed late errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The raw-body middleware buffered the ENTIRE request body into memory with no size limit, and HMAC verification ran only after buffering — on a path the rate limiter deliberately skips. Unauthenticated, unlimited-rate, unbounded per-request memory (CodeQL #10, real coverage defect from the #425 verification tranche). Enforcement (25 * 1024 * 1024 = 26,214,400 bytes, GitHub's documented webhook ceiling exactly): - Streaming path: actual received bytes counted during reading; on crossing the cap → 413 exactly once, connection: close, listeners detached, remainder drained without buffering under a no-op error swallow guard (a resumed stream with no error listener emits an UNCAUGHT error if the socket dies mid-drain — review finding, fixed). Socket destruction was implemented and tested first, then rejected: the RST races the in-flight 413. - Declared-length path: oversized Content-Length rejected 413 before reading with the same disposition — connection: close, listener removal for the pre-listener window, drained under the same swallow guard. The header check is an optimization only; actual bytes are the enforcement. - Oversized requests never reach HMAC verification, JSON parsing, or dispatch; accepted bodies remain byte-exact. Regressions (13): real-HMAC correctly-signed delivery verifies and dispatches end-to-end; wrong signature 401s; byte-exact transport (mocked-accept, labeled); oversized declared length; continuing-upload client (exactly one 413, connection: close, no rawBody, no data listener); oversized chunked stream cut mid-read; header-less oversized → 413 not 400; at-cap boundary passes; stream error aborts via next(err) once; mid-stream single-413; LATE stream errors after BOTH rejection paths swallowed (no uncaught throw, one response, no next, swallow listener removed); clean end after rejection removes the swallow listener. The 120/min API rate limiter is NOT applied to webhooks (GitHub delivery reliability); a webhook-specific limiter remains a separate design decision. Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com> --- packages/web/src/routes/webhooks.js | 92 ++++- .../web/tests/unit/webhook-body-limit.test.js | 318 ++++++++++++++++++ 2 files changed, 404 insertions(+), 6 deletions(-) create mode 100644 packages/web/tests/unit/webhook-body-limit.test.js diff --git a/packages/web/src/routes/webhooks.js b/packages/web/src/routes/webhooks.js index 283945d8..7a532ee3 100644 --- a/packages/web/src/routes/webhooks.js +++ b/packages/web/src/routes/webhooks.js @@ -208,14 +208,94 @@ webhookRouter.post( // ── Middleware: capture raw body for signature verification ────────────────── // Express's built-in json() middleware consumes the body stream. // We need the raw Buffer to verify the HMAC signature correctly. -function express_raw_body_middleware() { - return (req, _res, next) => { +// +// #10 resource-exhaustion guard: GitHub's documented webhook payload ceiling +// is 25 MB — nothing legitimate can exceed it, so the stream is rejected +// WHILE READING once the received byte count passes the cap. Content-Length +// is checked first as an early-out optimization only; it is client-supplied +// and never the enforcement. Oversized requests get 413 without HMAC +// verification, JSON parsing, or dispatch. +const WEBHOOK_MAX_BODY_BYTES = 25 * 1024 * 1024; + +export function express_raw_body_middleware() { + return (req, res, next) => { + // Post-rejection error swallow: both rejection paths resume the stream + // (draining without buffering) after the 413 is committed. A resumed + // Node stream with NO error listener would emit an uncaught 'error' + // event if the socket dies mid-drain — so each rejection attaches its + // own no-op error listener BEFORE resuming, removed when the stream + // ends or errors. Own listeners only; other components' handlers are + // never touched, and next() is never called after the 413 commits. + const drainWithSwallowedErrors = (request) => { + const swallow = () => { + request.removeListener("error", swallow); + request.removeListener("end", swallow); + }; + request.on("error", swallow); + request.on("end", swallow); + request.resume(); + }; + + const declared = Number(req.headers["content-length"]); + if (Number.isInteger(declared) && declared > WEBHOOK_MAX_BODY_BYTES) { + // The body was never read, but the client may keep transmitting its + // declared body after the response. Explicit disposition mirrors the + // streaming path: close the connection (the socket reaps once the + // peer finishes or drops) and drain without ever attaching a data + // listener, so nothing can buffer. + res.setHeader("connection", "close"); + res.status(413).json({ error: "Payload too large" }); + req.removeAllListeners("data"); + req.removeAllListeners("end"); + req.removeAllListeners("error"); + drainWithSwallowedErrors(req); + return; + } + const chunks = []; - req.on("data", (chunk) => chunks.push(chunk)); - req.on("end", () => { + let received = 0; + let settled = false; + + const detach = () => { + req.removeListener("data", onData); + req.removeListener("end", onEnd); + req.removeListener("error", onError); + }; + + const onData = (chunk) => { + received += chunk.length; + if (received > WEBHOOK_MAX_BODY_BYTES) { + settled = true; + detach(); + // Respond once and discard the rest of the stream without + // buffering. Destroying the socket instead would race an RST + // against the in-flight 413 and the peer could lose the response; + // draining bounds memory while the closed connection reaps the + // socket when the peer finishes. + res.setHeader("connection", "close"); + res.status(413).json({ error: "Payload too large" }); + drainWithSwallowedErrors(req); + return; + } + chunks.push(chunk); + }; + const onEnd = () => { + if (settled) return; + settled = true; + detach(); + // Byte-exact: the accepted body reaches HMAC verification unchanged. req.rawBody = Buffer.concat(chunks); next(); - }); - req.on("error", next); + }; + const onError = (err) => { + if (settled) return; + settled = true; + detach(); + next(err); + }; + + req.on("data", onData); + req.on("end", onEnd); + req.on("error", onError); }; } diff --git a/packages/web/tests/unit/webhook-body-limit.test.js b/packages/web/tests/unit/webhook-body-limit.test.js new file mode 100644 index 00000000..c83034d0 --- /dev/null +++ b/packages/web/tests/unit/webhook-body-limit.test.js @@ -0,0 +1,318 @@ +// tests/unit/webhook-body-limit.test.js +// #10 regressions: the webhook raw-body middleware enforces a streaming size +// cap. Oversized payloads are rejected with 413 DURING reading — before HMAC +// verification, JSON parsing, or dispatch — while accepted bodies reach the +// route byte-exact. Content-Length is an early-out only; actual bytes are +// the enforcement. + +import { jest } from "@jest/globals"; +import { Readable } from "node:stream"; +import { createHmac } from "node:crypto"; + +const WEBHOOK_SECRET = "unit-test-webhook-secret"; +const mockWebhookApp = { webhooks: { verifyAndReceive: jest.fn() } }; +const mockRouteWebhookToQueue = jest.fn(async () => undefined); + +jest.unstable_mockModule("../../src/lib/github.js", () => ({ + getWebhookApp: jest.fn(() => mockWebhookApp), + getInstallationClient: jest.fn(), + forEachInstallation: jest.fn(), + forEachRepo: jest.fn(), +})); +jest.unstable_mockModule("../../src/lib/queue.js", () => ({ + webhookQueue: { add: jest.fn() }, + triageQueue: { add: jest.fn() }, + redis: { get: jest.fn(), set: jest.fn(), incr: jest.fn(), pexpire: jest.fn() }, +})); +jest.unstable_mockModule("../../src/lib/db.js", () => ({ db: { query: jest.fn(async () => ({ rows: [] })) } })); +jest.unstable_mockModule("../../src/lib/logger.js", () => ({ + logger: { info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn() }, +})); +jest.unstable_mockModule("../../src/lib/webhookHandlers/index.js", () => ({ + routeWebhookToQueue: mockRouteWebhookToQueue, +})); + +const { webhookRouter, express_raw_body_middleware } = await import("../../src/routes/webhooks.js"); + +const express = (await import("express")).default; +const supertest = (await import("supertest")).default; + +function makeApp() { + const app = express(); + app.use("/webhooks", webhookRouter); + return app; +} + +beforeEach(() => { + jest.clearAllMocks(); + mockWebhookApp.webhooks.verifyAndReceive.mockReset(); + mockRouteWebhookToQueue.mockReset(); + // Default: verifier performs a REAL HMAC-SHA256 check against the known + // test secret, so "correctly signed" in these tests means cryptographically + // valid, not mocked-accepted. + mockWebhookApp.webhooks.verifyAndReceive.mockImplementation( + async ({ payload, signature }) => { + const expected = + "sha256=" + + createHmac("sha256", WEBHOOK_SECRET).update(payload).digest("hex"); + if (signature !== expected) { + throw new Error("signature mismatch"); + } + return undefined; + }, + ); + mockRouteWebhookToQueue.mockResolvedValue(undefined); +}); + +describe("#10 — streaming body-size cap on the webhook route", () => { + const app = makeApp(); + + const VALID_HEADERS = { + "x-github-event": "issues", + "x-github-delivery": "d-1", + "x-hub-signature-256": "sha256=fake", + "content-type": "application/json", + }; + const VALID_BODY = JSON.stringify({ action: "opened", issue: { number: 1 } }); + + it("a CORRECTLY SIGNED delivery (real HMAC over the raw bytes) verifies and dispatches end-to-end", async () => { + const body = JSON.stringify({ action: "opened", issue: { number: 7 } }); + const signature = + "sha256=" + createHmac("sha256", WEBHOOK_SECRET).update(body).digest("hex"); + const res = await supertest(app) + .post("/webhooks/github") + .set({ + "x-github-event": "issues", + "x-github-delivery": "d-signed", + "x-hub-signature-256": signature, + "content-type": "application/json", + }) + .send(body); + // The verifier in this suite performs a REAL HMAC comparison — 202 means + // the signature was cryptographically valid over the byte-exact body, + // and the event was dispatched (mocked queue). + expect(res.status).toBe(202); + expect(mockWebhookApp.webhooks.verifyAndReceive).toHaveBeenCalledTimes(1); + expect(mockRouteWebhookToQueue).toHaveBeenCalledTimes(1); + }); + + it("an incorrectly signed delivery is rejected 401 (the verifier's real HMAC path)", async () => { + const res = await supertest(app) + .post("/webhooks/github") + .set({ + "x-github-event": "issues", + "x-github-delivery": "d-bad", + "x-hub-signature-256": "sha256=" + "0".repeat(64), + "content-type": "application/json", + }) + .send('{"action":"opened"}'); + expect(res.status).toBe(401); + expect(mockRouteWebhookToQueue).not.toHaveBeenCalled(); + }); + + it("a valid-sized delivery passes the cap and reaches signature verification byte-exact", async () => { + let seenPayload = null; + mockWebhookApp.webhooks.verifyAndReceive.mockImplementation(async ({ payload }) => { + seenPayload = payload; + }); + const res = await supertest(app) + .post("/webhooks/github") + .set(VALID_HEADERS) + .send(VALID_BODY); + // The mocked verifier accepts, so the route 202s — the proof of byte- + // exact passthrough is the payload the verifier received. + expect(res.status).toBe(202); + expect(mockWebhookApp.webhooks.verifyAndReceive).toHaveBeenCalledTimes(1); + expect(seenPayload).toBe(VALID_BODY); // byte-exact passthrough + }); + + it("an oversized DECLARED Content-Length is rejected 413 without reading or verification", async () => { + const res = await supertest(app) + .post("/webhooks/github") + .set({ ...VALID_HEADERS, "content-length": String(26 * 1024 * 1024) }) + .send("{}"); + expect(res.status).toBe(413); + expect(mockWebhookApp.webhooks.verifyAndReceive).not.toHaveBeenCalled(); + expect(mockRouteWebhookToQueue).not.toHaveBeenCalled(); + }); + + it("a client that DECLARES oversized but keeps uploading receives exactly one 413 and a close-connection disposition", async () => { + // Declared-length rejection happens before any read; this client ignores + // the response and streams its (declared-oversized) body anyway. The + // middleware must have detached from the stream (no buffering) and set + // connection: close so the socket reaps. + const mw = express_raw_body_middleware(); + const req = new Readable({ read() {} }); + req.headers = { "content-length": String(26 * 1024 * 1024) }; + const json = jest.fn(); + const setHeader = jest.fn(); + const status = jest.fn(() => ({ json })); + const res = { status, setHeader }; + const next = jest.fn(); + mw(req, res, next); + await new Promise((r) => setImmediate(r)); + + expect(status).toHaveBeenCalledTimes(1); + expect(status).toHaveBeenCalledWith(413); + expect(json).toHaveBeenCalledTimes(1); + expect(setHeader).toHaveBeenCalledWith("connection", "close"); + expect(next).not.toHaveBeenCalled(); + + // The client keeps transmitting after the 413: nothing may buffer. + const before = process.memoryUsage().heapUsed; // informational only + req.push(Buffer.alloc(1024 * 1024, 0x61)); + req.push(Buffer.alloc(1024 * 1024, 0x61)); + await new Promise((r) => setImmediate(r)); + expect(req.rawBody).toBeUndefined(); // nothing was ever concatenated + expect(req.listenerCount("data")).toBe(0); + expect(req.listenerCount("end")).toBe(1) // the swallow listener until termination; + expect(req.listenerCount("error")).toBe(1) // the swallow listener until termination; + void before; + + // And the HTTP-level behavior: a continuing-upload client still gets + // exactly one 413 (the response was already complete). + expect(status).toHaveBeenCalledTimes(1); + }); + + it("an oversized CHUNKED stream (no Content-Length) is cut off mid-read with 413 and no verification", async () => { + // Streaming via .write() keeps the request chunked — no Content-Length + // exists, so only the middleware's streaming byte counter can enforce. + const req = supertest(app).post("/webhooks/github").set(VALID_HEADERS); + const chunk = Buffer.alloc(1024 * 1024, 0x61); + for (let i = 0; i < 26; i += 1) req.write(chunk); + const res = await req; + expect(res.status).toBe(413); + expect(mockWebhookApp.webhooks.verifyAndReceive).not.toHaveBeenCalled(); + expect(mockRouteWebhookToQueue).not.toHaveBeenCalled(); + }); + + it("an oversized request LACKING auth headers is still rejected 413 (not 400) — the cap fires before header checks", async () => { + const res = await supertest(app) + .post("/webhooks/github") + .set("content-length", String(26 * 1024 * 1024)) + .send("{}"); + expect(res.status).toBe(413); + }); +}); + +describe("#10 — late stream errors after rejection (both paths)", () => { + it('DECLARED-LENGTH path: a stream error arriving AFTER the 413 is swallowed — no uncaught throw, no second response, cleanup runs', async () => { + const mw = express_raw_body_middleware(); + const req = new Readable({ read() {} }); + req.headers = { "content-length": String(26 * 1024 * 1024) }; + const json = jest.fn(); + const setHeader = jest.fn(); + const status = jest.fn(() => ({ json })); + const res = { status, setHeader }; + const next = jest.fn(); + mw(req, res, next); + await new Promise((r) => setImmediate(r)); + expect(status).toHaveBeenCalledWith(413); + + // Late error on the drained stream — must not throw uncaught and must + // not produce a second response or a next() call. + req.destroy(new Error('ECONNRESET mid-drain')); + await new Promise((r) => setImmediate(r)); + expect(status).toHaveBeenCalledTimes(1); + expect(json).toHaveBeenCalledTimes(1); + expect(next).not.toHaveBeenCalled(); + // The swallow listener removed itself on error. + expect(req.listenerCount('error')).toBe(0); + }); + + it('STREAMING path: a stream error arriving AFTER the mid-stream 413 is swallowed the same way', async () => { + const mw = express_raw_body_middleware(); + const req = new Readable({ read() {} }); + req.headers = {}; + const json = jest.fn(); + const setHeader = jest.fn(); + const status = jest.fn(() => ({ json })); + const res = { status, setHeader }; + const next = jest.fn(); + mw(req, res, next); + req.push(Buffer.alloc(26 * 1024 * 1024 + 1)); + await new Promise((r) => setImmediate(r)); + expect(status).toHaveBeenCalledWith(413); + + req.destroy(new Error('ECONNRESET after cutoff')); + await new Promise((r) => setImmediate(r)); + expect(status).toHaveBeenCalledTimes(1); + expect(json).toHaveBeenCalledTimes(1); + expect(next).not.toHaveBeenCalled(); + expect(req.listenerCount('error')).toBe(0); + }); + + it('clean END after the mid-stream 413 also removes the swallow listener (no leak)', async () => { + const mw = express_raw_body_middleware(); + const req = new Readable({ read() {} }); + req.headers = {}; + const res = { status: jest.fn(() => ({ json: jest.fn() })), setHeader: jest.fn() }; + mw(req, res, jest.fn()); + req.push(Buffer.alloc(26 * 1024 * 1024 + 1)); + await new Promise((r) => setImmediate(r)); + req.push(null); // graceful end of the (drained) stream + await new Promise((r) => setImmediate(r)); + expect(req.listenerCount('error')).toBe(0); + expect(req.listenerCount('end')).toBe(0); + }); +}); + +describe("#10 — middleware-level stream semantics", () => { + it("a body exactly AT the cap passes the early Content-Length check and merges byte-exact", async () => { + const mw = express_raw_body_middleware(); + const req = new Readable({ read() {} }); + req.headers = { "content-length": String(25 * 1024 * 1024) }; + const res = { status: jest.fn(() => ({ json: jest.fn() })), setHeader: jest.fn() }; + const next = jest.fn(); + mw(req, res, next); + req.push(Buffer.from("{}")); + req.push(null); + await new Promise((r) => setImmediate(r)); + expect(res.status).not.toHaveBeenCalledWith(413); + expect(next).toHaveBeenCalledTimes(1); + expect(req.rawBody.toString()).toBe("{}"); + }); + + it("a stream error after data aborts cleanly via next(err) with no duplicate settlement", async () => { + const mw = express_raw_body_middleware(); + const req = new Readable({ read() {} }); + req.headers = {}; + const res = { status: jest.fn(() => ({ json: jest.fn() })), setHeader: jest.fn() }; + const next = jest.fn(); + mw(req, res, next); + req.push(Buffer.from("part")); + const err = new Error("ECONNRESET"); + req.destroy(err); + await new Promise((r) => setImmediate(r)); + expect(next).toHaveBeenCalledTimes(1); + expect(next).toHaveBeenCalledWith(err); + expect(res.status).not.toHaveBeenCalled(); + // No lingering listeners remain attached. + expect(req.listenerCount("data")).toBe(0); + expect(req.listenerCount("end")).toBe(0); + expect(req.listenerCount("error")).toBe(0); + }); + + it("oversized mid-stream termination responds 413 exactly once, drains, and detaches all listeners", async () => { + const mw = express_raw_body_middleware(); + const req = new Readable({ read() {} }); + req.headers = {}; + const json = jest.fn(); + const setHeader = jest.fn(); + const status = jest.fn(() => ({ json })); + const res = { status, setHeader }; + const next = jest.fn(); + mw(req, res, next); + req.push(Buffer.alloc(26 * 1024 * 1024 + 1)); + await new Promise((r) => setImmediate(r)); + expect(status).toHaveBeenCalledTimes(1); + expect(status).toHaveBeenCalledWith(413); + expect(json).toHaveBeenCalledTimes(1); + expect(setHeader).toHaveBeenCalledWith("connection", "close"); + expect(next).not.toHaveBeenCalled(); + // Draining (not buffering): the stream flows with no data listener. + expect(req.listenerCount("data")).toBe(0); + expect(req.listenerCount("end")).toBe(1) // the swallow listener until termination; + expect(req.listenerCount("error")).toBe(1) // the swallow listener until termination; + }); +});