https://github.com/python/cpython/commit/27efd65ec7a26c29268cd59dba9a6790e178b6cd
commit: 27efd65ec7a26c29268cd59dba9a6790e178b6cd
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2026-11-12T16:56:03Z
summary:

[3.15] gh-161104: Check the http.client tunnel host after IDNA encoding 
(GH-161105) (#161880)

gh-161104: Check the http.client tunnel host after IDNA encoding (GH-161105)

IDNA normalization maps U+00A0 and similar characters to an ASCII space,
so check the encoded host that is put into the CONNECT request line.

ANT-2026-PIZPMECM
(cherry picked from commit 7ac989f86503b6c7707c698ecb77eee93e861897)

Co-authored-by: Gregory P. Smith <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-10-22-05-23-04.gh-issue-161104.rvNPjJ.rst
M Lib/http/client.py
M Lib/test/test_httplib.py

diff --git a/Lib/http/client.py b/Lib/http/client.py
index 6a8437c57a58f0..f600ff2bddfb93 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -1004,11 +1004,15 @@ def _wrap_ipv6(self, ip):
         return ip
 
     def _tunnel(self):
-        if _contains_disallowed_url_pchar_re.search(self._tunnel_host):
+        # Check the host as it is sent: the IDNA codec normalizes the name
+        # first (NFKC), which maps some non-ASCII characters to ASCII ones,
+        # e.g. U+00A0 (NO-BREAK SPACE) to a space.
+        tunnel_host = self._tunnel_host.encode("idna")
+        if 
_contains_disallowed_url_pchar_re.search(tunnel_host.decode("ascii")):
             raise ValueError('Tunnel host can\'t contain control characters %r'
                              % (self._tunnel_host,))
         connect = b"CONNECT %s:%d %s\r\n" % (
-            self._wrap_ipv6(self._tunnel_host.encode("idna")),
+            self._wrap_ipv6(tunnel_host),
             self._tunnel_port,
             self._http_vsn_str.encode("ascii"))
         headers = [connect]
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index bd8cdfe55c6b4d..b87c9a87fb01d2 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -405,6 +405,11 @@ def test_invalid_tunnel_host(self):
             'invalid.host\r\n',
             'invalid.host\x00',
             'invalid host',
+            # The IDNA codec normalizes these to an ASCII space
+            'invalid\xa0host',
+            'invalid\u3000host',
+            # ... and this one to a space and a combining character
+            'invalid\xa8host',
         )
         for tunnel_host in cases:
             with self.subTest(tunnel_host):
diff --git 
a/Misc/NEWS.d/next/Library/2026-10-22-05-23-04.gh-issue-161104.rvNPjJ.rst 
b/Misc/NEWS.d/next/Library/2026-10-22-05-23-04.gh-issue-161104.rvNPjJ.rst
new file mode 100644
index 00000000000000..acae526043a297
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-10-22-05-23-04.gh-issue-161104.rvNPjJ.rst
@@ -0,0 +1,4 @@
+Fix :meth:`http.client.HTTPConnection.set_tunnel` accepting a tunnel host
+containing a non-ASCII character that IDNA encoding maps to a space, such as
+U+00A0 NO-BREAK SPACE. Connecting now raises :exc:`ValueError`, as it already
+does for an ASCII space.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to