Skip to content

Commit 00c2ff3

Browse files
committed
Defer SQLite query UTF-8 validation
Restore the previous query path and expected WordPress test outcomes so UTF-8 validation can be designed separately. Keep a focused TODO recommending PCRE validation while preserving wpdb exemptions.
1 parent 09b9fc5 commit 00c2ff3

2 files changed

Lines changed: 8 additions & 27 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const path = require( 'path' );
1212
const requiresNativeParserExtension = process.env.WP_SQLITE_REQUIRE_NATIVE_PARSER_EXTENSION === '1';
1313

1414
const expectedErrors = [
15+
'Tests_DB_Charset::test_invalid_characters_in_query',
1516
'Tests_DB_Charset::test_set_charset_changes_the_connection_collation',
1617
];
1718

@@ -53,6 +54,7 @@ const expectedFailures = [
5354
'Tests_DB_Charset::test_strip_invalid_text with data set #41',
5455
'Tests_DB_dbDelta::test_spatial_indices',
5556
'Tests_DB::test_mysqli_flush_sync',
57+
'Tests_DB::test_query_value_contains_invalid_chars',
5658
'Tests_DB::test_replace',
5759
'Tests_Menu_Walker_Nav_Menu::test_start_el_with_empty_attributes with data set #1',
5860
'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: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -562,35 +562,14 @@ public function query( $query ) {
562562
$last_query_count = count( $this->queries ?? array() );
563563

564564
/*
565-
* Strip invalid UTF-8 characters from non-ASCII queries.
565+
* @TODO: wpdb uses "$this->check_current_query" and table metadata to
566+
* reject queries containing invalid text. Implement equivalent handling
567+
* for SQLite without relying on the MySQL-specific conversion pipeline.
566568
*
567-
* SQLite stores all text as UTF-8, so we simply ensure the query
568-
* contains only valid UTF-8 sequences rather than using the parent's
569-
* MySQL-specific charset detection pipeline.
569+
* PCRE's "u" modifier can validate UTF-8 without constructing a converted
570+
* query copy: 1 === preg_match( '//u', $query ). The implementation must
571+
* preserve wpdb's exemptions for prevalidated and binary data.
570572
*/
571-
if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
572-
if ( function_exists( 'mb_convert_encoding' ) ) {
573-
$stripped_query = mb_convert_encoding( $query, 'UTF-8', 'UTF-8' );
574-
} else {
575-
$stripped_query = htmlspecialchars_decode(
576-
htmlspecialchars( $query, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8' ),
577-
ENT_NOQUOTES
578-
);
579-
}
580-
581-
if ( $stripped_query !== $query ) {
582-
$this->insert_id = 0;
583-
$this->last_query = $query;
584-
585-
wp_load_translations_early();
586-
587-
$this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' );
588-
589-
return false;
590-
}
591-
}
592-
$this->check_current_query = true;
593-
594573
$this->_do_query( $query );
595574

596575
if ( $this->last_error ) {

0 commit comments

Comments
 (0)