From 9733d63e6c524d27a14c9140f67e67095eb353f7 Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 25 Oct 2026 20:06:04 +0100 Subject: [PATCH] fix(realtime): reject WebSocket handshakes from unlisted Origins (CSWSH) HandshakeValidator::validate never read the Origin header, so any cross-site page could open a WebSocket to the app and ride a victim's cookies (CWE-1385, the CVE-2024-23898 class). Add HandshakeConfig's allowed_origins exact-match list and reject the handshake when an Origin header is present but not allow-listed. Empty/unset allowed_origins is closed by default (any Origin present is rejected), matching CorsPolicy's closed-by-default convention. Requests with no Origin header (non-browser clients) are unaffected. Co-Authored-By: Claude Sonnet 5 --- .../websocket/server/handshake_validator.cpp | 14 ++++++++++++++ .../websocket/server/handshake_validator.h | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/src/realtime/websocket/server/handshake_validator.cpp b/src/realtime/websocket/server/handshake_validator.cpp index 559d7c9..8f3d3e8 100644 --- a/src/realtime/websocket/server/handshake_validator.cpp +++ b/src/realtime/websocket/server/handshake_validator.cpp @@ -58,6 +58,20 @@ HandshakeResult HandshakeValidator::validate( shared::set_header(r.response, "Sec-WebSocket-Version", "13"); return r; } + std::string origin = shared::header_first(req, "Origin"); + if (!origin.empty()) { + bool allowed = false; + for (const std::string& o : config_.allowed_origins) { + if (o == origin) { + allowed = true; + break; + } + } + if (!allowed) { + return fail("Origin not allowed-listed"); + } + } + std::string picked; std::string asked = shared::header_first(req, "Sec-WebSocket-Protocol"); if (!asked.empty()) { diff --git a/src/realtime/websocket/server/handshake_validator.h b/src/realtime/websocket/server/handshake_validator.h index a1151ab..dd60f75 100644 --- a/src/realtime/websocket/server/handshake_validator.h +++ b/src/realtime/websocket/server/handshake_validator.h @@ -20,6 +20,13 @@ struct HandshakeConfig { // no protocol echoed. std::vector subprotocols; bool require_subprotocol = false; + + // Exact-match allow-list for the browser-sent Origin header (CSWSH + // defense, CWE-1385). Empty means closed: any request that carries an + // Origin header is rejected, matching CorsPolicy's closed-by-default + // convention (src/security/guard/CorsPolicy.h). A request with no + // Origin header at all (non-browser clients) is not checked here. + std::vector allowed_origins; }; struct HandshakeResult {