fix: stop sending fabricated Sec-WebSocket-Protocol and Origin headers on Android - #158
Merged
riteshshukla04 merged 1 commit intoAug 3, 2026
Conversation
|
@riteshshukla04 is attempting to deploy a commit to the Margelo Team on Vercel. A member of the Team first needs to authorize it. |
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.
Problem
On Android, every WebSocket handshake sent two headers the caller never asked for:
Sec-WebSocket-Protocol: nitro-wswhen JS requested no subprotocolsOrigin: http(s)://<host>alwaysStrict servers (e.g. the WalletConnect relay) reject such handshakes, so
pair()hangs forever. Lenient servers ignore the extra headers, which is why nothing else broke. Reported by MetaMask (worked around app-side in MetaMask/metamask-mobile#33871). iOS uses a separate NSURLSession path and never had the bug.Root cause
lws_client_connect_info.protocolis both the wireSec-WebSocket-Protocolvalue and the local callback-binding key unlesslocal_protocol_nameis set. The"nitro-ws"fallback existed only to satisfy callback binding and leaked onto the wire.i.originwas set unconditionally, making libwebsockets fabricate an Origin header.Fix
One site in
WebSocketConnection::connect()— covers JS connect, redirect replay, and prewarm:Callers who genuinely need
Origincan still pass it via the headers constructor arg (flows throughLWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADERunfiltered).Also fixes a same-family bug:
ws.protocolon Android always returned"nitro-ws"(read fromlws_get_protocol, the local entry). The server-selected subprotocol is now read vialws_hdr_copy(WSI_TOKEN_PROTOCOL)inLWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH— lws detaches the header table beforeCLIENT_ESTABLISHED, so that is the last callback where the header is readable.Tests
New
/ws/headerstest-server endpoint echoes the received handshake headers as the first message, plus a new "Handshake headers" harness suite:sec-websocket-protocol, noorigin,ws.protocol === ''(the WalletConnect regression)['chat', 'superchat']→ offered on the wire, negotiatedws.protocol === 'chat'Verified on device: full harness suite (9 suites, 254 tests) passes on both Android (Pixel 8 API 35 emulator) and iOS (iPhone 17 Pro simulator).
🤖 Generated with Claude Code