// Opening handshake validation. See handshake_validator.h. #include "realtime/websocket/server/handshake_validator.h" #include "realtime/shared/base64.h" #include "realtime/shared/realtime_metrics.h" #include "realtime/shared/sha1.h" namespace molo::realtime::websocket::server { namespace { constexpr char kGuid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; HandshakeResult fail(const std::string& reason) { HandshakeResult r; r.ok = false; r.failure_reason = reason; r.response.status = 400; shared::set_header(r.response, "Content-Type", "text/plain"); r.response.body = "Bad Request: " + reason; shared::RealtimeMetrics::instance().inc( shared::RealtimeMetric::kHandshakeFailures); return r; } } // namespace HandshakeValidator::HandshakeValidator(HandshakeConfig config) : config_(std::move(config)) {} std::string HandshakeValidator::accept_for(const std::string& key) { shared::Sha1::Digest d = shared::Sha1::hash(key + kGuid); return shared::base64_encode(d.data(), d.size()); } HandshakeResult HandshakeValidator::validate( const shared::HttpRequest& req) const { if (req.method != "GET") { return fail("WebSocket handshake requires GET"); } if (!shared::header_has_token(shared::header_first(req, "Upgrade"), "websocket")) { return fail("missing Upgrade: websocket"); } if (!shared::header_has_token(shared::header_first(req, "Connection"), "Upgrade")) { return fail("missing Connection: Upgrade"); } std::string key = shared::header_first(req, "Sec-WebSocket-Key"); if (key.empty()) { return fail("missing Sec-WebSocket-Key"); } auto raw = shared::base64_decode(key); if (!raw.has_value() || raw->size() != 16) { return fail("Sec-WebSocket-Key must decode to 16 bytes"); } std::string version = shared::header_first(req, "Sec-WebSocket-Version"); if (!version.empty() && version != "13") { HandshakeResult r = fail("unsupported Sec-WebSocket-Version"); 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()) { bool found = false; for (const std::string& offered : config_.subprotocols) { if (shared::header_has_token(asked, offered)) { picked = offered; found = true; break; } } if (!found && (config_.require_subprotocol || !config_.subprotocols.empty())) { return fail("no supported Sec-WebSocket-Protocol"); } } else if (config_.require_subprotocol) { return fail("Sec-WebSocket-Protocol required"); } HandshakeResult r; r.ok = true; r.accept_key = accept_for(key); r.subprotocol = picked; r.response.status = 101; shared::set_header(r.response, "Upgrade", "websocket"); shared::set_header(r.response, "Connection", "Upgrade"); shared::set_header(r.response, "Sec-WebSocket-Accept", r.accept_key); if (!picked.empty()) { shared::set_header(r.response, "Sec-WebSocket-Protocol", picked); } return r; } } // namespace molo::realtime::websocket::server