Repository navigation
Reject obs-text handshake headers with RemoteProtocolError - #212
Open
mayur-dhavan wants to merge 2 commits into
Open
mayur-dhavan wants to merge 2 commits into
mayur-dhavan wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_requestdecodesHostas IDNA andsplits
Connection,Sec-WebSocket-ExtensionsandSec-WebSocket-Protocolas ASCII, so a request likecrashes 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.mypyreports noissues.
ruff checkreports the same 9 issues on the touched files ason a clean checkout of main (no additions).
ruff format --checkpasses on the test file;
handshake.pyalready fails it on mainunder the preview formatter, and this change adds no new format
deviations there.