Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
14 changes: 13 additions & 1 deletion packages/bun-usockets/src/node_quic_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ static void nq_on_path_switch(lsquic_conn_t *c, int validated, int is_preferred,
}

#define US_NQ_HSET_MIN_BUF 256
/* Fallback ceiling for nq_hsi_prepare_decode when maxHeaderLength is
* unlimited: lsqpack requests the decode buffer from the peer-declared
* length varint before any value bytes arrive, so this bounds how much a
* few wire bytes can reserve per stream. */
#define US_NQ_HSET_MAX_BUF (128 * 1024)

struct nq_hset {
struct lsxpack_header xhdr;
Expand Down Expand Up @@ -216,7 +221,14 @@ static struct lsxpack_header *nq_hsi_prepare_decode(void *hset,
struct lsxpack_header *hdr,
size_t space) {
struct nq_hset *h = hset;
if (space > LSXPACK_MAX_STRLEN)
/* 2x max_bytes covers the Huffman decode-window headroom for a single
* header that is itself the whole configured maxHeaderLength; floored at
* US_NQ_HSET_MAX_BUF so a small or unset limit never lowers the abort
* threshold (a header over max_bytes is still silently dropped in
* process_header, which keeps the session alive). */
size_t cap = (size_t) h->max_bytes * 2;
if (cap < US_NQ_HSET_MAX_BUF) cap = US_NQ_HSET_MAX_BUF;
if (space > LSXPACK_MAX_STRLEN || space > cap)
return NULL;
Comment thread
robobun marked this conversation as resolved.
Resolved
if (space > h->decode_cap) {
size_t want = space < US_NQ_HSET_MIN_BUF ? US_NQ_HSET_MIN_BUF : space;
Expand Down
24 changes: 20 additions & 4 deletions packages/bun-usockets/src/quic.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ extern X509_STORE *us_get_default_ca_store(void);

#define US_QUIC_READ_BUF (16 * 1024)

/* Hard ceiling on the per-stream decoded header buffer. lsqpack calls
* prepare_decode with ~1.5x the peer-declared value length before a single
* value byte has arrived (DATA_STATE_READ_VAL_LEN -> guarantee_out_bytes),
* so without a cap a few wire bytes can reserve tens of MB; with 100 bidi
* streams that is a connection-level amplification attack. 128 KB covers a
* ~85 KB single header value (60 KB repros fine) while keeping the worst
* case at 128 KB x es_init_max_streams_bidi per connection. */
#define US_QUIC_HSET_MAX_BUF (128 * 1024)

/* Incoming header set: contiguous storage + index. Created before the
* stream object exists (lsquic decodes headers ahead of on_new_stream),
* so it lives standalone until on_read claims it via lsquic_stream_get_hset. */
Expand Down Expand Up @@ -429,7 +438,13 @@ static void *us_quic_hsi_create(void *hsi_ctx, lsquic_stream_t *s, int is_push)

static struct lsxpack_header *us_quic_hsi_prepare(void *hset_p, struct lsxpack_header *hdr, size_t space) {
struct us_quic_hset *h = (struct us_quic_hset *) hset_p;
if (space > 64 * 1024) return NULL;
/* NULL from this hook is connection-fatal in lsquic (LQRHS_ERROR ->
* HEC_QPACK_DECOMPRESSION_FAILED). The LSXPACK_MAX_STRLEN guard keeps the
* cast to lsxpack_strlen_t below from truncating; US_QUIC_HSET_MAX_BUF
* bounds the allocation a peer-declared length can force (see define). */
if (space > LSXPACK_MAX_STRLEN
|| (size_t) h->len + space > US_QUIC_HSET_MAX_BUF)
return NULL;
unsigned int need = h->len + (unsigned int) space;
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
if (need > h->cap) {
unsigned int ncap = h->cap ? h->cap : 512;
Expand Down Expand Up @@ -708,9 +723,10 @@ us_quic_socket_context_t *us_create_quic_socket_context(
lsquic_engine_init_settings(&ctx->settings, LSENG_HTTP_SERVER);
ctx->settings.es_versions = LSQUIC_DF_VERSIONS & LSQUIC_IETF_VERSIONS;
ctx->settings.es_ecn = 0;
/* QPACK can expand small dynamic-table refs into large header lists; cap
* the post-decode size at the same order as uWS H1's MAX_FALLBACK_SIZE so
* a single request can't run hsi_prepare to OOM. */
/* Advisory only: lsquic writes this into the SETTINGS frame but never
* enforces it on decode. The actual allocation guard is
* US_QUIC_HSET_MAX_BUF in us_quic_hsi_prepare. Kept at uWS H1's default
* so well-behaved clients don't send more than we intend to buffer. */
ctx->settings.es_max_header_list_size = 16 * 1024;
ctx->settings.es_init_max_streams_bidi = 100;
/* Static-table-only response encoding: skips the per-header dynamic
Expand Down
4 changes: 4 additions & 0 deletions packages/h3blast/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ OBJ := $(ROOT)/build/$(PROFILE)/obj/vendor
VENDOR := $(ROOT)/vendor

CC ?= clang
# LSXPACK_MAX_STRLEN must match scripts/build/flags.ts: the linked
# build/$(PROFILE)/obj/vendor/{lsquic,lsqpack,lshpack} objects are compiled
# with the 32-bit lsxpack_strlen_t layout.
CFLAGS := -std=c11 -O3 -g -Wall -Wextra -Wno-unused-parameter \
-DLSXPACK_MAX_STRLEN=UINT32_MAX \
-I$(VENDOR)/lsquic/include \
-I$(VENDOR)/hdrhistogram/include \
-I$(VENDOR)/boringssl/include
Expand Down
13 changes: 13 additions & 0 deletions scripts/build/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ export const globalFlags: Flag[] = [
desc: "Undefine _DLL (we link statically)",
},

// ─── lsxpack struct ABI (all platforms) ───
{
// lsxpack_header.h (shared by lshpack/lsqpack/lsquic and bun's own
// quic.c/node_quic_shim.c/c-bindings.cpp) sizes lsxpack_strlen_t from
// this macro; the upstream default (UINT16_MAX) caps any single decoded
// HTTP/2/3 header at 64 KB and makes a ~44 KB H3 field section abort the
// whole connection with H3_QPACK_DECOMPRESSION_FAILED. The value is
// struct-layout ABI, so every TU that includes the header must agree
// (packages/h3blast/Makefile sets it too for the same reason).
flag: "-DLSXPACK_MAX_STRLEN=UINT32_MAX",
desc: "lshpack/lsqpack: 32-bit header lengths (struct ABI; must match across all consumers)",
Comment thread
robobun marked this conversation as resolved.
Resolved
},
Comment thread
claude[bot] marked this conversation as resolved.
Resolved

// ─── Optimization ───
{
// cmake's Release/RelWithDebInfo build types append this to
Expand Down
17 changes: 17 additions & 0 deletions test/js/bun/http/serve-http3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,23 @@ describe("Bun.serve HTTP/3 adversarial", () => {
});
});

// A single ~60 KB value Huffman-encodes to ~45 KB, and the QPACK decoder
// reserves ~1.5x that, which used to hit quic.c's 64 KB prepare_decode
// ceiling and abort the whole connection with QPACK_DECOMPRESSION_FAILED.
test("single large request header value (>43 KB encoded) decodes without aborting the connection", async () => {
await withServer(async port => {
const big = Buffer.alloc(60000, "A").toString();
const res = await fetchH3(port, "/headers", { headers: { "x-big": big } });
expect(res.status).toBe(200);
const seen = (await res.json()) as Record<string, string>;
expect(seen["x-big"]?.length).toBe(60000);
expect(seen["x-big"]).toBe(big);
// The pooled session must still carry a follow-up request.
const after = await fetchH3(port, "/hello");
expect(await after.text()).toBe("hello over h3");
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

test("8 MB POST body echoes byte-exact", async () => {
await withServer(async port => {
// Patterned (not crypto-random) so the test is deterministic but still
Expand Down
Loading