diff --git a/packages/mysql-proxy/src/class-mysql-protocol.php b/packages/mysql-proxy/src/class-mysql-protocol.php index f44a5a7ea..af751ad73 100644 --- a/packages/mysql-proxy/src/class-mysql-protocol.php +++ b/packages/mysql-proxy/src/class-mysql-protocol.php @@ -495,16 +495,27 @@ public static function build_column_definition_packet( int $sequence_id, array $ } /** - * Build the row packet. + * Build the row packet(s). + * + * Rows whose encoded payload reaches or exceeds `MAX_PACKET_LENGTH` + * (0x00ffffff bytes) are split across multiple MySQL wire packets. * * @see https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response_text_resultset_row.html + * @see https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_packets.html * - * @param int $sequence_id The sequence ID of the packet. - * @param array $columns The columns. - * @param object $row The row. - * @return string The row packet. + * @param int $sequence_id The sequence ID of the first packet. + * @param array $columns The columns. + * @param object $row The row. + * @param int|null $next_sequence_id Optional. Receives the sequence ID following + * the last packet emitted (wrapped to 0..255). + * @return string The row packet(s). */ - public static function build_row_packet( int $sequence_id, array $columns, object $row ): string { + public static function build_row_packet( + int $sequence_id, + array $columns, + object $row, + ?int &$next_sequence_id = null + ): string { $payload = ''; foreach ( $columns as $column ) { $value = $row->{$column['name']} ?? null; @@ -514,19 +525,67 @@ public static function build_row_packet( int $sequence_id, array $columns, objec $payload .= self::encode_length_encoded_string( (string) $value ); } } - return self::build_packet( $sequence_id, $payload ); + $packets = self::build_packets( $sequence_id, $payload ); + $next_sequence_id = $sequence_id; + return $packets; } /** - * Build a MySQL packet. + * Build one or more MySQL packets for a message payload. + * + * Payloads of `MAX_PACKET_LENGTH` (0x00ffffff) bytes or larger are split + * across `MAX_PACKET_LENGTH` packets, terminated by a packet shorter than + * `MAX_PACKET_LENGTH` (empty when the payload length is an exact multiple + * of `MAX_PACKET_LENGTH`). * * @see https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_packets.html * - * @param int $sequence_id The sequence ID of the packet. - * @param string $payload The payload of the packet. + * @param int $sequence_id The sequence ID of the first packet. Advanced + * past the last packet emitted (wrapped to 0..255). + * @param string $payload The message payload. * @return string The packet data. */ + public static function build_packets( int &$sequence_id, string $payload ): string { + $length = strlen( $payload ); + if ( $length < self::MAX_PACKET_LENGTH ) { + $packet = self::build_packet( $sequence_id, $payload ); + $sequence_id = ( $sequence_id + 1 ) & 0xff; + return $packet; + } + + $packets = ''; + $offset = 0; + while ( $length - $offset >= self::MAX_PACKET_LENGTH ) { + $packets .= self::build_packet( + $sequence_id, + substr( $payload, $offset, self::MAX_PACKET_LENGTH ) + ); + $sequence_id = ( $sequence_id + 1 ) & 0xff; + $offset += self::MAX_PACKET_LENGTH; + } + $packets .= self::build_packet( + $sequence_id, + $offset < $length ? substr( $payload, $offset ) : '' + ); + $sequence_id = ( $sequence_id + 1 ) & 0xff; + return $packets; + } + + /** + * Build a single MySQL wire packet. + * + * @see https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_packets.html + * + * @param int $sequence_id The sequence ID of the packet. + * @param string $payload The payload of the packet. + * @return string The packet data. + * @throws MySQL_Proxy_Exception When the payload exceeds MAX_PACKET_LENGTH. + */ public static function build_packet( int $sequence_id, string $payload ): string { + if ( strlen( $payload ) > self::MAX_PACKET_LENGTH ) { + throw new MySQL_Proxy_Exception( 'Packet payload exceeds maximum MySQL packet length.' ); + } + /** * Assemble the packet. * diff --git a/packages/mysql-proxy/src/class-mysql-session.php b/packages/mysql-proxy/src/class-mysql-session.php index 384e62761..627ba320a 100644 --- a/packages/mysql-proxy/src/class-mysql-session.php +++ b/packages/mysql-proxy/src/class-mysql-session.php @@ -661,7 +661,14 @@ private function build_result_set_packets( array $columns, array $rows, int $aff // Rows. foreach ( $rows as $row ) { - $packets .= MySQL_Protocol::build_row_packet( $this->packet_id++, $columns, $row ); + $next_packet_id = $this->packet_id; + $packets .= MySQL_Protocol::build_row_packet( + $this->packet_id, + $columns, + $row, + $next_packet_id + ); + $this->packet_id = $next_packet_id; } // OK or EOF packet, based on the CLIENT_DEPRECATE_EOF capability. diff --git a/packages/mysql-proxy/tests/WP_MySQL_Protocol_Packet_Test.php b/packages/mysql-proxy/tests/WP_MySQL_Protocol_Packet_Test.php new file mode 100644 index 000000000..a784927bc --- /dev/null +++ b/packages/mysql-proxy/tests/WP_MySQL_Protocol_Packet_Test.php @@ -0,0 +1,89 @@ +iniSet( 'memory_limit', '512M' ); + $this->expectException( MySQL_Proxy_Exception::class ); + MySQL_Protocol::build_packet( 0, str_repeat( 'x', MySQL_Protocol::MAX_PACKET_LENGTH + 1 ) ); + } + + public function test_build_packet_allows_exact_max_packet_length_wire_frame(): void { + $this->iniSet( 'memory_limit', '512M' ); + $packet = MySQL_Protocol::build_packet( 7, str_repeat( 'a', MySQL_Protocol::MAX_PACKET_LENGTH ) ); + $this->assertSame( 4 + MySQL_Protocol::MAX_PACKET_LENGTH, strlen( $packet ) ); + $this->assertSame( "\xff\xff\xff\x07", substr( $packet, 0, 4 ) ); + } + + public function test_build_packets_splits_boundary_payloads_and_wraps_sequence_id(): void { + $this->iniSet( 'memory_limit', '512M' ); + + // Just below MAX_PACKET_LENGTH: single packet. + $sequence_id = 3; + $payload = str_repeat( 'a', MySQL_Protocol::MAX_PACKET_LENGTH - 1 ); + $frames = $this->decode_wire_packets( MySQL_Protocol::build_packets( $sequence_id, $payload ) ); + $this->assertSame( 4, $sequence_id ); + $this->assertCount( 1, $frames ); + $this->assertSame( 3, $frames[0]['sequence_id'] ); + $this->assertSame( MySQL_Protocol::MAX_PACKET_LENGTH - 1, $frames[0]['length'] ); + + // Exact MAX_PACKET_LENGTH: full packet followed by an empty terminating packet. + $sequence_id = 255; + $payload = str_repeat( 'b', MySQL_Protocol::MAX_PACKET_LENGTH ); + $frames = $this->decode_wire_packets( MySQL_Protocol::build_packets( $sequence_id, $payload ) ); + $this->assertSame( 1, $sequence_id ); + $this->assertCount( 2, $frames ); + $this->assertSame( 255, $frames[0]['sequence_id'] ); + $this->assertSame( MySQL_Protocol::MAX_PACKET_LENGTH, $frames[0]['length'] ); + $this->assertSame( 0, $frames[1]['sequence_id'] ); + $this->assertSame( 0, $frames[1]['length'] ); + + // MAX_PACKET_LENGTH + 1: full packet followed by a 1-byte packet. + $sequence_id = 10; + $payload = str_repeat( 'c', MySQL_Protocol::MAX_PACKET_LENGTH ) . 'Z'; + $frames = $this->decode_wire_packets( MySQL_Protocol::build_packets( $sequence_id, $payload ) ); + $this->assertSame( 12, $sequence_id ); + $this->assertCount( 2, $frames ); + $this->assertSame( 10, $frames[0]['sequence_id'] ); + $this->assertSame( MySQL_Protocol::MAX_PACKET_LENGTH, $frames[0]['length'] ); + $this->assertSame( 11, $frames[1]['sequence_id'] ); + $this->assertSame( 1, $frames[1]['length'] ); + $this->assertSame( 'Z', $frames[1]['payload'] ); + + // Exact 2 * MAX_PACKET_LENGTH: two full packets followed by an empty packet. + $sequence_id = 254; + $payload = str_repeat( 'd', 2 * MySQL_Protocol::MAX_PACKET_LENGTH ); + $frames = $this->decode_wire_packets( MySQL_Protocol::build_packets( $sequence_id, $payload ) ); + $this->assertSame( 1, $sequence_id ); + $this->assertCount( 3, $frames ); + $this->assertSame( 254, $frames[0]['sequence_id'] ); + $this->assertSame( MySQL_Protocol::MAX_PACKET_LENGTH, $frames[0]['length'] ); + $this->assertSame( 255, $frames[1]['sequence_id'] ); + $this->assertSame( MySQL_Protocol::MAX_PACKET_LENGTH, $frames[1]['length'] ); + $this->assertSame( 0, $frames[2]['sequence_id'] ); + $this->assertSame( 0, $frames[2]['length'] ); + } + + private function decode_wire_packets( string $bytes ): array { + $frames = array(); + $offset = 0; + $total = strlen( $bytes ); + while ( $offset < $total ) { + $this->assertGreaterThan( $offset + 3, $total ); + $length = unpack( 'V', substr( $bytes, $offset, 3 ) . "\0" )[1]; + $sequence_id = ord( $bytes[ $offset + 3 ] ); + $offset += 4; + $frames[] = array( + 'length' => $length, + 'sequence_id' => $sequence_id, + 'payload' => $length <= 16 ? substr( $bytes, $offset, $length ) : null, + ); + $offset += $length; + } + $this->assertSame( $total, $offset ); + return $frames; + } +} diff --git a/packages/mysql-proxy/tests/WP_MySQL_Proxy_PDO_Test.php b/packages/mysql-proxy/tests/WP_MySQL_Proxy_PDO_Test.php index f88a9a3fb..5f2feb9b5 100644 --- a/packages/mysql-proxy/tests/WP_MySQL_Proxy_PDO_Test.php +++ b/packages/mysql-proxy/tests/WP_MySQL_Proxy_PDO_Test.php @@ -63,4 +63,26 @@ public function test_prepared_statement(): void { $stmt->fetchAll( PDO::FETCH_ASSOC ) ); } + + public function test_large_result_row(): void { + $this->iniSet( 'memory_limit', '512M' ); + + $this->pdo->exec( 'CREATE TABLE t (v LONGTEXT)' ); + $this->pdo->exec( "INSERT INTO t (v) VALUES ('" . str_repeat( 'a', 1048576 ) . "')" ); + for ( $i = 0; $i < 4; $i++ ) { + $this->pdo->exec( 'UPDATE t SET v = CONCAT(v, v)' ); + } + + // 16,777,211 bytes + 4-byte 0xfd length prefix = 16,777,215 (exact MAX_PACKET_LENGTH). + $exact = $this->pdo->query( 'SELECT SUBSTRING(v, 1, 16777211) FROM t' )->fetchColumn(); + $this->assertSame( 16777211, strlen( $exact ) ); + + // 16,777,220 bytes + 9-byte 0xfe length prefix > MAX_PACKET_LENGTH. + $over = $this->pdo->query( "SELECT CONCAT(v, 'tail') FROM t" )->fetchColumn(); + $this->assertSame( 16777220, strlen( $over ) ); + $this->assertSame( 'aatail', substr( $over, -6 ) ); + + // Verify the connection remains synchronized for subsequent queries. + $this->assertEquals( 1, $this->pdo->query( 'SELECT 1' )->fetchColumn() ); + } } diff --git a/packages/mysql-proxy/tests/WP_MySQL_Session_Command_Test.php b/packages/mysql-proxy/tests/WP_MySQL_Session_Command_Test.php index 02bec81c4..dd91f9b15 100644 --- a/packages/mysql-proxy/tests/WP_MySQL_Session_Command_Test.php +++ b/packages/mysql-proxy/tests/WP_MySQL_Session_Command_Test.php @@ -211,6 +211,89 @@ public function test_failed_login_reports_its_close_reason(): void { $this->assertSame( MySQL_Session::CLOSE_LOGIN_FAILED, $session->get_close_reason() ); } + public function test_large_result_rows_are_split_across_packets(): void { + $this->iniSet( 'memory_limit', '512M' ); + + // 16,777,211 bytes + 4-byte 0xfd length prefix = 16,777,215 (exact MAX_PACKET_LENGTH). + $exact_value = str_repeat( 'a', MySQL_Protocol::MAX_PACKET_LENGTH - 4 ); + + // 16,777,212 bytes + 4-byte 0xfd length prefix = 16,777,216 (MAX_PACKET_LENGTH + 1). + $over_value = str_repeat( 'b', MySQL_Protocol::MAX_PACKET_LENGTH - 4 ) . 'Z'; + + $column = array( + 'name' => 'v', + 'length' => MySQL_Protocol::MAX_PACKET_LENGTH + 16, + 'type' => MySQL_Protocol::FIELD_TYPE_LONG_BLOB, + 'flags' => 0, + 'decimals' => 0, + ); + $rows = array( + (object) array( 'v' => $exact_value ), + (object) array( 'v' => $over_value ), + ); + $result = MySQL_Result::from_data( 0, 0, array( $column ), $rows ); + $adapter = new class( $result ) implements Adapter { + /** @var MySQL_Result */ + private $result; + + public function __construct( MySQL_Result $result ) { + $this->result = $result; + } + + public function handle_query( string $query ): MySQL_Result { + return $this->result; + } + }; + + $session = $this->authenticated_session( $adapter ); + $response = $session->receive_bytes( + MySQL_Protocol::build_packet( 0, chr( MySQL_Protocol::COM_QUERY ) . 'SELECT v FROM t' ) + ); + + // Reassemble split MySQL logical messages from the wire stream. + $messages = array(); + $wire_sequence_ids = array(); + $offset = 0; + $total = strlen( $response ); + $current_message = ''; + $expected_sequence = 1; + while ( $offset < $total ) { + $length = unpack( 'V', substr( $response, $offset, 3 ) . "\0" )[1]; + $sequence_id = ord( $response[ $offset + 3 ] ); + $this->assertSame( $expected_sequence, $sequence_id ); + $expected_sequence = ( $expected_sequence + 1 ) & 0xff; + $wire_sequence_ids[] = $sequence_id; + $current_message .= substr( $response, $offset + 4, $length ); + $offset += 4 + $length; + if ( MySQL_Protocol::MAX_PACKET_LENGTH !== $length ) { + $messages[] = $current_message; + $current_message = ''; + } + } + + // Wire packets without CLIENT_DEPRECATE_EOF: + // 1=column count, 2=column def, 3=columns EOF, 4+5=row 1 (0xffffff + 0), 6+7=row 2 (0xffffff + 1), 8=rows EOF. + $this->assertSame( array( 1, 2, 3, 4, 5, 6, 7, 8 ), $wire_sequence_ids ); + $this->assertCount( 6, $messages ); + $this->assertSame( MySQL_Protocol::EOF_PACKET_HEADER, ord( $messages[2][0] ) ); + + $row1_offset = 0; + $this->assertSame( $exact_value, MySQL_Protocol::read_length_encoded_string( $messages[3], $row1_offset ) ); + $this->assertSame( strlen( $messages[3] ), $row1_offset ); + + $row2_offset = 0; + $this->assertSame( $over_value, MySQL_Protocol::read_length_encoded_string( $messages[4], $row2_offset ) ); + $this->assertSame( strlen( $messages[4] ), $row2_offset ); + + // Trailing packet is the EOF terminator (0xfe), not swallowed into row 1 or row 2. + $this->assertSame( MySQL_Protocol::EOF_PACKET_HEADER, ord( $messages[5][0] ) ); + + // A subsequent command on the same session remains synchronized. + $ping_response = $session->receive_bytes( MySQL_Protocol::build_packet( 0, chr( MySQL_Protocol::COM_PING ) ) ); + $this->assertSame( 1, ord( $ping_response[3] ) ); + $this->assertSame( MySQL_Protocol::OK_PACKET_HEADER, ord( $ping_response[4] ) ); + } + private function authenticated_session( Adapter $adapter, ?Logger $logger = null ): MySQL_Session { $session = new MySQL_Session( $adapter, 1, new MySQL_Authentication( 'user', 'password' ), $logger ); $challenge = MySQL_Test_Client::challenge( $session->get_initial_handshake() );