Skip to content

Reject obs-text handshake headers with RemoteProtocolError - #212

Open
mayur-dhavan wants to merge 2 commits into
python-hyper:mainfrom
mayur-dhavan:reject-obs-text-handshake-headers
Open

mayur-dhavan wants to merge 2 commits into
python-hyper:mainfrom
mayur-dhavan:reject-obs-text-handshake-headers

Conversation

@mayur-dhavan

Copy link
Copy Markdown

h11 passes obs-text (non-ASCII) bytes in header field-values through to
wsproto (they are allowed in field-values by RFC 9110, section 5.5).
When such a byte appears in one of the WebSocket handshake headers
wsproto decodes, the decode raises an uncaught UnicodeDecodeError
instead of the rejection a server gets for other malformed handshake
headers (missing Connection: Upgrade, wrong version, bad accept token,
and so on), which surface as RemoteProtocolError with a
RejectConnection hint.

Server side, _process_connection_request decodes Host as IDNA and
splits Connection, Sec-WebSocket-Extensions and
Sec-WebSocket-Protocol as ASCII, so a request like

GET / HTTP/1.1
Host: \xff\xfe
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: VQr8cvwwZ1fEk62PDq8J3A==
Sec-WebSocket-Version: 13

crashes with UnicodeDecodeError. A server that catches
RemoteProtocolError to send a clean 400 rejection - the pattern the
RejectConnection hint exists for - instead gets an unhandled
exception. The client side has the same issue when parsing the
handshake response in _establish_client_connection.

This change routes those decodes through small helpers that convert
UnicodeDecodeError into RemoteProtocolError with a RejectConnection
hint (status 400). The request-target decode is not guarded: h11
rejects non-ASCII request targets before wsproto sees them.

Tests: parametrized server-side cases covering obs-text in Host,
Connection, Sec-WebSocket-Extensions and Sec-WebSocket-Protocol, and a
client-side case covering obs-text in the Sec-WebSocket-Protocol
response header (with the correct Sec-WebSocket-Accept token computed
from the actual request key). Each test asserts RemoteProtocolError
with a RejectConnection hint and status code 400; all five fail with
an uncaught UnicodeDecodeError before the change.

Testing notes: pytest tests/ gives 235 passed. mypy reports no
issues. ruff check reports the same 9 issues on the touched files as
on a clean checkout of main (no additions). ruff format --check
passes on the test file; handshake.py already fails it on main
under the preview formatter, and this change adds no new format
deviations there.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 07:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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