Skip to content

simple_httpclient: IDNA-encode non-ASCII hosts in redirect Locations - #3881

Closed
dltsum wants to merge 1 commit into
tornadoweb:masterfrom
dltsum:fix-redirect-idn-host
Closed

dltsum wants to merge 1 commit into
tornadoweb:masterfrom
dltsum:fix-redirect-idn-host

Conversation

@dltsum

@dltsum dltsum commented Nov 12, 2026

Copy link
Copy Markdown

Fixes #3870.

63b2b5c percent-encodes the entire Location value, which also rewrites non-ASCII characters in the host. A percent-encoded host is not a valid IDNA name (caf%E9.example), so the redirected request failed at resolution.

Now only the path, query and fragment are percent-encoded; a non-ASCII host is IDNA-encoded (punycode, café.example -> xn--caf-dma.example), matching WHATWG/browser handling. Punycode also keeps the Host header of the followed request ASCII, so strict servers (including tornado's own _ABNF.host check) accept it.

Regression test: a loopback server returns 302 with a latin-1 Location: http://café.example:PORT/...; socket.getaddrinfo is patched to log the host handed to the resolver and map the punycode name to loopback. The test asserts the resolver sees xn--caf-dma.example and the redirect completes with 200. It fails on current master (resolver receives caf%E9.example).

Verified: tornado.test.simple_httpclient_test + httpclient_test + web_test all pass; full suite passes except two pre-existing pycurl/Schannel environment errors (test_reuse_certs, test_digest_auth_non_ascii) that fail identically without this change.

Percent-encoding the whole Location header (from 63b2b5c) also encoded
non-ASCII characters in the host, and a percent-encoded host is not a
valid IDNA name, so redirects to internationalized domain names failed
at resolution. Encode only the path, query and fragment, and IDNA-encode
(punycode) a non-ASCII host, matching WHATWG/browser handling.

Fixes tornadoweb#3870
@bdarnell

Copy link
Copy Markdown
Member

Closing in favor of #3873. This PR decodes the location header as latin1, which is inconsistent with what web browsers use (utf8).

@bdarnell bdarnell closed this Nov 13, 2026
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.

simple_httpclient percent-encodes the redirect Location host, breaking IDN redirects

2 participants