A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasi-http: Prohibit duplicate content length headers (#14824)
Pat Hickeyff0a08667c27
diff --git a/crates/test-programs/src/bin/p2_http_outbound_request_invalid_header.rs b/crates/test-programs/src/bin/p2_http_outbound_request_invalid_header.rsindex d1e2a93457..459e0f93e4 100644--- a/crates/test-programs/src/bin/p2_http_outbound_request_invalid_header.rs+++ b/crates/test-programs/src/bin/p2_http_outbound_request_invalid_header.rs@@ -57,6 +57,41 @@ fn main() {Err(HeaderError::InvalidSyntax)));+ assert!(matches!(+ hdrs.append("Content-Length", b"5".as_ref()),+ Ok(())+ ));++ assert!(matches!(+ hdrs.append("Content-Length", b"6".as_ref()),+ Err(HeaderError::InvalidSyntax)+ ));++ assert!(matches!(+ hdrs.append("content-length", b"5".as_ref()),+ Err(HeaderError::InvalidSyntax)+ ));++ assert!(matches!(+ hdrs.set("Content-Length", &[b"5".to_vec(), b"6".to_vec()]),+ Err(HeaderError::InvalidSyntax)+ ));++ assert!(matches!(+ hdrs.set("Content-Length", &[b"10".to_vec()]),+ Ok(())+ ));++ assert!(matches!(hdrs.delete("Content-Length"), Ok(())));++ assert!(matches!(+ Headers::from_list(&[+ ("Content-Length".to_owned(), b"5".to_vec()),+ ("Content-Length".to_owned(), b"6".to_vec()),+ ]),+ Err(HeaderError::InvalidSyntax)+ ));+let req = OutgoingRequest::new(hdrs);let hdrs = req.headers();diff --git a/crates/test-programs/src/bin/p3_http_outbound_request_invalid_header.rs b/crates/test-programs/src/bin/p3_http_outbound_request_invalid_header.rsindex d7898ff642..4be3f41638 100644--- a/crates/test-programs/src/bin/p3_http_outbound_request_invalid_header.rs+++ b/crates/test-programs/src/bin/p3_http_outbound_request_invalid_header.rs@@ -63,6 +63,41 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {Err(HeaderError::InvalidSyntax)));+ assert!(matches!(+ hdrs.append("Content-Length", b"5".as_ref()),+ Ok(())+ ));++ assert!(matches!(+ hdrs.append("Content-Length", b"6".as_ref()),+ Err(HeaderError::InvalidSyntax)+ ));++ assert!(matches!(+ hdrs.append("content-length", b"5".as_ref()),+ Err(HeaderError::InvalidSyntax)+ ));++ assert!(matches!(+ hdrs.set("Content-Length", &[b"5".to_vec(), b"6".to_vec()]),+ Err(HeaderError::InvalidSyntax)+ ));++ assert!(matches!(+ hdrs.set("Content-Length", &[b"10".to_vec()]),+ Ok(())+ ));++ assert!(matches!(hdrs.delete("Content-Length"), Ok(())));++ assert!(matches!(+ Headers::from_list(&[+ ("Content-Length".to_owned(), b"5".to_vec()),+ ("Content-Length".to_owned(), b"6".to_vec()),+ ]),+ Err(HeaderError::InvalidSyntax)+ ));+let (_, rx) = wit_future::new(|| Ok(None));let (req, _) = Request::new(hdrs, None, rx, None);let hdrs = req.get_headers();diff --git a/crates/wasi-http/src/field_map.rs b/crates/wasi-http/src/field_map.rsindex 2bb55883f4..00e63baa11 100644--- a/crates/wasi-http/src/field_map.rs+++ b/crates/wasi-http/src/field_map.rs@@ -115,6 +115,11 @@ impl FieldMap {return Err(FieldMapError::Forbidden);}let (map, limit, size) = self.mutable()?;++ // `Content-Length` may only have a single value, see `append`.+ if key == http::header::CONTENT_LENGTH && values.len() > 1 {+ return Err(FieldMapError::InvalidHeaderValue);+ }let values = values.into_iter().map(|v| parse_header_value(&key, v))@@ -206,6 +211,12 @@ impl FieldMap {return Err(FieldMapError::Forbidden);}let value = parse_header_value(&key, value)?;+ // Multiple `Content-Length` values make message framing ambiguous, as+ // different parties may disagree on which one to use, so only a single+ // value is permitted.+ if key == http::header::CONTENT_LENGTH && self.contains_key(&key) {+ return Err(FieldMapError::InvalidHeaderValue);+ }self.append_raw(key, value)}@@ -490,7 +501,48 @@ mod tests {}#[test]- fn field_map_size() {+ fn content_length_rejects_multiple_values() {+ let hooks = default_hooks();+ let cl = || CONTENT_LENGTH.as_str().to_owned();++ let mut map = FieldMap::new_mutable(2000);+ map.append(hooks, cl(), b"5".to_vec()).unwrap();+ assert_eq!(+ map.append(hooks, cl(), b"6".to_vec()),+ Err(FieldMapError::InvalidHeaderValue)+ );+ assert_eq!(+ map.append(hooks, cl(), b"5".to_vec()),+ Err(FieldMapError::InvalidHeaderValue)+ );+ assert_eq!(+ map.append(hooks, "Content-Length".to_owned(), b"6".to_vec()),+ Err(FieldMapError::InvalidHeaderValue)+ );+ assert_eq!(map.get_all(CONTENT_LENGTH).iter().count(), 1);+ assert_eq!(map.size, FieldMap::content_size(&map.map));++ assert_eq!(+ map.set(hooks, cl(), vec![b"5".to_vec(), b"6".to_vec()]),+ Err(FieldMapError::InvalidHeaderValue)+ );+ assert_eq!(+ map.set(hooks, cl(), vec![b"5".to_vec(), b"5".to_vec()]),+ Err(FieldMapError::InvalidHeaderValue)+ );+ assert_eq!(map[CONTENT_LENGTH], "5");++ // Replacing the single value is still fine.+ map.set(hooks, cl(), vec![b"6".to_vec()]).unwrap();+ assert_eq!(map[CONTENT_LENGTH], "6");+ assert_eq!(map.get_all(CONTENT_LENGTH).iter().count(), 1);+ assert_eq!(map.size, FieldMap::content_size(&map.map));+ }++ #[test]+ fn full_map_is_not_invalid_syntax() {+ // try_entry reports MaxSizeReached as InvalidHeaderName (http map.rs:1149),+ // so a full map must report valid names as too many fields instead.let mut map = FieldMap::new_mutable(usize::MAX);let mut i = 0;let err = loop {diff --git a/crates/wasi-http/src/lib.rs b/crates/wasi-http/src/lib.rsindex da1266c3ed..df46f36112 100644--- a/crates/wasi-http/src/lib.rs+++ b/crates/wasi-http/src/lib.rs@@ -42,9 +42,15 @@ pub use request_options::*;/// header.#[cfg(any(feature = "p2", feature = "p3"))]fn get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>> {- let Some(v) = headers.get(header::CONTENT_LENGTH) else {+ let mut values = headers.get_all(header::CONTENT_LENGTH).iter();+ let Some(v) = values.next() else {return Ok(None);};+ // `FieldMap` rejects multiple `Content-Length` values, but double-check+ // here since the framing of the body depends on there being exactly one.+ if values.next().is_some() {+ wasmtime::bail!("multiple `content-length` header values");+ }let v = v.to_str()?;// RFC 9110 defines `Content-Length` as `1*DIGIT`. `u64`'s `FromStr` is more// lenient and also accepts a leading `+`, so reject anything that isn't a@@ -115,6 +121,11 @@ mod content_length_tests {assert!(get_content_length(&headers("-5")).is_err());assert!(get_content_length(&headers(" 5")).is_err());assert!(get_content_length(&headers("")).is_err());++ // Duplicate content-length definition is forbidden+ let mut map = headers("5");+ map.append(header::CONTENT_LENGTH, HeaderValue::from_static("5"));+ assert!(get_content_length(&map).is_err());}}