Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 88 additions & 6 deletions packages/web/src/routes/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,96 @@ 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);
};
Comment on lines +230 to +233

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Log errors encountered while draining rejected requests

When an oversized client resets the connection after the 413 response, this handler removes itself without recording the emitted error. The response should remain settled and next() should still be avoided, but the error should be logged before cleanup so repeated connection failures remain observable rather than being silently discarded.

AGENTS.md reference: AGENTS.md:L103-L105

Useful? React with 👍 / 👎.

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" });
// No blanket listener removal: this middleware has registered no
// streaming listeners of its own at this point, and
// removeAllListeners would delete handlers belonging to earlier
// middleware or framework instrumentation. The drain helper supplies
// the error handling; ownership stays where it was.
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);
};
}
Loading
Loading