Files
moloapp/src/realtime/websocket/server/handshake_validator.cpp
T
retoorandClaude Sonnet 5 9733d63e6c 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 <noreply@anthropic.com>
2026-10-25 20:06:04 +01:00

109 lines
3.4 KiB
C++

// 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