Skip to content

Commit 4a68e9f

Browse files
committed
Reject foreign keys that reference reserved identifiers
Validate REFERENCES identifiers with the existing driver checks before CREATE TABLE or ALTER TABLE records metadata. This prevents foreign keys from targeting internal tables and disrupting later operations. Cover rejected references, preserved schema and data, and valid foreign keys with similar names.
1 parent 2840d1a commit 4a68e9f

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
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
}
@@ -3115,6 +3118,22 @@ private function execute_alter_table_statement( WP_Parser_Node $node ): void {
31153118
// consist only of operations that SQLite's ALTER TABLE supports.
31163119
}
31173120

3121+
/**
3122+
* Validate foreign key identifiers before recording their metadata.
3123+
*
3124+
* REFERENCES clauses are reconstructed from metadata rather than translated.
3125+
*
3126+
* @param WP_Parser_Node $node The CREATE TABLE or ALTER TABLE AST node.
3127+
*/
3128+
private function validate_foreign_key_references( WP_Parser_Node $node ): void {
3129+
foreach ( $node->get_descendant_nodes( 'references' ) as $reference ) {
3130+
foreach ( $reference->get_descendant_nodes( 'pureIdentifier' ) as $identifier ) {
3131+
// Translation validates the identifier, including the reserved prefix.
3132+
$this->translate( $identifier );
3133+
}
3134+
}
3135+
}
3136+
31183137
/**
31193138
* Translate and execute a MySQL DROP TABLE statement in SQLite.
31203139
*

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8254,6 +8254,99 @@ public function testReservedPrefixInSavepoint(): void {
82548254
$this->assertQuery( 'ROLLBACK' );
82558255
}
82568256

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+
82578350
/**
82588351
* @dataProvider getNamesWithoutReservedPrefixTestData
82598352
*/

0 commit comments

Comments
 (0)