A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasmtime-wasi-http p3: apply forbidden header check to trailers (#14823)
Pat Hickey842cbb4e6f60
diff --git a/crates/wasi-http/src/ctx.rs b/crates/wasi-http/src/ctx.rsindex a8b9a5ea3d..642c5d2194 100644--- a/crates/wasi-http/src/ctx.rs+++ b/crates/wasi-http/src/ctx.rs@@ -7,6 +7,7 @@ use bytes::Bytes;use http::{HeaderName, uri::Scheme};use http_body_util::combinators::UnsyncBoxBody;use std::marker;+use std::sync::{Arc, LazyLock};use wasmtime::component::{HasData, ResourceTable};use wasmtime_wasi::{NamedId, WasiCtxNamedView};@@ -156,6 +157,11 @@ impl Default for WasiHttpCtx {/// Convenience type definition for the bodies used in this crate.pub type WasiBody = UnsyncBoxBody<Bytes, Error>;+/// A cloneable predicate deciding whether a header name is forbidden. This+/// predicate needs to be executed in tasks which do not have access to the+/// store.+pub type ForbiddenHeaderFilter = Arc<dyn Fn(&HeaderName) -> bool + Send + Sync>;+/// A trait which provides hooks into internal WASI HTTP operations.////// Note that when using this type if state is needed to implement the methods@@ -198,16 +204,28 @@ pub type WasiBody = UnsyncBoxBody<Bytes, Error>;/// struct MyHooks;////// impl WasiHttpHooks for MyHooks {-/// fn is_forbidden_header(&mut self, name: &http::HeaderName) -> bool {-/// *name == http::header::AUTHORIZATION ||-/// wasmtime_wasi_http::DEFAULT_FORBIDDEN_HEADERS.contains(name)+/// fn forbidden_header_filter(&mut self) -> wasmtime_wasi_http::ForbiddenHeaderFilter {+/// std::sync::Arc::new(|name| {+/// *name == http::header::AUTHORIZATION ||+/// wasmtime_wasi_http::DEFAULT_FORBIDDEN_HEADERS.contains(name)+/// })/// }/// }/// ```pub trait WasiHttpHooks: Send {- /// Whether a given header should be considered forbidden and not allowed.- fn is_forbidden_header(&mut self, name: &HeaderName) -> bool {- DEFAULT_FORBIDDEN_HEADERS.contains(name)+ /// Returns a predicate deciding whether a given header should be+ /// considered forbidden and not allowed.+ ///+ /// The predicate applies to header and trailer fields crossing the guest+ /// boundary in either direction. It may be retained and invoked after the+ /// store has been dropped, e.g. for trailers of a host body forwarded by a+ /// guest, so it is obtained as a value rather than called on `self`.+ ///+ /// The default forbids [`DEFAULT_FORBIDDEN_HEADERS`].+ fn forbidden_header_filter(&mut self) -> ForbiddenHeaderFilter {+ static DEFAULT: LazyLock<ForbiddenHeaderFilter> =+ LazyLock::new(|| Arc::new(|name| DEFAULT_FORBIDDEN_HEADERS.contains(name)));+ DEFAULT.clone()}/// Whether a given scheme should be considered supported.diff --git a/crates/wasi-http/src/field_map.rs b/crates/wasi-http/src/field_map.rsindex 00e63baa11..5b1ad26ea5 100644--- a/crates/wasi-http/src/field_map.rs+++ b/crates/wasi-http/src/field_map.rs@@ -1,4 +1,4 @@-use crate::WasiHttpHooks;+use crate::{ForbiddenHeaderFilter, WasiHttpHooks};use http::header::Entry;use http::{HeaderMap, HeaderName, HeaderValue};use std::fmt;@@ -53,16 +53,7 @@ impl FieldMap {pub fn new_immutable(hooks: &mut dyn WasiHttpHooks, mut map: HeaderMap) -> Self {// Strip out all forbidden headers from `map` to ensure they're never// able to enter into a WASI guest.- let forbidden_keys = Vec::from_iter(map.keys().filter_map(|name| {- if hooks.is_forbidden_header(name) {- Some(name.clone())- } else {- None- }- }));- for name in forbidden_keys {- map.remove(&name);- }+ remove_forbidden(&mut map, &hooks.forbidden_header_filter());let size = Self::content_size(&map);Self {@@ -111,7 +102,7 @@ impl FieldMap {values: Vec<Vec<u8>>,) -> Result<(), FieldMapError> {let key = key.parse()?;- if hooks.is_forbidden_header(&key) {+ if (hooks.forbidden_header_filter())(&key) {return Err(FieldMapError::Forbidden);}let (map, limit, size) = self.mutable()?;@@ -171,7 +162,7 @@ impl FieldMap {key: String,) -> Result<Vec<HeaderValue>, FieldMapError> {let key = key.parse()?;- if hooks.is_forbidden_header(&key) {+ if (hooks.forbidden_header_filter())(&key) {return Err(FieldMapError::Forbidden);}let (map, _limit, size) = self.mutable()?;@@ -207,7 +198,7 @@ impl FieldMap {value: Vec<u8>,) -> Result<bool, FieldMapError> {let key = key.parse()?;- if hooks.is_forbidden_header(&key) {+ if (hooks.forbidden_header_filter())(&key) {return Err(FieldMapError::Forbidden);}let value = parse_header_value(&key, value)?;@@ -604,3 +595,11 @@ mod tests {assert_eq!(map.size, FieldMap::content_size(&map.map));}}++/// Removes all fields from `map` whose name `is_forbidden`.+pub(crate) fn remove_forbidden(map: &mut HeaderMap, is_forbidden: &ForbiddenHeaderFilter) {+ let forbidden_keys = Vec::from_iter(map.keys().filter(|name| is_forbidden(name)).cloned());+ for name in forbidden_keys {+ map.remove(&name);+ }+}diff --git a/crates/wasi-http/src/p3/body.rs b/crates/wasi-http/src/p3/body.rsindex 5a71e0842c..f6cb899c6a 100644--- a/crates/wasi-http/src/p3/body.rs+++ b/crates/wasi-http/src/p3/body.rs@@ -1,6 +1,7 @@+use crate::field_map::remove_forbidden;use crate::p3::bindings::http::types::{ErrorCode, Trailers};use crate::p3::helpers::FutureReaderExt;-use crate::{Error, FieldMap, WasiHttpCtxView};+use crate::{Error, FieldMap, ForbiddenHeaderFilter, WasiHttpCtxView};use bytes::Bytes;use core::iter;use core::num::NonZeroUsize;@@ -714,7 +715,63 @@ where}}+/// A wrapper around [http_body::Body], which removes forbidden fields from+/// trailers+pub(crate) struct BodyWithFilteredTrailers<T> {+ body: T,+ is_forbidden: ForbiddenHeaderFilter,+}++impl<T> http_body::Body for BodyWithFilteredTrailers<T>+where+ T: http_body::Body<Data = Bytes> + Unpin,+{+ type Data = T::Data;+ type Error = T::Error;++ #[inline]+ fn poll_frame(+ mut self: Pin<&mut Self>,+ cx: &mut Context<'_>,+ ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {+ let frame = match ready!(Pin::new(&mut self.as_mut().body).poll_frame(cx)) {+ Some(Ok(frame)) => frame,+ other => return Poll::Ready(other),+ };+ match frame.into_trailers() {+ Ok(mut trailers) => {+ remove_forbidden(&mut trailers, &self.is_forbidden);+ Poll::Ready(Some(Ok(http_body::Frame::trailers(trailers))))+ }+ Err(frame) => Poll::Ready(Some(Ok(frame))),+ }+ }++ #[inline]+ fn is_end_stream(&self) -> bool {+ self.body.is_end_stream()+ }++ #[inline]+ fn size_hint(&self) -> http_body::SizeHint {+ self.body.size_hint()+ }+}+pub(crate) trait BodyExt {+ fn with_filtered_trailers(+ self,+ is_forbidden: ForbiddenHeaderFilter,+ ) -> BodyWithFilteredTrailers<Self>+ where+ Self: Sized,+ {+ BodyWithFilteredTrailers {+ body: self,+ is_forbidden,+ }+ }+fn with_state<T>(self, state: T) -> BodyWithState<Self, T>whereSelf: Sized,diff --git a/crates/wasi-http/src/p3/request.rs b/crates/wasi-http/src/p3/request.rsindex 0fe9414fc1..4f2b41a0c8 100644--- a/crates/wasi-http/src/p3/request.rs+++ b/crates/wasi-http/src/p3/request.rs@@ -187,6 +187,10 @@ impl Request {.map_err(HttpError::trap)?.boxed_unsync(),Body::Host { body, result_tx } => {+ let is_forbidden = getter(store.as_context_mut().data_mut())+ .hooks+ .forbidden_header_filter();+ let body = body.with_filtered_trailers(is_forbidden).boxed_unsync();if let Some(limit) = content_length {let (http_result_tx, http_result_rx) = oneshot::channel();_ = result_tx.send(Box::new(async move {diff --git a/crates/wasi-http/src/p3/response.rs b/crates/wasi-http/src/p3/response.rsindex 6adf406bd2..65a9279513 100644--- a/crates/wasi-http/src/p3/response.rs+++ b/crates/wasi-http/src/p3/response.rs@@ -54,9 +54,9 @@ impl Response {/// to a [`WasiHttpCtxView`].pub fn into_http_with_getter<T: 'static>(self,- store: impl AsContextMut<Data = T>,+ mut store: impl AsContextMut<Data = T>,fut: impl Future<Output = Result<(), Error>> + Send + 'static,- getter: impl FnMut(&mut T) -> WasiHttpCtxView<'_> + Clone + Unpin + Send + 'static,+ mut getter: impl FnMut(&mut T) -> WasiHttpCtxView<'_> + Clone + Unpin + Send + 'static,) -> wasmtime::Result<http::Response<UnsyncBoxBody<Bytes, Error>>> {let res = http::Response::try_from(self)?;let (res, body) = res.into_parts();@@ -69,7 +69,7 @@ impl Response {trailers_rx,result_tx,} => GuestBody::new(- store,+ &mut store,contents_rx,trailers_rx,result_tx,@@ -80,6 +80,10 @@ impl Response {)?.boxed_unsync(),Body::Host { body, result_tx } => {+ let is_forbidden = getter(store.as_context_mut().data_mut())+ .hooks+ .forbidden_header_filter();+ let body = body.with_filtered_trailers(is_forbidden);if let Some(limit) = content_length {let (http_result_tx, http_result_rx) = oneshot::channel();_ = result_tx.send(Box::new(async move {@@ -92,7 +96,7 @@ impl Response {.boxed_unsync()} else {_ = result_tx.send(Box::new(fut));- body+ body.boxed_unsync()}}};diff --git a/crates/wasi-http/tests/all/p2.rs b/crates/wasi-http/tests/all/p2.rsindex a6731c8cf5..56ccd61d9b 100644--- a/crates/wasi-http/tests/all/p2.rs+++ b/crates/wasi-http/tests/all/p2.rs@@ -101,9 +101,11 @@ impl WasiHttpHooks for MyHttpHooks {})}- fn is_forbidden_header(&mut self, name: &hyper::header::HeaderName) -> bool {- wasmtime_wasi_http::DEFAULT_FORBIDDEN_HEADERS.contains(name)- || name.as_str() == "custom-forbidden-header"+ fn forbidden_header_filter(&mut self) -> wasmtime_wasi_http::ForbiddenHeaderFilter {+ Arc::new(|name| {+ wasmtime_wasi_http::DEFAULT_FORBIDDEN_HEADERS.contains(name)+ || name.as_str() == "custom-forbidden-header"+ })}}diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rsindex cdefeb3d5f..9e2507dc29 100644--- a/crates/wasi-http/tests/all/p3/mod.rs+++ b/crates/wasi-http/tests/all/p3/mod.rs@@ -10,6 +10,7 @@ use http_body_util::{BodyExt as _, Collected, Empty};use std::io::Write;use std::path::Path;use std::pin::Pin;+use std::sync::Arc;use std::task::{Context, Poll};use test_programs_artifacts::*;use tokio::{fs, try_join};@@ -23,8 +24,8 @@ use wasmtime_wasi_http::p3::Request;use wasmtime_wasi_http::p3::bindings::Service;use wasmtime_wasi_http::p3::bindings::http::types::ErrorCode;use wasmtime_wasi_http::{- DEFAULT_FORBIDDEN_HEADERS, Error, RequestOptions, WasiBody, WasiHttpCtx, WasiHttpCtxView,- WasiHttpHooks, WasiHttpView, default_send_request,+ DEFAULT_FORBIDDEN_HEADERS, Error, ForbiddenHeaderFilter, RequestOptions, WasiBody, WasiHttpCtx,+ WasiHttpCtxView, WasiHttpHooks, WasiHttpView, default_send_request,};foreach_p3_http!(assert_test_exists);@@ -45,8 +46,10 @@ impl TestHooks {}impl WasiHttpHooks for TestHooks {- fn is_forbidden_header(&mut self, name: &http::header::HeaderName) -> bool {- name.as_str() == "custom-forbidden-header" || DEFAULT_FORBIDDEN_HEADERS.contains(name)+ fn forbidden_header_filter(&mut self) -> ForbiddenHeaderFilter {+ Arc::new(|name| {+ name.as_str() == "custom-forbidden-header" || DEFAULT_FORBIDDEN_HEADERS.contains(name)+ })}fn send_request(@@ -91,6 +94,18 @@ impl WasiHttpHooks for TestHooks {}) as Box<dyn Future<Output = _> + Send>,))})+ } else if let Some("p3-test-trailers") = request.uri().authority().map(|v| v.as_str()) {+ _ = self.request_tx.take().unwrap().send(request);+ let body = http_body_util::StreamBody::new(futures::stream::iter([+ Ok(http_body::Frame::data(Bytes::from_static(b"response"))),+ Ok(http_body::Frame::trailers(forbidden_trailers())),+ ]));+ Box::new(async {+ Ok((+ http::Response::new(body.boxed_unsync()),+ Box::new(async { Ok(()) }) as Box<dyn Future<Output = _> + Send>,+ ))+ })} else {Box::new(async move {use http_body_util::BodyExt;@@ -811,6 +826,69 @@ async fn p3_http_proxy_store_drop_cancels_io() -> Result<()> {Ok(())}+/// Trailers containing both forbidden and allowed fields.+fn forbidden_trailers() -> http::HeaderMap {+ let mut trailers = http::HeaderMap::new();+ trailers.insert("connection", HeaderValue::from_static("x-hop"));+ trailers.insert(+ "proxy-authorization",+ HeaderValue::from_static("Basic c2VjcmV0OnNlY3JldA=="),+ );+ trailers.insert("upgrade", HeaderValue::from_static("h2c"));+ trailers.insert("custom-forbidden-header", HeaderValue::from_static("yes"));+ trailers.insert("x-ok", HeaderValue::from_static("1"));+ trailers+}++#[track_caller]+fn assert_trailers_filtered(trailers: Option<&http::HeaderMap>, what: &str) {+ let trailers = trailers.unwrap_or_else(|| panic!("{what}: trailers missing"));+ for name in [+ "connection",+ "proxy-authorization",+ "upgrade",+ "custom-forbidden-header",+ ] {+ assert!(+ trailers.get(name).is_none(),+ "{what}: forbidden trailer `{name}` leaked: {trailers:?}",+ );+ }+ assert_eq!(+ trailers.get("x-ok").map(|v| v.as_bytes()),+ Some(b"1".as_slice()),+ "{what}: allowed trailer missing: {trailers:?}",+ );+}++// A guest which forwards a host body as-is must not let forbidden trailers+// through, in either direction.+#[test_log::test(tokio::test(flavor = "multi_thread"))]+async fn p3_http_forwarded_trailers_filtered() -> Result<()> {+ let request = http::Request::builder()+ .uri("http://localhost/")+ .method(http::Method::POST)+ .header("url", "http://p3-test-trailers/");+ let body = http_body_util::StreamBody::new(futures::stream::iter([+ Ok::<_, ErrorCode>(http_body::Frame::data(Bytes::from_static(b"request"))),+ Ok(http_body::Frame::trailers(forbidden_trailers())),+ ]));++ let (request_tx, request_rx) = oneshot::channel();+ let response = run_http(P3_HTTP_PROXY_COMPONENT, request.body(body)?, request_tx)+ .await?+ .unwrap();+ assert_eq!(response.status().as_u16(), 200);+ let response = response.into_body();+ assert_trailers_filtered(response.trailers(), "response to client");+ assert_eq!(response.to_bytes(), b"response".as_slice());++ let upstream = request_rx.await.unwrap().into_body().collect().await?;+ assert_trailers_filtered(upstream.trailers(), "request to upstream");+ assert_eq!(upstream.to_bytes(), b"request".as_slice());+ Ok(())+}+#[test_log::test(tokio::test(flavor = "multi_thread"))]async fn p3_http_forbidden_headers() -> Result<()> {let request = http::Request::builder()diff --git a/crates/wasi-http/tests/all/shared.rs b/crates/wasi-http/tests/all/shared.rsindex f64ae45a66..46d6c4f51a 100644--- a/crates/wasi-http/tests/all/shared.rs+++ b/crates/wasi-http/tests/all/shared.rs@@ -49,8 +49,9 @@ impl WasiHttpNamedView for NamedImportsData {}impl WasiHttpHooks for NamedImportsHooks {- fn is_forbidden_header(&mut self, name: &http::HeaderName) -> bool {- name == self.0+ fn forbidden_header_filter(&mut self) -> wasmtime_wasi_http::ForbiddenHeaderFilter {+ let forbidden = self.0;+ std::sync::Arc::new(move |name| name == forbidden)}}