Repository navigation
Conversation
The client only checked Sec-WebSocket-Accept when the server sent it, and compared it lowercased. A 101 reply without the header, or with a wrong-case value, completed the handshake. Now the client fails the handshake when Upgrade, Connection or Sec-WebSocket-Accept is missing, compares the accept value exactly, and accepts a Connection header that lists Upgrade among other tokens (RFC 6455 section 4.1). Fixes Lawouach#309
auvipy
self-requested a review
November 26, 2026 16:01
This branch has not been deployed
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.
Fixes #309.
What was wrong
The client checked
Sec-WebSocket-Acceptonly if the server sent it. A reply like this finished the handshake with no error:The same was true when
UpgradeorConnectionwas missing. Also, the accept value was lowercased before the compare, so a value with the wrong case passed too. RFC 6455 section 4.1 says the client MUST fail in all these cases.One more small bug:
Connection: keep-alive, Upgradewas rejected, because the check wanted the whole value to beupgrade. The RFC only says the header must contain theUpgradetoken.The fix
In
WebSocketBaseClient.process_handshake_header:Upgrade,ConnectionandSec-WebSocket-Accept. If any is missing at the end, raiseHandshakeError.base64(sha1(key + GUID)), with no lowercasing.Connectionhas anupgradetoken (case-insensitive), not that it equalsupgrade.Header names and the
Upgrade/Connectionvalues are still matched case-insensitively. The Tornado client uses the same method, so it gets the fix too.Tests
New tests in
test/test_client.py:test_missing_required_response_headers: drop each of the 3 headers in turn, handshake must fail and close the socket.test_invalid_accept_header: wrong, extended and empty accept values fail.test_accept_header_is_case_sensitive: the right value with swapped case fails.test_valid_response_headers_any_case: lowercase/uppercase header names,Upgrade: WebSocketandConnection: keep-alive, UPGRADEstill pass.Before the fix:
test/test_client.pygives 3 failed, 19 passed.After the fix: 22 passed. Full suite (
pytest): 136 passed.