Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions src/http/InternalState.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub struct InternalState<'a> {

pub(crate) transfer_encoding: Encoding,
pub(crate) encoding: Encoding,
pub(crate) content_encoding_i: u8,
pub(crate) chunked_decoder: bun_picohttp::phr_chunked_decoder,
pub(crate) decompressor: Decompressor,
pub(crate) stage: Stage,
Expand Down Expand Up @@ -61,7 +60,6 @@ pub struct InternalState<'a> {
pub struct InternalStateFlags {
pub(crate) allow_keepalive: bool,
pub(crate) received_last_chunk: bool,
pub(crate) did_set_content_encoding: bool,
pub(crate) is_redirect_pending: bool,
pub(crate) is_libdeflate_fast_path_disabled: bool,
pub(crate) resend_request_body_on_redirect: bool,
Expand All @@ -88,7 +86,6 @@ impl InternalStateFlags {
Self {
allow_keepalive: true,
received_last_chunk: false,
did_set_content_encoding: false,
is_redirect_pending: false,
is_libdeflate_fast_path_disabled: false,
resend_request_body_on_redirect: false,
Expand All @@ -107,7 +104,6 @@ impl Default for InternalState<'_> {
flags: InternalStateFlags::new(),
transfer_encoding: Encoding::Identity,
encoding: Encoding::Identity,
content_encoding_i: u8::MAX,
chunked_decoder: bun_picohttp::phr_chunked_decoder::default(),
decompressor: Decompressor::None,
stage: Stage::Pending,
Expand Down
3 changes: 2 additions & 1 deletion src/http/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ impl From<bun_picohttp::ParseResponseError> for Error {
fn from(e: bun_picohttp::ParseResponseError) -> Self {
match e {
bun_picohttp::ParseResponseError::ShortRead => Error::ShortRead,
_ => Error::Picohttp(e),
bun_picohttp::ParseResponseError::TooManyHeaders => Error::ResponseHeadersTooLarge,
bun_picohttp::ParseResponseError::MalformedHttpResponse => Error::Picohttp(e),
}
}
}
Expand Down
24 changes: 10 additions & 14 deletions src/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ impl HTTPClient<'_> {
status_code: u32,
headers: &'h [picohttp::Header],
) -> crate::Result<(HeaderResult, picohttp::Response<'h>)> {
if headers.len() > MAX_RESPONSE_HEADERS {
return Err(crate::Error::ResponseHeadersTooLarge);
}
let mut response = picohttp::Response {
minor_version: 0,
status_code,
Expand Down Expand Up @@ -1044,9 +1047,12 @@ const MAX_REQUEST_HEADERS: usize = 256;
static SHARED_REQUEST_HEADERS_BUF: bun_core::RacyCell<[picohttp::Header; MAX_REQUEST_HEADERS]> =
bun_core::RacyCell::new([picohttp::Header::ZERO; MAX_REQUEST_HEADERS]);

/// Field limit of a response on every protocol. Node's HTTP client enforces the same count.
const MAX_RESPONSE_HEADERS: usize = 1000;

// this doesn't need to be stack memory because it is immediately cloned after use
static SHARED_RESPONSE_HEADERS_BUF: bun_core::RacyCell<[picohttp::Header; 256]> =
bun_core::RacyCell::new([picohttp::Header::ZERO; 256]);
static SHARED_RESPONSE_HEADERS_BUF: bun_core::RacyCell<[picohttp::Header; MAX_RESPONSE_HEADERS]> =
bun_core::RacyCell::new([picohttp::Header::ZERO; MAX_RESPONSE_HEADERS]);

// the first packet for Transfer-Encoding: chunked
// is usually pretty small or sometimes even just a length
Expand All @@ -1069,7 +1075,7 @@ mod scratch {
unsafe { &mut *SHARED_REQUEST_HEADERS_BUF.get() }
}
#[inline]
pub(super) fn response_headers() -> &'static mut [picohttp::Header; 256] {
pub(super) fn response_headers() -> &'static mut [picohttp::Header; MAX_RESPONSE_HEADERS] {
// SAFETY: see module-level INVARIANT.
unsafe { &mut *SHARED_RESPONSE_HEADERS_BUF.get() }
}
Expand Down Expand Up @@ -3776,14 +3782,6 @@ impl<'a> HTTPClient<'a> {
// Headers complete: start the body-idle window fresh (see [`IDLE_TIMEOUT_SECONDS`]).
self.set_timeout(&socket);

if (self.state.content_encoding_i as usize) < response.headers.list.len()
&& !self.state.flags.did_set_content_encoding
{
// if it compressed with this header, it is no longer because we will decompress it
self.state.flags.did_set_content_encoding = true;
self.state.content_encoding_i = u8::MAX;
}

if should_continue == ShouldContinue::Finished {
if !to_read.is_empty() {
self.state.flags.allow_keepalive = false;
Expand Down Expand Up @@ -4821,7 +4819,7 @@ impl<'a> HTTPClient<'a> {
let mut is_server_sent_events = false;
let mut content_codings: u32 = 0;
let mut has_keep_alive_token = false;
for (header_i, header) in response.headers.list.iter().enumerate() {
for header in response.headers.list.iter() {
match hash_header_name(header.name()) {
h if h == hash_header_const(b"Content-Length") => {
// byte-level parse — header.value() is network bytes, not &str
Expand Down Expand Up @@ -4863,13 +4861,11 @@ impl<'a> HTTPClient<'a> {
Some(Encoding::Identity) => {}
Some(coding) if coding.is_compressed() && content_codings == 0 => {
self.state.encoding = coding;
self.state.content_encoding_i = header_i as u8;
content_codings = 1;
}
// Stacked or unknown codings: we can only strip one layer, so pass through raw.
_ => {
self.state.encoding = Encoding::Identity;
self.state.content_encoding_i = u8::MAX;
content_codings = u32::MAX;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/http_jsc/websocket_client/WebSocketUpgradeClient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ where

match parsed {
Ok(done) => done,
Err(picohttp::ParseResponseError::MalformedHttpResponse) => HeadParse::Invalid,
Err(
picohttp::ParseResponseError::MalformedHttpResponse
| picohttp::ParseResponseError::TooManyHeaders,
) => HeadParse::Invalid,
Comment thread
robobun marked this conversation as resolved.
Resolved
Err(picohttp::ParseResponseError::ShortRead) => {
if !buffered {
self.body.with_mut(|b| b.extend_from_slice(data));
Expand Down
4 changes: 4 additions & 0 deletions src/picohttp/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ impl fmt::Display for StatusCodeFormatter {
pub enum ParseResponseError {
#[strum(serialize = "Malformed_HTTP_Response")]
MalformedHttpResponse,
/// Every slot of `src` is in use and the header section has not ended.
TooManyHeaders,
ShortRead,
}
bun_core::impl_tag_error!(ParseResponseError);
Expand Down Expand Up @@ -498,6 +500,8 @@ impl<'a> Response<'a> {
};

match rc {
// A malformed field returns -1 with `num_headers` below `src.len()`.
-1 if num_headers == src.len() => Err(ParseResponseError::TooManyHeaders),
-1 => {
bun_core::debug!("Malformed HTTP response:\n{}", BStr::new(buf));
Err(ParseResponseError::MalformedHttpResponse)
Expand Down
86 changes: 84 additions & 2 deletions test/js/bun/http/fetch-header-count-limit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
import { describe, expect, test } from "bun:test";
import { once } from "node:events";
import { createServer } from "node:net";
import { type AddressInfo, createServer } from "node:net";

// Use a raw TCP server to avoid header count limits in HTTP servers.
// The server reads the raw request, extracts header info, and sends a JSON response.
Expand Down Expand Up @@ -105,3 +105,85 @@ test("default headers preserved when user headers overflow the buffer", async ()
expect(headerNames).toContain("user-agent");
expect(headerNames).toContain("accept");
});

describe("response header field count", () => {
// MAX_RESPONSE_HEADERS in src/http/lib.rs
const maxResponseHeaders = 1000;

// `total` header fields: Set-Cookie lines, then the two fields that frame the
// body. A parser that stops early cannot read the body.
const makeCookies = (total: number) => Array.from({ length: total - 2 }, (_, i) => `c${i}=v${i}`);
const toWire = (cookies: string[]) =>
"HTTP/1.1 200 OK\r\n" +
cookies.map(cookie => `Set-Cookie: ${cookie}\r\n`).join("") +
"Connection: close\r\nContent-Length: 2\r\n\r\nok";

// Answers the first request with `parts`, one write for each part.
async function serveRaw(...parts: string[]) {
const server = createServer(socket => {
socket.on("error", () => {});
socket.once("data", async () => {
socket.setNoDelay(true);
for (const part of parts.slice(0, -1)) {
await new Promise(resolve => socket.write(part, resolve));
// Give the client a turn to read this part alone. The client reads on
// another thread and gives no signal in the middle of a header block.
await new Promise(resolve => setImmediate(resolve));
}
socket.end(parts.at(-1));
});
}).listen(0, "127.0.0.1");
await once(server, "listening");
return server;
}

const outcomeOf = (server: Awaited<ReturnType<typeof serveRaw>>) =>
fetch(`http://127.0.0.1:${(server.address() as AddressInfo).port}/`).then(
async res => ({
status: res.status,
cookies: res.headers.getSetCookie(),
contentLength: res.headers.get("content-length"),
body: await res.text(),
}),
e => ({ rejected: e.code }),
);

// A response with 257 fields used to reject with Malformed_HTTP_Response.
test.concurrent.each([256, 257, maxResponseHeaders])("%i fields resolve with every field", async total => {
const cookies = makeCookies(total);
await using server = await serveRaw(toWire(cookies));
expect(await outcomeOf(server)).toEqual({ status: 200, cookies, contentLength: "2", body: "ok" });
});

test.concurrent("one field more than the maximum rejects with ResponseHeadersTooLarge", async () => {
await using server = await serveRaw(toWire(makeCookies(maxResponseHeaders + 1)));
expect(await outcomeOf(server)).toEqual({ rejected: "ResponseHeadersTooLarge" });
});

// The first read ends in the middle of the header block. At the maximum, every slot is
// in use when the buffer ends, and the parser must ask for more bytes, not give up.
test.concurrent.each([
["300 fields, cut after 280 complete lines", 300, (wire: string) => wire.indexOf("Set-Cookie: c280=")],
["the maximum, cut before the blank line", maxResponseHeaders, (wire: string) => wire.indexOf("\r\n\r\n") + 2],
["the maximum, cut inside the blank line", maxResponseHeaders, (wire: string) => wire.indexOf("\r\n\r\n") + 3],
])("two reads resolve with every field: %s", async (_, total, cutAt) => {
const cookies = makeCookies(total);
const wire = toWire(cookies);
const cut = cutAt(wire);
await using server = await serveRaw(wire.slice(0, cut), wire.slice(cut));
expect(await outcomeOf(server)).toEqual({ status: 200, cookies, contentLength: "2", body: "ok" });
});

test.concurrent("one field more than the maximum rejects when the first read ends after the last slot", async () => {
const wire = toWire(makeCookies(maxResponseHeaders + 1));
const cut = wire.indexOf("Content-Length: 2\r\n");
await using server = await serveRaw(wire.slice(0, cut), wire.slice(cut));
expect(await outcomeOf(server)).toEqual({ rejected: "ResponseHeadersTooLarge" });
});

test.concurrent("a malformed line after 290 fields still rejects with Malformed_HTTP_Response", async () => {
const wire = toWire(makeCookies(300)).replace("Set-Cookie: c290=v290\r\n", "not a header line\r\n");
await using server = await serveRaw(wire);
expect(await outcomeOf(server)).toEqual({ rejected: "Malformed_HTTP_Response" });
});
});
30 changes: 30 additions & 0 deletions test/js/web/fetch/fetch-http2-adversarial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,36 @@ describe.concurrent("fetch() HTTP/2 adversarial", () => {
},
);
});

// MAX_RESPONSE_HEADERS in src/http/lib.rs is the field-count limit of every protocol.
async function fetchSetCookies(cookies: string[]) {
const block = Buffer.concat([hpackStatus200, ...cookies.map(cookie => hpackLit("set-cookie", cookie))]);
let outcome: unknown;
await withAdversarialServer(
{
onStream: (socket, id) => {
// The block is larger than one frame: HEADERS, then CONTINUATION with END_HEADERS.
socket.write(
Buffer.concat([frame(1, 0x1, id, block.subarray(0, 16384)), frame(9, 0x4, id, block.subarray(16384))]),
);
},
},
async url => {
outcome = await fetch(url, h2).then(r => ({ status: r.status, cookies: r.headers.getSetCookie() }), errcode);
},
);
return outcome;
}
const makeCookies = (count: number) => Array.from({ length: count }, (_, i) => `c${i}=v${i}`);

test("a response with 1000 header fields resolves with every field", async () => {
const cookies = makeCookies(1000);
expect(await fetchSetCookies(cookies)).toEqual({ status: 200, cookies });
});

test("a response with 1001 header fields rejects with ResponseHeadersTooLarge", async () => {
expect(await fetchSetCookies(makeCookies(1001))).toBe("ResponseHeadersTooLarge");
});
});

// ─── session-key regressions ─────────────────────────────────────────────────
Expand Down
29 changes: 29 additions & 0 deletions test/js/web/fetch/fetch-http3-adversarial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,33 @@ describe("http3 response header field validation", () => {
expect(delivered?.headers.get("te") ?? null).toBeNull();
expect(failure).toBeInstanceOf(Error);
});

// MAX_RESPONSE_HEADERS in src/http/lib.rs is the field-count limit of every protocol.
test("accepts 1000 response header fields and rejects 1001 with ResponseHeadersTooLarge", async () => {
const makeCookies = (count: number) => Array.from({ length: count }, (_, i) => `c${i}=v${i}`);
using upstream = Bun.serve({
port: 0,
tls,
http3: true,
http1: false,
fetch(req) {
const count = Number(new URL(req.url).searchParams.get("cookies"));
return new Response("ok", { headers: makeCookies(count).map(cookie => ["set-cookie", cookie]) });
},
});
const outcomeOf = (cookieCount: number) =>
fetch(`https://127.0.0.1:${upstream.port}/?cookies=${cookieCount}`, h3).then(
r => ({ status: r.status, fields: [...r.headers].length, cookies: r.headers.getSetCookie() }),
e => e.code,
);

// The server adds fields of its own (content-length, content-type, date).
const own = await outcomeOf(0);
expect(own).toEqual({ status: 200, fields: expect.any(Number), cookies: [] });
const ownFields = (own as { fields: number }).fields;

const cookies = makeCookies(1000 - ownFields);
expect(await outcomeOf(cookies.length)).toEqual({ status: 200, fields: 1000, cookies });
expect(await outcomeOf(cookies.length + 1)).toBe("ResponseHeadersTooLarge");
});
});
Loading