Skip to content

Commit 7ebfeb9

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 490b42a commit 7ebfeb9

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
@@ -572,35 +572,14 @@ public function query( $query ) {
572572
$last_query_count = count( $this->queries ?? array() );
573573

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

606585
if ( $this->last_error ) {

0 commit comments

Comments
 (0)