Skip to content

node:http: honor server.maxHeadersCount above the uWS 198-field cap - #35907

Closed
robobun wants to merge 3 commits into
mainfrom
farm/91a80552/node-http-maxheaderscount
Closed

robobun wants to merge 3 commits into
mainfrom
farm/91a80552/node-http-maxheaderscount

Conversation

@robobun

@robobun robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

The uWS request parser stores header string_views in a fixed Header[UWS_HTTP_MAX_HEADERS_COUNT=200] array inside HttpRequest, giving a hard ceiling of 198 request header fields (slot 0 is the request line, the last slot is the null-key terminator) that no option could raise. server.maxHeadersCount was accepted but never reached the native parser, so setting it above ~198 had no effect and the server answered 431 Request Header Fields Too Large.

HTTP/1.1 431 Request Header Fields Too Large

HttpRequest now holds a Header* that defaults to the inline[200] array and can be repointed at a per-connection std::vector<Header> owned by HttpParser. The vector is sized once (on the first parse after the server raised maxHeadersCount) and reused for every request on that connection, so connections on a server that never raises the option see no allocation and no layout change. getHeaders' field-line loop bound is now the live capacity instead of the compile-time constant.

node:http's applyServerCustomOptions forwards server.maxHeadersCount (when > 0) through setServerCustomOptions → Server__setMaxHeadersCount → uws_app_set_max_headers_count → HttpContextData::maxHeadersCount → consumePostPadded. The JS-side rawHeaders truncation for values below the compiled-in cap is unchanged (matches Node's truncate-not-431 behavior).

Fixes #6982

How did you verify your code works?

test/js/node/http/node-http-maxHeadersCount.test.ts:

  • server.maxHeadersCount = 400 + 300 custom header lines → 200 OK with all 302 pairs in rawHeaders; 400 total → 200 OK; 401 total → 431 (cap enforced at the configured value).
  • Header values at index 0 / 175 / 349 are intact on the overflow path.
  • Default server (no maxHeadersCount set) still 431s at >198 fields.

Fail-before: on the released bun the first two tests fail with Expected: 200 / Received: 431.

Notes

  • Bun.serve has no per-server option surface for this yet (same as maxHeaderSize, which it reads from the process global); the plumbing added here (Server__setMaxHeadersCount over all four server variants) is ready for that follow-up.
  • maxHeadersCount = 0 ("no limit" in Node) currently keeps the compiled-in cap; the byte-size maxHeaderSize already bounds memory, so wiring 0 as "uncapped count" is a small follow-up if wanted.

[review] gate passed · iteration 0 · 12 files touched

fails on main (without fix)
ASAN without fix: 3 FAILED
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/node/http/node-http-maxHeadersCount.test.ts
bun test v1.4.0 (100e7f591)

test/js/node/http/node-http-maxHeadersCount.test.ts:
62 |     },
63 |     async port => {
64 |       // 300 custom headers (302 total): would 431 without a raised count cap.
65 |       {
66 |         const { status, body } = await rawRequest(port, 300);
67 |         expect(status).toBe(200);
                            ^
error: expect(received).toBe(expected)

Expected: 200
Received: 431

      at <anonymous> (/workspace/bun/test/js/node/http/node-http-maxHeadersCount.test.ts:67:24)
      at async withServer (/workspace/bun/test/js/node/http/node-http-maxHeadersCount.test.ts:46:11)
      at async <anonymous> (/workspace/bun/test/js/node/http/node-http-maxHeadersCount.test.ts:57:9)
 97 |   server.maxHeadersCount = 400;
 98 |   (server as any).maxHeaderSize = 1 << 20;
 99 |   const port = await listen(server);
100 |   try {
101 |     const { status, body } = await rawRequest(port, 350);
102 |     expect(status).toBe(200);
                         ^
error: ex
... (truncated)

release without fix: BUILD FAILED (no junit output)
bun test v1.4.0-canary.1 (1dbe57925)

test/js/node/http/node-http-maxHeadersCount.test.ts:
============================================================
Bun Canary v1.4.0-canary.1 (1dbe57925) Linux x64
Linux Kernel v6.17.0 | glibc v2.41
CPU: sse42 popcnt avx avx2 avx512
Args: "bun" "test" "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" "test/js/node/http/node-http-maxHeadersCount.test.ts"
Features: bunfig http_server(4) jsc transpiler_cache(2) tsconfig(3) tsconfig_paths 
Builtins: "bun:internal-for-testing" "bun:jsc" "bun:test" "node:child_process" "node:fs" "node:fs/promises" "node:http" "node:net" "node:os" "node:path" 

Elapsed: 180ms | User: 54ms | Sys: 47ms
RSS: 54.47 MB | Peak: 50.72 MB | Commit: 75.10 MB | Faults: 0 | Machine: 34.36 GB

panic(main thread): Segmentation fault at address 0x0
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

To send a redacted crash report to Bun's team,
please file a GitHub issue using the link below:

 https://bun.report/1.4.0/lt21dbe579gHgkgoC8hg+wB4+69wBgqw9wBw+p9wBi0o+wB87pz5B61nz5B4/9y5B+tp24CmmvmhD4tkmhDmxvq1BuorjjDm+wwiDyzpwiD0ys0lCA2AA

curl: (22) The requested URL returned error: 403
/bin/ba
... (truncated)
passes on PR (with fix)
ASAN with fix: all passed
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/node/http/node-http-maxHeadersCount.test.ts
bun test v1.4.0 (100e7f591)

test/js/node/http/node-http-maxHeadersCount.test.ts:
(pass) server.maxHeadersCount preserves every header value across the overflow path [1076.94ms]
(pass) server.maxHeadersCount near UINT32_MAX is clamped (no overflow, no giant allocation) [1057.26ms]
(pass) default server still rejects beyond the compiled-in header-field count cap [1285.01ms]
(pass) server.maxHeadersCount raises the request-header field count ceiling [1422.75ms]

 4 pass
 0 fail
 11 expect() calls
Ran 4 tests across 1 file. [4.23s]
__F:0:S:0

release with fix: all passed
$ bun scripts/build.ts --profile=release
[configured] bun-profile → bun (stripped) in 641ms (unchanged)
ninja: Entering directory `/workspace/bun/build/release'
[1/26] gen cpp.rs (cppbind)
[2/26] gen generated_host_exports.rs
generated_host_exports.rs: 94 exports (host=3, lazy=10, generic=81, rust=0); 240 extern-C blocks audited
[3/26] gen JS modules (bundle-modules)
Preprocess modules (8924ms)
Bundle modules (39ms)
Postprocesss modules (198ms)
Bundle Functions (722ms)
Generate Code (25ms)

[9.93s] Bundled "src/js" for production
  2570 kb
  193 internal modules
  13 native modules
  90 internal functions across 19 files
[3/15] cargo bun_bin → libbun_rust.a (--target x86_64-unknown-linux-gnu)

  nightly-2026-07-20-x86_64-unknown-linux-gnu unchanged - rustc 1.99.0-nightly (9f36de775 2026-07-19)

�[1m�[92m   Compiling�[0m bun_core v0.0.0 (/workspace/bun/src/bun_core)
�[1m�[92m   Compiling�[0m bun_errno v0.0.0 (/workspace/bun/src/errno)
�[1m�[92m   Compiling�[0m bun_ptr v0.0.0 (/workspace/bun/src/ptr)
�[1m�[92m   Compiling�[0m bun_boringssl_sys v0.0.0 (/workspace/bun/src/boringssl_sys)
�[1m�[92m   Compiling�[0m bun_safety v0.0.0 (/workspace/bun/src/safety)
�[1m�[92
... (truncated)
diff hotspot
packages/bun-uws/src/App.h                         |   5 +
 packages/bun-uws/src/HttpContext.h                 |   2 +-
 packages/bun-uws/src/HttpContextData.h             |   1 +
 packages/bun-uws/src/HttpParser.h                  |  42 ++++++-
 src/js/internal/http.ts                            |   4 +-
 src/js/node/_http_server.ts                        |   2 +
 src/jsc/bindings/NodeHTTP.cpp                      |  12 +-
 src/runtime/server/mod.rs                          |   7 ++
 src/runtime/server/server_body.rs                  |  43 +++++++
 src/uws_sys/App.rs                                 |   9 ++
 src/uws_sys/libuwsockets.cpp                       |   9 ++
 .../js/node/http/node-http-maxHeadersCount.test.ts | 139 +++++++++++++++++++++
 12 files changed, 266 insertions(+), 9 deletions(-)

gate history · 2 passed · 0 rejected · iteration 0

evidence per changed file
file                                                 reads  edits  tests
packages/bun-uws/src/App.h                               1      1      0
packages/bun-uws/src/HttpContext.h                       1      1      0
packages/bun-uws/src/HttpContextData.h                   1      1      0
packages/bun-uws/src/HttpParser.h                        5      8      0
src/js/internal/http.ts                                  2      1      0
src/js/node/_http_server.ts                              3      1      0
src/jsc/bindings/NodeHTTP.cpp                            2      2      0
src/runtime/server/mod.rs                                1      1      0
src/runtime/server/server_body.rs                        1      1      0
src/uws_sys/App.rs                                       2      2      0
src/uws_sys/libuwsockets.cpp                             1      1      0
test/js/node/http/node-http-maxHeadersCount.test.ts      1      5      0

The uWS request parser stored header string_views in a fixed
Header[UWS_HTTP_MAX_HEADERS_COUNT=200] array inside HttpRequest, giving
a hard ceiling of 198 request header fields (slot 0 is the request line,
the last slot is the null-key terminator) that no option could raise.
node:http's server.maxHeadersCount was accepted but never pushed to the
native parser, so setting it above ~198 had no effect and the server
answered 431 Request Header Fields Too Large.

HttpRequest now holds a Header* that defaults to the inline[200] array
and can be repointed at a per-connection std::vector<Header> owned by
HttpParser. The vector is sized once (on the first parse after the
server raised maxHeadersCount) and reused for every request on that
connection, so connections on servers that never raise the option see
no allocation and no layout change. getHeaders' loop bound is now the
live capacity instead of the compile-time constant.

node:http's applyServerCustomOptions forwards server.maxHeadersCount
(when > 0) through setServerCustomOptions → Server__setMaxHeadersCount
→ uws_app_set_max_headers_count → HttpContextData::maxHeadersCount →
consumePostPadded. The JS-side rawHeaders truncation for values below
the compiled-in cap is unchanged (matches Node's truncate-not-431
behavior).

Fixes #6982
@coderabbitai

coderabbitai Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 13 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4ef1cc64-e617-491a-90c0-a00c09b3b254

📥 Commits

Reviewing files that changed from the base of the PR and between 44f6469 and 100e7f5.

📒 Files selected for processing (12)
  • packages/bun-uws/src/App.h
  • packages/bun-uws/src/HttpContext.h
  • packages/bun-uws/src/HttpContextData.h
  • packages/bun-uws/src/HttpParser.h
  • src/js/internal/http.ts
  • src/js/node/_http_server.ts
  • src/jsc/bindings/NodeHTTP.cpp
  • src/runtime/server/mod.rs
  • src/runtime/server/server_body.rs
  • src/uws_sys/App.rs
  • src/uws_sys/libuwsockets.cpp
  • test/js/node/http/node-http-maxHeadersCount.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 7:53 AM PT - Jul 26th, 2026

❌ @robobun, your commit 100e7f5 has 2 failures in Build #82325 (All Failures):

  • test/js/node/worker_threads/worker-transfer-terminate-stress.test.ts - SIGABRT on 🐧 13 x64-asan
  • 📦 Binary size — 12 over 0.50 MB
  • targetthis build canary: main #79916
    sizeΔ
    ❌ bun-darwin-aarch6458.13 MB57.58 MB+564.9 KB
    ❌ bun-darwin-x6463.48 MB62.95 MB+544.5 KB
    ❌ bun-linux-aarch6470.98 MB70.42 MB+576.0 KB
    ❌ bun-linux-x6472.47 MB71.95 MB+528.0 KB
    ❌ bun-linux-aarch64-musl64.88 MB64.32 MB+576.0 KB
    ❌ bun-linux-x64-musl66.98 MB66.45 MB+544.0 KB
    ❌ bun-linux-aarch64-android78.47 MB77.97 MB+512.0 KB
    ❌ bun-linux-x64-android80.62 MB80.10 MB+529.2 KB
    ❌ bun-freebsd-x6483.07 MB82.56 MB+528.0 KB
    ❌ bun-freebsd-aarch6484.84 MB84.31 MB+544.0 KB
    ❌ bun-windows-x6480.26 MB79.70 MB+572.5 KB
    ❌ bun-windows-aarch6470.86 MB70.34 MB+534.0 KB

    Add [skip size check] to the commit message if this increase is intentional.


🧪   To try this PR locally:

bunx bun-pr 35907

That installs a local version of the PR into your bun-35907 executable, so you can run:

bun-35907 --bun

Comment thread packages/bun-uws/src/HttpParser.h Resolved
…ons in the test

The overflow vector size is now min(maxHeadersCount + 2, maxFallbackSize/4 + 2),
computed in size_t so a near-UINT32_MAX value can neither wrap the +2 nor
allocate more than the byte-size head cap can ever admit (the minimum field
line is 4 bytes). The rawRequest/listen helpers reject on socket 'error'.
@robobun

robobun commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator Author

CI on 100e7f5 is green for the diff; the red is unrelated:

  • binary-size (12 targets +~550 KB): stale main #79916 baseline affecting every PR right now (e.g. build 82300, same deltas to the KB); being fixed in ci(binary-size): warn instead of fail, compare against the PR merge-base #35906. My linux-x64 is 72.47 MB, 16 KB under 82300's.
  • worker-transfer-terminate-stress.test.ts SIGABRT on debian x64-asan: JSC ExceptionScope::assertNoException() during worker termination, untouched by this diff; reported for main-break triage.
  • bun-install-registry, hot.test, bun-upgrade, css.test, webview-chrome, test-fs-read-stream-pos, napi.test: pre-existing flakes on single retried lanes.

The new node-http-maxHeadersCount.test.ts passed on all lanes. Ready for review.

@claude claude 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.

Both earlier findings are addressed in 100e7f5 and I didn't find anything new, but this reworks HttpRequest's header storage (inline array → repointable Header* backed by a per-connection std::vector) inside the uWS request parser hot path, which is worth a human look.

What was reviewed:

  • consumePostPadded sizing: want now widened to size_t and clamped by maxFallbackSize/4 + 2, so the UINT32_MAX wrap and unbounded-resize cases are gone (test at 0xFFFF_FFFE covers it).
  • Lifetime of req.headers when repointed at headerOverflow.data(): the vector lives on HttpParser (connection-owned) and is only resized before parsing, so the pointer stays valid through requestHandler and header iteration/capture in assignHeadersFromUWebSocketsForCall.
  • getHeaders loop bound switched to headerCapacity - 1; the null-key terminator write at headers[...] still lands in-bounds for both the inline and overflow storage.
  • Test helpers now wire sock.on('error', reject) / server.on('error', reject).
Extended reasoning...

Overview

The PR threads server.maxHeadersCount from node:http through setServerCustomOptions → jsHTTPSetCustomOptions (NodeHTTP.cpp) → Server__setMaxHeadersCount (server_body.rs, all four server variants) → uws_app_set_max_headers_count → HttpContextData::maxHeadersCount, and then into consumePostPadded on every onData. Inside the parser, HttpRequest gains a Header* + headerCapacity pair (defaulting to the existing inline Header[200]); when the configured count exceeds 198, consumePostPadded grows a per-connection std::vector<Header> on HttpParser and repoints req.headers at it before calling getHeaders, whose field-line loop now uses the passed-in capacity. Twelve files across C++/Rust/TS plus a new test file.

Security risks

The uWS HTTP parser handles untrusted network bytes and is the first line of defense against request smuggling / header-overflow attacks. My earlier pass found a user-controlled uint32_t + 2 overflow that produced a null/1-element buffer and a confirmed release-build segfault at 0x0; that is now fixed by widening to size_t and clamping against maxFallbackSize/4 + 2 (the byte-size cap already bounds achievable field count). I re-checked the clamp: on Bun's 64-bit targets (size_t)maxHeadersCount + 2 cannot wrap, and want only exceeds UINT_MAX if maxHeaderSize is set to ≥~17 GB, at which point resize would bad_alloc first — not a practical vector. No new untrusted-input parsing was added; the loop-bound change is a straight substitution of a constant for a parameter.

Level of scrutiny

High. This is a memory-layout change in the HTTP request parser hot path — every request on every node:http/Bun.serve connection runs this code — and it introduces a raw pointer that can reference storage outside the stack-local HttpRequest. The default (option-not-raised) path is layout-unchanged and allocation-free, which limits blast radius, but changes here have historically been the source of memory-safety and smuggling issues. That combination is beyond what I'm comfortable auto-approving.

Other factors

The plumbing layers (Rust shim, libuwsockets.cpp wrapper, App.h setter, TS type signature) mirror the existing maxHeaderSize path exactly and are mechanical. The new tests exercise 300/398/399/350 header counts, the near-UINT32_MAX clamp, and the default-cap 431, and fail-before/pass-after evidence is in the PR body (including the pre-fix release segfault). Both prior inline threads are resolved by the author with matching commits.

@robobun

robobun commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator Author

Re-checked against current main (165dc9f) now that #6982 is closed. Not covered: main still has the fixed UWS_HTTP_MAX_HEADERS_COUNT (200) array and server.maxHeadersCount is not forwarded to the parser. Running this PR's test/js/node/http/node-http-maxHeadersCount.test.ts on main, 3 of 4 tests fail (server.maxHeadersCount = 400 with 300 or 350 header lines, and the near-UINT32_MAX clamp case), each with Expected: 200, Received: 431; the one that passes is the default-server case, which holds on main by construction.

#6982 was closed because the compiled-in limit had been raised, with the note that a configurable limit was not added, and this PR is that configurable limit, so whether it is still wanted is a product call. It currently conflicts with main and would need a rebase either way. Leaving it open for now.

@robobun

robobun commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator Author

Note for review: #42532 replaces part of this PR. It adds the same plumbing with the same names (Server__setMaxHeadersCount, uws_app_set_max_headers_count, HttpContextData::maxHeadersCount) and a test file at the same path, test/js/node/http/node-http-maxHeadersCount.test.ts. It rejects a request over server.maxHeadersCount with 431, like Node since nodejs/node@821688aaa0 (CVE-2026-58044). The truncation below the built-in limit that this PR keeps no longer matches Node. The overflow storage for more than 198 fields (#6982) is only in this PR.

@robobun

robobun commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as part of a cleanup of stale pull requests. This PR has had no new commits since 2026-07-26, it conflicts with main, and its last CI run failed. This is not a judgment on the fix itself. If the problem still reproduces on a current build, reopen this PR after a rebase or open a new one against main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow MAX_HEADERS to be configurable.

2 participants