Skip to content

Commit c5d9ab6

Browse files
committed
Make WP_MySQL_Token::get_value() binary-safe for string literals
The final backslash-stripping step used preg_replace() with the "u" (UTF-8) modifier. That modifier makes PCRE validate the whole subject as UTF-8 and return null on the first invalid byte; since get_value() is typed ": string", the null turned into a fatal TypeError. MySQL string literals may legitimately carry non-UTF-8 bytes (binary or other-charset payloads), and the lexer scans them at the byte level, so reading the value of such a literal crashed. Switch the modifier to "s" (DOTALL). A byte-wise strip is binary-safe, yields identical results for valid UTF-8 (no continuation byte is a backslash), and additionally handles a backslash preceding a newline byte.
1 parent bf181a2 commit c5d9ab6

5 files changed

Lines changed: 66 additions & 17 deletions

File tree

‎.github/workflows/wp-tests-phpunit-run.js‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const path = require( 'path' );
1212
const requiresNativeParserExtension = process.env.WP_SQLITE_REQUIRE_NATIVE_PARSER_EXTENSION === '1';
1313

1414
const expectedErrors = [
15-
'Tests_DB_Charset::test_invalid_characters_in_query',
1615
'Tests_DB_Charset::test_set_charset_changes_the_connection_collation',
1716
];
1817

@@ -38,20 +37,7 @@ const expectedFailures = [
3837
'Tests_DB_Charset::test_get_column_charset_non_mysql with data set #5',
3938
'Tests_DB_Charset::test_get_column_charset_non_mysql with data set #6',
4039
'Tests_DB_Charset::test_get_column_charset_non_mysql with data set #7',
41-
'Tests_DB_Charset::test_strip_invalid_text with data set #21',
42-
'Tests_DB_Charset::test_strip_invalid_text with data set #22',
43-
'Tests_DB_Charset::test_strip_invalid_text with data set #23',
44-
'Tests_DB_Charset::test_strip_invalid_text with data set #24',
45-
'Tests_DB_Charset::test_strip_invalid_text with data set #25',
46-
'Tests_DB_Charset::test_strip_invalid_text with data set #26',
47-
'Tests_DB_Charset::test_strip_invalid_text with data set #27',
48-
'Tests_DB_Charset::test_strip_invalid_text with data set #28',
49-
'Tests_DB_Charset::test_strip_invalid_text with data set #30',
50-
'Tests_DB_Charset::test_strip_invalid_text with data set #31',
51-
'Tests_DB_Charset::test_strip_invalid_text with data set #32',
52-
'Tests_DB_Charset::test_strip_invalid_text with data set #33',
53-
'Tests_DB_Charset::test_strip_invalid_text with data set #34',
54-
'Tests_DB_Charset::test_strip_invalid_text with data set #39',
40+
'Tests_DB_Charset::test_invalid_characters_in_query',
5541
'Tests_DB_Charset::test_strip_invalid_text with data set #40',
5642
'Tests_DB_Charset::test_strip_invalid_text with data set #41',
5743
'Tests_DB_dbDelta::test_column_type_change',

‎packages/mysql-on-sqlite/src/mysql/class-wp-mysql-token.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,12 @@ public function get_value(): string {
172172
/*
173173
* A backslash with any other character represents the character itself.
174174
* That is, \x evaluates to x, \\ evaluates to \, and \🙂 evaluates to 🙂.
175+
*
176+
* Use "s" (DOTALL) to match all characters including newlines. Do not use
177+
* "u", so non-UTF-8 bytes are preserved (binary or other character sets).
175178
*/
176179
$preg_quoted_backslash = preg_quote( $backslash );
177-
$value = preg_replace( "/$preg_quoted_backslash(.)/u", '$1', $value );
180+
$value = preg_replace( "/$preg_quoted_backslash(.)/s", '$1', $value );
178181
}
179182
return $value;
180183
}

‎packages/mysql-on-sqlite/tests/mysql/WP_MySQL_Lexer_Tests.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,30 @@ public function data_valid_escaped_strings(): array {
325325
);
326326
}
327327

328+
public function test_get_value_preserves_non_utf8_bytes_in_string_literals(): void {
329+
// A quoted literal may legitimately carry non-UTF-8 bytes (binary or
330+
// other-charset payloads). Value extraction must return them unchanged.
331+
$raw = chr( 0xFF ) . chr( 0xFE );
332+
$tokens = ( new WP_MySQL_Lexer( "SELECT '$raw'" ) )->remaining_tokens();
333+
$this->assertSame( $raw, $tokens[1]->get_value() );
334+
335+
// A backslash escape strips the backslash and preserves the following non-UTF-8 byte.
336+
$tokens = ( new WP_MySQL_Lexer( "SELECT '\\" . chr( 0xE9 ) . "'" ) )->remaining_tokens();
337+
$this->assertSame( chr( 0xE9 ), $tokens[1]->get_value() );
338+
339+
// Valid multibyte UTF-8 still round-trips.
340+
$tokens = ( new WP_MySQL_Lexer( "SELECT 'café 🙂'" ) )->remaining_tokens();
341+
$this->assertSame( 'café 🙂', $tokens[1]->get_value() );
342+
}
343+
344+
public function test_get_value_unescapes_newlines_and_multibyte_characters(): void {
345+
$tokens = ( new WP_MySQL_Lexer( "SELECT '\\\n'" ) )->remaining_tokens();
346+
$this->assertSame( "\n", $tokens[1]->get_value() );
347+
348+
$tokens = ( new WP_MySQL_Lexer( "SELECT '\\🙂'" ) )->remaining_tokens();
349+
$this->assertSame( '🙂', $tokens[1]->get_value() );
350+
}
351+
328352
/**
329353
* Test that a chunk boundary splitting a quoted string with a trailing
330354
* backslash does not cause an out-of-bounds string access.

‎packages/mysql-parser/src/class-wp-mysql-token.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,12 @@ public function get_value(): string {
170170
/*
171171
* A backslash with any other character represents the character itself.
172172
* That is, \x evaluates to x, \\ evaluates to \, and \🙂 evaluates to 🙂.
173+
*
174+
* Use "s" (DOTALL) to match all characters including newlines. Do not use
175+
* "u", so non-UTF-8 bytes are preserved (binary or other character sets).
173176
*/
174177
$preg_quoted_backslash = preg_quote( $backslash );
175-
$value = preg_replace( "/$preg_quoted_backslash(.)/u", '$1', $value );
178+
$value = preg_replace( "/$preg_quoted_backslash(.)/s", '$1', $value );
176179
}
177180
return $value;
178181
}

‎packages/mysql-parser/tests/WP_MySQL_Token_Tests.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@ public function test_get_value_honors_no_backslash_escapes_mode(): void {
3434
$this->assertSame( 'a\\nb', $token->get_value() );
3535
}
3636

37+
public function test_get_value_preserves_non_utf8_bytes_in_string_literals(): void {
38+
// A quoted literal may legitimately carry non-UTF-8 bytes (binary or
39+
// other-charset payloads). Value extraction must return them unchanged.
40+
$raw = chr( 0xFF ) . chr( 0xFE );
41+
$this->assertSame(
42+
$raw,
43+
self::first_token( "SELECT '$raw'", 'SINGLE_QUOTED_TEXT' )->get_value()
44+
);
45+
46+
// A backslash escape strips the backslash and preserves the following non-UTF-8 byte.
47+
$this->assertSame(
48+
chr( 0xE9 ),
49+
self::first_token( "SELECT '\\" . chr( 0xE9 ) . "'", 'SINGLE_QUOTED_TEXT' )->get_value()
50+
);
51+
52+
// Valid multibyte UTF-8 still round-trips.
53+
$this->assertSame(
54+
'café 🙂',
55+
self::first_token( "SELECT 'café 🙂'", 'SINGLE_QUOTED_TEXT' )->get_value()
56+
);
57+
}
58+
59+
public function test_get_value_unescapes_newlines_and_multibyte_characters(): void {
60+
$this->assertSame(
61+
"\n",
62+
self::first_token( "SELECT '\\\n'", 'SINGLE_QUOTED_TEXT' )->get_value()
63+
);
64+
$this->assertSame(
65+
'🙂',
66+
self::first_token( "SELECT '\\🙂'", 'SINGLE_QUOTED_TEXT' )->get_value()
67+
);
68+
}
69+
3770
public function test_get_value_unquotes_backtick_identifiers(): void {
3871
$this->assertSame( 'col name', self::first_token( 'SELECT `col name` FROM t', 'BACK_TICK_QUOTED_ID' )->get_value() );
3972
}

0 commit comments

Comments
 (0)