Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 782fce1

Browse files
committed
http: do not strip incoming request headers
As discussed with @pchickey See also bytecodealliance/wasmtime#7538 (comment) and following discussion Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent a029d27 commit 782fce1

4 files changed

Lines changed: 25 additions & 34 deletions

File tree

crates/test-programs/src/bin/api_0_3_proxy.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ impl test_programs::p3::proxy::exports::wasi::http::handler::Guest for T {
3232
"append of forbidden header succeeded"
3333
);
3434

35-
assert!(
36-
!req_hdrs.has("host"),
37-
"forbidden host header present in incoming request"
38-
);
39-
4035
let hdrs = Headers::new();
4136
let (mut contents_tx, contents_rx) = wit_stream::new();
4237
let (trailers_tx, trailers_rx) = wit_future::new();

crates/wasi-http/src/p3/host/types.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,6 @@ fn delete_request_options(
7979
.context("failed to delete request options from table")
8080
}
8181

82-
/// Returns `true` when the header is forbidden according to this [`WasiHttpView`] implementation.
83-
fn is_forbidden_header(view: &mut impl WasiHttpView, name: &http::header::HeaderName) -> bool {
84-
static FORBIDDEN_HEADERS: [http::header::HeaderName; 10] = [
85-
http::header::CONNECTION,
86-
http::header::HeaderName::from_static("keep-alive"),
87-
http::header::PROXY_AUTHENTICATE,
88-
http::header::PROXY_AUTHORIZATION,
89-
http::header::HeaderName::from_static("proxy-connection"),
90-
http::header::TE,
91-
http::header::TRANSFER_ENCODING,
92-
http::header::UPGRADE,
93-
http::header::HOST,
94-
http::header::HeaderName::from_static("http2-settings"),
95-
];
96-
97-
FORBIDDEN_HEADERS.contains(name) || view.is_forbidden_header(name)
98-
}
99-
10082
fn clone_trailer_result(
10183
res: &Result<Option<Resource<Trailers>>, ErrorCode>,
10284
) -> Result<Option<Resource<Trailers>>, ErrorCode> {
@@ -494,7 +476,7 @@ where
494476
let Ok(header) = header.parse() else {
495477
return Ok(Err(HeaderError::InvalidSyntax));
496478
};
497-
if is_forbidden_header(self, &header) {
479+
if self.is_forbidden_header(&header) {
498480
return Ok(Err(HeaderError::Forbidden));
499481
}
500482
let value = match http::header::HeaderValue::from_bytes(&value) {
@@ -538,7 +520,7 @@ where
538520
let Ok(name) = name.parse() else {
539521
return Ok(Err(HeaderError::InvalidSyntax));
540522
};
541-
if is_forbidden_header(self, &name) {
523+
if self.is_forbidden_header(&name) {
542524
return Ok(Err(HeaderError::Forbidden));
543525
}
544526
let mut values = Vec::with_capacity(value.len());
@@ -567,7 +549,7 @@ where
567549
Ok(header) => header,
568550
Err(_) => return Ok(Err(HeaderError::InvalidSyntax)),
569551
};
570-
if is_forbidden_header(self, &header) {
552+
if self.is_forbidden_header(&header) {
571553
return Ok(Err(HeaderError::Forbidden));
572554
}
573555
let Some(mut fields) = get_fields_inner_mut(self.table(), &fields)? else {
@@ -585,7 +567,7 @@ where
585567
let Ok(header) = http::header::HeaderName::from_bytes(name.as_bytes()) else {
586568
return Ok(Err(HeaderError::InvalidSyntax));
587569
};
588-
if is_forbidden_header(self, &header) {
570+
if self.is_forbidden_header(&header) {
589571
return Ok(Err(HeaderError::Forbidden));
590572
}
591573
let Some(mut fields) = get_fields_inner_mut(self.table(), &fields)? else {
@@ -608,7 +590,7 @@ where
608590
Ok(header) => header,
609591
Err(_) => return Ok(Err(HeaderError::InvalidSyntax)),
610592
};
611-
if is_forbidden_header(self, &header) {
593+
if self.is_forbidden_header(&header) {
612594
return Ok(Err(HeaderError::Forbidden));
613595
}
614596
let value = match http::header::HeaderValue::from_bytes(&value) {

crates/wasi-http/src/p3/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ impl<T: ResourceView> ResourceView for WasiHttpImpl<T> {
373373
}
374374
}
375375

376+
/// Set of [http::header::HeaderName], that are forbidden by default
377+
/// for requests and responses originating in the guest.
378+
pub const DEFAULT_FORBIDDEN_HEADERS: [http::header::HeaderName; 10] = [
379+
http::header::CONNECTION,
380+
http::header::HeaderName::from_static("keep-alive"),
381+
http::header::PROXY_AUTHENTICATE,
382+
http::header::PROXY_AUTHORIZATION,
383+
http::header::HeaderName::from_static("proxy-connection"),
384+
http::header::TE,
385+
http::header::TRANSFER_ENCODING,
386+
http::header::UPGRADE,
387+
http::header::HOST,
388+
http::header::HeaderName::from_static("http2-settings"),
389+
];
390+
376391
/// A trait which provides internal WASI HTTP state.
377392
pub trait WasiHttpView: ResourceView + Send {
378393
/// HTTP client
@@ -381,10 +396,11 @@ pub trait WasiHttpView: ResourceView + Send {
381396
/// Returns a reference to [WasiHttpCtx]
382397
fn http(&self) -> &WasiHttpCtx<Self::Client>;
383398

384-
/// Whether a given header should be considered forbidden and not allowed.
399+
/// Whether a given header should be considered forbidden and not allowed
400+
/// for requests and responses originating in the guest.
401+
/// Note: headers of incoming requests and responses are not validated.
385402
fn is_forbidden_header(&mut self, name: &http::header::HeaderName) -> bool {
386-
_ = name;
387-
false
403+
DEFAULT_FORBIDDEN_HEADERS.contains(name)
388404
}
389405
}
390406

crates/wasi-http/tests/all/p3/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ async fn run_wasi_http<E: Into<ErrorCode> + 'static>(
198198

199199
#[test_log::test(tokio::test)]
200200
async fn wasi_http_proxy_tests() -> anyhow::Result<()> {
201-
let req = hyper::Request::builder()
202-
// TODO: remove forbidden headers?
203-
//.header("custom-forbidden-header", "yes")
201+
let req = http::Request::builder()
204202
.uri("http://example.com:8080/test-path")
205203
.method(http::Method::GET);
206204

0 commit comments

Comments
 (0)