Skip to content

Commit a851194

Browse files
authored
deal with safari fragmented pong data
Pong in Chrome 8A 80 8B 9B 7E 64 Pong in Safari 8A 80 46 DC 9F 25 This commit addresses the pointer corruption that occurs when Safari sends a Pong. Without this change, the library will use the mask as beginning of the data packet and misbehave. Use _pinfo.masked as a counter to minimize change of the code.
1 parent b170b9d commit a851194

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
515515
_pinfo.index = 0;
516516
_pinfo.final = (fdata[0] & 0x80) != 0;
517517
_pinfo.opcode = fdata[0] & 0x0F;
518-
_pinfo.masked = (fdata[1] & 0x80) != 0;
518+
_pinfo.masked = ((fdata[1] & 0x80) != 0) ? 1 : 0;
519519
_pinfo.len = fdata[1] & 0x7F;
520520

521521
// async_ws_log_d("WS[%" PRIu32 "]: _onData: %" PRIu32, _clientId, plen);
@@ -536,12 +536,20 @@ void AsyncWebSocketClient::_onData(void *pbuf, size_t plen) {
536536
data += 8;
537537
plen -= 8;
538538
}
539+
}
539540

540-
if (_pinfo.masked
541-
&& plen >= 4) { // if ws.close() is called, Safari sends a close frame with plen 2 and masked bit set. We must not decrement plen which is already 0.
542-
memcpy(_pinfo.mask, data, 4);
543-
data += 4;
544-
plen -= 4;
541+
if (_pinfo.masked > 0 && _pinfo.masked < 5) {
542+
//mask not fully read yet
543+
while (_pinfo.masked < 5) {
544+
if (plen == 0) {
545+
//wait for more data
546+
_pstate = 1;
547+
return;
548+
}
549+
_pinfo.mask[_pinfo.masked - 1] = data[0];
550+
data += 1;
551+
plen -= 1;
552+
_pinfo.masked++;
545553
}
546554
}
547555

0 commit comments

Comments
 (0)