Files
moloapp/audit/MOLO-WS-001/reproduce.cpp
T

49 lines
2.0 KiB
C++

// MOLO-WS-001: WebSocket handshake accepts a cross-origin request with no
// Origin check, enabling Cross-Site WebSocket Hijacking. See README.md.
//
// There is no server binary that wires HandshakeValidator up to a live
// socket (example/fw's /ws/chat route never upgrades), so this check
// drives the real class directly, the same code every real upgrade path
// in src/realtime/websocket/server calls, with no server loop needed:
// HandshakeValidator::validate is documented as "Pure function of request
// plus config: no I/O."
#include <cstdio>
#include <cstdlib>
#include "realtime/websocket/server/handshake_validator.h"
using molo::realtime::websocket::server::HandshakeConfig;
using molo::realtime::websocket::server::HandshakeValidator;
namespace shared = molo::realtime::shared;
int main() {
shared::HttpRequest req;
req.method = "GET";
req.path = "/ws/chat";
req.headers.emplace("Upgrade", "websocket");
req.headers.emplace("Connection", "Upgrade");
req.headers.emplace("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="); // RFC 6455 example
req.headers.emplace("Sec-WebSocket-Version", "13");
// The victim's browser would attach this automatically; a same-site
// server has no business accepting it from an attacker's origin.
req.headers.emplace("Origin", "https://evil.example");
HandshakeValidator v(HandshakeConfig{});
auto result = v.validate(req);
if (result.ok) {
std::printf(
"FAIL: handshake for Origin: https://evil.example was accepted (status %d, "
"Sec-WebSocket-Accept: %s); HandshakeValidator::validate "
"(src/realtime/websocket/server/handshake_validator.cpp) never reads the "
"Origin header and HandshakeConfig has no allowed-origins field, so any "
"cross-site page can open this WebSocket carrying the victim's cookies "
"(CSWSH, CWE-1385)\n",
result.response.status, result.accept_key.c_str());
return 1;
}
std::printf("PASS: handshake for a cross-origin request was rejected (%s)\n",
result.failure_reason.c_str());
return 0;
}