Skip to content

fix(10): streaming 25MiB webhook body cap — explicit disposition, swallowed late errors, no blanket listener removal - #457

Merged
Alajmah merged 1 commit into
masterfrom
security/10-webhook-body-limit-v4
Nov 20, 2026
Merged

Alajmah merged 1 commit into
masterfrom
security/10-webhook-body-limit-v4

Conversation

@Alajmah

@Alajmah Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

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

The correction: no blanket listener removal

The declared-length rejection's three removeAllListeners() calls are gone — unnecessary (this middleware attached no listeners of its own at that point) and unsafe as a durable contract (they would delete handlers belonging to Express infrastructure, tracing, metrics, or any earlier middleware). The drain helper supplies the error handling; listener ownership stays where it was.

Regression: probes registered on data/end/error before the middleware survive the declared-length 413 — data keeps exactly the probe; end/error keep probe plus the drain's swallow listener. Zero removeAllListeners remain in the middleware.

Everything from #455 stands

Streaming actual-byte enforcement (25 × 1024 × 1024 = GitHub's exact ceiling); declared-length early-out with connection: close and drain; the late-error swallow guard on both rejection paths with its three regressions (late error after each rejection — no uncaught throw, one response, no next(), listener removed; clean end removes the swallow listener); the real-HMAC signed-delivery regression; continuing-upload regression; chunked mid-read cutoff; header-less oversized → 413 not 400; at-cap boundary; single-413 semantics throughout. 14 tests.

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

…llowed late errors, no blanket listener removal

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, OWN
  listeners detached precisely, 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).
- Declared-length path: oversized Content-Length rejected 413 before
  reading — connection: close, NO blanket listener removal (nothing
  of this middleware's own exists to remove; deleting others'
  handlers is not a durable ownership contract), 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 for verification.

Regressions (14): real-HMAC correctly-signed delivery verifies and
dispatches end-to-end; wrong signature 401s; byte-exact transport;
oversized declared length; continuing-upload client (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, listener removed); clean
end after rejection removes the swallow listener; pre-existing
listeners on data/end/error SURVIVE the declared-length rejection
(probe-pinned).

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-09T09:10:09.123375Z be87d1e 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: be87d1ec54

ℹ️ 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".

Comment on lines +230 to +233
const swallow = () => {
request.removeListener("error", swallow);
request.removeListener("end", swallow);
};

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 👍 / 👎.

@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: 0
AI judgment: APPROVE · Evidence: COMPLETE · Authority: advisory
Evidence complete · 2/2 changed files accounted for

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

@Alajmah
Alajmah merged commit 37ea5c4 into master Nov 20, 2026
21 checks passed
Alajmah added a commit that referenced this pull request Nov 20, 2026
…v9 (#464)

* fix(limiter): complete pre-auth family coverage + advisory remediation (next 16.4.6, body-parser, zod)

PRE-AUTH EXTENSION (maintainer finding on #460): /api/auth/check
(session-cookie Redis lookup) and /api/bootstrap/state + /first
(anonymous DB work) mount before apiKeyAuth and carried the same
bucket-rotation mechanism. The pre-auth match now covers the COMPLETE
/api/auth/* and /api/bootstrap/* families (prefix regex, case +
trailing-slash tolerant). Bearer identity unchanged for ordinary
authenticated routes. Three new regressions: AUTH-CHECK end-to-end
(130 rotating-Bearer session requests → 429@121 ONE bucket);
BOOTSTRAP-STATE end-to-end (130 anonymous → 429@121 ONE bucket);
FAMILY VARIANTS (8 path forms all key on the IP).

THREE-LAYER REMEDIATION (carried): session-cookie rotation closed
(apiKeyAuth-exact recognition); pre-auth login closed (IP-keyed);
route-variant closed (trailing slash + case); derivation isolated
(derived as an exported function — the CodeQL missing-rate-limiting
model pairing resolved without behavior change).

ADVISORY REMEDIATION (the #457 master-push CI failed on seven new
advisories, three critical, on next ≤16.4.0):
- next 16.3.8→16.4.6 (dashboard; GHSA-2464 critical, GHSA-673q
  critical, GHSA-p3m5 high): requires middleware.ts→proxy.ts rename
  (function export renamed too) + @next/swc platform binaries
  hand-carried to 16.4.6 (the version mismatch crashed builds with
  'Cannot read properties of undefined (reading project)').
- next 15.5.24→15.5.28 (landing; same advisories' 13.x-15.x range).
- body-parser 1.20.6→1.20.9 (override; GHSA-27r3 high).
- zod 3.25.76→4.6.7 (web; GHSA-mrhx high).
Lockfile entries hand-carried with registry integrities (npm on
Windows refused to re-resolve overrides + platform binaries — the
known quirk).

VERIFICATION: audit gate 0 high/critical; limiter identity (17) +
behavior (9) + webhook body-limit (14) = 40 tests green; dashboard
5 suites / 66 tests green; dashboard build verified locally
(compiled successfully, 29/29 static pages).

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>

* fix(limiter): structural bearer-recognition lockstep + corpus contract; demo-dashboard manifest sync

Adjudication corrections for the v7 chain (#462 review):

- apiKeyAuth and deriveLimiterIdentity held unlinked copies of the same
  Bearer recognition rule, kept in sync only by comments. Both now call a
  shared extractBearerToken() (bearerToken.js), and a 12-case corpus
  contract test pins the one rule both mechanisms depend on — divergence
  in either direction becomes a test failure, not a silent re-opening of
  the bucket-rotation bypass.
- Fixed a garbled test comment that cited a nonexistent ^Bearer\s+ regex
  for the tab-scheme case; apiKeyAuth actually uses a literal starts-with
  "Bearer " check, byte-identical to the limiter's recognition.
- demo-dashboard manifest pinned next 15.5.24 while the hand-carried
  lockfile resolves 15.5.28 — any npm install would have silently
  reverted the advisory floor. Manifest now matches the lockfile (npm ci
  dry-run verified); demo dashboard build verified green under 15.5.28.

Limiter semantics unchanged: 38 limiter tests + 15 triage-pr-guards green.

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>

* fix(auth): remove stale query-param claim from apiKeyAuth comment

The comment said 'Extract key from header or query param' but no
query-parameter authentication path exists (nor has one in the current
implementation's history) — the header is the only in-band key channel.
Confirmed against the code: zero req.query reads in auth.js. The stale
line drew a review finding ('query-param auth has no limiter identity
counterpart') whose premise was the comment, not the code.

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>

---------

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>
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