Merge branch 'master' into v0.4

This commit is contained in:
Matt Bonneau 2024-12-03 09:44:59 -05:00
commit 191926dd3f

View File

@ -150,6 +150,7 @@ class MessageBuffer {
$payload_length = unpack('C', $data[$frameStart + 1] & "\x7f")[1]; $payload_length = unpack('C', $data[$frameStart + 1] & "\x7f")[1];
$isMasked = ($data[$frameStart + 1] & "\x80") === "\x80"; $isMasked = ($data[$frameStart + 1] & "\x80") === "\x80";
$headerSize += $isMasked ? 4 : 0; $headerSize += $isMasked ? 4 : 0;
$payloadLenOver2GB = false;
if ($payload_length > 125 && ($dataLen - $frameStart < $headerSize + 125)) { if ($payload_length > 125 && ($dataLen - $frameStart < $headerSize + 125)) {
// no point of checking - this frame is going to be bigger than the buffer is right now // no point of checking - this frame is going to be bigger than the buffer is right now
break; break;
@ -158,9 +159,18 @@ class MessageBuffer {
$payloadLenBytes = $payload_length === 126 ? 2 : 8; $payloadLenBytes = $payload_length === 126 ? 2 : 8;
$headerSize += $payloadLenBytes; $headerSize += $payloadLenBytes;
$bytesToUpack = substr($data, $frameStart + 2, $payloadLenBytes); $bytesToUpack = substr($data, $frameStart + 2, $payloadLenBytes);
$payload_length = $payload_length === 126
? unpack('n', $bytesToUpack)[1] if ($payload_length === 126){
: unpack('J', $bytesToUpack)[1]; $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];
} else {
$payload_length = unpack('J', $bytesToUpack)[1];
}
}
} }
$closeFrame = null; $closeFrame = null;
@ -170,6 +180,10 @@ class MessageBuffer {
$closeFrame = $this->newCloseFrame(Frame::CLOSE_PROTOCOL, 'Invalid frame length'); $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) { if (!$closeFrame && $this->maxFramePayloadSize > 1 && $payload_length > $this->maxFramePayloadSize) {
$closeFrame = $this->newCloseFrame(Frame::CLOSE_TOO_BIG, 'Maximum frame size exceeded'); $closeFrame = $this->newCloseFrame(Frame::CLOSE_TOO_BIG, 'Maximum frame size exceeded');
} }