Skip to content

Commit b44ddd9

Browse files
committed
Restore WordPress query text validation
Use wpdb's charset checks to reject invalid text before executing a query, while preserving binary data and prevalidated queries. Keep Query Monitor entries aligned when validation performs charset lookups. Remove the two obsolete failure expectations for the existing WordPress invalid-text tests. Document that full charset enforcement belongs in MySQL on SQLite so it protects all callers, including direct driver use.
1 parent a65428a commit b44ddd9

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎.github/workflows/wp-tests-phpunit-run.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ const expectedFailures = [
3737
'Tests_DB_Charset::test_get_column_charset_non_mysql with data set #5',
3838
'Tests_DB_Charset::test_get_column_charset_non_mysql with data set #6',
3939
'Tests_DB_Charset::test_get_column_charset_non_mysql with data set #7',
40-
'Tests_DB_Charset::test_invalid_characters_in_query',
4140
'Tests_DB_Charset::test_strip_invalid_text with data set #40',
4241
'Tests_DB_Charset::test_strip_invalid_text with data set #41',
4342
'Tests_DB_dbDelta::test_column_type_change',
4443
'Tests_DB::test_mysqli_flush_sync',
45-
'Tests_DB::test_query_value_contains_invalid_chars',
4644
'Tests_DB::test_replace',
4745
'Tests_Menu_Walker_Nav_Menu::test_start_el_with_empty_attributes with data set #1',
4846
'Tests_Menu_Walker_Nav_Menu::test_start_el_with_empty_attributes with data set #2',

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ public function query( $query ) {
563563
}
564564

565565
if ( ! $this->ready ) {
566+
$this->check_current_query = true;
566567
return false;
567568
}
568569

@@ -578,21 +579,30 @@ public function query( $query ) {
578579
// Log how the function was called.
579580
$this->func_call = "\$db->query(\"$query\")";
580581

582+
/*
583+
* Mirror wpdb's query text validation.
584+
* TODO: Add full charset enforcement to MySQL on SQLite, where column
585+
* types and SQL mode are known, so all callers are protected.
586+
*/
587+
if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
588+
$stripped_query = $this->strip_invalid_text_from_query( $query );
589+
// Charset discovery can run queries, so clear their results.
590+
$this->flush();
591+
if ( $stripped_query !== $query ) {
592+
$this->insert_id = 0;
593+
$this->last_query = $query;
594+
wp_load_translations_early();
595+
$this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' );
596+
return false;
597+
}
598+
}
599+
$this->check_current_query = true;
600+
581601
// Keep track of the last query for debug.
582602
$this->last_query = $query;
583603

584-
// Save the query count before running another query.
604+
// Save the query count after any charset discovery queries.
585605
$last_query_count = count( $this->queries ?? array() );
586-
587-
/*
588-
* @TODO: wpdb uses "$this->check_current_query" and table metadata to
589-
* reject queries containing invalid text. Implement equivalent handling
590-
* for SQLite without relying on the MySQL-specific conversion pipeline.
591-
*
592-
* PCRE's "u" modifier can validate UTF-8 without constructing a converted
593-
* query copy: 1 === preg_match( '//u', $query ). The implementation must
594-
* preserve wpdb's exemptions for prevalidated and binary data.
595-
*/
596606
$this->_do_query( $query );
597607

598608
if ( $this->last_error ) {

0 commit comments

Comments
 (0)