Skip to content
Merged
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
266 changes: 257 additions & 9 deletions pkgs/fleet/crates/fleet-proxy/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,34 @@
//! caller's problem (the broker mirrors the Python: one retry on a
//! fresh connection regardless, because these requests are the
//! idempotent-shaped local RPCs the Python broker already retries).
//!
//! Peer-declared sizes are never trusted (#5579): a malformed or
//! conflicting `Content-Length` is a [`HttpError::Malformed`] that
//! marks the connection non-reusable (treating it as "no body"
//! would leave the real body bytes in the stream and desync the
//! next response parsed on the pooled connection), declared body
//! sizes are checked against [`MAX_RESPONSE_BODY_BYTES`] *before*
//! any allocation for them, and status/header/chunk-size lines are
//! read with the [`MAX_LINE_BYTES`] bound instead of accumulating
//! without limit.

use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream;

/// Largest response body this client will accept, on either the
/// Content-Length or the chunked path (cumulative for chunked).
/// Matches the fleet-proxy-proto frame ceiling
/// (`MAX_FRAME_BYTES`, 64 MiB): a peer declaring more than this is
/// buggy or hostile, and the error is raised before allocating
/// anything sized by the declaration.
pub const MAX_RESPONSE_BODY_BYTES: usize = 64 * 1024 * 1024;

/// Largest single line (status line, one header line, one chunk
/// size line, one trailer line) this client will read. Matches the
/// tunnel CONNECT response cap precedent (65536 bytes): legitimate
/// lines from the laptop's local services are nowhere near this.
pub const MAX_LINE_BYTES: usize = 64 * 1024;

#[derive(Debug)]
pub enum HttpError {
/// EOF before a single response byte: the connection was stale.
Expand Down Expand Up @@ -64,6 +88,35 @@ impl HttpConn {
self.reusable
}

/// Read one line with the [`MAX_LINE_BYTES`] bound. Returns the
/// number of bytes read (0 at EOF), like `BufRead::read_line`;
/// a line longer than the bound is [`HttpError::Malformed`] and
/// marks the connection non-reusable.
fn read_line_bounded(&mut self, out: &mut String) -> Result<usize, HttpError> {
let mut bytes = Vec::new();
let n = self
.reader
.by_ref()
.take((MAX_LINE_BYTES + 1) as u64)
.read_until(b'\n', &mut bytes)?;
if bytes.len() > MAX_LINE_BYTES {
self.reusable = false;
return Err(HttpError::Malformed(format!(
"response line exceeds {MAX_LINE_BYTES} bytes"
)));
}
match String::from_utf8(bytes) {
Ok(s) => {
out.push_str(&s);
Ok(n)
}
Err(e) => Err(HttpError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
e,
))),
}
}

/// One request/response exchange. `body` is the already-encoded
/// JSON payload, or None for a bodyless request (no
/// Content-Length is sent then — http.client's shape).
Expand Down Expand Up @@ -95,7 +148,7 @@ impl HttpConn {

// Status line. Zero bytes read = stale pooled connection.
let mut status_line = String::new();
let n = self.reader.read_line(&mut status_line)?;
let n = self.read_line_bounded(&mut status_line)?;
if n == 0 {
self.reusable = false;
return Err(HttpError::Stale);
Expand All @@ -115,7 +168,7 @@ impl HttpConn {
let mut close_after = false;
loop {
let mut line = String::new();
let n = self.reader.read_line(&mut line)?;
let n = self.read_line_bounded(&mut line)?;
if n == 0 {
self.reusable = false;
return Err(HttpError::Malformed("EOF inside headers".into()));
Expand All @@ -130,7 +183,31 @@ impl HttpConn {
};
let value = value.trim();
match name.trim().to_ascii_lowercase().as_str() {
"content-length" => content_length = value.parse().ok(),
"content-length" => {
// Strict: an unparsable value is NOT "no body"
// (that reading desyncs the pooled connection),
// duplicates must agree, and the declared size
// is capped before it is ever allocated for.
let parsed: usize = value.parse().map_err(|_| {
self.reusable = false;
HttpError::Malformed(format!("Content-Length {value:?}"))
})?;
if parsed > MAX_RESPONSE_BODY_BYTES {
self.reusable = false;
return Err(HttpError::Malformed(format!(
"Content-Length {parsed} exceeds {MAX_RESPONSE_BODY_BYTES}-byte response body cap"
)));
}
if let Some(prev) = content_length {
if prev != parsed {
self.reusable = false;
return Err(HttpError::Malformed(format!(
"conflicting Content-Length headers {prev} and {parsed}"
)));
}
}
content_length = Some(parsed);
}
"transfer-encoding" => chunked = value.to_ascii_lowercase().contains("chunked"),
"connection" => {
close_after = value.to_ascii_lowercase().contains("close");
Expand All @@ -144,13 +221,27 @@ impl HttpConn {
if chunked {
self.read_chunked(&mut data)?;
} else if let Some(len) = content_length {
// len <= MAX_RESPONSE_BODY_BYTES was enforced while
// parsing the header, before this allocation.
data.resize(len, 0);
self.reader.read_exact(&mut data).map_err(|e| {
self.reusable = false;
HttpError::Io(e)
})?;
} else if close_after {
self.reader.read_to_end(&mut data).map_err(HttpError::Io)?;
// Close-delimited body: still peer-sized, so read with
// the same cap (the connection is dead either way).
self.reader
.by_ref()
.take((MAX_RESPONSE_BODY_BYTES + 1) as u64)
.read_to_end(&mut data)
.map_err(HttpError::Io)?;
if data.len() > MAX_RESPONSE_BODY_BYTES {
self.reusable = false;
return Err(HttpError::Malformed(format!(
"close-delimited body exceeds {MAX_RESPONSE_BODY_BYTES}-byte response body cap"
)));
}
self.reusable = false;
return Ok((status, data));
}
Expand All @@ -165,7 +256,7 @@ impl HttpConn {
fn read_chunked(&mut self, out: &mut Vec<u8>) -> Result<(), HttpError> {
loop {
let mut size_line = String::new();
let n = self.reader.read_line(&mut size_line)?;
let n = self.read_line_bounded(&mut size_line)?;
if n == 0 {
self.reusable = false;
return Err(HttpError::Malformed("EOF inside chunked body".into()));
Expand All @@ -179,13 +270,24 @@ impl HttpConn {
// Trailer part: header lines until the blank line.
loop {
let mut trailer = String::new();
let n = self.reader.read_line(&mut trailer)?;
let n = self.read_line_bounded(&mut trailer)?;
if n == 0 || trailer.trim_end().is_empty() {
break;
}
}
return Ok(());
}
// Cap each chunk AND the cumulative body before
// allocating anything sized by the declaration.
match out.len().checked_add(size) {
Some(total) if total <= MAX_RESPONSE_BODY_BYTES => {}
_ => {
self.reusable = false;
return Err(HttpError::Malformed(format!(
"chunked body exceeds {MAX_RESPONSE_BODY_BYTES}-byte response body cap"
)));
}
}
let start = out.len();
out.resize(start + size, 0);
self.reader.read_exact(&mut out[start..]).map_err(|e| {
Expand All @@ -205,15 +307,161 @@ impl HttpConn {
#[cfg(test)]
mod tests {
use super::*;
use std::net::TcpListener;

// The reader machinery is exercised against canned responses by
// way of a loopback pair in the crate's integration tests; here
// we pin the header-parsing behavior on the error paths that do
// not need a socket.
// way of a loopback pair; the error-path pins live here, next to
// the reader they pin (#5579).

#[test]
fn stale_is_distinct_from_malformed() {
assert!(matches!(HttpError::Stale, HttpError::Stale));
let m = HttpError::Malformed("x".into());
assert!(m.to_string().contains("malformed"));
}

/// An HttpConn whose peer answers each request (head read up to
/// the blank line) with the next canned response, in order.
fn canned_conn(responses: Vec<Vec<u8>>) -> (HttpConn, std::thread::JoinHandle<()>) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let stream = TcpStream::connect(addr).unwrap();
let (mut peer, _) = listener.accept().unwrap();
let server = std::thread::spawn(move || {
for response in &responses {
let mut buf = [0u8; 1024];
let mut seen = Vec::new();
while !seen.windows(4).any(|w| w == b"\r\n\r\n") {
match peer.read(&mut buf) {
Ok(0) | Err(_) => return,
Ok(n) => seen.extend_from_slice(&buf[..n]),
}
}
if peer.write_all(response).is_err() {
return;
}
let _ = peer.flush();
}
});
(HttpConn::new(stream).unwrap(), server)
}

fn get(conn: &mut HttpConn) -> Result<(u16, Vec<u8>), HttpError> {
conn.request("GET", "/x", "127.0.0.1", "token", None)
}

#[test]
fn well_formed_content_length_round_trips_and_stays_reusable() {
let (mut conn, server) = canned_conn(vec![
b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n{}".to_vec(),
b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n[]".to_vec(),
]);
let (status, body) = get(&mut conn).unwrap();
assert_eq!(status, 200);
assert_eq!(body, b"{}");
assert!(conn.reusable());
// A second exchange on the same pooled connection parses
// cleanly — the first body was consumed exactly.
let (status, body) = get(&mut conn).unwrap();
assert_eq!(status, 200);
assert_eq!(body, b"[]");
assert!(conn.reusable());
server.join().unwrap();
}

#[test]
fn well_formed_chunked_round_trips_and_stays_reusable() {
let (mut conn, server) = canned_conn(vec![
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n"
.to_vec(),
]);
let (status, body) = get(&mut conn).unwrap();
assert_eq!(status, 200);
assert_eq!(body, b"hello");
assert!(conn.reusable());
server.join().unwrap();
}

#[test]
fn agreeing_duplicate_content_length_is_accepted() {
let (mut conn, server) = canned_conn(vec![
b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-Length: 2\r\n\r\n{}".to_vec(),
]);
let (status, body) = get(&mut conn).unwrap();
assert_eq!(status, 200);
assert_eq!(body, b"{}");
assert!(conn.reusable());
server.join().unwrap();
}

#[test]
fn invalid_content_length_is_malformed_and_not_reusable() {
let (mut conn, _server) = canned_conn(vec![
b"HTTP/1.1 200 OK\r\nContent-Length: abc\r\n\r\nhello".to_vec(),
]);
let err = get(&mut conn).unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
assert!(!conn.reusable());
}

#[test]
fn conflicting_duplicate_content_length_is_malformed_and_not_reusable() {
let (mut conn, _server) = canned_conn(vec![
b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nContent-Length: 0\r\n\r\nhello".to_vec(),
]);
let err = get(&mut conn).unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
assert!(!conn.reusable());
}

#[test]
fn oversize_declared_content_length_errors_before_the_body() {
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n",
MAX_RESPONSE_BODY_BYTES + 1
);
let (mut conn, _server) = canned_conn(vec![response.into_bytes()]);
// No body bytes are ever sent: the error must come from the
// header alone, before any allocation sized by it.
let err = get(&mut conn).unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
assert!(!conn.reusable());
}

#[test]
fn oversize_chunk_errors_before_the_chunk_body() {
let response = format!(
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n{:x}\r\n",
MAX_RESPONSE_BODY_BYTES + 1
);
let (mut conn, _server) = canned_conn(vec![response.into_bytes()]);
let err = get(&mut conn).unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
assert!(!conn.reusable());
}

#[test]
fn cumulative_chunked_body_over_the_cap_errors() {
// First chunk is small and fully sent; the second chunk is
// exactly at the cap on its own, so only the cumulative
// check can reject it — before its body is read or sized.
let mut response =
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n".to_vec();
response.extend_from_slice(format!("{:x}\r\n", MAX_RESPONSE_BODY_BYTES).as_bytes());
let (mut conn, _server) = canned_conn(vec![response]);
let err = get(&mut conn).unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
assert!(!conn.reusable());
}

#[test]
fn overlong_status_line_is_malformed_and_not_reusable() {
let mut response = b"HTTP/1.1 200 OK ".to_vec();
response.extend_from_slice("x".repeat(MAX_LINE_BYTES + 100).as_bytes());
response.extend_from_slice(b"\r\n\r\n");
let (mut conn, _server) = canned_conn(vec![response]);
let err = get(&mut conn).unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
assert!(!conn.reusable());
}
}