1717#include < json/value.h>
1818#include < json/writer.h>
1919#include < thread>
20+ #include < limits>
2021
2122using namespace drogon ;
2223
@@ -327,8 +328,13 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer)
327328 {
328329 indexFirstMask = 10 ;
329330 }
330- if (indexFirstMask > 2 && buffer-> readableBytes () >= indexFirstMask )
331+ if (indexFirstMask > 2 )
331332 {
333+ if (buffer->readableBytes () < indexFirstMask)
334+ {
335+ // Not enough data yet, wait for more.
336+ return true ;
337+ }
332338 if (isControlFrame)
333339 {
334340 // rfc6455-5.5
@@ -344,14 +350,17 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer)
344350 }
345351 else if (indexFirstMask == 10 )
346352 {
347- length = (unsigned char )(*buffer)[2 ];
348- length = (length << 8 ) + (unsigned char )(*buffer)[3 ];
349- length = (length << 8 ) + (unsigned char )(*buffer)[4 ];
350- length = (length << 8 ) + (unsigned char )(*buffer)[5 ];
351- length = (length << 8 ) + (unsigned char )(*buffer)[6 ];
352- length = (length << 8 ) + (unsigned char )(*buffer)[7 ];
353- length = (length << 8 ) + (unsigned char )(*buffer)[8 ];
354- length = (length << 8 ) + (unsigned char )(*buffer)[9 ];
353+ length = 0 ;
354+ for (int i = 2 ; i <= 9 ; ++i)
355+ {
356+ if (length > ((std::numeric_limits<size_t >::max)() >> 8 ))
357+ {
358+ LOG_ERROR
359+ << " Payload length too large to handle safely" ;
360+ return false ;
361+ }
362+ length = (length << 8 ) + (unsigned char )(*buffer)[i];
363+ }
355364 }
356365 else
357366 {
@@ -387,6 +396,11 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer)
387396 return true ;
388397 }
389398 }
399+ else
400+ {
401+ // Not enough data yet, wait for more.
402+ return true ;
403+ }
390404 }
391405 else
392406 {
@@ -401,6 +415,11 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer)
401415 return true ;
402416 }
403417 }
418+ else
419+ {
420+ // Not enough data yet, wait for more.
421+ return true ;
422+ }
404423 }
405424 }
406425 return true ;
0 commit comments