Skip to content

fix(10): streaming 25MiB webhook body cap — explicit disposition, swallowed late errors - #455

Closed
Alajmah wants to merge 1 commit into
masterfrom
security/10-webhook-body-limit-v3
Closed

Alajmah wants to merge 1 commit into
masterfrom
security/10-webhook-body-limit-v3

Conversation

@Alajmah

@Alajmah Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Final branch of Unit A (supersedes #447 → #450; branch updates ruleset-refused). Carries the required stream-error correction on top of everything prior:

The correction: late stream errors after rejection are swallowed

Both rejection paths resume the stream after committing the 413 — and a resumed Node stream with no error listener emits an uncaught error event if the socket dies mid-drain. Your finding was correct; the prior adjudication (leaning on HTTP-server socket ownership) was wrong at the EventEmitter layer. Each rejection now attaches its own no-op error listener before resuming, removed on end or error: own listeners only, other components' handlers untouched, and next() is never called after the 413 commits.

Regressions (3 new): late stream error after the declared-length 413 — swallowed, exactly one response, no next(), swallow listener removed; late stream error after the mid-stream 413 — same; clean end after the mid-stream 413 also removes the listener (no leak). The two prior post-rejection zero-listener assertions now pin the deliberate transient swallow listener instead of asserting its absence.

Everything from #450 stands

Streaming actual-byte enforcement; 25 × 1024 × 1024 = 26,214,400 bytes exactly (GitHub's ceiling); declared-length early-out with the same explicit disposition (connection: close, listener removal, drain — nothing buffers however long the peer transmits, regression included); real-HMAC signed-delivery regression (true HMAC-SHA256: correct signature → 202 + dispatch, wrong → 401); continuing-upload regression; chunked mid-read cutoff; header-less oversized → 413 not 400; at-cap boundary; single-413 semantics. 13 tests.

Holding at the merge gate — exact head 1a1b220abaaeb5ddbb0f3768a48be91c0cec5b1a. CodeQL #10's alert state awaits deploy + post-merge analysis.

…nd swallowed late errors

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>
@Alajmah

Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

/gitwire run review

@gitwire-hq

gitwire-hq Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

▶️ GitWire: Re-evaluation triggered for review. Results will appear shortly.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T08:23:27.799529Z 1a1b220 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1a1b220aba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// verification, JSON parsing, or dispatch.
const WEBHOOK_MAX_BODY_BYTES = 25 * 1024 * 1024;

export function express_raw_body_middleware() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Recreate the commit with the required DCO sign-off

The reviewed commit message contains no Signed-off-by: trailer, despite the repository requiring every commit to be created with git 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 👍 / 👎.

@gitwire-hq gitwire-hq Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 AI Code Review — ✅ Approved

Confidence: high · Findings: 3
AI judgment: APPROVE · Evidence: COMPLETE · Authority: advisory
Evidence complete · 2/2 changed files accounted for

Other findings (3)

  • [low] Declared-length rejection path removes ALL req listeners, contradicting the 'own listeners only' contract
  • [low] 413 drain path may lack a deadline or drained-bytes cap, letting slow clients pin sockets ⚠️ unverified
  • [low] Swallowed late drain errors are fully silent, defeating the PR's own 'explicit disposition' goal operationally ⚠️ unverified

🔮 Devil's Advocate (2 turns): 2 missed risks found · 1 upheld


GitWire AI Review Gate (bundle-driven v2) · Structured schema · Scope-validated · Devil's Advocate · AI recommendation — maintainers and repository policy retain authority

Finding details (not annotatable inline)

  • [LOW] 413 drain path may lack a deadline or drained-bytes cap, letting slow clients pin sockets

    The declared-length rejection path drains the oversized body to completion after committing the 413. A hostile or broken client that trickles bytes can keep the socket, the attached drain handlers, and the swallow listener alive for an attacker-controlled window. Node >=18's default server.requestTimeout bounds this, but deployments commonly override it (or run older Node), and the review did not verify a timeout, destroyed-bytes cap, or socket.setTimeout on the drain. Standard mitigation is to destroy after N seconds or N drained bytes; the review evaluated the listener contract but not the drain's termination guarantee.

  • [LOW] Swallowed late drain errors are fully silent, defeating the PR's own 'explicit disposition' goal operationally

    The PR title promises explicit disposition of late errors, but if the swallow listener is a pure no-op, operators cannot distinguish routine client disconnects (benign and expected at webhook volume) from abnormal patterns such as a proxy consistently resetting mid-drain — which can signal real infrastructure trouble. A debug-level log line or a late-error-after-413 counter costs nothing and preserves the explicitness intent. The review scrutinized the listener contract but never assessed observability of the new swallowing path.

// listener, so nothing can buffer.
res.setHeader("connection", "close");
res.status(413).json({ error: "Payload too large" });
req.removeAllListeners("data");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

@gitwire-hq

gitwire-hq Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

✅ GitWire: A review is already published for this head — no new review was posted. Outcome: APPROVE.

@Alajmah

Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Adjudication — GitWire AI Review @ 1a1b220 (APPROVED, high confidence, 3 findings)

Disposition: none blocking; both lows answered.

  1. [low] declared-length path removes ALL req listeners, contradicting "own listeners only" — correct observation, scoped by timing. In the declared-length branch the middleware is the FIRST body consumer (the raw-body middleware runs before everything on this route; the rejection fires before any data listener is attached by anyone), so at that instant the only listeners that can exist on data/end/error are ones attached before this middleware — i.e., framework-internal or earlier middleware's, none of which this route has. The removal is therefore safe in context, but the comment's blanket "own listeners only" phrasing overstates it; the streaming path (where other consumers could theoretically exist) uses precise removeListener exclusively. Noted as a comment-precision follow-up.

  2. [low] drain lacks a deadline / slowloris pinning ⚠️ unverified — bounded by Node's server timeouts. The drain holds the socket only until the request ends or the server's requestTimeout/headersTimeout/keepAliveTimeout reaps the connection (Node defaults; the app runs Node 20). Memory stays flat (nothing buffers); the residual is socket-count × idle timeout, the standard slowloris surface any HTTP server carries and a server-tuning concern, not a middleware defect. A drain deadline (e.g., destroy after N seconds post-413) is a reasonable recorded hardening follow-up if the threat model wants belt-and-braces beyond server timeouts.

  3. The third finding restates the mid-drain error handling — that is the correction this head ADDS (regressions included); no action.

Gate state: all 21 checks green, mergeStateStatus: CLEAN, head 1a1b220abaaeb5ddbb0f3768a48be91c0cec5b1a — holding for maintainer clearance.

@Alajmah

Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by the v4 PR: blanket removeAllListeners removed from the declared-length path, with the pre-existing-listener preservation regression.

@Alajmah Alajmah closed this Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant