Skip to content

Require a valid Sec-WebSocket-Accept header in the client handshake - #310

Open
bardrop wants to merge 1 commit into
Lawouach:masterfrom
bardrop-oss:fix-accept-header-check
Open

bardrop wants to merge 1 commit into
Lawouach:masterfrom
bardrop-oss:fix-accept-header-check

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown

Fixes #309.

What was wrong

The client checked Sec-WebSocket-Accept only if the server sent it. A reply like this finished the handshake with no error:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: upgrade

The same was true when Upgrade or Connection was 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, Upgrade was rejected, because the check wanted the whole value to be upgrade. The RFC only says the header must contain the Upgrade token.

The fix

In WebSocketBaseClient.process_handshake_header:

  • Track Upgrade, Connection and Sec-WebSocket-Accept. If any is missing at the end, raise HandshakeError.
  • Compare the accept value exactly against base64(sha1(key + GUID)), with no lowercasing.
  • Check that Connection has an upgrade token (case-insensitive), not that it equals upgrade.

Header names and the Upgrade/Connection values 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: WebSocket and Connection: keep-alive, UPGRADE still pass.

Before the fix: test/test_client.py gives 3 failed, 19 passed.
After the fix: 22 passed. Full suite (pytest): 136 passed.

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
auvipy self-requested a review November 26, 2026 16:01

This branch has not been deployed

No deployments
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.

ws4py Client Completes the RFC 6455 Handshake Without Requiring Sec-WebSocket-Accept, Accepting Forged Server Responses

1 participant