Skip to content

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

[wasmtime-wasi-http] always accept empty path-with-query (#14446)

Joel Dice0c9db3031d1c
diff --git a/crates/wasi-http/src/lib.rs b/crates/wasi-http/src/lib.rs
index 88e5c09eb3..02b61743a6 100644
--- a/crates/wasi-http/src/lib.rs
+++ b/crates/wasi-http/src/lib.rs
@@ -56,6 +56,28 @@ fn get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>
Ok(Some(v))
}
+/// Parse the specified string as an `http::uri::PathAndQuery`, if possible.
+///
+/// As of `http` v1.4.1, `PathAndQuery::from_str` will return an error if given
+/// an empty string per https://github.com/hyperium/http/pull/826. However,
+/// https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 says an empty path
+/// is okay:
+///
+/// > If a URI contains an authority component, then the path component must
+/// > either be empty or begin with a slash ("/") character.
+///
+/// ...and they are still allowed when using
+/// `http::uri::Builder::path_and_query` per
+/// https://github.com/hyperium/http/pull/853, so we use that here:
+#[cfg(any(feature = "p2", feature = "p3"))]
+fn parse_path_with_query(path_with_query: &str) -> Option<http::uri::PathAndQuery> {
+ http::uri::Builder::new()
+ .path_and_query(path_with_query)
+ .build()
+ .ok()
+ .and_then(|uri| uri.path_and_query().cloned())
+}
+
#[cfg(all(test, any(feature = "p2", feature = "p3")))]
mod content_length_tests {
use super::get_content_length;
diff --git a/crates/wasi-http/src/p2/types_impl.rs b/crates/wasi-http/src/p2/types_impl.rs
index fa00f557ff..4a98468ecf 100644
--- a/crates/wasi-http/src/p2/types_impl.rs
+++ b/crates/wasi-http/src/p2/types_impl.rs
@@ -284,7 +284,7 @@ impl types::HostOutgoingRequest for WasiHttpCtxView<'_> {
let req = self.table.get_mut(&request)?;
if let Some(s) = path_with_query.as_ref() {
- if let Err(_) = http::uri::PathAndQuery::from_str(s) {
+ if crate::parse_path_with_query(s).is_none() {
return Ok(Err(()));
}
}
diff --git a/crates/wasi-http/src/p3/host/types.rs b/crates/wasi-http/src/p3/host/types.rs
index e8aeaa5349..a8c86b5129 100644
--- a/crates/wasi-http/src/p3/host/types.rs
+++ b/crates/wasi-http/src/p3/host/types.rs
@@ -311,11 +311,12 @@ impl HostRequest for WasiHttpCtxView<'_> {
path_with_query: Option<String>,
) -> wasmtime::Result<Result<(), ()>> {
let req = get_request_mut(self.table, &req)?;
+
let Some(path_with_query) = path_with_query else {
req.path_with_query = None;
return Ok(Ok(()));
};
- let Ok(path_with_query) = path_with_query.try_into() else {
+ let Some(path_with_query) = crate::parse_path_with_query(&path_with_query) else {
return Ok(Err(()));
};
req.path_with_query = Some(path_with_query);