Repository navigation
fix(gateway): require a pairing token on WebSocket upgrade - #139
Draft
cursor[bot] wants to merge 1 commit into
Draft
cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
libwebsockets 4.3 delivers HTTP_CONFIRM_UPGRADE on the HTTP protocol, so the check in ws_callback never ran. A client that offered the registered "ws" subprotocol was accepted with no token and could inject WebChat messages. The dashboard offered only bearer.<token>, which is not a registered protocol, so its handshake never completed. Check the token on the HTTP confirm callback and on FILTER_PROTOCOL_CONNECTION. Browsers offer both "ws" and bearer.<token>.
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.
WebSocket upgrade accepted any client that named the registered
wssubprotocol, with no pairing token. The same handshake path also rejected the dashboard, which offered onlybearer.<token>.Bug and impact
libwebsockets 4.3 delivers
LWS_CALLBACK_HTTP_CONFIRM_UPGRADEto the HTTP protocol callback (protocols[0]), before it binds thewsprotocol. The token check lived only inws_callback, so it never ran. Returning 0 fromhttp_callbackallowed the upgrade.A client that sent
Sec-WebSocket-Protocol: wsand noAuthorizationheader received101 Switching Protocolsand could push{"type":"message"}into the agent (shell and other tools). Reproduced on libwebsockets 4.3.3:http_cbgotCONFIRM_UPGRADE, thenws_cbgotFILTER_PROTOCOL_CONNECTIONandESTABLISHED, andws_cbnever sawCONFIRM_UPGRADE.The dashboard opened the socket with only
bearer.<token>. That name is not a registered protocol, so libwebsockets closed the handshake (No supported protocol) and WebChat never connected.Fix
http_callbackonLWS_CALLBACK_HTTP_CONFIRM_UPGRADEunless the request carries a stored pairing token.ws_callbackonLWS_CALLBACK_FILTER_PROTOCOL_CONNECTION(this one does run on thewsprotocol, before the 101) and keep the old confirm case as a backstop.bearer.<token>from anywhere in theSec-WebSocket-Protocollist, not only as a prefix.Authorization: Bearerstill works.wsandbearer.<token>. libwebsockets selectswsand echoes that name.Validation
tests/test_gateway_http.ccovers the handshake against a live server:wsalone, no subprotocol,bearer.<token>alone, both orders ofwsplus a valid token, a wrong subprotocol token, andAuthorization: Bearerwithws(valid and invalid). The unauthenticatedwscase returned 101 before the fix and is rejected after it.CC=gcc CI=true make testpassed, includingtest_gateway_http.Notes
Clients that followed the old README and offered only
bearer.<token>still cannot connect. They needwsin the offered list (orAuthorization: Bearerplus subprotocolws).