From 040d5a6f9163328dbc0407b9f30d02f663e8e92a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 24 Apr 2026 23:16:46 +0200 Subject: [PATCH 1/5] Add tests for read_quoted_text() bounds checking Add tests to cover the backslash-counting loop in read_quoted_text(). These test cases expose an out-of-bounds string access triggered by unclosed quoted strings with trailing backslashes. Test cases: - Unclosed strings with odd/even trailing backslashes (single/double quotes) - Regression tests for valid escaped strings, doubled quotes, and backticks --- .../tests/mysql/WP_MySQL_Lexer_Tests.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php b/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php index 574249d03..05ff047a0 100644 --- a/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php +++ b/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php @@ -258,6 +258,73 @@ public function data_identifier_or_number(): array { ); } + /** + * Test that unclosed quoted strings with trailing backslashes do not + * cause out-of-bounds string access in read_quoted_text(). + * + * The backslash-counting loop walks backward from the position returned + * by strcspn(). When the closing quote is missing, strcspn() returns + * the remaining string length. If the last byte is a backslash, the + * loop treats the absent quote as escaped and advances past the end + * of the string. On the next iteration, the loop accesses an invalid + * string offset, triggering "Uninitialized string offset" warnings. + * + * @dataProvider data_unclosed_strings_with_backslashes + */ + public function test_unclosed_string_with_trailing_backslash( string $sql ): void { + set_error_handler( + function ( $severity, $message, $file, $line ) { + throw new \ErrorException( $message, 0, $severity, $file, $line ); + }, + E_WARNING | E_NOTICE + ); + + try { + $lexer = new WP_MySQL_Lexer( $sql ); + while ( $lexer->next_token() ) { + // Consume all tokens. + } + } finally { + restore_error_handler(); + } + + // If we reach here without an ErrorException, no OOB access occurred. + $this->assertNull( $lexer->get_token() ); + } + + public function data_unclosed_strings_with_backslashes(): array { + return array( + 'single-quoted trailing backslash' => array( "SELECT '\\" ), + 'double-quoted trailing backslash' => array( 'SELECT "\\' ), + 'even trailing backslashes' => array( "SELECT '\\\\" ), + 'odd trailing backslashes' => array( "SELECT '\\\\\\" ), + 'backslash-only single-quoted' => array( "'\\" ), + 'backslash-only double-quoted' => array( '"\\' ), + ); + } + + /** + * Regression: valid strings with escapes must still tokenize correctly. + * + * @dataProvider data_valid_escaped_strings + */ + public function test_valid_escaped_string( string $sql, int $expected_token_id ): void { + $lexer = new WP_MySQL_Lexer( $sql ); + $this->assertTrue( $lexer->next_token() ); + $this->assertSame( $expected_token_id, $lexer->get_token()->id ); + } + + public function data_valid_escaped_strings(): array { + return array( + 'escaped single quote' => array( "'it\\'s'", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ), + 'trailing escaped backslash' => array( "'path\\\\'", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ), + 'doubled single quote' => array( "'it''s'", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ), + 'empty single-quoted string' => array( "''", WP_MySQL_Lexer::SINGLE_QUOTED_TEXT ), + 'escaped double quote' => array( '"col\\"name"', WP_MySQL_Lexer::DOUBLE_QUOTED_TEXT ), + 'backtick identifier' => array( '`my_column`', WP_MySQL_Lexer::BACK_TICK_QUOTED_ID ), + ); + } + private function get_token_names( array $token_types ): array { return array_map( function ( $token_type ) { From f8bd020424e83942e0895b5dbc2853d0fec534d0 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 24 Apr 2026 23:17:26 +0200 Subject: [PATCH 2/5] Fix out-of-bounds access in read_quoted_text() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The backslash-counting loop in read_quoted_text() ran before the EOF check. When strcspn() reached the end of the string without finding a closing quote, the loop accessed offsets past the string boundary: 1. strcspn() sets $at = strlen($sql) (no quote found). 2. Backslash check finds '\' at $at-1 (last byte), counts it ($i=1). 3. Odd count → treats absent quote as escaped, does $at += 1 (past end). 4. Next iteration: strcspn returns 0, $at stays past end. 5. Backslash check accesses $this->sql[strlen($sql)] → PHP warning. Fix: move the EOF check before the backslash-counting loop so unclosed strings are detected immediately. Also add a lower-bound guard to the backward walk to prevent underflow when a quote appears early in the string. --- .../src/mysql/class-wp-mysql-lexer.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php b/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php index b9f582c3a..b84dbe2e0 100644 --- a/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php +++ b/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php @@ -2842,6 +2842,11 @@ private function read_quoted_text(): ?int { while ( true ) { $at += strcspn( $this->sql, $quote, $at ); + // Unclosed string - unexpected EOF. + if ( ( $this->sql[ $at ] ?? null ) !== $quote ) { + return null; // Invalid input. + } + /* * By default, quotes can be escaped with a "\". * When NO_BACKSLASH_ESCAPES SQL mode is active, the "\" treated as @@ -2852,18 +2857,13 @@ private function read_quoted_text(): ?int { * "\\\" is an escaped backslash and an escape sequence, and so on. */ if ( ! $no_backslash_escapes ) { - for ($i = 0; '\\' === $this->sql[ $at - $i - 1 ]; $i += 1); + for ( $i = 0; ( $at - $i - 1 ) >= 0 && '\\' === $this->sql[ $at - $i - 1 ]; $i += 1 ); if ( 1 === $i % 2 ) { $at += 1; continue; } } - // Unclosed string - unexpected EOF. - if ( ( $this->sql[ $at ] ?? null ) !== $quote ) { - return null; // Invalid input. - } - // Check if the quote is doubled. if ( ( $this->sql[ $at + 1 ] ?? null ) === $quote ) { $at += 2; From 23b4581afed934529e83f3bccd8ac9e28f5e8447 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 24 Apr 2026 23:51:54 +0200 Subject: [PATCH 3/5] Add chunk boundary test for read_quoted_text() OOB Add a test that simulates the real-world trigger: a streaming SQL processor splitting input at a chunk boundary that falls inside a quoted string, with a backslash as the last byte. This is the exact scenario from MySQL dump processing where FROM_BASE64() is replaced with regular escaped string literals. --- .../tests/mysql/WP_MySQL_Lexer_Tests.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php b/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php index 05ff047a0..d90dd3ea6 100644 --- a/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php +++ b/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php @@ -325,6 +325,48 @@ public function data_valid_escaped_strings(): array { ); } + /** + * Test that a chunk boundary splitting a quoted string with a trailing + * backslash does not cause an out-of-bounds string access. + * + * This simulates streaming SQL processing where a buffer boundary falls + * inside a string literal right after a backslash escape character. + */ + public function test_chunk_boundary_inside_escaped_string(): void { + set_error_handler( + function ( $severity, $message, $file, $line ) { + throw new \ErrorException( $message, 0, $severity, $file, $line ); + }, + E_WARNING | E_NOTICE + ); + + try { + // Build a SQL string where a backslash falls at the chunk boundary. + // The string content before the boundary is padded to place the + // backslash at exactly position $chunk_size - 1. + $chunk_size = 8192; + + // "SELECT '" = 8 bytes, so we need chunk_size - 8 - 1 bytes of + // padding before the trailing backslash to place '\' at the last + // byte of the chunk. + $padding = str_repeat( 'A', $chunk_size - 8 - 1 ); + $sql = "SELECT '" . $padding . "\\"; + + // The chunk is exactly $chunk_size bytes. The last byte is '\'. + // The lexer should handle this as an unclosed string without OOB. + $this->assertSame( $chunk_size, strlen( $sql ) ); + + $lexer = new WP_MySQL_Lexer( $sql ); + while ( $lexer->next_token() ) { + // Consume all tokens. + } + } finally { + restore_error_handler(); + } + + $this->assertNull( $lexer->get_token() ); + } + private function get_token_names( array $token_types ): array { return array_map( function ( $token_type ) { From 432b78cb7147ef02b00390aa843963a4a0e51489 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 27 Apr 2026 09:54:50 +0200 Subject: [PATCH 4/5] Fix quoting lint --- packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php b/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php index d90dd3ea6..8f18cf170 100644 --- a/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php +++ b/packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php @@ -350,7 +350,7 @@ function ( $severity, $message, $file, $line ) { // padding before the trailing backslash to place '\' at the last // byte of the chunk. $padding = str_repeat( 'A', $chunk_size - 8 - 1 ); - $sql = "SELECT '" . $padding . "\\"; + $sql = "SELECT '" . $padding . '\\'; // The chunk is exactly $chunk_size bytes. The last byte is '\'. // The lexer should handle this as an unclosed string without OOB. From 10e77964cdd5ac0c002d9dfa7259cbec4aa27940 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Mon, 27 Apr 2026 09:56:09 +0200 Subject: [PATCH 5/5] Remove stale param doc --- packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php b/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php index b84dbe2e0..10ecd90ad 100644 --- a/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php +++ b/packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php @@ -2825,8 +2825,6 @@ private function read_number(): ?int { * Rules: * 1. Quotes can be escaped by doubling them ('', "", ``). * 2. Backslashes escape the next character, unless NO_BACKSLASH_ESCAPES is set. - * - * @param string $quote The quote character - ', ", or `. */ private function read_quoted_text(): ?int { $quote = $this->sql[ $this->bytes_already_read ];