From d0a757775795fa0e84dd0c6d5bfede2d5ed634e7 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 13 Jul 2026 15:50:30 +0200 Subject: [PATCH] WebSocketClient: check extended length/mask bytes are available Only 2 header bytes were guaranteed before reading the extended length and mask, so a partial header made read() return -1 and corrupt iRxSize. Bail out until all header bytes have arrived. --- src/WebSocketClient.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index f7c1ddc..e97d4a9 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -206,6 +206,26 @@ int WebSocketClient::parseMessage() iRxMasked = (length & 0x80); length &= 0x7f; + // work out how many extended length and mask bytes follow the + // initial opcode + length bytes + int extendedLength = 0; + if (length == 126) + { + extendedLength = 2; + } + else if (length == 127) + { + extendedLength = 8; + } + int maskLength = iRxMasked ? (int)sizeof(iRxMaskKey) : 0; + + // make sure the remaining header bytes are available before reading + // them, otherwise the frame header is incomplete + if (HttpClient::available() < (extendedLength + maskLength)) + { + return 0; + } + // read the RX size iRxSize = length; if (length == 126)