@@ -172,8 +172,13 @@ class _RealWebSocket : public easywsclient::WebSocket
172
172
socket_t sockfd;
173
173
readyStateValues readyState;
174
174
bool useMask;
175
+ bool isRxBad;
175
176
176
- _RealWebSocket (socket_t sockfd, bool useMask) : sockfd(sockfd), readyState(OPEN), useMask(useMask) {
177
+ _RealWebSocket (socket_t sockfd, bool useMask)
178
+ : sockfd(sockfd)
179
+ , readyState(OPEN)
180
+ , useMask(useMask)
181
+ , isRxBad(false ) {
177
182
}
178
183
179
184
readyStateValues getReadyState () const {
@@ -264,6 +269,9 @@ class _RealWebSocket : public easywsclient::WebSocket
264
269
265
270
virtual void _dispatchBinary (BytesCallback_Imp & callable) {
266
271
// TODO: consider acquiring a lock on rxbuf...
272
+ if (isRxBad) {
273
+ return ;
274
+ }
267
275
while (true ) {
268
276
wsheader_type ws;
269
277
if (rxbuf.size () < 2 ) { return ; /* Need at least 2 */ }
@@ -296,6 +304,20 @@ class _RealWebSocket : public easywsclient::WebSocket
296
304
ws.N |= ((uint64_t ) data[8 ]) << 8 ;
297
305
ws.N |= ((uint64_t ) data[9 ]) << 0 ;
298
306
i = 10 ;
307
+ if (ws.N & 0x8000000000000000ull ) {
308
+ // https://tools.ietf.org/html/rfc6455 writes the "the most
309
+ // significant bit MUST be 0."
310
+ //
311
+ // We can't drop the frame, because (1) we don't we don't
312
+ // know how much data to skip over to find the next header,
313
+ // and (2) this would be an impractically long length, even
314
+ // if it were valid. So just close() and return immediately
315
+ // for now.
316
+ isRxBad = true ;
317
+ fprintf (stderr, " ERROR: Frame has invalid frame length. Closing.\n " );
318
+ close ();
319
+ return ;
320
+ }
299
321
}
300
322
if (ws.mask ) {
301
323
ws.masking_key [0 ] = ((uint8_t ) data[i+0 ]) << 0 ;
@@ -309,6 +331,9 @@ class _RealWebSocket : public easywsclient::WebSocket
309
331
ws.masking_key [2 ] = 0 ;
310
332
ws.masking_key [3 ] = 0 ;
311
333
}
334
+
335
+ // Note: The checks above should hopefully ensure this addition
336
+ // cannot overflow:
312
337
if (rxbuf.size () < ws.header_size +ws.N ) { return ; /* Need: ws.header_size+ws.N - rxbuf.size() */ }
313
338
314
339
// We got a whole message, now do something with it:
0 commit comments