Skip to content

httputil: reject hostnames IDNA2003 would rewrite - #3929

Open
BlueX888 wants to merge 1 commit into
tornadoweb:masterfrom
BlueX888:contrib/idna-roundtrip
Open

BlueX888 wants to merge 1 commit into
tornadoweb:masterfrom
BlueX888:contrib/idna-roundtrip

Conversation

@BlueX888

@BlueX888 BlueX888 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

httputil._url_to_ascii converts non-ASCII hostnames with Python's idna
codec, which implements IDNA2003. Browsers implement UTS46, and the two
disagree on some names: straße.de becomes strasse.de (browsers use
xn--strae-oqa.de), a label containing a ZWJ is silently dropped, and final
sigma is folded to sigma. The request can then go to a host the URL did not
name, including the Host header and TLS verification.

Per the review, this now uses the idna package as a soft dependency, like
pycurl:

  • With idna installed, the host is encoded with
    idna.encode(host, uts46=True, transitional=False).
  • Without it, a non-ASCII hostname raises ValueError. There is no fallback
    to the stdlib codec, whose IDNA2003 mapping is known to be wrong.

idna is added to requirements.in (it was already present transitively via
requests) and listed as an optional package in docs/index.rst. It is not
an install requirement.

Behavior

  • straße.de -> xn--strae-oqa.de (browser behavior; previously
    strasse.de).
  • Final sigma and other UTS46-valid names keep their correct A-label.
  • Labels with ZWJ/ZWNJ, empty labels and over-long labels raise ValueError.
  • Existing conversions (café.example, 例え。jp, mixed A-label/Unicode,
    fullwidth hosts, trailing dots) are unchanged.
  • With idna unavailable, non-ASCII hostnames are rejected; ASCII hosts and
    non-ASCII paths still work.

Verification

  • python3 -m tornado.test tornado.test.httputil_test — 93 tests OK.
  • python3 -m tornado.test — 1722 tests OK (131 skipped).
  • black, flake8 and mypy report no errors in the changed files.
  • New tests cover the UTS46 mappings, joiner rejection, and the missing
    package path.

Related to #3890. Reported and implemented by an AI agent operating the
BlueX888 account (Claude Code).

@bdarnell

Copy link
Copy Markdown
Member

I'd rather depend on idna than have another partial ad-hoc implementation. We can make it a soft dependency like pycurl is. When idna is not installed, we simply reject URLs containing non-ascii characters in the hostname. (avoid falling back to the known-incorrect stdlib version)

@BlueX888
BlueX888 force-pushed the contrib/idna-roundtrip branch from 27b54fb to 39221b3 Compare November 24, 2026 22:33
@BlueX888

Copy link
Copy Markdown
Contributor Author

Reworked in 39221b3d (force-pushed): the ad-hoc round-trip is gone. With idna installed the host is encoded with idna.encode(host, uts46=True, transitional=False); without it, a non-ASCII hostname raises ValueError instead of using the stdlib codec. idna is in requirements.in (it was already present via requests) and listed as optional in docs/index.rst; it is not an install requirement. Tests cover straße.de -> xn--strae-oqa.de, final sigma, ZWJ/ZWNJ rejection and the missing-package path. Full suite 1722 OK (131 skipped); black/flake8 clean, mypy clean on the changed files. The PR body is updated to match.

Review feedback: prefer the idna package (UTS46) over a partial ad-hoc
implementation, as a soft dependency. With idna installed, encode the
host with idna.encode(host, uts46=True, transitional=False). Without it,
reject a non-ASCII hostname instead of falling back to the stdlib codec,
which implements IDNA2003 and maps some names (straße.de) to a different
host than browsers use.

Tests cover the UTS46 mappings IDNA2003 got wrong (straße.de, final
sigma), ZWJ/ZWNJ rejection, the missing-package rejection, and the
existing conversion cases; the conversion and joiner tests skip when
idna is absent, as do the two httpclient IDN tests. idna is a test
requirement and an optional runtime dependency, documented in
docs/index.rst.

tornadoweb#3890
@BlueX888
BlueX888 force-pushed the contrib/idna-roundtrip branch from 39221b3 to 501ef37 Compare November 24, 2026 22:38

This branch has not been deployed

No deployments
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.

2 participants