httpcore icon indicating copy to clipboard operation
httpcore copied to clipboard

Fix SSL certificate verification failure for hostnames with trailing dots (#1063)

Open r266-tech opened this issue 8 months ago • 1 comments

Problem

When using httpx/httpcore with FQDNs that have trailing dots (e.g., myhost.mycompany.internal.), SSL certificate verification fails with:

httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Host name mismatch, certificate is not valid for 'myhost.mycompany.internal.'.

This is a known issue — Python's ssl module doesn't handle trailing dots in server_hostname. The Python SSL team has acknowledged this as an application-layer issue: https://bugs.python.org/issue31997

Solution

Strip the trailing dot from server_hostname before passing it to SSL backends. This follows the same approach used by urllib3.

The fix is applied at the connection level (where server_hostname is constructed) rather than in each backend, keeping the change minimal and centralized across 6 files:

  • httpcore/_async/connection.py
  • httpcore/_sync/connection.py
  • httpcore/_async/http_proxy.py
  • httpcore/_sync/http_proxy.py
  • httpcore/_async/socks_proxy.py
  • httpcore/_sync/socks_proxy.py

DNS resolution still uses the original hostname with the trailing dot — only the SSL SNI is normalized.

Fixes #1063

r266-tech avatar Mar 22 '26 19:03 r266-tech

Rebased check: branch is up to date with master, and the lint failure has been fixed (a line exceeding the ruff format line length in _async/http_proxy.py). CI is now green across all Python versions (3.8-3.14).

The linked issue #1063 is still open, so this remains ready for review whenever maintainers have bandwidth. The change strips the trailing dot from server_hostname only (mirroring urllib3's approach), keeping the original FQDN for DNS resolution, and covers direct connections, HTTP tunnel proxies, and SOCKS proxies in both sync and async variants.

r266-tech avatar Jun 10 '26 08:06 r266-tech