Make WordPress Core

Changeset 65901


Ignore:
Timestamp:
12/03/2026 08:08:01 PM (less than one hour ago)
Author:
dmsnell
Message:

KSES: Normalize text content while preserving escaped characters.

When wp_kses() was rewritten to rely on the HTML API for its HTML parsing, a decision was made to fully-normalize text nodes, un-escaping text and re-escaping deterministically. This raised several issues relating to syntax which had been intentionally esacped. Notably, wp_kses() could create Shortcodes where they had been prevented by escaping the square brackets.

This patch walks back most of that change, preserving text that has been escaped. However, it still applies a weakened normalization to prevent downstream risk. Namely, it ensures the following properties of the output HTML text nodes and attribute values:

  • Escaped characters remain escaped.
  • Un-escaped syntax characters will be escaped.
  • All character references will terminate in a semicolon to remove ambiguity.
  • All leading zero digits are removed from numeric character references.
  • Hexadecimal numeric character comprise a lowercase x and uppercase hex digits.
  • Invalid numeric character references are replced by the Unicode Replacement Character.

Given the undecidedness of knowing which original characters were escaped when attribute values are modified by filtering funtions, if an attribute value is changed during sanitization, it will be entirely re-encoded as it was before this patch.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/15971
Discussed in: https://core.trac.wordpress.org/ticket/66673

Follow-up to r64233.

Props abcd95, dmsnell, jonsurrell, mmtr86, salvatorecorsi, therssoftware.
Fixes #66673, See #66208.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/kses.php

    r65875 r65901  
    11981198
    11991199                /**
    1200                  * Sets the value of a given HTML attribute, adjusting URL attributes
    1201                  * to ensure compatibility with legacy {@see wp_kses()} behavior.
     1200                 * Returns a representation of the given substring of input HTML text content where the
     1201                 * character references are normalized and unescaped syntax characters are escaped.
    12021202                 *
    12031203                 * @since 7.2.0
    12041204                 * @access private
     1205                 * @ignore
    12051206                 *
    1206                  * @param string      $name  Name of HTML attribute to set.
    1207                  * @param string|true $value Value to set; `true` for boolean attribute.
    1208                  * @return bool Whether the attribute was set.
     1207                 * @param 'data'|'attribute' $context Apply decoding rules pertaining to the appropriate context.
     1208                 * @param string             $html    Raw HTML document containing the referenced text content.
     1209                 * @param int                $start   The text span starts here.
     1210                 * @param int                $length  The text span extends this many bytes.
     1211                 * @return string Normalized text node content.
    12091212                 */
    1210                 public function set_attribute( $name, $value ): bool {
    1211                         $lower_name = strtolower( $name );
    1212                         $is_url_ish = in_array( $lower_name, $this->uri_attributes, true );
    1213 
    1214                         if ( ! $is_url_ish || ! is_string( $value ) ) {
    1215                                 return parent::set_attribute( $name, $value );
     1213                private static function normalize_text( string $context, string $html, int $start, int $length ): string {
     1214                        $output     = '';
     1215                        $ref_length = 0;
     1216                        $at         = $start;
     1217                        $was_at     = $start;
     1218                        $end        = $start + $length;
     1219
     1220                        while ( $at < $end ) {
     1221                                $next_at = $at + strcspn( $html, '&', $at, $end - $at );
     1222                                if ( $next_at >= $end ) {
     1223                                        break;
     1224                                }
     1225
     1226                                $decoded = WP_HTML_Decoder::read_character_reference( $context, $html, $next_at, $ref_length );
     1227
     1228                                /**
     1229                                 * Decoding past the end of the text span might return a different decode than
     1230                                 * one which decodes a truncated string.
     1231                                 *
     1232                                 * In the cases of a failure to decode, the length of seven bytes comes from the
     1233                                 * fact that the longest matchable named character references containing no
     1234                                 * semicolons are seven ASCII bytes long. It’s exactly these situations within
     1235                                 * attributes where the following character might impact what’s decoded; this is
     1236                                 * what {@see WP_HTML_Decoder::read_character_reference()} might see since it
     1237                                 * doesn’t allow specifying a final offset at which to stop searching.
     1238                                 *
     1239                                 * Therefore, if a match failed within that distance from the end inside an HTML
     1240                                 * attribute’s value, then it is best to cut off the trailing part of the input
     1241                                 * and normalize that separately.
     1242                                 *
     1243                                 * Example:
     1244                                 *
     1245                                 *     '&middot=' is rejected when the length is seven because the following '='
     1246                                 *                is not unambiguous. However, that '=' was not part of the span
     1247                                 *                of text to decode.
     1248                                 *
     1249                                 *     '&middot' on the other hand, has no ambiguous follower, and will thus be
     1250                                 *               decoded in attribute value contexts.
     1251                                 */
     1252                                $remaining_length = $start + $length - $next_at;
     1253                                if (
     1254                                        $end < strlen( $html ) &&
     1255                                        (
     1256                                                ( null !== $decoded && $ref_length > $remaining_length ) ||
     1257                                                ( null === $decoded && 'attribute' === $context && $remaining_length <= 7 )
     1258                                        )
     1259                                ) {
     1260                                        $output .= substr( $html, $was_at, $next_at - $was_at );
     1261                                        $output .= self::normalize_text( $context, substr( $html, $next_at, $remaining_length ), 0, $remaining_length );
     1262                                        $was_at  = $end;
     1263                                        break;
     1264                                }
     1265
     1266                                /*
     1267                                 * Failure to decode implies that there’s an unescaped AMPERSAND character; escape it.
     1268                                 */
     1269                                if ( null === $decoded ) {
     1270                                        $output .= substr( $html, $was_at, $next_at - $was_at );
     1271                                        $output .= '&amp;';
     1272                                        $at      = $next_at + 1;
     1273                                        $was_at  = $at;
     1274                                        continue;
     1275                                }
     1276
     1277                                $has_semicolon = ';' === $html[ $next_at + $ref_length - 1 ];
     1278
     1279                                if ( '#' === $html[ $next_at + 1 ] ) {
     1280                                        /*
     1281                                         * Given that the character decoded at least partially, there are
     1282                                         * certain guarantees on the syntax forms here. The only exceptions
     1283                                         * are where the decoder returns U+FFFD because the digits failed
     1284                                         * to represent a valid code point.
     1285                                         */
     1286                                        $digits_end     = $next_at + $ref_length - ( $has_semicolon ? 1 : 0 );
     1287                                        $hex_sigil      = $html[ $next_at + 2 ];
     1288                                        $is_hex         = 'x' === $hex_sigil || 'X' === $hex_sigil;
     1289                                        $digits_at      = $next_at + ( $is_hex ? 3 : 2 );
     1290                                        $zero_count     = strspn( $html, '0', $digits_at, $digits_end - $digits_at );
     1291                                        $non_zero_start = $digits_at + $zero_count;
     1292                                        $digit_count    = $digits_end - $non_zero_start;
     1293                                        $has_no_lowers  = strcspn( $html, 'abcdef', $digits_at + $zero_count, $digit_count ) === $digit_count;
     1294
     1295                                        /*
     1296                                         * Only an actual numeric character reference indicating U+FFFD should be retained;
     1297                                         * other malformed sequences should actually receive the U+FFFD in the output.
     1298                                         */
     1299                                        if (
     1300                                                "\u{FFFD}" === $decoded &&
     1301                                                ( 0 === $digit_count || 0 !== substr_compare( $html, $is_hex ? 'FFFD' : '65533', $digits_at + $zero_count, $digit_count, true ) )
     1302                                        ) {
     1303                                                $output .= substr( $html, $was_at, $next_at - $was_at );
     1304                                                $output .= "\u{FFFD}";
     1305                                                $at      = $next_at + $ref_length;
     1306                                                $was_at  = $at;
     1307                                                continue;
     1308                                        }
     1309
     1310                                        // Fully-normalized numeric character references may be passed through.
     1311                                        if ( 0 === $zero_count && $has_semicolon && ( ! $is_hex || ( 'x' === $hex_sigil && $has_no_lowers ) ) ) {
     1312                                                $at = $next_at + $ref_length;
     1313                                                continue;
     1314                                        }
     1315
     1316                                        // Otherwise rebuild them.
     1317                                        $sigil   = $is_hex ? 'x' : '';
     1318                                        $digits  = strtoupper( substr( $html, $digits_at + $zero_count, $digit_count ) );
     1319                                        $output .= substr( $html, $was_at, $next_at - $was_at );
     1320                                        $output .= "&#{$sigil}{$digits};";
     1321                                        $at      = $next_at + $ref_length;
     1322                                        $was_at  = $at;
     1323                                        continue;
     1324                                }
     1325
     1326                                // Semicolon-terminated named character references may pass through.
     1327                                if ( $has_semicolon ) {
     1328                                        $at = $next_at + $ref_length;
     1329                                        continue;
     1330                                }
     1331
     1332                                // Otherwise add the missing semicolon.
     1333                                $output .= substr( $html, $was_at, $next_at + $ref_length - $was_at );
     1334                                $output .= ';';
     1335                                $at      = $next_at + $ref_length;
     1336                                $was_at  = $at;
    12161337                        }
    12171338
    1218                         $escaped = strtr(
    1219                                 $value,
    1220                                 array(
    1221                                         '<' => '&lt;',
    1222                                         '>' => '&gt;',
    1223                                         '&' => '&amp;',
    1224                                         '"' => '&quot;',
    1225                                         "'" => '&apos;',
    1226                                 )
     1339                        if ( $start === $was_at ) {
     1340                                $output = substr( $html, $start, $length );
     1341                        } elseif ( $was_at < $end ) {
     1342                                $output .= substr( $html, $was_at, $end - $was_at );
     1343                        }
     1344
     1345                        return strtr(
     1346                                $output,
     1347                                'attribute' === $context
     1348                                        ? array(
     1349                                                '<' => '&lt;',
     1350                                                '>' => '&gt;',
     1351                                                '"' => '&quot;',
     1352                                                "'" => '&apos;',
     1353                                        )
     1354                                        : array(
     1355                                                '<' => '&lt;',
     1356                                                '>' => '&gt;',
     1357                                        )
    12271358                        );
    1228 
    1229                         // Set a benign placeholder to replace below.
    1230                         if ( ! parent::set_attribute( $name, true ) ) {
    1231                                 return false;
    1232                         }
    1233 
    1234                         $this->lexical_updates[ $lower_name ]->text = " {$lower_name}=\"{$escaped}\"";
    1235 
    1236                         return true;
    12371359                }
    12381360
    … …  
    14551577                        $template_depth            = 0;
    14561578                        $output                    = '';
     1579                        $input_special_newline_at  = PHP_INT_MIN;
    14571580                        $special_newline_at        = PHP_INT_MIN;
    14581581                        $foreign_content_starts_at = PHP_INT_MAX;
    … …  
    14851608                                $is_closer  = $this->is_tag_closer();
    14861609                                $here       = $this->get_span();
     1610
     1611                                if ( ! $is_closer && 'html' === $namespace && ( 'PRE' === $token_name || 'LISTING' === $token_name ) ) {
     1612                                        $input_special_newline_at = $here->start + $here->length;
     1613                                }
    14871614
    14881615                                /*
    … …  
    15561683                                );
    15571684
    1558                                 /*
    1559                                  * While content inside integration points is generally not allowed here,
    1560                                  * character data inside the MathML text elements _is_ allowed. This is
    1561                                  * because the rules only change slightly: NULL bytes are removed instead
    1562                                  * of being replaced with the Unicode replacement character U+FFFD; and
    1563                                  * active formats are reconstructed. The format reconstruction doesn’t
    1564                                  * occur here but a browser will still do so; this sanitizer is generally
    1565                                  * unaware of nesting structure.
    1566                                  */
    1567                                 if ( $is_in_text_integration_point && '#text' === $token_type ) {
    1568                                         $this->change_parsing_namespace( 'html' );
    1569                                         $text = $this->get_modifiable_text();
    1570                                         $this->change_parsing_namespace( $namespace );
    1571                                 } else {
    1572                                         $text = $this->get_modifiable_text();
    1573                                 }
     1685                                $text = ( '#text' !== $token_type ) ? $this->get_modifiable_text() : '';
    15741686
    15751687                                /*
    … …  
    16471759                                                }
    16481760
    1649                                                 $needs_special_newline = (
    1650                                                         strlen( $output ) === $special_newline_at &&
    1651                                                         1 === strspn( $text, "\n\r", 0, 1 )
    1652                                                 );
    1653 
    1654                                                 $text = strtr(
    1655                                                         $text,
    1656                                                         array(
    1657                                                                 "\r" => '&#xD;',
    1658                                                                 '<'  => '&lt;',
    1659                                                                 '&'  => '&amp;',
    1660                                                                 '>'  => '&gt;',
    1661                                                         )
    1662                                                 );
    1663 
    1664                                                 if ( $needs_special_newline ) {
    1665                                                         $output .= "\n{$text}";
    1666                                                 } else {
    1667                                                         $output .= $text;
     1761                                                $text = self::normalize_text( 'data', $this->html, $here->start, $here->length );
     1762                                                $text = str_replace( array( "\r\n", "\r" ), "\n", $text );
     1763
     1764                                                /*
     1765                                                 * A PRE or LISTING element consumes a leading newline even if stripped from the
     1766                                                 * output, so detect and remove one, remembering the original leading newline.
     1767                                                 */
     1768                                                $special_newline = "\n";
     1769                                                if ( $here->start === $input_special_newline_at ) {
     1770                                                        $starting_char  = $text[0] ?? '';
     1771                                                        $newline_length = 1;
     1772                                                        if ( '&' === $starting_char ) {
     1773                                                                $starting_char = WP_HTML_Decoder::read_character_reference( 'data', $text, 0, $newline_length );
     1774                                                        }
     1775
     1776                                                        if ( "\n" === $starting_char ) {
     1777                                                                $special_newline = substr( $text, 0, $newline_length );
     1778                                                                $text            = substr( $text, $newline_length );
     1779                                                        }
    16681780                                                }
     1781
     1782                                                $text = ( 'html' === $namespace || $is_in_text_integration_point )
     1783                                                        ? str_replace( "\x00", '', $text )
     1784                                                        : str_replace( "\x00", "\u{FFFD}", $text );
     1785
     1786                                                if ( strlen( $output ) === $special_newline_at && strlen( $text ) > 0 ) {
     1787                                                        $starting_char = $text[0];
     1788                                                        if ( '&' === $starting_char ) {
     1789                                                                $starting_char = WP_HTML_Decoder::read_character_reference( 'data', $text );
     1790                                                        }
     1791
     1792                                                        if ( 1 === strspn( $starting_char, "\r\n", 0, 1 ) ) {
     1793                                                                $output .= $special_newline;
     1794                                                        }
     1795                                                }
     1796
     1797                                                $output .= $text;
    16691798                                                break;
    16701799
    … …  
    19522081                                                                }
    19532082
    1954                                                                 $raw_value = $this->get_attribute( $name );
    1955                                                                 $value     = is_string( $raw_value ) ? $raw_value : '';
     2083                                                                $raw_value   = $this->get_attribute( $name );
     2084                                                                $value       = is_string( $raw_value ) ? $raw_value : '';
     2085                                                                $given_value = $value;
    19562086
    19572087                                                                // Process the style attribute through CSS sanitization.
    … …  
    19882118                                                                }
    19892119
    1990                                                                 $did_set = ( true === $raw_value && '' === $value )
    1991                                                                         ? $tag_maker->set_attribute( $name, true )
    1992                                                                         : $tag_maker->set_attribute( $name, $value );
    1993 
    1994                                                                 if ( $did_set ) {
    1995                                                                         unset( $required_attributes[ $name ] );
     2120                                                                // All updates are enqueued as boolean placeholders.
     2121                                                                if ( ! $tag_maker->set_attribute( $name, true ) ) {
     2122                                                                        continue;
    19962123                                                                }
     2124
     2125                                                                $should_remain_true = true === $raw_value && '' === $value;
     2126
     2127                                                                if ( ! $should_remain_true ) {
     2128                                                                        $lower_name = strtolower( $name );
     2129                                                                        $escaped    = null;
     2130
     2131                                                                        /*
     2132                                                                         * If the value did not change through sanitization, then preserve
     2133                                                                         * as much of the original escaping as possible.
     2134                                                                         *
     2135                                                                         * Fetch the original value by modifying it from the input string,
     2136                                                                         * reading the span, and then erasing the update.
     2137                                                                         */
     2138                                                                        if (
     2139                                                                                is_string( $raw_value ) &&
     2140                                                                                $given_value === $value &&
     2141                                                                                $this->set_attribute( $lower_name, true )
     2142                                                                        ) {
     2143                                                                                $original     = $this->lexical_updates[ $lower_name ];
     2144                                                                                $original_at  = $original->start;
     2145                                                                                $original_end = $original->start + $original->length;
     2146                                                                                unset( $this->lexical_updates[ $lower_name ] );
     2147
     2148                                                                                // Assignment follows the first `=` not at the beginning of the attribute name.
     2149                                                                                $equals_at = $original_at + 1 + strcspn( $this->html, '=', $original_at + 1, $original->length - 1 );
     2150                                                                                if ( $equals_at < $original_end && 1 === strspn( $this->html, '"\'', $original_end - 1, 1 ) ) {
     2151                                                                                        // This might be a quoted attribute; find the opening quote.
     2152                                                                                        $quoting_at = $equals_at + 1 + strspn( $this->html, " \t\f\r\n", $equals_at + 1, $original_end - $equals_at - 1 );
     2153                                                                                        if ( $this->html[ $original_end - 1 ] === $this->html[ $quoting_at ] ) {
     2154                                                                                                $escaped = self::normalize_text( 'attribute', $this->html, $quoting_at + 1, $original_end - $quoting_at - 2 );
     2155                                                                                                $escaped = str_replace( "\r\n", "\n", $escaped );
     2156                                                                                                $escaped = str_replace( "\r", "\n", $escaped );
     2157                                                                                                $escaped = str_replace( "\x00", "\u{FFFD}", $escaped );
     2158                                                                                        }
     2159                                                                                }
     2160                                                                        }
     2161
     2162                                                                        if ( ! isset( $escaped ) ) {
     2163                                                                                $escaped = strtr(
     2164                                                                                        $value,
     2165                                                                                        array(
     2166                                                                                                "\r" => '&#xD;',
     2167                                                                                                '"'  => '&quot;',
     2168                                                                                                "'"  => '&apos;',
     2169                                                                                                '<'  => '&lt;',
     2170                                                                                                '&'  => '&amp;',
     2171                                                                                                '>'  => '&gt;',
     2172                                                                                                '['  => '&#x5B;',
     2173                                                                                                ']'  => '&#x5D;',
     2174                                                                                        )
     2175                                                                                );
     2176                                                                        }
     2177
     2178                                                                        $tag_maker->lexical_updates[ $lower_name ]->text = " {$lower_name}=\"{$escaped}\"";
     2179                                                                }
     2180
     2181                                                                unset( $required_attributes[ $name ] );
    19972182                                                        }
    19982183                                                }
    … …  
    20302215                                                                case 'TEXTAREA':
    20312216                                                                case 'TITLE':
    2032                                                                         /*
    2033                                                                          * RCDATA nodes can be safely escaped, but this must be
    2034                                                                          * done after enqueuing the update to avoid double-escaping.
    2035                                                                          */
    2036                                                                         $tag_maker->set_modifiable_text( $text );
    2037                                                                         $tag_maker->lexical_updates['modifiable text']->text = strtr(
    2038                                                                                 $tag_maker->lexical_updates['modifiable text']->text,
     2217                                                                        // Extract the raw text span via temporarily modifying the modifiable text.
     2218                                                                        $this->set_modifiable_text( '' );
     2219                                                                        $raw_text_bounds = $this->lexical_updates['modifiable text'];
     2220                                                                        unset( $this->lexical_updates['modifiable text'] );
     2221
     2222                                                                        $normalized_text = self::normalize_text( 'data', $this->html, $raw_text_bounds->start, $raw_text_bounds->length );
     2223                                                                        $normalized_text = strtr(
     2224                                                                                $normalized_text,
    20392225                                                                                array(
    20402226                                                                                        "\x00" => "\u{FFFD}",
    … …  
    20422228                                                                                )
    20432229                                                                        );
     2230
     2231                                                                        /*
     2232                                                                         * RCDATA nodes can be safely escaped, but this must be
     2233                                                                         * done after enqueuing the update to avoid double-escaping.
     2234                                                                         */
     2235                                                                        $tag_maker->set_modifiable_text( '' );
     2236                                                                        $tag_maker->lexical_updates['modifiable text']->text = $normalized_text;
    20442237                                                                        break;
    20452238
  • trunk/tests/phpunit/tests/kses.php

    r65040 r65901  
    150150                                        ),
    151151                                ),
     152                        ),
     153                        array(
     154                                '<pre class="wp-block-code"><code>&#91;audio src="https://example.com/a.mp3"]</code></pre>',
     155                        ),
     156                        array(
     157                                "<pre>\n\nHello</pre>",
     158                                array( 'pre' => array() ),
    152159                        ),
    153160                );
    … …  
    30233030                );
    30243031        }
     3032
     3033        /**
     3034         * Tests character reference handling in text nodes and attribute values.
     3035         *
     3036         * @ticket 66673
     3037         *
     3038         * @dataProvider data_wp_kses_preserves_escaped_character_references
     3039         *
     3040         * @param string     $input             HTML to sanitize.
     3041         * @param string     $expected          Expected sanitized HTML.
     3042         * @param array|null $allowed_html      Optional. Allowed HTML configuration. Defaults to the post context.
     3043         * @param string[]   $allowed_protocols Optional. Allowed URL protocols. Empty uses wp_allowed_protocols().
     3044         */
     3045        public function test_wp_kses_preserves_escaped_character_references( string $input, string $expected, ?array $allowed_html = null, array $allowed_protocols = array() ): void {
     3046                $this->assertSame(
     3047                        $expected,
     3048                        wp_kses( $input, $allowed_html ?? 'post', $allowed_protocols ),
     3049                        'Should have preserved original text escaping where possible.'
     3050                );
     3051        }
     3052
     3053        /**
     3054         * Data provider for test_wp_kses_preserves_escaped_character_references().
     3055         *
     3056         * @return array[]
     3057         */
     3058        public static function data_wp_kses_preserves_escaped_character_references(): array {
     3059                return array(
     3060                        'code block with decimal entity'               => array(
     3061                                '<pre class="wp-block-code"><code>&#91;audio src="https://example.com/a.mp3"]</code></pre>',
     3062                                '<pre class="wp-block-code"><code>&#91;audio src="https://example.com/a.mp3"]</code></pre>',
     3063                        ),
     3064                        'code block with hexadecimal entity'           => array(
     3065                                '<pre class="wp-block-code"><code>&#x5b;audio src="https://example.com/a.mp3"]</code></pre>',
     3066                                '<pre class="wp-block-code"><code>&#x5B;audio src="https://example.com/a.mp3"]</code></pre>',
     3067                        ),
     3068                        'code block with named entity'                 => array(
     3069                                '<pre class="wp-block-code"><code>&lbrack;audio src="https://example.com/a.mp3"]</code></pre>',
     3070                                '<pre class="wp-block-code"><code>&lbrack;audio src="https://example.com/a.mp3"]</code></pre>',
     3071                        ),
     3072                        'leading zeros and missing semicolon'          => array(
     3073                                '<code>&#0091;gallery]</code> and <p>&#91gallery]</p>',
     3074                                '<code>&#91;gallery]</code> and <p>&#91;gallery]</p>',
     3075                        ),
     3076                        'literal brackets remain unescaped'            => array(
     3077                                '<code>[gallery]</code> and <p>[gallery]</p>',
     3078                                '<code>[gallery]</code> and <p>[gallery]</p>',
     3079                        ),
     3080                        'disallowed code element keeps entity'         => array(
     3081                                '<code>&#91;gallery]</code>',
     3082                                '&#91;gallery]',
     3083                                array( 'p' => array() ),
     3084                        ),
     3085                        'existing entities remain untouched'           => array(
     3086                                '<p>&lt;tag&gt; &amp; &nbsp;&mdash;&#60;</p>',
     3087                                '<p>&lt;tag&gt; &amp; &nbsp;&mdash;&#60;</p>',
     3088                        ),
     3089                        'stray ampersand and angle brackets'           => array(
     3090                                '<p>AT&T & 1 < 2 > 0</p>',
     3091                                '<p>AT&amp;T &amp; 1 &lt; 2 &gt; 0</p>',
     3092                        ),
     3093                        'pre leading newline preserved'                => array(
     3094                                "<pre>\n\nHello</pre>",
     3095                                "<pre>\n\nHello</pre>",
     3096                        ),
     3097                        'pre leading escaped newline preserved'        => array(
     3098                                "<pre>&NewLine;\nHello</pre>",
     3099                                "<pre>&NewLine;\nHello</pre>",
     3100                        ),
     3101                        'pre leading escaped newline preserved after skipped tags' => array(
     3102                                "<pre><img>&NewLine;\nHello</pre>",
     3103                                "<pre>\n&NewLine;\nHello</pre>",
     3104                                array( 'pre' => true ),
     3105                        ),
     3106                        'newline normalization after skipped tags'     => array(
     3107                                "<p>A\r<drop></drop>\nB</p>",
     3108                                "<p>A\n\nB</p>",
     3109                                array( 'p' => true ),
     3110                        ),
     3111                        'character references do not merge after skipped tags' => array(
     3112                                '<p>&not<drop></drop>in;</p>',
     3113                                '<p>&not;in;</p>',
     3114                        ),
     3115                        'stripped pre consumes a leading LF reference' => array(
     3116                                '<pre>&NewLine;Hello</pre>',
     3117                                'Hello',
     3118                                array(),
     3119                        ),
     3120                        'leading NUL precedes LF in a stripped pre'    => array(
     3121                                "<pre>\x00\nHello</pre>",
     3122                                "\nHello",
     3123                                array(),
     3124                        ),
     3125                        'pre pads a referenced CR'                     => array(
     3126                                '<pre>&#13;Hello</pre>',
     3127                                "<pre>\n&#13;Hello</pre>",
     3128                        ),
     3129                        'quoted attribute with whitespace around the equals sign' => array(
     3130                                "<p title \t=  '&#65;'>x</p>",
     3131                                '<p title="&#65;">x</p>',
     3132                        ),
     3133                        'unquoted attribute ending in a quote'         => array(
     3134                                '<p title=one"&#65;two">x</p>',
     3135                                '<p title="one&quot;Atwo&quot;">x</p>',
     3136                        ),
     3137                );
     3138        }
     3139
     3140        /**
     3141         * Tests that shortcodes inside Code blocks do not execute after wp_kses_post().
     3142         *
     3143         * @ticket 66673
     3144         */
     3145        public function test_shortcode_in_code_block_does_not_run_after_wp_kses_post(): void {
     3146                $callback_calls = 0;
     3147
     3148                add_shortcode(
     3149                        'kses_code_test',
     3150                        static function () use ( &$callback_calls ) {
     3151                                ++$callback_calls;
     3152                                return 'rendered-shortcode';
     3153                        }
     3154                );
     3155
     3156                try {
     3157                        $input     = '<pre class="wp-block-code"><code>&#91;kses_code_test]</code></pre><p>[kses_code_test]</p>';
     3158                        $sanitized = wp_kses_post( $input );
     3159
     3160                        $this->assertSame(
     3161                                '<pre class="wp-block-code"><code>&#91;kses_code_test]</code></pre><p>[kses_code_test]</p>',
     3162                                $sanitized,
     3163                                'wp_kses_post() should keep &#91; encoded inside the Code block while preserving [ outside it.'
     3164                        );
     3165
     3166                        $rendered = do_shortcode( $sanitized );
     3167
     3168                        $this->assertSame(
     3169                                1,
     3170                                $callback_calls,
     3171                                'do_shortcode() should only execute the shortcode outside the Code block.'
     3172                        );
     3173                        $this->assertSame(
     3174                                '<pre class="wp-block-code"><code>&#091;kses_code_test]</code></pre><p>rendered-shortcode</p>',
     3175                                $rendered,
     3176                                'Rendered output should leave the Code block shortcode example unexecuted.'
     3177                        );
     3178                } finally {
     3179                        remove_shortcode( 'kses_code_test' );
     3180                }
     3181        }
    30253182}
Note: See TracChangeset for help on using the changeset viewer.