Repository navigation
fix(10): streaming 25MiB webhook body cap — explicit disposition, swallowed late errors #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [LOW] Declared-length rejection path removes ALL req listeners, contradicting the 'own listeners only' contract The declared Content-Length path calls req.removeAllListeners('data'/'end'/'error') before draining. This directly conflicts with the in-code contract a few lines above ('Own listeners only; other components' handlers are never touched'): any listener attached by an earlier middleware (e.g., metrics/tracing that finalizes on 'end' or 'error') is silently discarded, so its cleanup would never run for that request — the late error would instead hit the no-op swallow listener. Because this only happens after the 413 is committed and the request is being discarded, practical impact is minimal, but either document the blanket removal as intentional (with the reason) or restrict it to removing this middleware's own handlers, mirroring the streaming path's detach() approach for consistency. |
||
| 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); | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reviewed commit message contains no
Signed-off-by:trailer, despite the repository requiring every commit to be created withgit commit -s; this leaves the commit noncompliant and can block a DCO-gated merge, so recreate it with the required sign-off before acceptance.AGENTS.md reference: AGENTS.md:L204-L209
Useful? React with 👍 / 👎.