Skip to content

Commit 647bb25

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 d8fbcfa commit 647bb25

7 files changed

Lines changed: 152 additions & 46 deletions

File tree

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

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

2914-
$no_backslash_escapes = $this->is_sql_mode_active(
2915-
self::SQL_MODE_NO_BACKSLASH_ESCAPES
2916-
);
2916+
/*
2917+
* Determine whether the quote opens an identifier or a string literal.
2918+
* An identifier is quoted with a backtick or a double quote when the
2919+
* ANSI_QUOTES SQL mode is active. Otherwise, it is a string literal.
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+
2926+
/*
2927+
* Backslash escapes apply only to string literals, and only when the
2928+
* NO_BACKSLASH_ESCAPES SQL mode is not set.
2929+
*/
2930+
$backslash_is_escape = ! $is_identifier_quote
2931+
&& ! $this->is_sql_mode_active( self::SQL_MODE_NO_BACKSLASH_ESCAPES );
29172932

29182933
// We need to look for the closing quote in a loop, as it can be escaped,
29192934
// in which case the escape sequence is consumed and the loop continues.
@@ -2926,9 +2941,9 @@ private function read_quoted_text(): ?int {
29262941
$at = $quote_at;
29272942

29282943
/*
2929-
* By default, quotes can be escaped with a "\".
2930-
* When NO_BACKSLASH_ESCAPES SQL mode is active, the "\" treated as
2931-
* a regular character.
2944+
* In string literals, quotes can be escaped with a backslash. When
2945+
* NO_BACKSLASH_ESCAPES SQL mode is active, the backslash is treated
2946+
* as a regular character. Identifiers never use backslash escaping.
29322947
*
29332948
* The quote is escaped only when the number of preceding backslashes
29342949
* is odd - "\" is an escape sequence, "\\" is an escaped backslash,
@@ -2939,7 +2954,7 @@ private function read_quoted_text(): ?int {
29392954
* sits at the very start of the input. The `?? null` covers
29402955
* positive out-of-range indexes belt-and-suspenders.
29412956
*/
2942-
if ( ! $no_backslash_escapes ) {
2957+
if ( $backslash_is_escape ) {
29432958
$i = 0;
29442959
while ( ( $at - $i - 1 ) >= 0 && '\\' === ( $this->sql[ $at - $i - 1 ] ?? null ) ) {
29452960
$i += 1;
@@ -2962,22 +2977,10 @@ private function read_quoted_text(): ?int {
29622977

29632978
$this->bytes_already_read = $at;
29642979

2965-
if ( '`' === $quote ) {
2980+
if ( $is_identifier_quote ) {
29662981
return self::BACK_TICK_QUOTED_ID;
2967-
} elseif ( '"' === $quote ) {
2968-
/*
2969-
* With the ANSI_QUOTES SQL mode enabled, MySQL treats double quotes
2970-
* as identifier delimiters. Match this behavior by using the same token
2971-
* type as for backtick-quoted identifiers.
2972-
*
2973-
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi_quotes
2974-
*/
2975-
return $this->is_sql_mode_active( self::SQL_MODE_ANSI_QUOTES )
2976-
? self::BACK_TICK_QUOTED_ID
2977-
: self::DOUBLE_QUOTED_TEXT;
2978-
} else {
2979-
return self::SINGLE_QUOTED_TEXT;
29802982
}
2983+
return '"' === $quote ? self::DOUBLE_QUOTED_TEXT : self::SINGLE_QUOTED_TEXT;
29812984
}
29822985

29832986
private function read_line_comment(): int {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ 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+
* For quoted identifiers and when the NO_BACKSLASH_ESCAPES SQL mode
76+
* is active, we only need to handle escaped bounding quotes, as all
77+
* other characters preserve their literal values.
7878
*/
79-
if ( $this->sql_mode_no_backslash_escapes_enabled ) {
79+
if (
80+
WP_MySQL_Lexer::BACK_TICK_QUOTED_ID === $this->id
81+
|| $this->sql_mode_no_backslash_escapes_enabled
82+
) {
8083
return str_replace( $quote . $quote, $quote, $value );
8184
}
8285

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,49 @@ public function test_composite_ansi_mode_expands_to_lexer_component_modes(): voi
452452
$this->assertSame( WP_MySQL_Lexer::COUNT_SYMBOL, $lexer->get_token()->id );
453453
}
454454

455+
/**
456+
* Quoted identifiers escape delimiters by doubling, not with backslashes.
457+
*
458+
* @dataProvider data_quoted_identifier_backslash
459+
*/
460+
public function test_quoted_identifiers_treat_backslash_literally(
461+
string $sql,
462+
array $sql_modes,
463+
int $expected_token_id,
464+
string $expected_value
465+
): void {
466+
$lexer = new WP_MySQL_Lexer( $sql, 80038, $sql_modes );
467+
$this->assertTrue( $lexer->next_token() );
468+
469+
$token = $lexer->get_token();
470+
$this->assertSame(
471+
WP_MySQL_Lexer::get_token_name( $expected_token_id ),
472+
$token->get_name(),
473+
$sql
474+
);
475+
$this->assertSame( $expected_value, $token->get_value(), $sql );
476+
}
477+
478+
public function data_quoted_identifier_backslash(): array {
479+
$bs = chr( 92 ); // A single backslash.
480+
$bt = '`';
481+
$dq = '"';
482+
$sq = "'";
483+
return array(
484+
// Backtick identifiers: backslash is a literal character.
485+
'backtick keeps backslash-n literal' => array( $bt . 'a' . $bs . 'nb' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
486+
'backtick allows trailing backslash' => array( $bt . 'a' . $bs . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
487+
'backtick escapes only by doubling' => array( $bt . 'a' . $bt . $bt . 'b' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bt . 'b' ),
488+
489+
// ANSI-quoted identifiers behave like backtick identifiers.
490+
'ansi-quoted keeps backslash literal' => array( $dq . 'a' . $bs . 'nb' . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
491+
'ansi-quoted allows trailing backslash' => array( $dq . 'a' . $bs . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
492+
493+
// String literals still process backslash escapes.
494+
'single-quoted string escapes newline' => array( $sq . 'a' . $bs . 'nb' . $sq, array(), WP_MySQL_Lexer::SINGLE_QUOTED_TEXT, "a\nb" ),
495+
);
496+
}
497+
455498
private function get_token_names( array $token_types ): array {
456499
return array_map(
457500
function ( $token_type ) {

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,15 +1209,29 @@ private function read_number(): ?int {
12091209
*
12101210
* Rules:
12111211
* 1. Quotes can be escaped by doubling them ('', "", ``).
1212-
* 2. Backslashes escape the next character, unless NO_BACKSLASH_ESCAPES is set.
1212+
* 2. In string literals, backslashes escape the next character,
1213+
* unless the NO_BACKSLASH_ESCAPES SQL mode is set.
1214+
* 3. In identifiers, backslashes are always literal and never escape.
12131215
*/
12141216
private function read_quoted_text(): ?int {
12151217
$quote = $this->sql[ $this->bytes_already_read ];
12161218
$this->bytes_already_read += 1; // Consume the quote.
12171219

1218-
$no_backslash_escapes = $this->is_sql_mode_active(
1219-
self::SQL_MODE_NO_BACKSLASH_ESCAPES
1220-
);
1220+
/*
1221+
* Determine whether the quote opens an identifier or a string literal.
1222+
* An identifier is quoted with a backtick or a double quote when the
1223+
* ANSI_QUOTES SQL mode is active. Otherwise, it is a string literal.
1224+
*
1225+
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi_quotes
1226+
*/
1227+
$is_identifier_quote = '`' === $quote
1228+
|| ( '"' === $quote && $this->is_sql_mode_active( self::SQL_MODE_ANSI_QUOTES ) );
1229+
1230+
/*
1231+
* Backslash escapes apply only to string literals, and only when the
1232+
* NO_BACKSLASH_ESCAPES SQL mode is not set.
1233+
*/
1234+
$backslash_is_escape = ! $is_identifier_quote && ! $this->no_backslash_escapes;
12211235

12221236
// We need to look for the closing quote in a loop, as it can be escaped,
12231237
// in which case the escape sequence is consumed and the loop continues.
@@ -1230,9 +1244,9 @@ private function read_quoted_text(): ?int {
12301244
$at = $quote_at;
12311245

12321246
/*
1233-
* By default, quotes can be escaped with a "\".
1234-
* When NO_BACKSLASH_ESCAPES SQL mode is active, the "\" treated as
1235-
* a regular character.
1247+
* In string literals, quotes can be escaped with a backslash. When
1248+
* NO_BACKSLASH_ESCAPES SQL mode is active, the backslash is treated
1249+
* as a regular character. Identifiers never use backslash escaping.
12361250
*
12371251
* The quote is escaped only when the number of preceding backslashes
12381252
* is odd - "\" is an escape sequence, "\\" is an escaped backslash,
@@ -1243,7 +1257,7 @@ private function read_quoted_text(): ?int {
12431257
* sits at the very start of the input. The `?? null` covers
12441258
* positive out-of-range indexes belt-and-suspenders.
12451259
*/
1246-
if ( ! $no_backslash_escapes ) {
1260+
if ( $backslash_is_escape ) {
12471261
$i = 0;
12481262
while ( ( $at - $i - 1 ) >= 0 && '\\' === ( $this->sql[ $at - $i - 1 ] ?? null ) ) {
12491263
$i += 1;
@@ -1266,10 +1280,7 @@ private function read_quoted_text(): ?int {
12661280

12671281
$this->bytes_already_read = $at;
12681282

1269-
if (
1270-
'`' === $quote
1271-
|| ( '"' === $quote && $this->is_sql_mode_active( self::SQL_MODE_ANSI_QUOTES ) )
1272-
) {
1283+
if ( $is_identifier_quote ) {
12731284
return self::BACK_TICK_QUOTED_ID;
12741285
} elseif ( '"' === $quote ) {
12751286
return self::DOUBLE_QUOTED_TEXT;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ 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+
* For quoted identifiers and when the NO_BACKSLASH_ESCAPES SQL mode
76+
* is active, we only need to handle escaped bounding quotes, as all
77+
* other characters preserve their literal values.
7878
*/
79-
if ( $this->sql_mode_no_backslash_escapes_enabled ) {
79+
if (
80+
WP_MySQL_Lexer::BACK_TICK_QUOTED_ID === $this->id
81+
|| $this->sql_mode_no_backslash_escapes_enabled
82+
) {
8083
return str_replace( $quote . $quote, $quote, $value );
8184
}
8285

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,45 @@ public function test_composite_ansi_mode_expands_to_lexer_component_modes(): voi
593593
$this->assertSame( WP_MySQL_Lexer::KEYWORDS['COUNT'], $lexer->get_token()->id );
594594
}
595595

596+
/**
597+
* Quoted identifiers escape delimiters by doubling, not with backslashes.
598+
*
599+
* @dataProvider data_quoted_identifier_backslash
600+
*/
601+
public function test_quoted_identifiers_treat_backslash_literally(
602+
string $sql,
603+
array $sql_modes,
604+
int $expected_token_id,
605+
string $expected_value
606+
): void {
607+
$lexer = new WP_MySQL_Lexer( $sql, 80400, $sql_modes );
608+
$this->assertTrue( $lexer->next_token() );
609+
610+
$token = $lexer->get_token();
611+
$this->assertSame( $expected_token_id, $token->id, $sql );
612+
$this->assertSame( $expected_value, $token->get_value(), $sql );
613+
}
614+
615+
public function data_quoted_identifier_backslash(): array {
616+
$bs = chr( 92 ); // A single backslash.
617+
$bt = '`';
618+
$dq = '"';
619+
$sq = "'";
620+
return array(
621+
// Backtick identifiers: backslash is a literal character.
622+
'backtick keeps backslash-n literal' => array( $bt . 'a' . $bs . 'nb' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
623+
'backtick allows trailing backslash' => array( $bt . 'a' . $bs . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
624+
'backtick escapes only by doubling' => array( $bt . 'a' . $bt . $bt . 'b' . $bt, array(), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bt . 'b' ),
625+
626+
// ANSI-quoted identifiers behave like backtick identifiers.
627+
'ansi-quoted keeps backslash literal' => array( $dq . 'a' . $bs . 'nb' . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs . 'nb' ),
628+
'ansi-quoted allows trailing backslash' => array( $dq . 'a' . $bs . $dq, array( 'ANSI_QUOTES' ), WP_MySQL_Lexer::BACK_TICK_QUOTED_ID, 'a' . $bs ),
629+
630+
// String literals still process backslash escapes.
631+
'single-quoted string escapes newline' => array( $sq . 'a' . $bs . 'nb' . $sq, array(), WP_MySQL_Lexer::SINGLE_QUOTED_TEXT, "a\nb" ),
632+
);
633+
}
634+
596635
private function get_token_names( array $token_types ): array {
597636
return array_map(
598637
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)