@@ -547,32 +547,19 @@ public function query( $query ) {
547547 $ last_query_count = count ( $ this ->queries ?? array () );
548548
549549 /*
550- * Strip invalid UTF-8 characters from non-ASCII queries .
550+ * Reject queries containing invalid UTF-8 byte sequences .
551551 *
552- * SQLite stores all text as UTF-8, so we simply ensure the query
553- * contains only valid UTF-8 sequences rather than using the parent's
554- * MySQL-specific charset detection pipeline.
552+ * SQLite does not reject malformed UTF-8, so validate queries locally
553+ * instead of relying on the database server as a backstop.
555554 */
556- if ( $ this ->check_current_query && ! $ this ->check_ascii ( $ query ) ) {
557- if ( function_exists ( 'mb_convert_encoding ' ) ) {
558- $ stripped_query = mb_convert_encoding ( $ query , 'UTF-8 ' , 'UTF-8 ' );
559- } else {
560- $ stripped_query = htmlspecialchars_decode (
561- htmlspecialchars ( $ query , ENT_NOQUOTES | ENT_SUBSTITUTE , 'UTF-8 ' ),
562- ENT_NOQUOTES
563- );
564- }
565-
566- if ( $ stripped_query !== $ query ) {
567- $ this ->insert_id = 0 ;
568- $ this ->last_query = $ query ;
555+ if ( $ this ->check_current_query && ! $ this ->check_utf8 ( $ query ) ) {
556+ $ this ->insert_id = 0 ;
569557
570- wp_load_translations_early ();
558+ wp_load_translations_early ();
571559
572- $ this ->last_error = __ ( 'WordPress database error: Could not perform query because it contains invalid data. ' );
560+ $ this ->last_error = __ ( 'WordPress database error: Could not perform query because it contains invalid data. ' );
573561
574- return false ;
575- }
562+ return false ;
576563 }
577564 $ this ->check_current_query = true ;
578565
@@ -674,6 +661,52 @@ private function _do_query( $query ) {
674661 }
675662 }
676663
664+ /**
665+ * Checks whether a query is safe to run without invalid text checking.
666+ *
667+ * This overrides wpdb::check_safe_collation() to preserve the parent's
668+ * collation checks while ensuring malformed UTF-8 is never considered safe.
669+ *
670+ * @see wpdb::check_safe_collation()
671+ *
672+ * @param string $query The query to check.
673+ * @return bool True when the query uses a safe collation and contains valid UTF-8,
674+ * false otherwise.
675+ */
676+ protected function check_safe_collation ( $ query ) {
677+ return parent ::check_safe_collation ( $ query ) && $ this ->check_utf8 ( $ query );
678+ }
679+
680+ /**
681+ * Checks whether a string is valid UTF-8.
682+ *
683+ * ASCII-only strings pass as well, since ASCII is a subset of UTF-8.
684+ *
685+ * @param string $input The string to check.
686+ * @return bool True when the string is valid UTF-8, false otherwise.
687+ */
688+ private function check_utf8 ( $ input ) {
689+ if ( function_exists ( 'mb_check_encoding ' ) ) {
690+ return mb_check_encoding ( $ input , 'UTF-8 ' );
691+ }
692+
693+ // Check once whether the PCRE library was compiled with UTF-8 support.
694+ static $ pcre_utf8 = null ;
695+ if ( null === $ pcre_utf8 ) {
696+ // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
697+ $ pcre_utf8 = 1 === @preg_match ( '//u ' , '' );
698+ }
699+
700+ // With no way to validate, let the input pass. This mirrors the
701+ // behavior of wp_check_invalid_utf8().
702+ if ( ! $ pcre_utf8 ) {
703+ return true ;
704+ }
705+
706+ // The "u" modifier fails the match when the subject is not valid UTF-8.
707+ return 1 === preg_match ( '//u ' , $ input );
708+ }
709+
677710 /**
678711 * Method to set the class variable $col_info.
679712 *
0 commit comments