A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
reject userinfo in wasi-http request authorities (#14747)
netliomax25-codef157b3e0302b
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 e391527f4e..9985126019 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@@ -52,6 +52,14 @@ fn main() {// IPv6 addresses with and without a port should be allowedassert!(req.set_authority(Some("[::]:443")).is_ok());assert!(req.set_authority(Some("[::]")).is_ok());+ // RFC 9110 forbids sending a userinfo subcomponent, which would+ // otherwise end up verbatim in the `Host` header.+ assert!(req.set_authority(Some("[email protected]")).is_err());+ assert!(+ req.set_authority(Some("user:[email protected]:443"))+ .is_err()+ );+ assert!(req.set_authority(Some("user@[::1]:443")).is_err());assert!(req.set_scheme(Some(&http_types::Scheme::Other("bad\nscheme".to_string())))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 2248ea8167..04d6d3430b 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@@ -57,6 +57,14 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {// IPv6 addresses with and without a port should be allowed.assert!(req.set_authority(Some("[::]:443")).is_ok());assert!(req.set_authority(Some("[::]")).is_ok());+ // RFC 9110 forbids sending a userinfo subcomponent, which would+ // otherwise end up verbatim in the `Host` header.+ assert!(req.set_authority(Some("[email protected]")).is_err());+ assert!(+ req.set_authority(Some("user:[email protected]:443"))+ .is_err()+ );+ assert!(req.set_authority(Some("user@[::1]:443")).is_err());assert!(req.set_scheme(Some(&Scheme::Other("bad\nscheme".to_string())))diff --git a/crates/wasi-http/src/authority.rs b/crates/wasi-http/src/authority.rsindex fed22aebd9..5fd1d9f84b 100644--- a/crates/wasi-http/src/authority.rs+++ b/crates/wasi-http/src/authority.rs@@ -7,7 +7,19 @@/// is validated here as well. A `:` inside an IPv6 literal host such as `[::1]`/// is part of the host rather than a port delimiter, so the port is only looked/// for after any closing bracket.+///+/// `http::uri::Authority` also accepts an RFC 3986 userinfo subcomponent such+/// as `[email protected]`. RFC 9110 forbids a sender from generating userinfo+/// (and its `@` delimiter) when an "http" or "https" URI is placed within a+/// message, and the authority is emitted verbatim as the `Host` header of+/// outgoing requests, whose grammar is `uri-host [":" port]`. An `@` can only+/// appear in an authority as the userinfo delimiter (percent-encoding is+/// already rejected by `http::uri::Authority`), so any authority containing+/// one is rejected here.pub(crate) fn parse_authority(authority: String) -> Result<http::uri::Authority, ()> {+ if authority.contains('@') {+ return Err(());+ }let has_port = match authority.rfind(']') {Some(i) => authority[i..].contains(':'),None => authority.contains(':'),@@ -42,4 +54,14 @@ mod tests {assert!(parse_authority("example.com:65536".into()).is_err());assert!(parse_authority("[::1]:abc".into()).is_err());}++ #[test]+ fn authority_rejects_userinfo() {+ assert!(parse_authority("[email protected]".into()).is_err());+ assert!(parse_authority("[email protected]:443".into()).is_err());+ assert!(parse_authority("user:[email protected]:443".into()).is_err());+ assert!(parse_authority("user:pass@[::1]".into()).is_err());+ assert!(parse_authority("@example.com".into()).is_err());+ assert!(parse_authority("a@[email protected]".into()).is_err());+ }}