From c53fef14b5a9f1d8cf5e00763aebe47f8fcab0d2 Mon Sep 17 00:00:00 2001
From: salvatorecorsi <86927009+salvatorecorsi@users.noreply.github.com>
Date: Thu, 22 Oct 2026 09:08:52 +0200
Subject: [PATCH] KSES: Preserve escaped shortcode brackets in text nodes.
---
src/wp-includes/kses.php | 62 ++++++++++++
tests/phpunit/tests/kses.php | 183 +++++++++++++++++++++++++++++++++++
2 files changed, 245 insertions(+)
diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php
index c68b71fcc1527..b57963c967cc5 100644
--- a/src/wp-includes/kses.php
+++ b/src/wp-includes/kses.php
@@ -1183,6 +1183,62 @@ private function get_span() {
return $this->bookmarks['here'];
}
+ /**
+ * Preserves character references for shortcode brackets in serialized text.
+ *
+ * Shortcodes are processed before HTML character references are decoded by a
+ * browser. Decoding an escaped bracket can therefore activate a shortcode.
+ * Text normalization preserves bracket order, so the raw token identifies
+ * which brackets must remain escaped without changing literal shortcodes.
+ *
+ * @since 7.2.0
+ * @access private
+ *
+ * @param string $text Serialized text with HTML-sensitive characters escaped.
+ * @param string $raw_text Original text token before decoding.
+ * @return string Serialized text retaining escaped shortcode brackets.
+ */
+ private function preserve_escaped_shortcode_brackets( string $text, string $raw_text ): string {
+ if ( ! str_contains( $raw_text, '&' ) ) {
+ return $text;
+ }
+
+ $escaped_brackets = '';
+ $length = strlen( $raw_text );
+ for ( $at = 0; $at < $length; ++$at ) {
+ $at += strcspn( $raw_text, '&[]', $at );
+ if ( $at >= $length ) {
+ break;
+ }
+
+ if ( '&' === $raw_text[ $at ] ) {
+ $reference = WP_HTML_Decoder::read_character_reference( 'data', $raw_text, $at, $reference_length );
+ if ( null !== $reference ) {
+ if ( '[' === $reference || ']' === $reference ) {
+ $escaped_brackets .= '1';
+ }
+ $at += $reference_length - 1;
+ }
+ } else {
+ $escaped_brackets .= '0';
+ }
+ }
+ if ( ! str_contains( $escaped_brackets, '1' ) ) {
+ return $text;
+ }
+
+ $bracket_index = 0;
+ return preg_replace_callback(
+ '/[\[\]]/',
+ static function ( $matches ) use ( $escaped_brackets, &$bracket_index ) {
+ return '1' === $escaped_brackets[ $bracket_index++ ]
+ ? ( '[' === $matches[0] ? '[' : ']' )
+ : $matches[0];
+ },
+ $text
+ );
+ }
+
/**
* Sets the value of a given HTML attribute, adjusting URL attributes
* to ensure compatibility with legacy {@see wp_kses()} behavior.
@@ -1590,6 +1646,12 @@ public function sanitize() {
'>' => '>',
)
);
+ if ( strcspn( $text, '[]' ) !== strlen( $text ) ) {
+ $text = $this->preserve_escaped_shortcode_brackets(
+ $text,
+ substr( $this->html, $here->start, $here->length )
+ );
+ }
if ( $needs_special_newline ) {
$output .= "\n{$text}";
diff --git a/tests/phpunit/tests/kses.php b/tests/phpunit/tests/kses.php
index 74a136569ad66..e4d7171fc33cb 100644
--- a/tests/phpunit/tests/kses.php
+++ b/tests/phpunit/tests/kses.php
@@ -153,6 +153,189 @@ public static function data_wp_kses_idempotent_inputs(): array {
);
}
+ /**
+ * Tests that sanitizing character references does not activate literal shortcode examples.
+ *
+ * @ticket 66208
+ * @dataProvider data_wp_kses_preserves_shortcode_brackets
+ *
+ * @param string $input HTML to sanitize before processing shortcodes.
+ * @param string $expected Expected rendered HTML with character references decoded.
+ * @param int $expected_calls Expected shortcode callback calls per rendering.
+ */
+ public function test_wp_kses_preserves_shortcode_brackets( $input, $expected, $expected_calls ) {
+ global $shortcode_tags;
+
+ $shortcode = 'kses_bracket_probe';
+ $had_shortcode = shortcode_exists( $shortcode );
+ $previous_shortcode = $shortcode_tags[ $shortcode ] ?? null;
+ $callback_calls = 0;
+
+ add_shortcode(
+ $shortcode,
+ static function () use ( &$callback_calls ) {
+ ++$callback_calls;
+ return 'kses-bracket-probe-output';
+ }
+ );
+
+ try {
+ $sanitized = $input;
+ for ( $pass = 1; $pass <= 2; ++$pass ) {
+ $previous = $sanitized;
+ $sanitized = wp_kses_post( $sanitized );
+ $rendered = do_shortcode( $sanitized );
+
+ $this->assertSame(
+ $expected_calls * $pass,
+ $callback_calls,
+ 'Only literal, unescaped shortcode delimiters should invoke the callback.'
+ );
+ $this->assertSame(
+ $expected,
+ html_entity_decode( $rendered, ENT_QUOTES | ENT_HTML5, 'UTF-8' ),
+ 'Sanitization should preserve the content and shortcode behavior.'
+ );
+
+ if ( 2 === $pass ) {
+ $this->assertSame( $previous, $sanitized, 'Repeated sanitization should be idempotent.' );
+ }
+ }
+ } finally {
+ if ( $had_shortcode ) {
+ add_shortcode( $shortcode, $previous_shortcode );
+ } else {
+ remove_shortcode( $shortcode );
+ }
+ }
+ }
+
+ /**
+ * Tests escaped shortcode examples through a restricted author's real post-save filters.
+ *
+ * @ticket 66208
+ */
+ public function test_post_save_preserves_shortcode_brackets_for_restricted_author() {
+ global $shortcode_tags;
+
+ $previous_user_id = get_current_user_id();
+ $shortcode = 'kses_bracket_probe';
+ $had_shortcode = shortcode_exists( $shortcode );
+ $previous_shortcode = $shortcode_tags[ $shortcode ] ?? null;
+ $callback_calls = 0;
+
+ add_shortcode(
+ $shortcode,
+ static function () use ( &$callback_calls ) {
+ ++$callback_calls;
+ return 'kses-bracket-probe-output';
+ }
+ );
+
+ try {
+ $author_id = self::factory()->user->create( array( 'role' => 'author' ) );
+ wp_set_current_user( $author_id );
+ $this->assertFalse( current_user_can( 'unfiltered_html' ) );
+
+ $post_id = wp_insert_post(
+ wp_slash(
+ array(
+ 'post_author' => $author_id,
+ 'post_title' => 'Escaped shortcode example',
+ 'post_status' => 'publish',
+ 'post_content' => '[kses_bracket_probe] [kses_bracket_probe]',
+ )
+ ),
+ true
+ );
+ $this->assertNotWPError( $post_id );
+
+ $stored = get_post_field( 'post_content', $post_id, 'raw' );
+ $this->assertSame(
+ '[kses_bracket_probe] [kses_bracket_probe]',
+ html_entity_decode( $stored, ENT_QUOTES | ENT_HTML5, 'UTF-8' ),
+ 'Saving should preserve both the escaped example and the actual shortcode.'
+ );
+
+ $rendered = do_shortcode( $stored );
+ $this->assertSame( 1, $callback_calls, 'Only the actual shortcode should execute after saving.' );
+ $this->assertSame(
+ '[kses_bracket_probe] kses-bracket-probe-output',
+ html_entity_decode( $rendered, ENT_QUOTES | ENT_HTML5, 'UTF-8' )
+ );
+ } finally {
+ if ( $had_shortcode ) {
+ add_shortcode( $shortcode, $previous_shortcode );
+ } else {
+ remove_shortcode( $shortcode );
+ }
+ wp_set_current_user( $previous_user_id );
+ }
+ }
+
+ /**
+ * Data provider for test_wp_kses_preserves_shortcode_brackets().
+ *
+ * @return array[]
+ */
+ public static function data_wp_kses_preserves_shortcode_brackets(): array {
+ $examples = array(
+ 'decimal references' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'hexadecimal references' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'named references' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'named bracket aliases' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'leading decimal zeros' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'leading hexadecimal zeros' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'encoded opener' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'encoded closer' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'decimal without semicolons' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'hexadecimal without semicolons' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'nested references' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'nested closer' => array( '[kses_bracket_probe]', '[kses_bracket_probe]', 0 ),
+ 'literal shortcode' => array( '[kses_bracket_probe]', 'kses-bracket-probe-output', 1 ),
+ 'escaped literal shortcode' => array( '[[kses_bracket_probe]]', '[kses_bracket_probe]', 0 ),
+ 'encoded then literal shortcode' => array( '[kses_bracket_probe] [kses_bracket_probe]', '[kses_bracket_probe] kses-bracket-probe-output', 1 ),
+ 'literal then encoded shortcode' => array( '[kses_bracket_probe] [kses_bracket_probe]', 'kses-bracket-probe-output [kses_bracket_probe]', 1 ),
+ 'unknown entity before mixed shortcodes' => array( '&bogus; [kses_bracket_probe] [kses_bracket_probe]', '&bogus; [kses_bracket_probe] kses-bracket-probe-output', 1 ),
+ 'non-bracket numeric reference' => array( 'Ύ [kses_bracket_probe]', "\u{038E} [kses_bracket_probe]", 0 ),
+ 'ampersand before literal shortcode' => array( '& [kses_bracket_probe]', '& kses-bracket-probe-output', 1 ),
+ );
+ $contexts = array(
+ 'text' => '%s',
+ 'code text' => '%s',
+ 'pre text' => '
%s', + ); + $data = array(); + + foreach ( $contexts as $context => $template ) { + foreach ( $examples as $description => $example ) { + $data[ "{$description} in {$context}" ] = array( + sprintf( $template, $example[0] ), + sprintf( $template, $example[1] ), + $example[2], + ); + } + } + + $data['normalizes CRLF and CR'] = array( + "Before\r\n[kses_bracket_probe]\rAfter", + "Before\n[kses_bracket_probe]\nAfter", + 0, + ); + $data['preserves leading PRE newline'] = array( + "
\r\n\r\n[kses_bracket_probe]", + "
\n\n[kses_bracket_probe]", + 0, + ); + $data['removes NULL bytes in text'] = array( + "Before\x00[kses_bracket_probe]\x00After", + 'Before[kses_bracket_probe]After', + 0, + ); + + return $data; + } + /** * Test video tag. *