Repository navigation
Conversation
|
I'd rather depend on |
27b54fb to
39221b3
Compare
|
Reworked in |
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
39221b3 to
501ef37
Compare
Summary
httputil._url_to_asciiconverts non-ASCII hostnames with Python'sidnacodec, which implements IDNA2003. Browsers implement UTS46, and the two
disagree on some names:
straße.debecomesstrasse.de(browsers usexn--strae-oqa.de), a label containing a ZWJ is silently dropped, and finalsigma is folded to sigma. The request can then go to a host the URL did not
name, including the
Hostheader and TLS verification.Per the review, this now uses the
idnapackage as a soft dependency, likepycurl:idnainstalled, the host is encoded withidna.encode(host, uts46=True, transitional=False).ValueError. There is no fallbackto the stdlib codec, whose IDNA2003 mapping is known to be wrong.
idnais added torequirements.in(it was already present transitively viarequests) and listed as an optional package indocs/index.rst. It is notan install requirement.
Behavior
straße.de->xn--strae-oqa.de(browser behavior; previouslystrasse.de).ValueError.café.example,例え。jp, mixed A-label/Unicode,fullwidth hosts, trailing dots) are unchanged.
idnaunavailable, non-ASCII hostnames are rejected; ASCII hosts andnon-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,flake8andmypyreport no errors in the changed files.package path.
Related to #3890. Reported and implemented by an AI agent operating the
BlueX888account (Claude Code).