Skip to content

Commit 023d1e3

Browse files
authored
Merge pull request #3566 from A5rocks/idna-consistently
Encode hostnames consistently
2 parents b0265fd + f71cbda commit 023d1e3

6 files changed

Lines changed: 52 additions & 14 deletions

File tree

‎docs/source/reference-io.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
I/O in Trio
44
===========
55

6+
Knowing what hostname you are actually connected to
7+
---------------------------------------------------
8+
9+
If you pass a string hostname to Trio, for example in
10+
`trio.socket.getaddrinfo` or `trio.SSLStream`, Trio will need to get a
11+
bytes version of it at some point. To do this conversion yourself, we
12+
recommend `trio.lowlevel.encode_hostname`. Notably, this differs from
13+
what the Python standard library typically encodes hosts as.
14+
15+
.. autofunction:: trio.lowlevel.encode_hostname
16+
617
.. _abstract-stream-api:
718

819
The abstract Stream API

‎newsfragments/11.bugfix.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure that we consistently encode all hostnames using IDNA 2008.

‎src/trio/_socket.py‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ async def __aexit__(
110110
return False
111111

112112

113+
def encode_hostname(hostname: str) -> bytes:
114+
"""Encode a hostname using the same IDNA 2008 encoding Trio uses.
115+
116+
If you want to know which exact hostname Trio will connect to, use this
117+
function.
118+
"""
119+
# Given a unicode hostname, we want to do our own encoding using the idna
120+
# module, rather than letting Python do it. (Python will use the old IDNA
121+
# 2003 standard, and possibly get the wrong answer - see bpo-17305).
122+
# However, the idna module is picky, and will refuse to process some valid
123+
# hostname strings, like "::1". So if it's already ascii, we pass it
124+
# through; otherwise, we encode it via IDNA.
125+
try:
126+
return hostname.encode("ascii")
127+
except UnicodeEncodeError:
128+
# UTS-46 defines various normalizations; in particular, by default
129+
# idna.encode will error out if the hostname has Capital Letters
130+
# in it; with uts46=True it will lowercase them instead.
131+
return _idna.encode(hostname, uts46=True)
132+
133+
113134
################################################################
114135
# Overrides
115136
################################################################
@@ -252,21 +273,8 @@ def numeric_only_failure(exc: BaseException) -> bool:
252273
flags | _NUMERIC_ONLY,
253274
)
254275
# That failed; it's a real hostname. We better use a thread.
255-
#
256-
# Also, it might be a unicode hostname, in which case we want to do our
257-
# own encoding using the idna module, rather than letting Python do
258-
# it. (Python will use the old IDNA 2003 standard, and possibly get the
259-
# wrong answer - see bpo-17305). However, the idna module is picky, and
260-
# will refuse to process some valid hostname strings, like "::1". So if
261-
# it's already ascii, we pass it through; otherwise, we encode it to.
262276
if isinstance(host, str):
263-
try:
264-
host = host.encode("ascii")
265-
except UnicodeEncodeError:
266-
# UTS-46 defines various normalizations; in particular, by default
267-
# idna.encode will error out if the hostname has Capital Letters
268-
# in it; with uts46=True it will lowercase them instead.
269-
host = _idna.encode(host, uts46=True)
277+
host = encode_hostname(host)
270278
hr = _resolver.get(None)
271279
if hr is not None:
272280
return await hr.getaddrinfo(host, port, family, type, proto, flags)

‎src/trio/_ssl.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from . import _sync
1212
from ._highlevel_generic import aclose_forcefully
13+
from ._socket import encode_hostname
1314
from ._util import ConflictDetector, final
1415
from .abc import Listener, Stream
1516

@@ -362,6 +363,12 @@ def __init__(
362363
server_side: bool = False,
363364
https_compatible: bool = False,
364365
) -> None:
366+
server_hostname = (
367+
encode_hostname(server_hostname)
368+
if isinstance(server_hostname, str)
369+
else server_hostname
370+
)
371+
365372
self.transport_stream: T_Stream = transport_stream
366373
self._state = _State.OK
367374
self._https_compatible = https_compatible

‎src/trio/_tests/test_ssl.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,3 +1378,13 @@ async def setup(
13781378
await aclose_forcefully(ssl_listener)
13791379
await aclose_forcefully(ssl_client)
13801380
await aclose_forcefully(ssl_server)
1381+
1382+
1383+
async def test_server_hostname_is_idna_2008(client_ctx: SSLContext) -> None:
1384+
async with ssl_echo_server_raw(expect_fail=True) as sock:
1385+
good_ctx = client_ctx
1386+
s = SSLStream(sock, good_ctx, server_hostname="faß.de")
1387+
1388+
assert s.server_hostname.encode("ascii") == trio.lowlevel.encode_hostname(
1389+
"faß.de"
1390+
)

‎src/trio/lowlevel.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
wait_task_rescheduled as wait_task_rescheduled,
5353
wait_writable as wait_writable,
5454
)
55+
from ._socket import encode_hostname as encode_hostname
5556
from ._subprocess import open_process as open_process
5657

5758
# This is the union of a subset of trio/_core/ and some things from trio/*.py.

0 commit comments

Comments
 (0)