Skip to content

Remove the port from the Host header before checking the certificate name - #59

Merged
garethsb merged 1 commit into
cpprest-ltsfrom
fix/host-header-port
Nov 6, 2026
Merged

garethsb merged 1 commit into
cpprest-ltsfrom
fix/host-header-port

Conversation

@garethsb

@garethsb garethsb commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

When a request has a Host header, the Asio and WinHTTP clients check the server certificate against that value instead of the URI host. A Host header may include a port, for example example.com:8443. No certificate name includes the port, so the check fails: the TLS handshake fails with the Asio client, and the request fails with "Incorrect common name" with WinHTTP.

This PR adds web::http::details::host_without_port to internal_http_helpers.h and uses it in both clients' certificate name checks. It keeps the brackets around an IPv6 address, as uri::host() does. The request still sends the Host header unchanged. In the Asio client, the stripped name is also the TLS server name and the connection pool key, so the port no longer appears in either of those.

As agreed, the new test host_without_port covers only the host parsing. A test of the certificate check itself would need a TLS server.

On Ubuntu 26.04 with Clang 21 in Debug, the HTTP client, HTTP listener and WebSocket client suites pass. On Windows with Visual Studio 2026 in Debug, all 192 WinHTTP client tests pass.

Fixes microsoft#1790 and microsoft#1471.

Co-authored-by: Gareth's 🤖 buddy

…name

When a request has a Host header, the Asio and WinHTTP clients check the
server certificate against that value instead of the URI host. A Host
header may include a port, for example "example.com:8443", and the
check then failed because no certificate name includes the port.

Remove the port, keeping the brackets around an IPv6 address as
uri::host() does. The request still sends the Host header unchanged.

Fixes microsoft#1790 and microsoft#1471.

Signed-off-by: Gareth Sylvester-Bradley <garethsb@nvidia.com>
@garethsb
garethsb merged commit 6d2a18a into cpprest-lts Nov 6, 2026
16 of 17 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.

[http_client] CN checking against caller-specified Host header doesn't strip a port number

1 participant