Raise error if frame is over 2GB on 32bits PHP

This commit is contained in:
DomoChip 2022-06-30 17:23:29 +02:00
parent c018f30d0c
commit 0050245eb4

View File

@ -158,10 +158,12 @@ class MessageBuffer {
$payloadLenBytes = $payload_length === 126 ? 2 : 8;
$headerSize += $payloadLenBytes;
$bytesToUpack = substr($data, $frameStart + 2, $payloadLenBytes);
$payloadLenOver2GB = false
if ($payload_length === 126){
$payload_length = unpack('n', $bytesToUpack)[1];
} else {
$payloadLenOver2GB = unpack('N', $bytesToUpack)[1] > 0; //Decode only the 4 first bytes
if (PHP_INT_SIZE == 4) { // if 32bits PHP
$bytesToUpack = substr($bytesToUpack, 4); //Keep only 4 last bytes
$payload_length = unpack('N', $bytesToUpack)[1];
@ -178,6 +180,10 @@ class MessageBuffer {
$closeFrame = $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Invalid frame length');
}
if (!$closeFrame && PHP_INT_SIZE == 4 && $payloadLenOver2GB) {
$closeFrame = $this->newCloseFrame(Frame::CLOSE_TOO_BIG, 'Frame over 2GB can\'t be handled on 32bits PHP');
}
if (!$closeFrame && $this->maxFramePayloadSize > 1 && $payload_length > $this->maxFramePayloadSize) {
$closeFrame = $this->newCloseFrame(Frame::CLOSE_TOO_BIG, 'Maximum frame size exceeded');
}