Skip to content

wasi-http: validate the Host header in one place - #14988

Merged
pchickey merged 1 commit into
bytecodealliance:mainfrom
xia-chao:serve-duplicate-host
Oct 28, 2026
Merged

pchickey merged 1 commit into
bytecodealliance:mainfrom
xia-chao:serve-duplicate-host

Conversation

@xia-chao

@xia-chao xia-chao commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

serve now derives and validates the request authority in one helper, so a missing, duplicated, malformed, or userinfo-bearing Host header is rejected with 400, and HTTP/2 :authority is checked against Host.

HTTP/2 requests usually carry no Host header, so one is only required for HTTP/1.1, and the two values must agree when both are present.

@xia-chao
xia-chao requested review from a team as code owners October 25, 2026 16:19
@xia-chao
xia-chao requested review from cfallin and rvolosatovs and removed request for a team October 25, 2026 16:19

@pchickey pchickey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the RFC gives a couple of conditions here and we are detecting them piecemeal. To me, this means its time to create a single helper function get_request_target(&Request) -> Result<Authority>, which has rustdocs saying it enforces the full set of conditions in https://www.rfc-editor.org/info/rfc9112/#section-3.2

A server MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message that lacks a Host header field and to any request message that contains more than one Host header field line or a Host header field with an invalid field value.

And some of that can be de-duplicated out of the p3 branch of code into that auxillary function. In the p2 branch it can just let _ = authority; so it builds without warnings when p3 disabled.

@xia-chao
xia-chao force-pushed the serve-duplicate-host branch from 3f2b8e6 to 8177934 Compare October 27, 2026 09:22
@xia-chao xia-chao changed the title serve: reject requests with duplicate Host headers wasi-http: validate the Host header in one place Oct 27, 2026
Comment thread crates/wasi-http/src/handler.rs Outdated
Comment on lines +1098 to +1107
// The p2 path builds its own copy of the request, so only the
// request-target checks of the helper are used here.
let _ = match get_request_target(&request) {
Ok(authority) => authority,
Err(e) => {
_ = tx.send(Err(e));
wasmtime::bail!(
"request was rejected before it could be turned into a guest request"
);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't be just let _ = authority; -- for the 400s the helper returns Err and it has to go through tx or serve sends 500.

@pchickey pchickey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this restructure helps a lot, I have just a couple more notes for how we should test this to make it super clear we covered all the bases

Comment thread crates/wasi-http/src/handler.rs Resolved
Comment thread tests/all/cli_tests.rs Resolved
Comment thread tests/all/cli_tests.rs Resolved
Comment thread tests/all/cli_tests.rs Resolved
@xia-chao

Copy link
Copy Markdown
Contributor Author

Done. Thanks!
#14983 and #14993 have been folded into this PR, which already covers #15023.

@xia-chao
xia-chao force-pushed the serve-duplicate-host branch from 758404a to 5c22612 Compare October 28, 2026 16:06
@xia-chao

Copy link
Copy Markdown
Contributor Author

Rebased onto the merged #14908: HTTP/2 requests carry the authority in :authority and often omit Host, so a missing Host is now only an error for HTTP/1.1. Added h2 coverage for missing, duplicate, invalid, and disagreeing Host values.

@xia-chao
xia-chao force-pushed the serve-duplicate-host branch from 5c22612 to 9d7956d Compare October 28, 2026 16:24
RFC 9112 section 3.2 has three conditions for the request target.
Enforce them in get_request_target and use it from the p2 and p3
request paths.
@xia-chao
xia-chao force-pushed the serve-duplicate-host branch from 9d7956d to 2e2038e Compare October 28, 2026 17:43

@pchickey pchickey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for seeing this through

@pchickey
pchickey added this pull request to the merge queue Oct 28, 2026
Merged via the queue into bytecodealliance:main with commit 829c86c Oct 28, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants