Skip to content

Commit 9975f45

Browse files
committed
Reject reserved identifier prefixes regardless of case
Match the reserved prefix using the same ASCII case-insensitive comparison as SQLite identifier resolution. Cover case variants, quoted and qualified names, reads and writes, and similar names that remain valid.
1 parent dd0bcd6 commit 9975f45

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4709,7 +4709,8 @@ private function translate_pure_identifier( WP_Parser_Node $node ): string {
47094709
$token = $node->get_first_child_token();
47104710
$value = $token->get_value();
47114711

4712-
if ( str_starts_with( $value, self::RESERVED_PREFIX ) ) {
4712+
// SQLite resolves ASCII identifiers case insensitively.
4713+
if ( 0 === strncasecmp( $value, self::RESERVED_PREFIX, strlen( self::RESERVED_PREFIX ) ) ) {
47134714
throw $this->new_driver_exception(
47144715
sprintf(
47154716
"Invalid identifier '%s', prefix '%s' is reserved",

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8164,7 +8164,11 @@ public function testCompoundPrimaryKeyWithAutoincrement(): void {
81648164
/**
81658165
* @dataProvider getReservedPrefixTestData
81668166
*/
8167-
public function testReservedPrefix( string $query, string $error ): void {
8167+
public function testReservedPrefix( string $query, string $error, bool $ansi_quotes = false ): void {
8168+
if ( $ansi_quotes ) {
8169+
$this->assertQuery( "SET sql_mode = 'ANSI_QUOTES'" );
8170+
}
8171+
81688172
$this->expectException( WP_MySQL_On_SQLite_Exception::class );
81698173
$this->expectExceptionMessage( $error );
81708174
$this->assertQuery( $query );
@@ -8196,6 +8200,42 @@ public function getReservedPrefixTestData(): array {
81968200
'DROP TABLE _wp_sqlite_t',
81978201
"Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
81988202
),
8203+
array(
8204+
'SELECT COUNT(*) FROM _WP_SQLITE_mysql_information_schema_tables',
8205+
"Invalid identifier '_WP_SQLITE_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8206+
),
8207+
array(
8208+
'SELECT COUNT(*) FROM _Wp_SqLiTe_mysql_information_schema_tables',
8209+
"Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8210+
),
8211+
array(
8212+
'SELECT COUNT(*) FROM `wp`.`_Wp_SqLiTe_mysql_information_schema_tables`',
8213+
"Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8214+
),
8215+
array(
8216+
'SELECT COUNT(*) FROM "_WP_SQLITE_mysql_information_schema_tables"',
8217+
"Invalid identifier '_WP_SQLITE_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8218+
true,
8219+
),
8220+
);
8221+
}
8222+
8223+
/**
8224+
* @dataProvider getNamesWithoutReservedPrefixTestData
8225+
*/
8226+
public function testNamesWithoutReservedPrefix( string $identifier ): void {
8227+
$name = $this->engine->get_connection()->quote_identifier( $identifier );
8228+
$this->assertQuery( "CREATE TABLE $name ($name INT)" );
8229+
$this->assertQuery( "INSERT INTO $name ($name) VALUES (42)" );
8230+
$result = $this->assertQuery( "SELECT wp.$name.$name AS $name FROM wp.$name" );
8231+
$this->assertEquals( array( (object) array( $identifier => '42' ) ), $result );
8232+
}
8233+
8234+
public function getNamesWithoutReservedPrefixTestData(): array {
8235+
return array(
8236+
array( '_WP_SQLITE' ),
8237+
array( 'x_WP_SQLITE_t' ),
8238+
array( '_Wp_SqLiTeX_t' ),
81998239
);
82008240
}
82018241

0 commit comments

Comments
 (0)