Skip to content

simple_httpclient,web,httputil: Stricter error handling - #3860

Merged
bdarnell merged 9 commits into
tornadoweb:masterfrom
bdarnell:claude/project-thread-rdyj8b
Nov 6, 2026
Merged

bdarnell merged 9 commits into
tornadoweb:masterfrom
bdarnell:claude/project-thread-rdyj8b

Conversation

@bdarnell

@bdarnell bdarnell commented Nov 6, 2026

Copy link
Copy Markdown
Member
  • simple_httpclient rejects more classes of invalid URLs and no longer emits non-ascii characters in its requests.
  • web.py handles some non-browser edge cases in origin/host matching
  • Fix open redirects in demos
  • Use ascii-only regexes for HTTP parsing (in addition to enforcement at other layers that only latin1 chars are used)

urlsplit silently removes tabs and newlines, and other control
characters and spaces were sent as-is in the request line, producing a
malformed request. Reject them instead, as http.client and curl do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
A URL like http://user@example.com/ has a username but no password, and
urlsplit returns None for the password, which tripped an assertion.
Treat a missing password as empty, as we already do for auth_username.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
Non-ASCII characters in the path and query were sent as raw UTF-8, and
non-ASCII characters in a redirect's Location header were mangled
(decoded as latin1 and re-encoded as UTF-8). Percent-encode them as
browsers do, following the WHATWG URL spec, and percent-encode spaces in
Location headers as browsers and curl do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
The cross-origin protection fallback compared the netloc of the Origin
header with the Host header. For "Origin: null" (an opaque origin) the
netloc is empty, so an HTTP/1.0 request with no Host header was treated
as same-origin. Browsers always send Host, but there's no reason to
allow this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
A fragment is never part of an HTTP request target (RFC 9112 section
3.2), and browsers never send one. Previously "#" and everything after it
ended up in the request's path or query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
The blog and chat demos rejected "next" URLs starting with "//", but
browsers treat backslashes like slashes and ignore tabs and newlines, so
"/\evil.com" and "/<tab>/evil.com" still redirected to another host.
Since these demos are often used as a starting point, check the URL the
way a browser sees it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
In str patterns, \d matches any Unicode decimal digit, and int() accepts
them, so split_host_and_port("example.com:٨٠") returned port
80. HTTP and URLs only allow ASCII digits (and urlsplit().port rejects
others). Neither use is reachable from HTTP headers, which are decoded as
latin1, but there's no reason to accept them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
The negative lookahead added to reject "#" made parsing the request
line about 2x slower in a microbenchmark (23% in the start_line CI
benchmark). Spell out the allowed characters as a single class instead,
which is as fast as before and accepts exactly the same characters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
Keep _ABNF's request_target derived directly from the RFC grammar
(field_vchar+) and reject "#" with a separate scan after the regex
match, instead of a hand-written character class. This keeps the
performance of the original regex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
@bdarnell
bdarnell merged commit 6665240 into tornadoweb:master Nov 6, 2026
20 checks passed
@bdarnell
bdarnell deleted the claude/project-thread-rdyj8b branch November 6, 2026 20:01
bdarnell pushed a commit to bdarnell/tornado that referenced this pull request Nov 13, 2026
Convert non-ASCII hostnames with IDNA and percent-encode other non-ASCII
characters as UTF-8, as browsers do, in a shared helper
(httputil._url_to_ascii) used by both simple_httpclient and
curl_httpclient. Both clients now accept IDN URLs (tornadoweb#1490), and
simple_httpclient sends the IDNA form in the Host header instead of raw
latin1.

When following a redirect, simple_httpclient decodes the Location header
as UTF-8 and converts it the same way. This replaces the percent-encoding
of the raw header bytes from tornadoweb#3860, which also percent-encoded the
hostname (tornadoweb#3870). Like browsers, a Location header that is not valid
UTF-8 is now an error. Previously a latin1 hostname happened to work,
but it was sent in the Host header without IDNA conversion and a UTF-8
hostname did not work at all.

The tests move to the common httpclient tests. libcurl follows redirects
itself, so the redirect tests are skipped for curl_httpclient where its
behavior differs: it percent-encodes non-UTF-8 bytes, and converts IDN
hostnames only when built with IDN support.

Fixes tornadoweb#1490
Closes tornadoweb#3870

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
bdarnell pushed a commit to bdarnell/tornado that referenced this pull request Nov 13, 2026
Convert non-ASCII hostnames with IDNA and percent-encode other non-ASCII
characters as UTF-8, as browsers do, in a shared helper
(httputil._url_to_ascii) used by both simple_httpclient and
curl_httpclient. Both clients now accept IDN URLs (tornadoweb#1490), and
simple_httpclient sends the IDNA form in the Host header instead of raw
latin1.

When following a redirect, simple_httpclient decodes the Location header
as UTF-8 and converts it the same way. This replaces the percent-encoding
of the raw header bytes from tornadoweb#3860, which also percent-encoded the
hostname (tornadoweb#3870). Like browsers, a Location header that is not valid
UTF-8 is now an error. Previously a latin1 hostname happened to work,
but it was sent in the Host header without IDNA conversion and a UTF-8
hostname did not work at all.

The tests move to the common httpclient tests. libcurl follows redirects
itself, so the redirect tests are skipped for curl_httpclient where its
behavior differs: it percent-encodes non-UTF-8 bytes, and converts IDN
hostnames only when built with IDN support.

Fixes tornadoweb#1490
Closes tornadoweb#3870

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VamvWqdwiBTCFwhDLCbBee
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