Skip to content

Commit ec510fb

Browse files
authored
Fix reserved prefix checks for case variants and foreign keys (#580)
Queries could bypass the `_wp_sqlite_` prefix guard by changing letter case or declaring foreign keys that reference internal tables. SQLite resolves ASCII identifiers case insensitively, while foreign-key references skipped the guard entirely. **This change rejects both forms of reserved references.** Prefix checks are now case insensitive, and CREATE TABLE and ALTER TABLE validate foreign-key identifiers before recording metadata. Regression tests cover rejected references, preserved schema and data, later table drops, and valid names and foreign keys. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Reserved-prefix checks now handle mixed-case and qualified identifiers consistently, including identifiers in foreign-key references and savepoints, and remain consistent when `ANSI_QUOTES` is enabled. * Invalid foreign-key references are rejected without changing existing tables or schema metadata. * Identifiers that only resemble reserved names remain usable, and table-option changes continue to work without foreign-key identifier validation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent dd0bcd6 commit ec510fb

2 files changed

Lines changed: 189 additions & 2 deletions

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,8 @@ private function execute_create_table_statement( WP_Parser_Node $node ): void {
30053005
}
30063006
}
30073007

3008+
$this->validate_foreign_key_references( $node );
3009+
30083010
// Save information to information schema tables.
30093011
$this->information_schema_builder->record_create_table( $node );
30103012

@@ -3104,6 +3106,7 @@ private function execute_alter_table_statement( WP_Parser_Node $node ): void {
31043106
* change the schema, so the recreate would be a pointless full copy.
31053107
*/
31063108
if ( count( $node->get_descendant_nodes( 'alterListItem' ) ) > 0 ) {
3109+
$this->validate_foreign_key_references( $node );
31073110
$this->information_schema_builder->record_alter_table( $node );
31083111
$this->recreate_table_from_information_schema( $table_is_temporary, $table_name, $column_map );
31093112
}
@@ -4709,7 +4712,8 @@ private function translate_pure_identifier( WP_Parser_Node $node ): string {
47094712
$token = $node->get_first_child_token();
47104713
$value = $token->get_value();
47114714

4712-
if ( str_starts_with( $value, self::RESERVED_PREFIX ) ) {
4715+
// SQLite resolves ASCII identifiers case insensitively.
4716+
if ( 0 === strncasecmp( $value, self::RESERVED_PREFIX, strlen( self::RESERVED_PREFIX ) ) ) {
47134717
throw $this->new_driver_exception(
47144718
sprintf(
47154719
"Invalid identifier '%s', prefix '%s' is reserved",
@@ -6999,6 +7003,22 @@ private function create_table_reference_map( WP_Parser_Node $node ): array {
69997003
return $table_map;
70007004
}
70017005

7006+
/**
7007+
* Validate foreign key identifiers before recording their metadata.
7008+
*
7009+
* REFERENCES clauses are reconstructed from metadata rather than translated.
7010+
*
7011+
* @param WP_Parser_Node $node The CREATE TABLE or ALTER TABLE AST node.
7012+
*/
7013+
private function validate_foreign_key_references( WP_Parser_Node $node ): void {
7014+
foreach ( $node->get_descendant_nodes( 'references' ) as $reference ) {
7015+
foreach ( $reference->get_descendant_nodes( 'pureIdentifier' ) as $identifier ) {
7016+
// Translation validates the identifier, including the reserved prefix.
7017+
$this->translate( $identifier );
7018+
}
7019+
}
7020+
}
7021+
70027022
/**
70037023
* Emulate MySQL type casting for values to be saved to the database
70047024
* using INSERT, REPLACE, or UPDATE statements.

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

Lines changed: 168 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,169 @@ 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+
array(
8221+
"INSERT INTO _Wp_SqLiTe_global_variables (name, value) VALUES ('test', 'value')",
8222+
"Invalid identifier '_Wp_SqLiTe_global_variables', prefix '_wp_sqlite_' is reserved",
8223+
),
8224+
array(
8225+
'DELETE FROM _Wp_SqLiTe_mysql_information_schema_tables',
8226+
"Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8227+
),
8228+
array(
8229+
'DROP TABLE _Wp_SqLiTe_mysql_information_schema_tables',
8230+
"Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8231+
),
8232+
array(
8233+
'TRUNCATE TABLE _Wp_SqLiTe_mysql_information_schema_tables',
8234+
"Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8235+
),
8236+
array(
8237+
'CREATE TEMPORARY TABLE _Wp_SqLiTe_mysql_information_schema_tables (id INT)',
8238+
"Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8239+
),
8240+
array(
8241+
'CREATE TABLE _WP_SQLITE_ (id INT)',
8242+
"Invalid identifier '_WP_SQLITE_', prefix '_wp_sqlite_' is reserved",
8243+
),
8244+
);
8245+
}
8246+
8247+
public function testReservedPrefixInSavepoint(): void {
8248+
$this->assertQuery( 'START TRANSACTION' );
8249+
$this->assertQueryError(
8250+
'SAVEPOINT _Wp_SqLiTe_savepoint_wrapper',
8251+
"Invalid identifier '_Wp_SqLiTe_savepoint_wrapper', prefix '_wp_sqlite_' is reserved"
8252+
);
8253+
$this->assertTrue( $this->engine->inTransaction() );
8254+
$this->assertQuery( 'ROLLBACK' );
8255+
}
8256+
8257+
/**
8258+
* @dataProvider getReservedForeignKeyIdentifiersTestData
8259+
*/
8260+
public function testReservedForeignKeyIdentifiers( string $query, string $identifier, bool $ansi_quotes = false ): void {
8261+
if ( $ansi_quotes ) {
8262+
$this->assertQuery( "SET sql_mode = 'ANSI_QUOTES'" );
8263+
}
8264+
$this->assertQuery( 'CREATE TABLE child (n VARCHAR(64))' );
8265+
$this->assertQuery( "INSERT INTO child VALUES ('kept')" );
8266+
$schema = $this->assertQuery( 'SHOW CREATE TABLE child' );
8267+
8268+
$this->assertQueryError( $query, "Invalid identifier '$identifier', prefix '_wp_sqlite_' is reserved" );
8269+
8270+
$this->assertEquals( $schema, $this->assertQuery( 'SHOW CREATE TABLE child' ) );
8271+
$this->assertEquals( array( (object) array( 'n' => 'kept' ) ), $this->assertQuery( 'SELECT * FROM child' ) );
8272+
$this->assertSame( 0, (int) $this->sqlite->query( "SELECT COUNT(*) FROM sqlite_temp_master WHERE name = 'new_child'" )->fetchColumn() );
8273+
8274+
// Rejected references must leave no metadata behind or block later drops.
8275+
$this->assertQuery( 'CREATE TABLE new_child (id INT)' );
8276+
$this->assertQuery( 'DROP TABLE new_child, child' );
8277+
}
8278+
8279+
public function getReservedForeignKeyIdentifiersTestData(): array {
8280+
return array(
8281+
array(
8282+
'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _wp_sqlite_mysql_information_schema_tables (TABLE_NAME))',
8283+
'_wp_sqlite_mysql_information_schema_tables',
8284+
),
8285+
array(
8286+
'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _WP_SQLITE_global_variables (name))',
8287+
'_WP_SQLITE_global_variables',
8288+
),
8289+
array(
8290+
'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES `wp`.`_Wp_SqLiTe_mysql_information_schema_tables` (`TABLE_NAME`))',
8291+
'_Wp_SqLiTe_mysql_information_schema_tables',
8292+
),
8293+
array(
8294+
'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES "_Wp_SqLiTe_global_variables" ("name"))',
8295+
'_Wp_SqLiTe_global_variables',
8296+
true,
8297+
),
8298+
array(
8299+
'CREATE TEMPORARY TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _Wp_SqLiTe_mysql_information_schema_tmp_tables (TABLE_NAME))',
8300+
'_Wp_SqLiTe_mysql_information_schema_tmp_tables',
8301+
),
8302+
array(
8303+
'ALTER TABLE child ADD FOREIGN KEY (n) REFERENCES _wp_sqlite_mysql_information_schema_tables (TABLE_NAME)',
8304+
'_wp_sqlite_mysql_information_schema_tables',
8305+
),
8306+
array(
8307+
'ALTER TABLE child ADD COLUMN extra INT, ADD CONSTRAINT fk FOREIGN KEY (n) REFERENCES _Wp_SqLiTe_global_variables (name)',
8308+
'_Wp_SqLiTe_global_variables',
8309+
),
8310+
array(
8311+
'CREATE TABLE new_child (n VARCHAR(64) REFERENCES _wp_sqlite_global_variables (name))',
8312+
'_wp_sqlite_global_variables',
8313+
),
8314+
array(
8315+
'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _WP_SQLITE_db.parent (n))',
8316+
'_WP_SQLITE_db',
8317+
),
8318+
array(
8319+
'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES child (_Wp_SqLiTe_column))',
8320+
'_Wp_SqLiTe_column',
8321+
),
8322+
);
8323+
}
8324+
8325+
public function testForeignKeyWithSimilarPrefix(): void {
8326+
$this->assertQuery( 'CREATE TABLE `_WP_SQLITE` (n VARCHAR(64) PRIMARY KEY)' );
8327+
$this->assertQuery( "INSERT INTO `_WP_SQLITE` VALUES ('_wp_sqlite_t')" );
8328+
$this->assertQuery(
8329+
"CREATE TABLE child (
8330+
id INT,
8331+
n VARCHAR(64) DEFAULT '_wp_sqlite_t',
8332+
FOREIGN KEY (n) REFERENCES `wp`.`_WP_SQLITE` (n)
8333+
)"
8334+
);
8335+
$this->assertQuery( 'CREATE TABLE other_child (n VARCHAR(64))' );
8336+
$this->assertQuery( 'ALTER TABLE other_child ADD FOREIGN KEY (n) REFERENCES `_WP_SQLITE` (n)' );
8337+
$this->assertQuery( 'INSERT INTO child (id) VALUES (1)' );
8338+
$this->assertQuery( "INSERT INTO other_child VALUES ('_wp_sqlite_t')" );
8339+
$this->assertEquals( array( (object) array( 'n' => '_wp_sqlite_t' ) ), $this->assertQuery( 'SELECT n FROM child' ) );
8340+
$this->assertQueryError(
8341+
"INSERT INTO child (n) VALUES ('missing')",
8342+
'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
8343+
);
8344+
$this->assertQueryError(
8345+
"INSERT INTO other_child VALUES ('missing')",
8346+
'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
8347+
);
8348+
}
8349+
8350+
/**
8351+
* @dataProvider getNamesWithoutReservedPrefixTestData
8352+
*/
8353+
public function testNamesWithoutReservedPrefix( string $identifier ): void {
8354+
$name = $this->engine->get_connection()->quote_identifier( $identifier );
8355+
$this->assertQuery( "CREATE TABLE $name ($name INT)" );
8356+
$this->assertQuery( "INSERT INTO $name ($name) VALUES (42)" );
8357+
$result = $this->assertQuery( "SELECT wp.$name.$name AS $name FROM wp.$name" );
8358+
$this->assertEquals( array( (object) array( $identifier => '42' ) ), $result );
8359+
}
8360+
8361+
public function getNamesWithoutReservedPrefixTestData(): array {
8362+
return array(
8363+
array( '_WP_SQLITE' ),
8364+
array( 'x_WP_SQLITE_t' ),
8365+
array( '_Wp_SqLiTeX_t' ),
81998366
);
82008367
}
82018368

0 commit comments

Comments
 (0)