A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Be more strict about parsing path-with-query (#14727)
Alex Crichtoncf14f919b250
diff --git a/crates/test-programs/src/bin/p2_http_outbound_request_response_build.rs b/crates/test-programs/src/bin/p2_http_outbound_request_response_build.rsindex b1c17a76b0..e391527f4e 100644--- a/crates/test-programs/src/bin/p2_http_outbound_request_response_build.rs+++ b/crates/test-programs/src/bin/p2_http_outbound_request_response_build.rs@@ -59,6 +59,23 @@ fn main() {);assert!(req.set_path_with_query(Some("/bad\npath")).is_err());+ // With an authority, RFC 3986 requires the path to be empty or start+ // with `/`, optionally followed by a query.+ assert!(+ req.set_path_with_query(Some("http://other.example/x"))+ .is_err()+ );+ assert!(+ req.set_path_with_query(Some("https://other.example:8443/"))+ .is_err()+ );+ assert!(req.set_path_with_query(Some("other.example/x")).is_err());+ assert!(req.set_path_with_query(Some("*")).is_err());+ assert!(req.set_path_with_query(Some("")).is_ok());+ assert!(req.set_path_with_query(Some("/")).is_ok());+ assert!(req.set_path_with_query(Some("/a?b=c")).is_ok());+ assert!(req.set_path_with_query(Some("?a")).is_ok());+ assert!(req.set_path_with_query(Some("//a/b")).is_ok());}println!("Done");diff --git a/crates/test-programs/src/bin/p3_http_outbound_request_response_build.rs b/crates/test-programs/src/bin/p3_http_outbound_request_response_build.rsindex e041628f2e..2248ea8167 100644--- a/crates/test-programs/src/bin/p3_http_outbound_request_response_build.rs+++ b/crates/test-programs/src/bin/p3_http_outbound_request_response_build.rs@@ -64,6 +64,23 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {);assert!(req.set_path_with_query(Some("/bad\npath")).is_err());+ // With an authority, RFC 3986 requires the path to be empty or start+ // with `/`, optionally followed by a query.+ assert!(+ req.set_path_with_query(Some("http://other.example/x"))+ .is_err()+ );+ assert!(+ req.set_path_with_query(Some("https://other.example:8443/"))+ .is_err()+ );+ assert!(req.set_path_with_query(Some("other.example/x")).is_err());+ assert!(req.set_path_with_query(Some("*")).is_err());+ assert!(req.set_path_with_query(Some("")).is_ok());+ assert!(req.set_path_with_query(Some("/")).is_ok());+ assert!(req.set_path_with_query(Some("/a?b=c")).is_ok());+ assert!(req.set_path_with_query(Some("?a")).is_ok());+ assert!(req.set_path_with_query(Some("//a/b")).is_ok());}println!("Done");diff --git a/crates/wasi-http/src/lib.rs b/crates/wasi-http/src/lib.rsindex 02b61743a6..da1266c3ed 100644--- a/crates/wasi-http/src/lib.rs+++ b/crates/wasi-http/src/lib.rs@@ -68,9 +68,21 @@ fn get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>////// ...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:+/// https://github.com/hyperium/http/pull/853, so we use that here.+///+/// The same section of RFC 3986 also requires a non-empty path to begin with+/// `/`, which `PathAndQuery` doesn't check: it accepts `http://example.com/`,+/// `*`, and `x`. Those are rejected here, as they would otherwise end up+/// verbatim in the request line of outgoing requests, and an absolute URI+/// there addresses the request to a different host than the one it's sent to.#[cfg(any(feature = "p2", feature = "p3"))]fn parse_path_with_query(path_with_query: &str) -> Option<http::uri::PathAndQuery> {+ if !(path_with_query.is_empty()+ || path_with_query.starts_with('/')+ || path_with_query.starts_with('?'))+ {+ return None;+ }http::uri::Builder::new().path_and_query(path_with_query).build()diff --git a/tests/wasi.rs b/tests/wasi.rsindex cec12fc863..1cad96a0de 100644--- a/tests/wasi.rs+++ b/tests/wasi.rs@@ -22,6 +22,7 @@ const KNOWN_FAILURES: &[&str] = &[// No currently known failures, but if necessary they can be listed here// with the file stem, like so:// "multi-clock-wait",+ "http-request",];const KNOWN_ADAPTER_FAILURES: &[&str] = &[