Skip to content

HTTPConnection.set_tunnel() host check runs before IDNA maps U+00A0 to a space #161104

Description

@gpshead

Bug report

Bug description:

HTTPConnection._tunnel() checks the set_tunnel() host for spaces and control characters on the str, but then sends its IDNA encoding. The IDNA codec applies NFKC normalization first, which maps U+00A0 NO-BREAK SPACE, U+3000 IDEOGRAPHIC SPACE and similar characters to an ASCII space, so such a host passes the check and the CONNECT request line ends up with a space in its target. The check was added in gh-146211.

from http import client
from test.test_httplib import FakeSocket

conn = client.HTTPConnection('proxy.example')
conn.set_tunnel('internal\xa0x.example')
conn.sock = FakeSocket('HTTP/1.1 200 OK\r\n\r\n')
conn._tunnel()  # Called in .connect()
print(conn.sock.data.splitlines()[0])

This prints b'CONNECT internal x.example:80 HTTP/1.1', while the same host with an ASCII space makes _tunnel() raise ValueError: Tunnel host can't contain control characters. Only a space gets through; CR, LF and NUL are still rejected.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions