A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
WASIP3 HTTP return error for TooManyFields/TotalSizeTooBig (#12788)
Bailey Hayesa1a74d3a174e
diff --git a/crates/test-programs/src/bin/p2_cli_http_headers.rs b/crates/test-programs/src/bin/p2_cli_http_headers.rsindex c5c21c1fe3..a5fd29bffa 100644--- a/crates/test-programs/src/bin/p2_cli_http_headers.rs+++ b/crates/test-programs/src/bin/p2_cli_http_headers.rs@@ -38,7 +38,8 @@ fn main() {let fields = wasip3::http::types::Fields::new();for i in 0.. {if fields.append(&format!("a{i}"), b"a").is_err() {- break;+ println!("error received");+ return;}}}@@ -46,7 +47,8 @@ fn main() {let fields = wasip3::http::types::Fields::new();for i in 0.. {if fields.append(&format!("a{i}"), b"").is_err() {- break;+ println!("error received");+ return;}}}@@ -54,7 +56,8 @@ fn main() {let fields = wasip3::http::types::Fields::new();loop {if fields.append("a", b"b").is_err() {- break;+ println!("error received");+ return;}}}@@ -62,12 +65,14 @@ fn main() {let fields = wasip3::http::types::Fields::new();loop {if fields.append("a", b"").is_err() {- break;+ println!("error received");+ return;}}}other => panic!("unknown test {other:?}"),}+ // p2 cases trap before reaching hereunreachable!();}diff --git a/crates/wasi-http/src/p3/mod.rs b/crates/wasi-http/src/p3/mod.rsindex 04e50b3a75..153956fe01 100644--- a/crates/wasi-http/src/p3/mod.rs+++ b/crates/wasi-http/src/p3/mod.rs@@ -44,9 +44,9 @@ impl From<FieldMapError> for HeaderError {match e {FieldMapError::Immutable => types::HeaderError::Immutable.into(),FieldMapError::InvalidHeaderName => types::HeaderError::InvalidSyntax.into(),- // FIXME(WebAssembly/WASI#889): these ideally would map to an error- // code instead of trapping.- FieldMapError::TooManyFields | FieldMapError::TotalSizeTooBig => HeaderError::trap(e),+ FieldMapError::TooManyFields | FieldMapError::TotalSizeTooBig => {+ types::HeaderError::SizeExceeded.into()+ }}}}diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rsindex b303fbc534..682765c1ca 100644--- a/tests/all/cli_tests.rs+++ b/tests/all/cli_tests.rs@@ -2316,55 +2316,98 @@ start a print 1234let cwasm = td.path().join("http_headers.cwasm");let cwasm = cwasm.to_str().unwrap();run_wasmtime(&["compile", P2_CLI_HTTP_HEADERS_COMPONENT, "-o", cwasm])?;- for wasi in ["p2", "p3"] {- for test in ["append", "append-empty", "append-same", "append-same-empty"] {- let err = run_wasmtime(&[- "run",- "-Shttp,p3",- "-Smax-http-fields-size=1048576",- "--allow-precompiled",- cwasm,- &format!("{wasi}-{test}"),- ])- .unwrap_err();- assert!(- err.to_string()- .contains("total size of fields exceeds limit")- || err.to_string().contains("too many fields in the field map"),- "bad error message: {err:?}"- );-- // gated by default too- let err = run_wasmtime(&[- "run",- "-Shttp,p3",- "--allow-precompiled",- cwasm,- &format!("{wasi}-{test}"),- ])- .unwrap_err();- assert!(- err.to_string()- .contains("total size of fields exceeds limit"),- "bad error message: {err:?}"- );- }+ // p2 traps on too-many-fields/size-too-big, so expect error messages.+ for test in ["append", "append-empty", "append-same", "append-same-empty"] {+ let err = run_wasmtime(&[+ "run",+ "-Shttp,p3",+ "-Smax-http-fields-size=1048576",+ "--allow-precompiled",+ cwasm,+ &format!("p2-{test}"),+ ])+ .unwrap_err();+ assert!(+ err.to_string()+ .contains("total size of fields exceeds limit")+ || err.to_string().contains("too many fields in the field map"),+ "bad error message: {err:?}"+ );- // With an extremely large limit Wasmtime still shouldn't panic.+ // gated by default toolet err = run_wasmtime(&["run","-Shttp,p3",- &format!("-Smax-http-fields-size={}", 1 << 30),"--allow-precompiled",cwasm,- &format!("{wasi}-append"),+ &format!("p2-{test}"),]).unwrap_err();assert!(- err.to_string().contains("too many fields in the field map"),+ err.to_string()+ .contains("total size of fields exceeds limit"),"bad error message: {err:?}");}++ // With an extremely large limit Wasmtime still shouldn't panic (p2).+ let err = run_wasmtime(&[+ "run",+ "-Shttp,p3",+ &format!("-Smax-http-fields-size={}", 1 << 30),+ "--allow-precompiled",+ cwasm,+ "p2-append",+ ])+ .unwrap_err();+ assert!(+ err.to_string().contains("too many fields in the field map"),+ "bad error message: {err:?}"+ );++ // p3 returns an error code instead of trapping, so the guest+ // program exits successfully after receiving the error.+ for test in ["append", "append-empty", "append-same", "append-same-empty"] {+ let output = run_wasmtime(&[+ "run",+ "-Shttp,p3",+ "-Smax-http-fields-size=1048576",+ "--allow-precompiled",+ cwasm,+ &format!("p3-{test}"),+ ])?;+ assert!(+ output.contains("error received"),+ "p3-{test}: guest should have received an error"+ );++ // gated by default too+ let output = run_wasmtime(&[+ "run",+ "-Shttp,p3",+ "--allow-precompiled",+ cwasm,+ &format!("p3-{test}"),+ ])?;+ assert!(+ output.contains("error received"),+ "p3-{test} (default limit): guest should have received an error"+ );+ }++ // With an extremely large limit Wasmtime still shouldn't panic (p3).+ let output = run_wasmtime(&[+ "run",+ "-Shttp,p3",+ &format!("-Smax-http-fields-size={}", 1 << 30),+ "--allow-precompiled",+ cwasm,+ "p3-append",+ ])?;+ assert!(+ output.contains("error received"),+ "p3-append (large limit): guest should have received an error"+ );Ok(())}