Skip to content

Commit 0c8f523

Browse files
committed
Handle invalid WordPress table prefixes
Check the wpdb prefix result before using derived table names so invalid configuration produces a clear exception instead of a TypeError.
1 parent b370861 commit 0c8f523

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ private function get_wp_create_table_statements(): array {
203203
* the "$table_prefix" global so we can get correct table names.
204204
*/
205205
global $table_prefix;
206-
$wpdb->set_prefix( $table_prefix );
206+
$set_prefix_result = $wpdb->set_prefix( $table_prefix );
207+
if ( $set_prefix_result instanceof WP_Error ) {
208+
throw new Exception( $set_prefix_result->get_error_message() );
209+
}
207210

208211
// Get schema for global tables.
209212
$schema = wp_get_db_schema( 'global' );

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function wp_get_db_schema( $scope = 'all', $blog_id = null ) {
4444
// Before each test, we create a new database
4545
public function setUp(): void {
4646
$GLOBALS['wp_sqlite_is_multisite'] = false;
47+
$GLOBALS['table_prefix'] = 'wptests_';
4748

4849
$this->initializeDatabase();
4950

@@ -53,6 +54,20 @@ public function setUp(): void {
5354
public function tearDown(): void {
5455
$GLOBALS['wp_sqlite_is_multisite'] = false;
5556
$GLOBALS['wp_sqlite_db_schema_calls'] = array();
57+
$GLOBALS['table_prefix'] = 'wptests_';
58+
$GLOBALS['wpdb']->blogs = 'wptests_blogs';
59+
}
60+
61+
public function testInvalidWpTablePrefix(): void {
62+
$GLOBALS['wp_sqlite_is_multisite'] = true;
63+
$GLOBALS['table_prefix'] = 'invalid-prefix';
64+
$GLOBALS['wpdb']->blogs = null;
65+
$this->engine->get_connection()->query( 'CREATE TABLE t ( id INTEGER )' );
66+
67+
$this->expectException( Exception::class );
68+
$this->expectExceptionMessage( 'Invalid database prefix' );
69+
70+
$this->reconstructor->ensure_correct_information_schema();
5671
}
5772

5873
public function testEmptyMultisiteDatabase(): void {

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,30 @@
2323
$GLOBALS['wpdb'] = new class() {
2424
public $blogs;
2525

26-
public function set_prefix( string $prefix ): void {
26+
public function set_prefix( string $prefix ) {
27+
if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) {
28+
return new WP_Error( 'invalid_db_prefix', 'Invalid database prefix' );
29+
}
30+
2731
$this->blogs = $prefix . 'blogs';
32+
return $prefix;
2833
}
2934
};
3035

36+
if ( ! class_exists( 'WP_Error' ) ) {
37+
class WP_Error {
38+
private $message;
39+
40+
public function __construct( $code, $message ) {
41+
$this->message = $message;
42+
}
43+
44+
public function get_error_message() {
45+
return $this->message;
46+
}
47+
}
48+
}
49+
3150
/**
3251
* Polyfills for WordPress functions
3352
*/

0 commit comments

Comments
 (0)