MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / isValidWebSocketKey

Function isValidWebSocketKey

src/Server/WebTerminalRequestHandler.cpp:56–68  ·  view source on GitHub ↗

Validate that Sec-WebSocket-Key is a base64-encoded 16-byte nonce per RFC 6455. `base64Decode` (via `Poco::Base64Decoder`) throws `DataFormatException` on malformed input, so swallow exceptions here and treat any decode failure as an invalid key. Otherwise a crafted header would escape as a `500` instead of the deterministic `400` handshake rejection the caller expects.

Source from the content-addressed store, hash-verified

54/// as an invalid key. Otherwise a crafted header would escape as a `500`
55/// instead of the deterministic `400` handshake rejection the caller expects.
56bool isValidWebSocketKey(const String & key)
57{
58 if (key.empty() || key.size() > 128)
59 return false;
60 try
61 {
62 return base64Decode(key).size() == 16;
63 }
64 catch (...) /// Ok: malformed base64 is just an invalid key; the caller maps it to a 400 handshake rejection.
65 {
66 return false;
67 }
68}
69
70/// Send all bytes to the socket, handling partial writes.
71void sendAllBytes(Poco::Net::StreamSocket & socket, const char * data, size_t len)

Callers 1

handleWebSocketMethod · 0.85

Calls 3

base64DecodeFunction · 0.85
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected