bugfix: client: reject a handshake response whose status is not 101 - #104
Merged
zhuizhuhaomeng merged 1 commit intoAug 17, 2026
Merged
Conversation
Per RFC 6455 section 4.1, a status code other than 101 means the server has not accepted the upgrade and the client must fail the connection. connect() only checked that the status line starts with "HTTP/1.1", so any plain HTTP response (403, 301, 502, ...) was reported as a successful handshake and subsequent frames were written into a non-WebSocket connection. Fixes openresty#47.
|
Tick the box to add this pull request to the merge queue (same as
|
zhuizhuhaomeng
approved these changes
Aug 17, 2026
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.
What this fixes
client:connect()validates the handshake response with:Any HTTP/1.1 status line passes, so a server (or an intermediary that does not support
Upgrade, e.g. a load balancer) answering403,301, or502is reported as a successfully connected WebSocket. The caller then writes masked frames into a plain HTTP connection and only finds out via garbage on the nextrecv_frame(), with no way to distinguish this from a transient network error and react (e.g. fall back to a non-WebSocket transport).RFC 6455 section 4.1 requires the client to fail the connection whenever the handshake status is not
101.Change
connect()now captures the status code and returnsnil, "failed websocket handshake: unexpected response status: <code>", headerfor anything other than101. The raw response header is still returned as the third value so callers can inspect the full response.t/handshake.t: refused upgrade (403), redirect (301), and a success control againstresty.websocket.server.Fixes #47 (open since 2019, asking for exactly this check, at the
FIXME: verify the response headerssite).Related: #94 adds an opt-in
validate_handshakeoption as part of a larger feature. This PR is only the minimal always-on status check: treating a non-101 response as a connected WebSocket is never correct, so it should apply by default rather than behind a flag. Happy to rebase either way if the maintainers prefer the #94 approach.