Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasmtime-wasi-http: enforce check-write budget on outgoing bodies (#14408)

Pat Hickey968bfaec546c
diff --git a/crates/test-programs/src/bin/p2_http_outbound_body_write_backpressure.rs b/crates/test-programs/src/bin/p2_http_outbound_body_write_backpressure.rs
new file mode 100644
index 0000000000..ab754383b3
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_http_outbound_body_write_backpressure.rs
@@ -0,0 +1,13 @@
+use test_programs::wasi::http::types as http_types;
+
+fn main() {
+ let request = http_types::OutgoingRequest::new(http_types::Headers::new());
+ let outgoing_body = request.body().unwrap();
+
+ let request_body = outgoing_body.write().unwrap();
+ request_body.subscribe().block();
+ let permitted = request_body.check_write().unwrap() as usize;
+ let too_much = vec![0; permitted + 1];
+ let _ = request_body.write(&too_much);
+ unreachable!("writing more than permitted should trap");
+}
diff --git a/crates/wasi-http/src/p2/body.rs b/crates/wasi-http/src/p2/body.rs
index 75d305c4b9..8ae7c05741 100644
--- a/crates/wasi-http/src/p2/body.rs
+++ b/crates/wasi-http/src/p2/body.rs
@@ -545,6 +545,9 @@ impl BodyWriteStream {
impl OutputStream for BodyWriteStream {
fn write(&mut self, bytes: Bytes) -> Result<(), StreamError> {
let len = bytes.len();
+ if len > self.write_budget {
+ return Err(StreamError::Trap(format_err!("write exceeded budget")));
+ }
match self.writer.try_send(bytes) {
// If the message was sent then it's queued up now in hyper to get
// received.
diff --git a/crates/wasi-http/tests/all/p2/async_.rs b/crates/wasi-http/tests/all/p2/async_.rs
index 05cbac3176..20234c4469 100644
--- a/crates/wasi-http/tests/all/p2/async_.rs
+++ b/crates/wasi-http/tests/all/p2/async_.rs
@@ -124,3 +124,18 @@ async fn p2_http_named_imports() -> Result<()> {
)
.await
}
+
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_http_outbound_body_write_backpressure() -> Result<()> {
+ let server = Server::http1(0)?;
+ let e = run(P2_HTTP_OUTBOUND_BODY_WRITE_BACKPRESSURE_COMPONENT, &server)
+ .await
+ .err()
+ .expect("guest execution should trap");
+ let e_debug = format!("{e:?}");
+ assert!(
+ e_debug.contains("write exceeded budget"),
+ "expected trap to contain 'write exceeded budget': {e:?}"
+ );
+ Ok(())
+}
diff --git a/crates/wasi-http/tests/all/p2/sync.rs b/crates/wasi-http/tests/all/p2/sync.rs
index a234dbc7c2..c55faf3383 100644
--- a/crates/wasi-http/tests/all/p2/sync.rs
+++ b/crates/wasi-http/tests/all/p2/sync.rs
@@ -107,8 +107,21 @@ fn p2_http_outbound_request_missing_path_and_query() -> Result<()> {
&server,
)
}
-
#[test_log::test]
fn p2_http_named_imports() {
// named imports are intentionally not implemented for sync at this time
}
+
+#[test_log::test]
+fn p2_http_outbound_body_write_backpressure() -> Result<()> {
+ let server = Server::http1(0)?;
+ let e = run(P2_HTTP_OUTBOUND_BODY_WRITE_BACKPRESSURE_COMPONENT, &server)
+ .err()
+ .expect("guest execution should trap");
+ let e_debug = format!("{e:?}");
+ assert!(
+ e_debug.contains("write exceeded budget"),
+ "expected trap to contain 'write exceeded budget': {e:?}"
+ );
+ Ok(())
+}