Emit 1006 with some usable reason messages
This commit is contained in:
+10
-9
@@ -52,8 +52,8 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void forceClose(uWS::WebSocketState<isServer> *wState, void *s) {
|
static void forceClose(uWS::WebSocketState<isServer> *wState, void *s, std::string_view reason = {}) {
|
||||||
us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
|
us_socket_close(SSL, (us_socket_t *) s, (int) reason.length(), (void *) reason.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns true on breakage */
|
/* Returns true on breakage */
|
||||||
@@ -74,7 +74,7 @@ private:
|
|||||||
LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(SSL, us_socket_context(SSL, (us_socket_t *) s)));
|
LoopData *loopData = (LoopData *) us_loop_ext(us_socket_context_loop(SSL, us_socket_context(SSL, (us_socket_t *) s)));
|
||||||
auto [inflatedFrame, valid] = loopData->inflationStream->inflate(loopData->zlibContext, {data, length}, webSocketContextData->maxPayloadLength);
|
auto [inflatedFrame, valid] = loopData->inflationStream->inflate(loopData->zlibContext, {data, length}, webSocketContextData->maxPayloadLength);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
forceClose(webSocketState, s);
|
forceClose(webSocketState, s, ERR_TOO_BIG_MESSAGE_INFLATION);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
data = (char *) inflatedFrame.data();
|
data = (char *) inflatedFrame.data();
|
||||||
@@ -84,7 +84,7 @@ private:
|
|||||||
|
|
||||||
/* Check text messages for Utf-8 validity */
|
/* Check text messages for Utf-8 validity */
|
||||||
if (opCode == 1 && !protocol::isValidUtf8((unsigned char *) data, length)) {
|
if (opCode == 1 && !protocol::isValidUtf8((unsigned char *) data, length)) {
|
||||||
forceClose(webSocketState, s);
|
forceClose(webSocketState, s, ERR_INVALID_TEXT);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +102,7 @@ private:
|
|||||||
}
|
}
|
||||||
/* Fragments forming a big message are not caught until appending them */
|
/* Fragments forming a big message are not caught until appending them */
|
||||||
if (refusePayloadLength(length + webSocketData->fragmentBuffer.length(), webSocketState, s)) {
|
if (refusePayloadLength(length + webSocketData->fragmentBuffer.length(), webSocketState, s)) {
|
||||||
forceClose(webSocketState, s);
|
forceClose(webSocketState, s, ERR_TOO_BIG_MESSAGE);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
webSocketData->fragmentBuffer.append(data, length);
|
webSocketData->fragmentBuffer.append(data, length);
|
||||||
@@ -126,7 +126,7 @@ private:
|
|||||||
|
|
||||||
auto [inflatedFrame, valid] = loopData->inflationStream->inflate(loopData->zlibContext, {webSocketData->fragmentBuffer.data(), webSocketData->fragmentBuffer.length() - 4}, webSocketContextData->maxPayloadLength);
|
auto [inflatedFrame, valid] = loopData->inflationStream->inflate(loopData->zlibContext, {webSocketData->fragmentBuffer.data(), webSocketData->fragmentBuffer.length() - 4}, webSocketContextData->maxPayloadLength);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
forceClose(webSocketState, s);
|
forceClose(webSocketState, s, ERR_TOO_BIG_MESSAGE_INFLATION);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
data = (char *) inflatedFrame.data();
|
data = (char *) inflatedFrame.data();
|
||||||
@@ -142,7 +142,7 @@ private:
|
|||||||
|
|
||||||
/* Check text messages for Utf-8 validity */
|
/* Check text messages for Utf-8 validity */
|
||||||
if (opCode == 1 && !protocol::isValidUtf8((unsigned char *) data, length)) {
|
if (opCode == 1 && !protocol::isValidUtf8((unsigned char *) data, length)) {
|
||||||
forceClose(webSocketState, s);
|
forceClose(webSocketState, s, ERR_INVALID_TEXT);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,7 +245,7 @@ private:
|
|||||||
auto *webSocketContextData = (WebSocketContextData<SSL> *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s));
|
auto *webSocketContextData = (WebSocketContextData<SSL> *) us_socket_context_ext(SSL, us_socket_context(SSL, (us_socket_t *) s));
|
||||||
|
|
||||||
if (webSocketContextData->closeHandler) {
|
if (webSocketContextData->closeHandler) {
|
||||||
webSocketContextData->closeHandler((WebSocket<SSL, true> *) s, 1006, {});
|
webSocketContextData->closeHandler((WebSocket<SSL, true> *) s, 1006, {(char *) reason, (size_t) code});
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make sure to unsubscribe from any pub/sub node at exit */
|
/* Make sure to unsubscribe from any pub/sub node at exit */
|
||||||
@@ -360,7 +360,8 @@ private:
|
|||||||
us_socket_context_on_timeout(SSL, getSocketContext(), [](auto *s) {
|
us_socket_context_on_timeout(SSL, getSocketContext(), [](auto *s) {
|
||||||
|
|
||||||
/* Timeout is very simple; we just close it */
|
/* Timeout is very simple; we just close it */
|
||||||
us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
|
/* Warning: we happen to know forceClose will not use first parameter so pass nullptr here */
|
||||||
|
forceClose(nullptr, s, ERR_WEBSOCKET_TIMEOUT);
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -21,9 +21,17 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
|
|
||||||
|
/* We should not overcomplicate these */
|
||||||
|
const std::string_view ERR_TOO_BIG_MESSAGE("Received too big message");
|
||||||
|
const std::string_view ERR_WEBSOCKET_TIMEOUT("WebSocket timed out from inactivity");
|
||||||
|
const std::string_view ERR_INVALID_TEXT("Received invalid UTF-8");
|
||||||
|
const std::string_view ERR_TOO_BIG_MESSAGE_INFLATION("Received too big message, or other inflation error");
|
||||||
|
const std::string_view ERR_INVALID_CLOSE_PAYLOAD("Received invalid close payload");
|
||||||
|
|
||||||
enum OpCode : unsigned char {
|
enum OpCode : unsigned char {
|
||||||
TEXT = 1,
|
TEXT = 1,
|
||||||
BINARY = 2,
|
BINARY = 2,
|
||||||
@@ -310,7 +318,7 @@ protected:
|
|||||||
wState->state.lastFin = isFin(src);
|
wState->state.lastFin = isFin(src);
|
||||||
|
|
||||||
if (Impl::refusePayloadLength(payLength, wState, user)) {
|
if (Impl::refusePayloadLength(payLength, wState, user)) {
|
||||||
Impl::forceClose(wState, user);
|
Impl::forceClose(wState, user, ERR_TOO_BIG_MESSAGE);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user