Skip to content

Commit 3295335

Browse files
committed
Treat backslashes literally in quoted identifiers
MySQL does not process backslash escape sequences inside quoted identifiers; the bounding quote is escaped only by doubling it. The lexer was applying string-literal backslash escaping to backtick identifiers (and, with the new ANSI_QUOTES support, to double-quoted identifiers), both when scanning for the closing quote and when unquoting the value. As a result, an identifier like `a\nb` resolved to "a<newline>b", and `a\` (a trailing backslash) failed to tokenize because the backslash was treated as escaping the closing quote. Restrict backslash escaping to string literals so identifiers preserve backslashes verbatim. See: https://dev.mysql.com/doc/refman/8.4/en/identifiers.html
1 parent f51a0e3 commit 3295335

4 files changed

Lines changed: 85 additions & 30 deletions

File tree

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,15 +2904,26 @@ private function read_number(): ?int {
29042904
*
29052905
* Rules:
29062906
* 1. Quotes can be escaped by doubling them ('', "", ``).
2907-
* 2. Backslashes escape the next character, unless NO_BACKSLASH_ESCAPES is set.
2907+
* 2. In string literals, backslashes escape the next character, unless the
2908+
* NO_BACKSLASH_ESCAPES SQL mode is set. In quoted identifiers, backslashes
2909+
* are always literal; only doubling the quote escapes it.
29082910
*/
29092911
private function read_quoted_text(): ?int {
29102912
$quote = $this->sql[ $this->bytes_already_read ];
29112913
$this->bytes_already_read += 1; // Consume the quote.
29122914

2913-
$no_backslash_escapes = $this->is_sql_mode_active(
2914-
self::SQL_MODE_NO_BACKSLASH_ESCAPES
2915-
);
2915+
/*
2916+
* A backtick always quotes an identifier, and a double quote quotes an
2917+
* identifier under ANSI_QUOTES; otherwise it opens a string literal.
2918+
* Backslash escapes apply only to string literals, and only when the
2919+
* NO_BACKSLASH_ESCAPES SQL mode is not set.
2920+
*
2921+
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi_quotes
2922+
*/
2923+
$is_identifier_quote = '`' === $quote
2924+
|| ( '"' === $quote && $this->is_sql_mode_active( self::SQL_MODE_ANSI_QUOTES ) );
2925+
$backslash_is_escape = ! $is_identifier_quote
2926+
&& ! $this->is_sql_mode_active( self::SQL_MODE_NO_BACKSLASH_ESCAPES );
29162927

29172928
// We need to look for the closing quote in a loop, as it can be escaped,
29182929
// in which case the escape sequence is consumed and the loop continues.
@@ -2925,9 +2936,10 @@ private function read_quoted_text(): ?int {
29252936
$at = $quote_at;
29262937

29272938
/*
2928-
* By default, quotes can be escaped with a "\".
2929-
* When NO_BACKSLASH_ESCAPES SQL mode is active, the "\" treated as
2930-
* a regular character.
2939+
* In string literals, a quote can be escaped with a "\", unless the
2940+
* NO_BACKSLASH_ESCAPES SQL mode is active, in which case the "\" is
2941+
* treated as a regular character. Quoted identifiers never use
2942+
* backslash escaping.
29312943
*
29322944
* The quote is escaped only when the number of preceding backslashes
29332945
* is odd - "\" is an escape sequence, "\\" is an escaped backslash,
@@ -2938,7 +2950,7 @@ private function read_quoted_text(): ?int {
29382950
* sits at the very start of the input. The `?? null` covers
29392951
* positive out-of-range indexes belt-and-suspenders.
29402952
*/
2941-
if ( ! $no_backslash_escapes ) {
2953+
if ( $backslash_is_escape ) {
29422954
$i = 0;
29432955
while ( ( $at - $i - 1 ) >= 0 && '\\' === ( $this->sql[ $at - $i - 1 ] ?? null ) ) {
29442956
$i += 1;
@@ -2961,22 +2973,11 @@ private function read_quoted_text(): ?int {
29612973

29622974
$this->bytes_already_read = $at;
29632975

2964-
if ( '`' === $quote ) {
2976+
// Identifiers (backtick, or double quote under ANSI_QUOTES) share a token type.
2977+
if ( $is_identifier_quote ) {
29652978
return self::BACK_TICK_QUOTED_ID;
2966-
} elseif ( '"' === $quote ) {
2967-
/*
2968-
* With the ANSI_QUOTES SQL mode active, a double-quoted sequence is a
2969-
* quoted identifier rather than a string literal, so it is emitted as
2970-
* a backtick-quoted identifier token.
2971-
*
2972-
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi_quotes
2973-
*/
2974-
return $this->is_sql_mode_active( self::SQL_MODE_ANSI_QUOTES )
2975-
? self::BACK_TICK_QUOTED_ID
2976-
: self::DOUBLE_QUOTED_TEXT;
2977-
} else {
2978-
return self::SINGLE_QUOTED_TEXT;
29792979
}
2980+
return '"' === $quote ? self::DOUBLE_QUOTED_TEXT : self::SINGLE_QUOTED_TEXT;
29802981
}
29812982

29822983
private function read_line_comment(): int {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ public function get_value(): string {
7272
$value = substr( $value, 1, -1 );
7373

7474
/*
75-
* When the NO_BACKSLASH_ESCAPES SQL mode is enabled, we only need to
76-
* handle escaped bounding quotes, as the other characters preserve
77-
* their literal values.
75+
* Quoted identifiers never use backslash escaping, and when the
76+
* NO_BACKSLASH_ESCAPES SQL mode is enabled string literals don't
77+
* either. In both cases, we only need to handle escaped bounding
78+
* quotes, as the other characters preserve their literal values.
7879
*/
79-
if ( $this->sql_mode_no_backslash_escapes_enabled ) {
80+
if (
81+
WP_MySQL_Lexer::BACK_TICK_QUOTED_ID === $this->id
82+
|| $this->sql_mode_no_backslash_escapes_enabled
83+
) {
8084
return str_replace( $quote . $quote, $quote, $value );
8185
}
8286

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,52 @@ public function test_composite_ansi_mode_expands_to_lexer_component_modes(): voi
477477
$this->assertSame( WP_MySQL_Lexer::COUNT_SYMBOL, $lexer->get_token()->id );
478478
}
479479

480+
/**
481+
* MySQL does not process backslash escape sequences inside quoted identifiers;
482+
* the bounding quote is escaped only by doubling it. This applies to backtick
483+
* identifiers and, under ANSI_QUOTES, to double-quoted identifiers. String
484+
* literals are unaffected and keep their usual backslash escaping.
485+
*
486+
* @dataProvider data_quoted_identifier_backslash
487+
*/
488+
public function test_quoted_identifiers_treat_backslash_literally(
489+
string $sql,
490+
array $sql_modes,
491+
int $expected_token_id,
492+
string $expected_value
493+
): void {
494+
$lexer = new WP_MySQL_Lexer( $sql, 80038, $sql_modes );
495+
$this->assertTrue( $lexer->next_token() );
496+
497+
$token = $lexer->get_token();
498+
$this->assertSame(
499+
WP_MySQL_Lexer::get_token_name( $expected_token_id ),
500+
$token->get_name(),
501+
$sql
502+
);
503+
$this->assertSame( $expected_value, $token->get_value(), $sql );
504+
}
505+
506+
public function data_quoted_identifier_backslash(): array {
507+
$bs = chr( 92 ); // A single backslash.
508+
$bt = '`';
509+
$dq = '"';
510+
$sq = "'";
511+
return array(
512+
// Backtick identifiers: backslash is a literal character.
513+
'backtick keeps backslash-n literal' => array( $bt . 'a' . $bs . 'nb' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
514+
'backtick allows trailing backslash' => array( $bt . 'a' . $bs . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
515+
'backtick escapes only by doubling' => array( $bt . 'a' . $bt . $bt . 'b' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bt . 'b' ),
516+
517+
// ANSI-quoted identifiers behave like backtick identifiers.
518+
'ansi-quoted keeps backslash literal' => array( $dq . 'a' . $bs . 'nb' . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
519+
'ansi-quoted allows trailing backslash' => array( $dq . 'a' . $bs . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
520+
521+
// String literals still process backslash escapes.
522+
'single-quoted string escapes newline' => array( $sq . 'a' . $bs . 'nb' . $sq, array(), WP_MySQL_Lexer::SINGLE_QUOTED_TEXT, "a\nb" ),
523+
);
524+
}
525+
480526
private function get_token_names( array $token_types ): array {
481527
return array_map(
482528
function ( $token_type ) {

‎packages/php-ext-wp-mysql-parser/src/lib.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -784,13 +784,16 @@ impl WpMySqlNativeLexer {
784784
fn read_quoted_text(&mut self) -> Option<i64> {
785785
let quote = self.byte_at(self.bytes_already_read)?;
786786
self.bytes_already_read += 1;
787-
let no_backslash_escapes = self.is_sql_mode_active(SQL_MODE_NO_BACKSLASH_ESCAPES);
787+
let is_identifier_quote =
788+
quote == b'`' || (quote == b'"' && self.is_sql_mode_active(SQL_MODE_ANSI_QUOTES));
789+
let backslash_is_escape =
790+
!is_identifier_quote && !self.is_sql_mode_active(SQL_MODE_NO_BACKSLASH_ESCAPES);
788791
let mut at = self.bytes_already_read;
789792

790793
loop {
791794
at = span_until(&self.sql, at, &[quote]);
792795

793-
if !no_backslash_escapes {
796+
if backslash_is_escape {
794797
let mut i = 0usize;
795798
while at > i && self.byte_at(at - i - 1) == Some(b'\\') {
796799
i += 1;
@@ -816,9 +819,10 @@ impl WpMySqlNativeLexer {
816819
}
817820

818821
self.bytes_already_read = at + 1;
822+
if is_identifier_quote {
823+
return Some(lex::BACK_TICK_QUOTED_ID);
824+
}
819825
Some(match quote {
820-
b'`' => lex::BACK_TICK_QUOTED_ID,
821-
b'"' if self.is_sql_mode_active(SQL_MODE_ANSI_QUOTES) => lex::BACK_TICK_QUOTED_ID,
822826
b'"' => lex::DOUBLE_QUOTED_TEXT,
823827
_ => lex::SINGLE_QUOTED_TEXT,
824828
})

0 commit comments

Comments
 (0)