A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasi-http p2: fix corner case allowing malformed content-length headers (#15070)
Pat Hickey72f8e53cffc5
diff --git a/crates/test-programs/src/bin/p2_http_outbound_request_content_length.rs b/crates/test-programs/src/bin/p2_http_outbound_request_content_length.rsindex 59b6254316..6bed203fa9 100644--- a/crates/test-programs/src/bin/p2_http_outbound_request_content_length.rs+++ b/crates/test-programs/src/bin/p2_http_outbound_request_content_length.rs@@ -94,4 +94,17 @@ fn main() {"unexpected error: {e:#?}");}++ {+ println!("sending with nonzero content-length and without a body");+ let request = make_request();++ let e = test_programs::wasi::http::outgoing_handler::handle(request, None)+ .expect_err("handle should fail because content-length doesn't match body");++ assert!(+ matches!(&e, http_types::ErrorCode::HttpRequestBodySize(Some(0))),+ "unexpected error: {e:#?}"+ );+ }}diff --git a/crates/test-programs/src/bin/p2_http_outbound_request_forward_duplicate_content_length.rs b/crates/test-programs/src/bin/p2_http_outbound_request_forward_duplicate_content_length.rsnew file mode 100644index 0000000000..42281e85ae--- /dev/null+++ b/crates/test-programs/src/bin/p2_http_outbound_request_forward_duplicate_content_length.rs@@ -0,0 +1,49 @@+//! Forwards the headers of a response with two `content-length` values to a+//! new request, which must be rejected rather than sent.++use test_programs::wasi::http::outgoing_handler;+use test_programs::wasi::http::types::{ErrorCode, Fields, Method, OutgoingRequest, Scheme};++fn request(addr: &str, path: &str, headers: Fields) -> OutgoingRequest {+ let request = OutgoingRequest::new(headers);+ request.set_method(&Method::Get).unwrap();+ request.set_scheme(Some(&Scheme::Http)).unwrap();+ request.set_authority(Some(addr)).unwrap();+ request.set_path_with_query(Some(path)).unwrap();+ request+}++/// Fetches `/dup`, whose response has two `content-length` values, and+/// returns its (immutable) headers.+fn duplicate_headers(addr: &str) -> Fields {+ let future = outgoing_handler::handle(request(addr, "/dup", Fields::new()), None).unwrap();+ future.subscribe().block();+ let response = future.get().unwrap().unwrap().unwrap();+ let headers = response.headers();+ assert_eq!(+ headers.get("content-length"),+ [b"5".to_vec(), b"100".to_vec()],+ "precondition: the response must carry both values"+ );+ headers+}++fn assert_rejected(addr: &str, headers: Fields) {+ let err = outgoing_handler::handle(request(addr, "/victim", headers), None)+ .err()+ .expect("request with multiple `content-length` values must be rejected");+ assert!(+ matches!(&err, ErrorCode::InternalError(Some(msg)) if msg.contains("content-length")),+ "unexpected error: {err:?}"+ );+}++fn main() {+ let addr = std::env::var("HTTP_SERVER").unwrap();++ println!("forwarding response headers");+ assert_rejected(&addr, duplicate_headers(&addr));++ println!("forwarding cloned response headers");+ assert_rejected(&addr, duplicate_headers(&addr).clone());+}diff --git a/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rs b/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rsindex bf06d9468b..5b62826c1f 100644--- a/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rs+++ b/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rs@@ -2,7 +2,7 @@ use futures::join;use test_programs::p3::wasi::http::client;use test_programs::p3::wasi::http::types::{ErrorCode, Headers, Method, Request, Scheme, Trailers};use test_programs::p3::{wit_future, wit_stream};-use wit_bindgen::{FutureReader, FutureWriter, StreamWriter};+use wit_bindgen::{FutureReader, FutureWriter, StreamReader, StreamWriter};struct Component;@@ -15,10 +15,21 @@ fn make_request() -> (FutureReader<Result<(), ErrorCode>>,) {let (contents_tx, contents_rx) = wit_stream::new();+ let (request, trailers_tx, transmit) = make_request_with_contents(Some(contents_rx));+ (request, contents_tx, trailers_tx, transmit)+}++fn make_request_with_contents(+ contents_rx: Option<StreamReader<u8>>,+) -> (+ Request,+ FutureWriter<Result<Option<Trailers>, ErrorCode>>,+ FutureReader<Result<(), ErrorCode>>,+) {let (trailers_tx, trailers_rx) = wit_future::new(|| todo!());let (request, transmit) = Request::new(Headers::from_list(&[("Content-Length".to_string(), b"11".to_vec())]).unwrap(),- Some(contents_rx),+ contents_rx,trailers_rx,None,);@@ -38,7 +49,7 @@ fn make_request() -> (.set_path_with_query(Some("/")).expect("setting path with query");- (request, contents_tx, trailers_tx, transmit)+ (request, trailers_tx, transmit)}impl test_programs::p3::exports::wasi::cli::run::Guest for Component {@@ -127,6 +138,39 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {"unexpected error: {err:#?}");}++ println!("sending with nonzero content-length and without a body");+ {+ let (request, trailers_tx, transmit) = make_request_with_contents(None);+ let (handle, transmit, ()) = join!(+ async { client::send(request).await },+ async { transmit.await },+ async {+ _ = trailers_tx.write(Ok(None)).await;+ },+ );+ // As above, whether `handle` observes the error races with the+ // body being polled, so it may also succeed.+ assert!(+ matches!(+ handle,+ Ok(..)+ | Err(ErrorCode::HttpRequestBodySize(Some(0)))+ | Err(ErrorCode::HttpProtocolError)+ ),+ "unexpected handle result: {handle:#?}"+ );+ // With no contents stream the body ends before reaching the+ // declared length, so the host aborts the request body write.+ let err = transmit.expect_err("request transmission should have failed");+ assert!(+ matches!(+ err,+ ErrorCode::HttpProtocolError | ErrorCode::HttpRequestBodySize(Some(0))+ ),+ "unexpected error: {err:#?}"+ );+ }Ok(())}}diff --git a/crates/test-programs/src/bin/p3_http_outbound_request_forward_duplicate_content_length.rs b/crates/test-programs/src/bin/p3_http_outbound_request_forward_duplicate_content_length.rsnew file mode 100644index 0000000000..9fff3ac7ce--- /dev/null+++ b/crates/test-programs/src/bin/p3_http_outbound_request_forward_duplicate_content_length.rs@@ -0,0 +1,65 @@+//! Forwards the headers of a response with two `content-length` values to a+//! new request, which must be rejected rather than sent.++use test_programs::p3::wasi::http::client;+use test_programs::p3::wasi::http::types::{ErrorCode, Fields, Method, Request, Scheme};+use test_programs::p3::wit_future;++struct Component;++test_programs::p3::export!(Component);++fn request(addr: &str, path: &str, headers: Fields) -> Request {+ let (_, trailers_rx) = wit_future::new(|| Ok(None));+ let (request, _transmit) = Request::new(headers, None, trailers_rx, None);+ request.set_method(&Method::Get).unwrap();+ request.set_scheme(Some(&Scheme::Http)).unwrap();+ request.set_authority(Some(addr)).unwrap();+ request.set_path_with_query(Some(path)).unwrap();+ request+}++/// Fetches `/dup`, whose response has two `content-length` values, and+/// returns its (immutable) headers.+async fn duplicate_headers(addr: &str) -> Fields {+ let response = client::send(request(addr, "/dup", Fields::new()))+ .await+ .unwrap();+ let headers = response.get_headers();+ assert_eq!(+ headers.get("content-length"),+ [b"5".to_vec(), b"100".to_vec()],+ "precondition: the response must carry both values"+ );+ headers+}++async fn assert_rejected(addr: &str, headers: Fields) {+ let err = client::send(request(addr, "/victim", headers))+ .await+ .err()+ .expect("request with multiple `content-length` values must be rejected");+ assert!(+ matches!(&err, ErrorCode::InternalError(Some(msg)) if msg.contains("content-length")),+ "unexpected error: {err:?}"+ );+}++impl test_programs::p3::exports::wasi::cli::run::Guest for Component {+ async fn run() -> Result<(), ()> {+ let addr = test_programs::p3::wasi::cli::environment::get_environment()+ .into_iter()+ .find_map(|(k, v)| k.eq("HTTP_SERVER").then_some(v))+ .unwrap();++ println!("forwarding response headers");+ assert_rejected(&addr, duplicate_headers(&addr).await).await;++ println!("forwarding cloned response headers");+ assert_rejected(&addr, duplicate_headers(&addr).await.clone()).await;++ Ok(())+ }+}++fn main() {}diff --git a/crates/wasi-http/Cargo.toml b/crates/wasi-http/Cargo.tomlindex e222f06a0c..1a05210f42 100644--- a/crates/wasi-http/Cargo.toml+++ b/crates/wasi-http/Cargo.toml@@ -51,7 +51,7 @@ test-programs-artifacts = { workspace = true }test-log = { workspace = true }tracing-subscriber = { workspace = true }wasmtime = { workspace = true, features = ['default', 'anyhow'] }-tokio = { workspace = true, features = ['fs', 'macros'] }+tokio = { workspace = true, features = ['fs', 'macros', 'io-util'] }futures = { workspace = true, default-features = false, features = ['alloc', 'async-await'] }sha2 = { workspace = true }base64 = { workspace = true }diff --git a/crates/wasi-http/src/p2/http_impl.rs b/crates/wasi-http/src/p2/http_impl.rsindex ae21f17952..5072261881 100644--- a/crates/wasi-http/src/p2/http_impl.rs+++ b/crates/wasi-http/src/p2/http_impl.rs@@ -1,6 +1,5 @@//! Implementation of the `wasi:http/outgoing-handler` interface.-use crate::WasiHttpCtxView;use crate::p2::{HttpResult,bindings::http::{@@ -11,6 +10,7 @@ use crate::p2::{http_request_error,types::{HostFutureIncomingResponse, HostOutgoingRequest},};+use crate::{WasiHttpCtxView, get_content_length};use bytes::Bytes;use http_body_util::{BodyExt, Empty};use hyper::Method;@@ -31,6 +31,17 @@ impl outgoing_handler::Host for WasiHttpCtxView<'_> {};let req = self.table.delete(request_id)?;++ // The `content-length` header is otherwise only validated in+ // `outgoing-request.body` and `outgoing-body.finish`, neither of which+ // a guest is required to call. Validate it here too so that malformed+ // requests never reach the wire.+ let content_length =+ get_content_length(&req.headers).map_err(|err| internal_error(format!("{err:#}")))?;+ if req.body.is_none() && content_length.is_some_and(|n| n != 0) {+ return Err(types::ErrorCode::HttpRequestBodySize(Some(0)).into());+ }+let mut builder = hyper::Request::builder();builder = builder.method(match req.method {diff --git a/crates/wasi-http/tests/all/http_server.rs b/crates/wasi-http/tests/all/http_server.rsindex 5bc8731110..2bbbec97d6 100644--- a/crates/wasi-http/tests/all/http_server.rs+++ b/crates/wasi-http/tests/all/http_server.rs@@ -3,7 +3,9 @@ use hyper::service::service_fn;use hyper::{Request, Response};use std::future::Future;use std::net::{SocketAddr, TcpStream};+use std::sync::{Arc, Mutex};use std::thread::JoinHandle;+use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};use tokio::net::TcpListener;use tracing::{debug, trace, warn};use wasmtime::{Result, error::Context as _};@@ -36,7 +38,7 @@ pub struct Server {impl Server {fn new<F>(conns: usize,- run: impl Fn(TokioIo<tokio::net::TcpStream>) -> F + Send + 'static,+ run: impl Fn(tokio::net::TcpStream) -> F + Send + 'static,) -> Result<Self>whereF: Future<Output = Result<()>>,@@ -62,7 +64,7 @@ impl Server {match listener.accept().await {Ok((stream, ..)) => {debug!(i, "accepted connection");- if let Err(err) = run(TokioIo::new(stream)).await {+ if let Err(err) = run(stream).await {warn!(i, ?err, "failed to serve connection");}}@@ -82,7 +84,8 @@ impl Server {pub fn http1(conns: usize) -> Result<Self> {debug!("initializing http1 server");- Self::new(conns, |io| async move {+ Self::new(conns, |stream| async move {+ let io = TokioIo::new(stream);let mut builder = hyper::server::conn::http1::Builder::new();let http = builder.keep_alive(false).pipeline_flush(true);@@ -97,7 +100,8 @@ impl Server {/// Test-only: delay the response headers, to exercise `first-byte-timeout`.pub fn http1_delayed(conns: usize, header_delay: std::time::Duration) -> Result<Self> {debug!("initializing delayed http1 server");- Self::new(conns, move |io| async move {+ Self::new(conns, move |stream| async move {+ let io = TokioIo::new(stream);let service = service_fn(move |_req| async move {tokio::time::sleep(header_delay).await;Ok::<_, std::convert::Infallible>(Response::new(crate::body::full(@@ -116,7 +120,8 @@ impl Server {/// Exercises `between-bytes-timeout`.pub fn http1_stalled_body(conns: usize, stall: std::time::Duration) -> Result<Self> {debug!("initializing stalled-body http1 server");- Self::new(conns, move |io| async move {+ Self::new(conns, move |stream| async move {+ let io = TokioIo::new(stream);let service = service_fn(move |_req| async move {use futures::StreamExt as _;let stream = futures::stream::once(async {@@ -144,7 +149,8 @@ impl Server {pub fn http2(conns: usize) -> Result<Self> {debug!("initializing http2 server");- Self::new(conns, |io| async move {+ Self::new(conns, |stream| async move {+ let io = TokioIo::new(stream);let mut builder = hyper::server::conn::http2::Builder::new(TokioExecutor);let http = builder.max_concurrent_streams(20);@@ -164,11 +170,71 @@ impl Server {})}+ /// Test-only: a raw HTTP/1.1 server which doesn't go through `hyper`, for+ /// sending responses `hyper` would refuse to produce.+ ///+ /// For each connection the request head (everything up to and including+ /// the blank line) is read and passed to `respond`, whose return value is+ /// written back verbatim before the connection is closed. Request heads+ /// are recorded and can be retrieved with [`RawRequests::get`]; request+ /// bodies are neither read nor recorded.+ pub fn raw(+ conns: usize,+ respond: impl Fn(&[u8]) -> Vec<u8> + Send + Sync + 'static,+ ) -> Result<(Self, RawRequests)> {+ debug!("initializing raw http1 server");+ let requests = RawRequests::default();+ let recorded = requests.clone();+ let respond = Arc::new(respond);+ let server = Self::new(conns, move |mut stream| {+ let recorded = recorded.clone();+ let respond = respond.clone();+ async move {+ let mut head = Vec::new();+ let mut buf = [0; 1024];+ loop {+ // Any body bytes read along with the head are dropped.+ if let Some(end) = head.windows(4).position(|w| w == b"\r\n\r\n") {+ head.truncate(end + 4);+ break;+ }+ let n = stream.read(&mut buf).await?;+ if n == 0 {+ break;+ }+ head.extend_from_slice(&buf[..n]);+ }+ // `Drop for Server` connects without sending anything to+ // unblock the accept loop; don't record those.+ if head.is_empty() {+ return Ok(());+ }+ trace!(head = %String::from_utf8_lossy(&head), "raw request");+ let response = respond(&head);+ recorded.0.lock().unwrap().push(head);+ stream.write_all(&response).await?;+ stream.shutdown().await?;+ Ok(())+ }+ })?;+ Ok((server, requests))+ }+pub fn addr(&self) -> String {self.addr.to_string()}}+/// The request heads received by a [`Server::raw`], in order.+#[derive(Clone, Default)]+pub struct RawRequests(Arc<Mutex<Vec<Vec<u8>>>>);++impl RawRequests {+ pub fn get(&self) -> Vec<Vec<u8>> {+ self.0.lock().unwrap().clone()+ }+}+impl Drop for Server {fn drop(&mut self) {debug!("shutting down http1 server");@@ -193,3 +259,38 @@ wheretokio::task::spawn(fut);}}++#[cfg(test)]+mod tests {+ use super::Server;+ use std::io::{Read as _, Write as _};+ use std::net::TcpStream;++ #[test]+ fn raw_server_records_and_responds() {+ const RESPONSE: &[u8] = b"HTTP/1.1 200 OK\r\n\+ content-length: 5\r\n\+ content-length: 100\r\n\+ transfer-encoding: chunked\r\n\+ \r\n\+ 0\r\n\r\n";+ let (server, requests) = Server::raw(2, |head| {+ assert!(head.starts_with(b"GET /dup "));+ RESPONSE.to_vec()+ })+ .unwrap();++ // Send a body in the same write as the head; only the head is recorded.+ let request = b"GET /dup HTTP/1.1\r\nhost: example.com\r\ncontent-length: 4\r\n\r\n";+ let mut stream = TcpStream::connect(server.addr()).unwrap();+ stream.write_all(&[&request[..], b"body"].concat()).unwrap();+ let mut response = Vec::new();+ stream.read_to_end(&mut response).unwrap();+ assert_eq!(response, RESPONSE);++ // Dropping the server connects once more for the unused connection;+ // that empty connection must not be recorded.+ drop(server);+ assert_eq!(requests.get(), [request.to_vec()]);+ }+}diff --git a/crates/wasi-http/tests/all/p2/async_.rs b/crates/wasi-http/tests/all/p2/async_.rsindex 3a04dd7f02..3215f607f0 100644--- a/crates/wasi-http/tests/all/p2/async_.rs+++ b/crates/wasi-http/tests/all/p2/async_.rs@@ -126,6 +126,18 @@ async fn p2_http_outbound_request_content_length() -> Result<()> {run(P2_HTTP_OUTBOUND_REQUEST_CONTENT_LENGTH_COMPONENT, &server).await}+#[test_log::test(tokio::test(flavor = "multi_thread"))]+async fn p2_http_outbound_request_forward_duplicate_content_length() -> Result<()> {+ crate::shared::forward_duplicate_content_length(async |server| {+ run(+ P2_HTTP_OUTBOUND_REQUEST_FORWARD_DUPLICATE_CONTENT_LENGTH_COMPONENT,+ server,+ )+ .await+ })+ .await+}+#[test_log::test(tokio::test(flavor = "multi_thread"))]async fn p2_http_outbound_request_missing_path_and_query() -> Result<()> {let server = Server::http1(1)?;diff --git a/crates/wasi-http/tests/all/p2/sync.rs b/crates/wasi-http/tests/all/p2/sync.rsindex fd99b13626..1ce55ce19d 100644--- a/crates/wasi-http/tests/all/p2/sync.rs+++ b/crates/wasi-http/tests/all/p2/sync.rs@@ -123,6 +123,20 @@ fn p2_http_outbound_request_content_length() -> Result<()> {run(P2_HTTP_OUTBOUND_REQUEST_CONTENT_LENGTH_COMPONENT, &server)}+#[test_log::test]+fn p2_http_outbound_request_forward_duplicate_content_length() -> Result<()> {+ // `run` blocks on its own runtime so this can't be driven by another one,+ // but nothing here awaits anything, so a single poll completes it.+ crate::shared::forward_duplicate_content_length(async |server| {+ run(+ P2_HTTP_OUTBOUND_REQUEST_FORWARD_DUPLICATE_CONTENT_LENGTH_COMPONENT,+ server,+ )+ })+ .now_or_never()+ .expect("future completes without awaiting")+}+#[test_log::test]fn p2_http_outbound_request_missing_path_and_query() -> Result<()> {let server = Server::http1(1)?;diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rsindex 9f436f5c4f..b45fd53937 100644--- a/crates/wasi-http/tests/all/p3/mod.rs+++ b/crates/wasi-http/tests/all/p3/mod.rs@@ -383,9 +383,21 @@ async fn p3_http_outbound_request_response_build() -> wasmtime::Result<()> {run_cli(P3_HTTP_OUTBOUND_REQUEST_RESPONSE_BUILD_COMPONENT, &server).await}+#[test_log::test(tokio::test(flavor = "multi_thread"))]+async fn p3_http_outbound_request_forward_duplicate_content_length() -> wasmtime::Result<()> {+ crate::shared::forward_duplicate_content_length(async |server| {+ run_cli(+ P3_HTTP_OUTBOUND_REQUEST_FORWARD_DUPLICATE_CONTENT_LENGTH_COMPONENT,+ server,+ )+ .await+ })+ .await+}+#[test_log::test(tokio::test(flavor = "multi_thread"))]async fn p3_http_outbound_request_content_length() -> wasmtime::Result<()> {- let server = Server::http1(3)?;+ let server = Server::http1(4)?;run_cli(P3_HTTP_OUTBOUND_REQUEST_CONTENT_LENGTH_COMPONENT, &server).await}diff --git a/crates/wasi-http/tests/all/shared.rs b/crates/wasi-http/tests/all/shared.rsindex 46d6c4f51a..6c7fbbdc83 100644--- a/crates/wasi-http/tests/all/shared.rs+++ b/crates/wasi-http/tests/all/shared.rs@@ -1,3 +1,4 @@+use crate::http_server::Server;use wasmtime::component::{Component, Linker, ResourceTable};use wasmtime::{Result, Store};use wasmtime_wasi::{NamedId, WasiCtx, WasiCtxView, WasiView};@@ -95,3 +96,46 @@ pub async fn run_named_imports_test<T>(})?;run(&mut store, &component, &linker).await}++/// Runs a guest which fetches `/dup` from a raw server, whose response+/// carries two conflicting `content-length` values, and then forwards those+/// headers in a new request to `/victim`, which the host must reject.+///+/// `run_guest` instantiates and runs the guest against the given server; the+/// guest itself asserts the error it observes. Afterwards this asserts that+/// nothing but the guest's `/dup` requests ever reached the wire.+pub async fn forward_duplicate_content_length(+ run_guest: impl AsyncFnOnce(&Server) -> Result<()>,+) -> Result<()> {+ // `hyper`'s client accepts this response since `transfer-encoding` takes+ // precedence over `content-length`, but both values reach the guest.+ const DUP: &[u8] = b"HTTP/1.1 200 OK\r\n\+ content-length: 5\r\n\+ content-length: 100\r\n\+ transfer-encoding: chunked\r\n\+ \r\n\+ 0\r\n\r\n";+ // The guest fetches `/dup` twice, and must never send `/victim`; the+ // extra connections let a `/victim` request be observed should it+ // escape.+ let (server, requests) = Server::raw(4, |head| {+ if head.starts_with(b"GET /dup ") {+ DUP.to_vec()+ } else {+ b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\n\r\n".to_vec()+ }+ })?;+ run_guest(&server).await?;+ drop(server);++ let requests = requests.get();+ assert_eq!(requests.len(), 2, "unexpected requests: {requests:?}");+ for head in &requests {+ assert!(+ head.starts_with(b"GET /dup "),+ "unexpected request reached the wire: {}",+ String::from_utf8_lossy(head)+ );+ }+ Ok(())+}