← 输入框|github.com|刷新
Skip to content

Commit 2165415

Browse files
committed
fix(websocket): encode 65,536-byte payload lengths correctly
write() picked the 16-bit extended length only when the payload was above 65,536 bytes. A payload of exactly 65,536 bytes got the 16-bit form, and pack('n') wrapped its length to 0, so the daemon read a corrupt frame. Switch to the 64-bit form above 0xFFFF and write it with pack('J').
1 parent 459a1f2 commit 2165415

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎src/Stream/AttachWebsocketStream.php‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public function write($data): void
5454
}
5555
}
5656

57-
if ($frame['len'] > 2 ** 16) {
57+
// 126 carries a 16-bit length, so anything above 0xFFFF needs the 64-bit form.
58+
if ($frame['len'] > 0xFFFF) {
5859
$len = 127;
5960
} elseif ($frame['len'] > 125) {
6061
$len = 126;
@@ -71,10 +72,7 @@ public function write($data): void
7172
if (126 === $len) {
7273
$this->socketWrite(pack('n', $frame['len']));
7374
} elseif (127 === $len) {
74-
$higher = $frame['len'] >> 32;
75-
$lower = ($frame['len'] << 32) >> 32;
76-
$this->socketWrite(pack('N', $higher));
77-
$this->socketWrite(pack('N', $lower));
75+
$this->socketWrite(pack('J', $frame['len']));
7876
}
7977

8078
if (1 === $frame['mask']) {

‎tests/Stream/AttachWebsocketStreamTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Docker\Stream\AttachWebsocketStream;
88
use Nyholm\Psr7\Stream;
9+
use PHPUnit\Framework\Attributes\DataProvider;
910
use PHPUnit\Framework\TestCase;
1011

1112
class AttachWebsocketStreamTest extends TestCase
@@ -23,6 +24,33 @@ public function testReadsEmptyFrameBeforePayload(): void
2324
fclose($server);
2425
}
2526

27+
public static function payloadLengths(): iterable
28+
{
29+
yield '7-bit length' => [125, "\xFD", ''];
30+
yield '16-bit length' => [126, "\xFE", pack('n', 126)];
31+
yield 'largest 16-bit length' => [0xFFFF, "\xFE", pack('n', 0xFFFF)];
32+
yield 'smallest 64-bit length' => [0x10000, "\xFF", pack('J', 0x10000)];
33+
}
34+
35+
#[DataProvider('payloadLengths')]
36+
public function testWriteEncodesPayloadLength(int $length, string $lengthByte, string $extendedLength): void
37+
{
38+
$socket = fopen('php://temp', 'w+');
39+
$stream = new AttachWebsocketStream(Stream::create($socket));
40+
$data = str_repeat('x', $length);
41+
42+
$stream->write($data);
43+
44+
rewind($socket);
45+
$frame = stream_get_contents($socket);
46+
$header = 2 + \strlen($extendedLength);
47+
$this->assertSame("\x81".$lengthByte.$extendedLength, substr($frame, 0, $header));
48+
$mask = substr($frame, $header, 4);
49+
$payload = substr($frame, $header + 4);
50+
$this->assertSame($length, \strlen($payload));
51+
$this->assertSame($data, $payload ^ str_repeat($mask, intdiv($length, 4) + 1));
52+
}
53+
2654
public function testCloseSendsCloseFrameAndClosesSocket(): void
2755
{
2856
[$client, $server] = stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);

0 commit comments

Comments
 (0)