- Issues
- #165
WebSocket upgrade fails (1006) whenever the client requests a Sec-WebSocket-Protocol
valkyriweb opened ·
celld 0.3.0, macOS arm64, single node, MinIO bucket backend.
`handle_websocket` in `crates/celld/main/websocket.rs:979` builds the client-facing 101 with `fastwebsockets::upgrade::upgrade(&mut request)` before the Worker runs. That function's own docs say it "does not inspect... `Sec-WebSocket-Protocol`" and expects the caller to patch the response headers afterward. celld never does: once `worker_response.ws` is taken (line 1018), the rest of `worker_response` — including any `sec-websocket-protocol` the Worker's 101 set — is discarded, and the untouched `upgrade_response` is what gets sent to the client. Per RFC 6455 §4.1, a client that offered subprotocols must fail the connection when the handshake response omits the header, which is the 1006 close every client-side `WebSocket` sees here. Nothing errors server-side; the DO's own `acceptWebSocket` succeeds and logs "accepted".
Minimal repro, no auth/routing:
```js export class EchoCell { constructor(state) { this.state = state; } async fetch(request) { const [client, server] = Object.values(new WebSocketPair()); this.state.acceptWebSocket(server); return new Response(null, { status: 101, webSocket: client }); } async webSocketMessage(ws, data) { ws.send(`echo:${data}`); } } export default { async fetch(request, env) { return env.ECHO_CELL.get(env.ECHO_CELL.idFromName("only")).fetch(request); }, }; ```
`new WebSocket(url)` connects and echoes normally, 6/6 runs. `new WebSocket(url, "any-protocol")` closes 1006 before `open`, 6/6 runs, independent of path, query string, DO binding count, or whether the DO's own 101 echoes the protocol back.
Fix: once the 101/ws target is confirmed, copy `sec-websocket-protocol` from `worker_response.headers` into `upgrade_response` before returning it.
```diff - let (upgrade_response, upgrade) = match fastwebsockets::upgrade::upgrade(&mut request) { + let (mut upgrade_response, upgrade) = match fastwebsockets::upgrade::upgrade(&mut request) { Ok(upgrade) => upgrade, Err(error) => return response(StatusCode::BAD_REQUEST, format!("ws upgrade: {error}")), }; @@ in handle_websocket, right after the `status != 101` check: + if let Some((_, value)) = worker_response + .headers + .iter() + .find(|(name, _)| name.eq_ignore_ascii_case("sec-websocket-protocol")) + { + if let Ok(value) = hyper::header::HeaderValue::from_str(value) { + upgrade_response + .headers_mut() + .insert(hyper::header::SEC_WEBSOCKET_PROTOCOL, value); + } + } ```
Verified with a local debug build: the repro above, and the same request through a real multi-Worker deployment negotiating a custom subprotocol end-to-end, both go from consistent 1006 to a clean handshake and message exchange. Happy to open a PR with this plus a regression test.
- VA
Fix is ready and verified, but `gh pr create`/the REST API both refuse with "does not have the correct permissions to execute `CreatePullRequest`" — this repo appears to restrict PR creation to collaborators, so I can't open the PR directly from a fork.
Branch: https://github.com/valkyriweb/celld/tree/fix/ws-subprotocol-lost-on-do-upgrade (one commit, `8a3c305`, on top of `v0.3.0`)
Diff: https://github.com/valkyriweb/celld/compare/main...valkyriweb:celld:fix/ws-subprotocol-lost-on-do-upgrade
Once the 101/`ws` target is confirmed in `handle_websocket`, it copies `sec-websocket-protocol` from the Worker's response onto the upgrade response (previously discarded). Pulled the copy into a small pure `apply_negotiated_subprotocol` function with 4 unit tests (present/absent/case-insensitive/invalid-value) so it doesn't need the runtime to test.
Verified: `cargo test -p celld negotiated_subprotocol` (4 passed), `cargo fmt --check` and `cargo clippy --no-deps` clean on the file, and manually end-to-end against both the minimal repro in the issue and a real multi-Worker deployment negotiating a custom subprotocol — both went from a consistent 1006 to a clean handshake and message exchange.
Happy to have a maintainer open the PR from that branch, or to be added as a collaborator so I can open it myself — whichever fits your process.
- BA
Thanks for the report, we already fixed the problem and it will be released later this week.
tboserreferenced this in WebSocket upgrade returned by the stateless Worker gets no handshake headers #1673 months ago on GitHub
bartlomiejureferenced 554fa82872e62 months ago on GitHub
bartlomiejureferenced a52f9905425b2 months ago on GitHub