Skip to content

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

wasi-http: reject invalid status codes on the p2 path (#14659)

Xia Chaoa473f42e4596
diff --git a/crates/wasi-http/src/p2/types_impl.rs b/crates/wasi-http/src/p2/types_impl.rs
index 73689f37bc..9bae88c367 100644
--- a/crates/wasi-http/src/p2/types_impl.rs
+++ b/crates/wasi-http/src/p2/types_impl.rs
@@ -578,8 +578,8 @@ impl types::HostOutgoingResponse for WasiHttpCtxView<'_> {
let resp = self.table.get_mut(&id)?;
match http::StatusCode::from_u16(status) {
- Ok(status) => resp.status = status,
- Err(_) => return Ok(Err(())),
+ Ok(parsed) if matches!(status, 100..=599) => resp.status = parsed,
+ _ => return Ok(Err(())),
};
Ok(Ok(()))
@@ -1394,3 +1394,52 @@ mod named {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use crate::p2::bindings::http::types::HostOutgoingResponse as _;
+ use crate::p2::types::HostOutgoingResponse;
+ use crate::{FieldMap, WasiHttpCtx, WasiHttpCtxView};
+
+ fn push(view: &mut WasiHttpCtxView<'_>) -> wasmtime::component::Resource<HostOutgoingResponse> {
+ view.table
+ .push(HostOutgoingResponse {
+ status: http::StatusCode::OK,
+ headers: FieldMap::new_mutable(1024),
+ body: None,
+ })
+ .expect("push outgoing-response")
+ }
+
+ /// `set-status-code` must fail for codes outside the RFC 9110 range, as the
+ /// `wasi:http` contract promises and the p3 implementation already does.
+ #[test]
+ fn set_status_code_range() {
+ let mut table = wasmtime::component::ResourceTable::new();
+ let mut http = WasiHttpCtx::new();
+ let hooks: &mut dyn crate::WasiHttpHooks = crate::default_hooks();
+ let mut view = WasiHttpCtxView {
+ ctx: &mut http,
+ table: &mut table,
+ hooks,
+ };
+
+ for code in [100u16, 199, 200, 599] {
+ let id = push(&mut view);
+ let r = view.set_status_code(id, code);
+ assert!(
+ matches!(r, Ok(Ok(()))),
+ "code {code} should be accepted: {r:?}"
+ );
+ }
+
+ for code in [0u16, 99, 600, 999, 1000, 65535] {
+ let id = push(&mut view);
+ let r = view.set_status_code(id, code);
+ assert!(
+ matches!(r, Ok(Err(()))),
+ "code {code} should be rejected: {r:?}"
+ );
+ }
+ }
+}