Make WordPress Core

Changeset 64233


Ignore:
Timestamp:
10/07/2026 07:52:13 PM (5 weeks ago)
Author:
dmsnell
Message:

KSES: Reimplement with Tag Processor.

This patch introduces a new version of wp_kses(), temporarily living alongside the legacy implementation in a new function named wp_sanitize_html_kses(). This new implementation is based on the HTML API, which provides more reliable parsing of HTML inputs.

  • A new filter, wp_kses_force_legacy_parser, offers an opt-out for the new code, and for testing side-by-side.
  • Many tests have been updated because of hard-coded values which result from historic HTML parsing oddities. They have been updated to reflect the newer parsing, the spec-compliant parsing.

There should be no changes necessary to calling function with this change.

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

Props barry, dmsnell, johnbillion, jonsurrell, jorbin, rfaile313, westonruter.
Fixes #37698, #58921, #59310, #63208.

Location:
trunk
Files:
26 edited

Legend:

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

    r64120 r64233  
    307307add_filter( 'pre_kses', 'wp_pre_kses_less_than' );
    308308add_filter( 'pre_kses', 'wp_pre_kses_block_attributes', 10, 3 );
     309add_filter( 'wp_kses_force_legacy_parser', '__return_false' );
    309310add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 );
    310311add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
  • trunk/src/wp-includes/formatting.php

    r64163 r64233  
    53195319 * @since 2.3.0
    53205320 *
     5321 * @global string $wp_kses_operating_mode Indicates if this filter should run.
     5322 *
    53215323 * @param string $content Text to be converted.
    53225324 * @return string Converted text.
    53235325 */
    53245326function wp_pre_kses_less_than( $content ) {
     5327        global $wp_kses_operating_mode;
     5328
     5329        if ( 'legacy' !== ( $wp_kses_operating_mode ?? 'legacy' ) ) {
     5330                return $content;
     5331        }
     5332
    53255333        return preg_replace_callback( '%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $content );
    53265334}
    … …  
    53465354 *
    53475355 * @since 5.3.1
     5356 *
     5357 * @global string $wp_kses_operating_mode Indicates if this filter should run.
    53485358 *
    53495359 * @param string         $content           Content to be run through KSES.
    … …  
    53555365 */
    53565366function wp_pre_kses_block_attributes( $content, $allowed_html, $allowed_protocols ) {
     5367        global $wp_kses_operating_mode;
     5368
     5369        if ( 'legacy' !== ( $wp_kses_operating_mode ?? 'legacy' ) ) {
     5370                return $content;
     5371        }
     5372
    53575373        /*
    53585374         * `filter_block_content` is expected to call `wp_kses`. Temporarily remove
  • trunk/src/wp-includes/kses.php

    r64163 r64233  
    5656// (e.g. if using namespaces / autoload in the current PHP environment).
    5757global $allowedposttags, $allowedtags, $allowedentitynames, $allowedxmlentitynames;
     58
     59/**
     60 * Indicates which implementation of {@see \wp_kses()} is running.
     61 *
     62 * Nominally `legacy` unless temporarily-switched for {@see \wp_sanitize_html_kses()}.
     63 * It’s safe to latch this into `legacy`.
     64 *
     65 * @global 'legacy'|'html-api' $wp_kses_operating_mode
     66 *
     67 * @since 7.2.0
     68 */
     69global $wp_kses_operating_mode;
     70$wp_kses_operating_mode = 'legacy';
    5871
    5972if ( ! CUSTOM_TAGS ) {
    … …  
    949962 * @see wp_allowed_protocols() for the default allowed protocols in link URLs.
    950963 *
     964 * @see 'wp_kses_force_legacy_parser' to opt-out of the new HTML-API-based implementation.
     965 *
    951966 * @since 1.0.0
     967 * @since 7.2.0 Rewritten with the HTML API for more reliable parsing.
     968 *
     969 * @global string $wp_kses_operating_mode
    952970 *
    953971 * @param string         $content           Text content to filter.
    954972 * @param array[]|string $allowed_html      An array of allowed HTML elements and attributes,
    955  *                                          or a context name such as 'post'. See wp_kses_allowed_html()
     973 *                                          or a context name such as 'post'. {@see wp_kses_allowed_html()}
    956974 *                                          for the list of accepted context names.
    957975 * @param string[]       $allowed_protocols Optional. Array of allowed URL protocols.
    … …  
    960978 */
    961979function wp_kses( $content, $allowed_html, $allowed_protocols = array() ) {
     980        global $wp_kses_operating_mode;
     981
     982        /**
     983         * Filters whether to rely on the legacy parsing inside `wp_kses()`.
     984         *
     985         * @since 7.2.0
     986         *
     987         * @param bool $force_legacy_parser Whether to force using the legacy parser
     988         *                                  instead of relying on the HTML API.
     989         */
     990        if ( ! apply_filters( 'wp_kses_force_legacy_parser', true ) ) {
     991                return wp_sanitize_html_kses( (string) $content, $allowed_html, $allowed_protocols );
     992        }
     993
    962994        if ( empty( $allowed_protocols ) ) {
    963995                $allowed_protocols = wp_allowed_protocols();
    … …  
    966998        $content = wp_kses_no_null( $content, array( 'slash_zero' => 'keep' ) );
    967999        $content = wp_kses_normalize_entities( $content );
    968         $content = wp_kses_hook( $content, $allowed_html, $allowed_protocols );
     1000
     1001        // Ensure that the legacy pre-kses filters run by setting the operating mode appropriately.
     1002        try {
     1003                $previous_kses_mode     = $wp_kses_operating_mode;
     1004                $wp_kses_operating_mode = 'legacy';
     1005                $content                = wp_kses_hook( $content, $allowed_html, $allowed_protocols );
     1006        } finally {
     1007                $wp_kses_operating_mode = $previous_kses_mode;
     1008        }
    9691009
    9701010        return wp_kses_split( $content, $allowed_html, $allowed_protocols );
     1011}
     1012
     1013/**
     1014 * Filters HTML content, sanitizing according to given policies.
     1015 *
     1016 * Modern implementation of {@see wp_kses()} built with the HTML API.
     1017 *
     1018 * @since 7.2.0
     1019 *
     1020 * @global string $wp_kses_operating_mode
     1021 *
     1022 * @access private This function should not be called directly, as it serves a transitionary
     1023 *                 role while replacing the implementation of {@see \wp_kses()}.
     1024 *
     1025 * @param string $content              Text content to filter.
     1026 * @param array[]|string $allowed_html An array of allowed HTML elements and attributes,
     1027 *                                     or a context name such as 'post'. See wp_kses_allowed_html()
     1028 *                                     for the list of accepted context names.
     1029 * @param string[] $allowed_protocols  Optional. Array of allowed URL protocols.
     1030 *                                     Defaults to the result of wp_allowed_protocols().
     1031 * @return string Filtered content containing only the allowed HTML.
     1032 */
     1033function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = array() ) {
     1034        global $wp_kses_operating_mode;
     1035
     1036        $specified_allowed_html = $allowed_html;
     1037
     1038        $allowed_protocols = empty( $allowed_protocols )
     1039                ? wp_allowed_protocols()
     1040                : $allowed_protocols;
     1041
     1042        // Preserve legacy behavior of stripping unwanted C0 control characters.
     1043        $content = preg_replace( '/[\x01-\x08\x0B\x0C\x0E-\x1F]/', '', $content );
     1044
     1045        /*
     1046         * Call legacy pre-kses filters that might have been added by plugins.
     1047         *
     1048         * Also set the operating mode to bypass the pre-filters from the legacy
     1049         * implementation of `wp_kses()`, as these filters are now run in-band
     1050         * during the processing of the input document.
     1051         */
     1052        try {
     1053                $previous_kses_mode     = $wp_kses_operating_mode;
     1054                $wp_kses_operating_mode = 'html-api';
     1055                $content                = wp_kses_hook( $content, $specified_allowed_html, $allowed_protocols );
     1056        } finally {
     1057                $wp_kses_operating_mode = $previous_kses_mode;
     1058        }
     1059
     1060        $allowed_html = is_array( $allowed_html )
     1061                ? $allowed_html
     1062                : wp_kses_allowed_html( $allowed_html );
     1063
     1064        /*
     1065         * The explanation for this call is that “the quoting from `preg_replace(//e)`
     1066         * requires” it, but this version of `wp_kses()` doesn’t rely on PCRE functions
     1067         * to parse HTML. Given that this corrupts text, it will be skipped.
     1068         */
     1069        //$content = wp_kses_stripslashes( $content );
     1070
     1071        $processor = new class( $content, $specified_allowed_html, $allowed_html, $allowed_protocols, wp_kses_uri_attributes() ) extends WP_HTML_Tag_Processor {
     1072                /**
     1073                 * An array of allowed HTML elements and attributes, or a context name such as 'post'.
     1074                 *
     1075                 * It’s important to store this alongside the resolved allowable HTML because some
     1076                 * filters in some plugins look for the string values, e.g. for “post” instead of
     1077                 * the resolved array, and apply logic based on that context.
     1078                 *
     1079                 * @see wp_kses_allowed_html() for the list of accepted context names.
     1080                 * @see self::$allowed_html for the resolved array of allowable HTML elements and attributes.
     1081                 *
     1082                 * @since 7.2.0
     1083                 *
     1084                 * @var array[]|string
     1085                 */
     1086                private $specified_allowed_html;
     1087
     1088                /**
     1089                 * An array of allowed HTML elements and attributes.
     1090                 *
     1091                 * This array of allowable HTML elements and attributes is resolved from the value provided
     1092                 * to the sanitizer function. It’s resolved at the start to avoid repeatedly calling the
     1093                 * filter stack and array-merging computations. However, it’s still necessary to carry along
     1094                 * the provided context so that filters expecting the array-or-string version continue to
     1095                 * operate properly.
     1096                 *
     1097                 * @see self::$specified_allowed_html
     1098                 *
     1099                 * @since 7.2.0
     1100                 *
     1101                 * @var array[]
     1102                 */
     1103                private $allowed_html;
     1104
     1105                /**
     1106                 * Array of allowed URL protocols.
     1107                 *
     1108                 * @see \wp_allowed_protocols()
     1109                 *
     1110                 * @since 7.2.0
     1111                 *
     1112                 * @var string[]
     1113                 */
     1114                private $allowed_protocols;
     1115
     1116                /**
     1117                 * List of attributes whose values are expected to be considered URLs.
     1118                 *
     1119                 * @see \wp_kses_uri_attributes()
     1120                 *
     1121                 * @since 7.2.0
     1122                 *
     1123                 * @var string[]
     1124                 */
     1125                private $uri_attributes;
     1126
     1127                /**
     1128                 * Tracks balanced tags when inside foreign content.
     1129                 *
     1130                 * @since 7.2.0
     1131                 *
     1132                 * @var string[]
     1133                 */
     1134                private $foreign_content_stack = array();
     1135
     1136                /**
     1137                 * Tracks how deeply into a MathML ANNOTATION-XML element the current token is;
     1138                 * an optimization to avoid checking up the open-element stack on every token.
     1139                 *
     1140                 * @since 7.2.0
     1141                 *
     1142                 * @var int
     1143                 */
     1144                private $math_annotation_xml_depth = 0;
     1145
     1146                /**
     1147                 * Constructor function, used internally.
     1148                 *
     1149                 * @since 7.2.0
     1150                 * @access private
     1151                 *
     1152                 * @param string         $html                   Content to filter.
     1153                 * @param array[]|string $specified_allowed_html Context passed into sanitizer.
     1154                 * @param array[]        $allowed_html           Resolved allowable HTML.
     1155                 * @param string[]       $allowed_protocols      Resolved allowable protocols.
     1156                 * @param string[]       $uri_attributes         Resolved URI attributes.
     1157                 */
     1158                public function __construct( $html, $specified_allowed_html, $allowed_html, $allowed_protocols, $uri_attributes ) {
     1159                        parent::__construct( $html );
     1160
     1161                        $this->specified_allowed_html = $specified_allowed_html;
     1162                        $this->allowed_html           = $allowed_html;
     1163                        $this->allowed_protocols      = $allowed_protocols;
     1164                        $this->uri_attributes         = $uri_attributes;
     1165                }
     1166
     1167                /**
     1168                 * Returns the raw byte-span of the currently-matched token,
     1169                 * assuming that the parser is matched on a token.
     1170                 *
     1171                 * @since 7.2.0
     1172                 * @access private
     1173                 *
     1174                 * @return WP_HTML_Span|null
     1175                 */
     1176                private function get_span() {
     1177                        $this->set_bookmark( 'here' );
     1178
     1179                        if ( ! isset( $this->bookmarks['here'] ) ) {
     1180                                return null;
     1181                        }
     1182
     1183                        return $this->bookmarks['here'];
     1184                }
     1185
     1186                /**
     1187                 * Sets the value of a given HTML attribute, adjusting URL attributes
     1188                 * to ensure compatibility with legacy {@see wp_kses()} behavior.
     1189                 *
     1190                 * @since 7.2.0
     1191                 * @access private
     1192                 *
     1193                 * @param string      $name  Name of HTML attribute to set.
     1194                 * @param string|true $value Value to set; `true` for boolean attribute.
     1195                 * @return bool Whether the attribute was set.
     1196                 */
     1197                public function set_attribute( $name, $value ): bool {
     1198                        $lower_name = strtolower( $name );
     1199                        $is_url_ish = in_array( $lower_name, $this->uri_attributes, true );
     1200
     1201                        if ( ! $is_url_ish || ! is_string( $value ) ) {
     1202                                return parent::set_attribute( $name, $value );
     1203                        }
     1204
     1205                        $escaped = strtr(
     1206                                $value,
     1207                                array(
     1208                                        '<' => '&lt;',
     1209                                        '>' => '&gt;',
     1210                                        '&' => '&amp;',
     1211                                        '"' => '&quot;',
     1212                                        "'" => '&apos;',
     1213                                )
     1214                        );
     1215
     1216                        // Set a benign placeholder to replace below.
     1217                        if ( ! parent::set_attribute( $name, true ) ) {
     1218                                return false;
     1219                        }
     1220
     1221                        $this->lexical_updates[ $lower_name ]->text = " {$lower_name}=\"{$escaped}\"";
     1222
     1223                        return true;
     1224                }
     1225
     1226                /**
     1227                 * Indicates if the currently-matched token, given the parsed context, would require more
     1228                 * complicated parsing rules to proceed without risking escape from foreign content.
     1229                 *
     1230                 * @since 7.2.0
     1231                 * @access private
     1232                 *
     1233                 * @param bool $is_inside_mathml_text_integration_point
     1234                 * @param bool $is_inside_svg_html_integration_point
     1235                 * @return bool
     1236                 */
     1237                private function could_escape_foreign_content( bool $is_inside_mathml_text_integration_point, bool $is_inside_svg_html_integration_point ): bool {
     1238                        $token_name   = $this->get_token_name();
     1239                        $is_closer    = $this->is_tag_closer();
     1240                        $namespace    = $this->get_namespace();
     1241                        $self_closing = ! $is_closer && $this->has_self_closing_flag();
     1242
     1243                        if ( ! $is_closer && $is_inside_svg_html_integration_point ) {
     1244                                return true;
     1245                        }
     1246
     1247                        /*
     1248                         * These two elements are excepted in HTML from the normal processing
     1249                         * rules because they function in similar ways to character data.
     1250                         *
     1251                         * > The mglyph element is used to represent non-standard characters or
     1252                         * > symbols by images; the malignmark element establishes an alignment
     1253                         * > point for use within table constructs, and is otherwise invisible.
     1254                         *
     1255                         * They must contain no elements, so only allow self-closing tags.
     1256                         */
     1257                        if ( ! $is_closer && $is_inside_mathml_text_integration_point ) {
     1258                                return ! ( $self_closing && ( 'MGLYPH' === $token_name || 'MALIGNMARK' === $token_name ) );
     1259                        }
     1260
     1261                        if (
     1262                                ! $is_closer &&
     1263                                'FONT' === $token_name &&
     1264                                (
     1265                                        null !== $this->get_attribute( 'color' ) ||
     1266                                        null !== $this->get_attribute( 'face' ) ||
     1267                                        null !== $this->get_attribute( 'size' )
     1268                                )
     1269                        ) {
     1270                                return true;
     1271                        }
     1272
     1273                        if (
     1274                                ! $is_closer &&
     1275                                in_array(
     1276                                        $token_name,
     1277                                        array(
     1278                                                'B',
     1279                                                'BIG',
     1280                                                'BLOCKQUOTE',
     1281                                                'BODY',
     1282                                                'BR',
     1283                                                'CENTER',
     1284                                                'CODE',
     1285                                                'DD',
     1286                                                'DIV',
     1287                                                'DL',
     1288                                                'DT',
     1289                                                'EM',
     1290                                                'EMBED',
     1291                                                'H1',
     1292                                                'H2',
     1293                                                'H3',
     1294                                                'H4',
     1295                                                'H5',
     1296                                                'H6',
     1297                                                'HEAD',
     1298                                                'HR',
     1299                                                'I',
     1300                                                'IMG',
     1301                                                'LI',
     1302                                                'LISTING',
     1303                                                'MENU',
     1304                                                'META',
     1305                                                'NOBR',
     1306                                                'OL',
     1307                                                'P',
     1308                                                'PRE',
     1309                                                'RUBY',
     1310                                                'S',
     1311                                                'SMALL',
     1312                                                'SPAN',
     1313                                                'STRONG',
     1314                                                'STRIKE',
     1315                                                'SUB',
     1316                                                'SUP',
     1317                                                'TABLE',
     1318                                                'TT',
     1319                                                'U',
     1320                                                'UL',
     1321                                                'VAR',
     1322                                        ),
     1323                                        true
     1324                                ) ||
     1325                                (
     1326                                        $is_closer &&
     1327                                        in_array(
     1328                                                $token_name,
     1329                                                array(
     1330                                                        'BR',
     1331                                                        'P',
     1332                                                ),
     1333                                                true
     1334                                        )
     1335                                )
     1336                        ) {
     1337                                return true;
     1338                        }
     1339
     1340                        if ( 'math' === $namespace && ! $is_closer && ! $self_closing ) {
     1341                                $encoding = $this->get_attribute( 'encoding' );
     1342                                if (
     1343                                        'ANNOTATION-XML' === $token_name &&
     1344                                        is_string( $encoding ) &&
     1345                                        (
     1346                                                0 === strcasecmp( $encoding, 'text/html' ) ||
     1347                                                0 === strcasecmp( $encoding, 'application/xhtml+xml' )
     1348                                        )
     1349                                ) {
     1350                                        return true;
     1351                                }
     1352
     1353                                /*
     1354                                 * When SVG becomes a direct descendant of a MathML ANNOTATION-XML,
     1355                                 * the namespace remains `math` but there could be an SVG element
     1356                                 * with an HTML integration point. Conservatively reject any child
     1357                                 * SVG element inside a MathML ANNOTATION-XML to prevent this.
     1358                                 */
     1359                                if ( 'SVG' === $token_name && $this->math_annotation_xml_depth > 0 ) {
     1360                                        return true;
     1361                                }
     1362                        }
     1363
     1364                        return false;
     1365                }
     1366
     1367                /**
     1368                 * Indicates if a given string contains text that would parse as a block delimiter.
     1369                 *
     1370                 * @since 7.2.0
     1371                 * @access private
     1372                 *
     1373                 * @param string $text Does a block comment delimiter exist in this string value?
     1374                 * @return bool Whether a block comment delimiter of any kind was found in the given string.
     1375                 */
     1376                private static function contains_a_block_delimiter( string $text ): bool {
     1377                        if ( '' === $text ) {
     1378                                return false;
     1379                        }
     1380
     1381                        $processor = new WP_Block_Processor( $text );
     1382
     1383                        return $processor->next_delimiter();
     1384                }
     1385
     1386                /**
     1387                 * Returns a sanitized copy of the input HTML.
     1388                 *
     1389                 * @since 7.2.0
     1390                 * @access private
     1391                 *
     1392                 * @return string Sanitized copy of given input HTML.
     1393                 */
     1394                public function sanitize() {
     1395                        $template_depth            = 0;
     1396                        $output                    = '';
     1397                        $special_newline_at        = PHP_INT_MIN;
     1398                        $foreign_content_starts_at = PHP_INT_MAX;
     1399                        $open_blocks               = array();
     1400                        $open_blocks_at            = array();
     1401                        $foreign_closed_blocks     = array();
     1402
     1403                        $is_in_mathml_text_integration_point = false;
     1404                        $is_in_svg_html_integration_point    = false;
     1405
     1406                        /**
     1407                         * These are treated as void elements inside the HTML API
     1408                         * due to the special handling of their inner text content.
     1409                         */
     1410                        $special_atomic_elements = array(
     1411                                'IFRAME',
     1412                                'NOEMBED',
     1413                                'NOFRAMES',
     1414                                'SCRIPT',
     1415                                'STYLE',
     1416                                'TEXTAREA',
     1417                                'TITLE',
     1418                                'XMP',
     1419                        );
     1420
     1421                        while ( $this->next_token() ) {
     1422                                $token_name = $this->get_token_name();
     1423                                $token_type = $this->get_token_type();
     1424                                $namespace  = $this->get_namespace();
     1425                                $is_closer  = $this->is_tag_closer();
     1426                                $here       = $this->get_span();
     1427
     1428                                /*
     1429                                 * Prevent allowing NOSCRIPT elements whose parsing rules change
     1430                                 * based on whether the scripting flag is enabled in a browser.
     1431                                 * Rely on trusted inputs for producing the appropriate NOSCRIPT
     1432                                 * content, and prevent untrusted inputs from generating it.
     1433                                 */
     1434                                if ( 'NOSCRIPT' === $token_name && ! $is_closer ) {
     1435                                        break;
     1436                                }
     1437
     1438                                /*
     1439                                 * PLAINTEXT is unique in that it changes the tokenization. Everything in
     1440                                 * a document following the PLAINTEXT opening tag is interpreted as character
     1441                                 * data, meaning there are no more tags, comments, or anything. Because of
     1442                                 * this, it is the only element without a closing tag.
     1443                                 *
     1444                                 * Because of its unique role, it’s risky to leave in place because very few
     1445                                 * parsers are going to recognize the tag and properly switch modes for the
     1446                                 * rest of the document. Instead, the PLAINTEXT tag is removed and its content
     1447                                 * is re-written as an escaped text node.
     1448                                 *
     1449                                 * This changes the structure of the document, because no PLAINTEXT element
     1450                                 * will appear, and it won’t be possible to style its contents, but the element
     1451                                 * is discouraged anyway and should not be allowed from untrusted inputs.
     1452                                 */
     1453                                if ( 'PLAINTEXT' === $token_name && 'html' === $namespace && ! $is_closer ) {
     1454                                        if ( $template_depth > 0 && ! isset( $this->allowed_html['template'] ) ) {
     1455                                                break;
     1456                                        }
     1457
     1458                                        $text = substr( $this->html, $here->start + $here->length );
     1459
     1460                                        $needs_special_newline = (
     1461                                                strlen( $output ) === $special_newline_at &&
     1462                                                1 === strspn( $text, "\n\r", 0, 1 )
     1463                                        );
     1464
     1465                                        if ( $needs_special_newline ) {
     1466                                                $output .= "\n";
     1467                                        }
     1468
     1469                                        $output .= strtr(
     1470                                                $text,
     1471                                                array(
     1472                                                        "\x00" => "\u{FFFD}",
     1473                                                        "\r\n" => "\n",
     1474                                                        "\r"   => "\n",
     1475                                                        '<'    => '&lt;',
     1476                                                        '&'    => '&amp;',
     1477                                                        '>'    => '&gt;',
     1478                                                )
     1479                                        );
     1480
     1481                                        break;
     1482                                }
     1483
     1484                                $is_in_text_integration_point = (
     1485                                        $is_in_mathml_text_integration_point ||
     1486                                        ( ! $is_closer && $is_in_svg_html_integration_point )
     1487                                );
     1488
     1489                                /*
     1490                                 * While content inside integration points is generally not allowed here,
     1491                                 * character data inside the MathML text elements _is_ allowed. This is
     1492                                 * because the rules only change slightly: NULL bytes are removed instead
     1493                                 * of being replaced with the Unicode replacement character U+FFFD; and
     1494                                 * active formats are reconstructed. The format reconstruction doesn’t
     1495                                 * occur here but a browser will still do so; this sanitizer is generally
     1496                                 * unaware of nesting structure.
     1497                                 */
     1498                                if ( $is_in_text_integration_point && '#text' === $token_type ) {
     1499                                        $this->change_parsing_namespace( 'html' );
     1500                                        $text = $this->get_modifiable_text();
     1501                                        $this->change_parsing_namespace( $namespace );
     1502                                } else {
     1503                                        $text = $this->get_modifiable_text();
     1504                                }
     1505
     1506                                /*
     1507                                 * Enter the foreign content and change the parsing namespace
     1508                                 * so that the parser recognizes real self-closing elements.
     1509                                 */
     1510                                $is_svg_or_math        = 'MATH' === $token_name || 'SVG' === $token_name;
     1511                                $has_self_closing_flag = ! $is_closer && $this->has_self_closing_flag();
     1512                                if ( $is_svg_or_math && ! $is_closer && 'html' === $namespace ) {
     1513                                        $this->change_parsing_namespace( strtolower( $token_name ) );
     1514                                        $namespace = $this->get_namespace();
     1515                                }
     1516
     1517                                if ( 'html' !== $namespace && '#tag' === $token_type ) {
     1518                                        /*
     1519                                         * Ensure that only well-formed foreign content is allowed.
     1520                                         * Since un-balanced closing tags might implicitly close the
     1521                                         * open foreign-content element, these must be rejected.
     1522                                         */
     1523                                        if ( $is_closer ) {
     1524                                                $open_element = array_pop( $this->foreign_content_stack );
     1525                                                if ( null === $open_element || $token_name !== $open_element ) {
     1526                                                        break;
     1527                                                }
     1528
     1529                                                if ( 'math' === $namespace && 'ANNOTATION-XML' === $open_element ) {
     1530                                                        --$this->math_annotation_xml_depth;
     1531                                                }
     1532
     1533                                                /*
     1534                                                 * Reset the foreign content tracker so it doesn’t truncate
     1535                                                 * unintentionally after foreign content has properly closed.
     1536                                                 */
     1537                                                if ( empty( $this->foreign_content_stack ) ) {
     1538                                                        $foreign_content_starts_at = PHP_INT_MAX;
     1539                                                }
     1540                                        } else {
     1541                                                /*
     1542                                                 * Track the opening of the last transition into foreign
     1543                                                 * content so that it can be discarded when encountering
     1544                                                 * tags that would require more substantial parsing.
     1545                                                 */
     1546                                                if ( empty( $this->foreign_content_stack ) ) {
     1547                                                        $foreign_content_starts_at = strlen( $output );
     1548                                                }
     1549
     1550                                                $this->foreign_content_stack[] = $token_name;
     1551
     1552                                                if ( 'math' === $namespace && 'ANNOTATION-XML' === $token_name ) {
     1553                                                        ++$this->math_annotation_xml_depth;
     1554                                                }
     1555                                        }
     1556                                }
     1557
     1558                                if ( 'TEMPLATE' === $token_name && 'html' === $namespace && ! $is_closer ) {
     1559                                        ++$template_depth;
     1560                                }
     1561
     1562                                $skip_token = (
     1563                                        (
     1564                                                $template_depth > 0 &&
     1565                                                ! isset( $this->allowed_html['template'] )
     1566                                        ) ||
     1567                                        (
     1568                                                ! empty( $this->foreign_content_stack ) &&
     1569                                                ! isset( $this->allowed_html[ strtolower( $this->foreign_content_stack[0] ) ] )
     1570                                        )
     1571                                );
     1572
     1573                                switch ( $token_type ) {
     1574                                        case '#text':
     1575                                                if ( $skip_token ) {
     1576                                                        break;
     1577                                                }
     1578
     1579                                                $needs_special_newline = (
     1580                                                        strlen( $output ) === $special_newline_at &&
     1581                                                        1 === strspn( $text, "\n\r", 0, 1 )
     1582                                                );
     1583
     1584                                                $text = strtr(
     1585                                                        $text,
     1586                                                        array(
     1587                                                                "\r" => '&#xD;',
     1588                                                                '<'  => '&lt;',
     1589                                                                '&'  => '&amp;',
     1590                                                                '>'  => '&gt;',
     1591                                                        )
     1592                                                );
     1593
     1594                                                if ( $needs_special_newline ) {
     1595                                                        $output .= "\n{$text}";
     1596                                                } else {
     1597                                                        $output .= $text;
     1598                                                }
     1599                                                break;
     1600
     1601                                        /*
     1602                                         * Untrusted sources should not be creating these kinds of tokens,
     1603                                         * so remove them entirely from the output.
     1604                                         */
     1605                                        case '#doctype':
     1606                                        case '#presumptuous-tag':
     1607                                        case '#processing-instruction':
     1608                                                break;
     1609
     1610                                        /*
     1611                                         * It’s questionable whether these should be allowed through, but
     1612                                         * the legacy behavior supports it. Therefore, allow them as long
     1613                                         * as they don’t contain potentially confusing syntax characters.
     1614                                         */
     1615                                        case '#funky-comment':
     1616                                                if ( ! $skip_token && ! str_contains( $text, '<' ) ) {
     1617                                                        $output .= substr( $this->html, $here->start, $here->length );
     1618                                                }
     1619                                                break;
     1620
     1621                                        /*
     1622                                         * `wp_kses()` runs iteratively on the content inside of these tokens,
     1623                                         * but the content is benign in a browser.
     1624                                         */
     1625                                        case '#comment':
     1626                                                if ( $skip_token ) {
     1627                                                        break;
     1628                                                }
     1629
     1630                                                /*
     1631                                                 * There are several kinds of malformed HTML which are handled by interpreting
     1632                                                 * them as HTML comments. For example, `<?>` is called a “bogus comment” by the
     1633                                                 * HTML specification, but when loaded by a browser is equivalent to `<!--?-->`.
     1634                                                 * In this way, interacting with the DOM via JavaScript differs from handling
     1635                                                 * the textual representation of a page in PHP.
     1636                                                 *
     1637                                                 * Ignore these non-normative comment forms to protect downstream parsers which
     1638                                                 * might not be expecting their kinds of syntax. This prevents mis-parses for
     1639                                                 * code which over-simplifies HTML parsing.
     1640                                                 */
     1641                                                if ( WP_HTML_Tag_Processor::COMMENT_AS_HTML_COMMENT !== $this->get_comment_type() ) {
     1642                                                        break;
     1643                                                }
     1644
     1645                                                // Apply special filtering for block comment delimiters with JSON attributes.
     1646                                                $comment = substr( $this->html, $here->start, $here->length );
     1647
     1648                                                /*
     1649                                                 * A comment like `<!-- notes --!>` still appears as a normative HTML comment,
     1650                                                 * but as an incorrectly-closed comment. Ignore these as well, as part of only
     1651                                                 * allowing normative comment contents.
     1652                                                 */
     1653                                                $was_incorrectly_closed = '!' === $comment[ strlen( $comment ) - 2 ];
     1654                                                if ( $was_incorrectly_closed ) {
     1655                                                        break;
     1656                                                }
     1657
     1658                                                $block_processor = new WP_Block_Processor( $comment );
     1659                                                if ( $block_processor->next_token() && ! $block_processor->is_html() ) {
     1660                                                        $block_type          = $block_processor->get_block_type();
     1661                                                        $implicit_block_type = str_starts_with( $block_type, 'core/' )
     1662                                                                ? substr( $block_type, /* 'core/' */ 5 )
     1663                                                                : $block_type;
     1664
     1665                                                        switch ( $block_processor->get_delimiter_type() ) {
     1666                                                                // Track when blocks open and when they don’t self-close.
     1667                                                                case WP_Block_Processor::OPENER:
     1668                                                                        $open_blocks[]    = $implicit_block_type;
     1669                                                                        $open_blocks_at[] = strlen( $output );
     1670                                                                        break;
     1671
     1672                                                                // Track when blocks close.
     1673                                                                case WP_Block_Processor::CLOSER:
     1674                                                                        if ( empty( $open_blocks ) ) {
     1675                                                                                break 2;
     1676                                                                        }
     1677
     1678                                                                        /*
     1679                                                                         * The default parser closes any open block, even when
     1680                                                                         * the names don’t match. Preserve this behavior here
     1681                                                                         * to avoid differences in sanitization and parsing.
     1682                                                                         */
     1683                                                                        $closed_block    = array_pop( $open_blocks );
     1684                                                                        $closed_block_at = array_pop( $open_blocks_at );
     1685
     1686                                                                        if ( 'html' !== $namespace && $closed_block_at < $foreign_content_starts_at ) {
     1687                                                                                $foreign_closed_blocks[] = $closed_block;
     1688                                                                        }
     1689                                                        }
     1690
     1691                                                        // Filter block attributes for opening delimiters.
     1692                                                        if ( $block_processor->opens_block() ) {
     1693                                                                $original_attributes = $block_processor->allocate_and_return_parsed_attributes();
     1694
     1695                                                                if ( isset( $original_attributes ) ) {
     1696                                                                        $filtered_attributes = filter_block_kses_value(
     1697                                                                                $original_attributes,
     1698                                                                                $this->specified_allowed_html,
     1699                                                                                $this->allowed_protocols,
     1700                                                                                array( 'blockName' => $block_type )
     1701                                                                        );
     1702
     1703                                                                        if ( $original_attributes !== $filtered_attributes ) {
     1704                                                                                $serialized_attributes = serialize_block_attributes( $filtered_attributes );
     1705                                                                                $voider                = WP_Block_Processor::VOID === $block_processor->get_delimiter_type() ? '/' : '';
     1706                                                                                $text                  = " wp:{$implicit_block_type} {$serialized_attributes} {$voider}";
     1707                                                                        }
     1708                                                                }
     1709                                                        }
     1710                                                }
     1711
     1712                                                /*
     1713                                                 * Legacy `wp_kses()` recursively calls itself on the contents of comments.
     1714                                                 * Since comment content is not escaped, this changes the meaning of those
     1715                                                 * comments when parsed. Still, code often expects to find tag-like syntax
     1716                                                 * only when they are real tags. This legacy defect is preserved to avoid
     1717                                                 * presenting content that downstream parsers might misinterpret as markup.
     1718                                                 */
     1719                                                $text = strtr( $text, array( '<' => '&lt;' ) );
     1720
     1721                                                $output .= "<!--{$text}-->";
     1722                                                break;
     1723
     1724                                        /*
     1725                                         * True CDATA sections only exist within embedded SVG and MathML content,
     1726                                         * where they represent text data without any escaping. However, because
     1727                                         * parsers tend to vary on how to parse these, for untrusted inputs,
     1728                                         * rewrite all CDATA sections as normal escaped text.
     1729                                         */
     1730                                        case '#cdata-section':
     1731                                                if ( ! $skip_token ) {
     1732                                                        $output .= strtr(
     1733                                                                $text,
     1734                                                                array(
     1735                                                                        "\x00" => "\u{FFFD}",
     1736                                                                        '<'    => '&lt;',
     1737                                                                        '&'    => '&amp;',
     1738                                                                        '>'    => '&gt;',
     1739                                                                )
     1740                                                        );
     1741                                                }
     1742
     1743                                                break;
     1744
     1745                                        case '#tag':
     1746                                                /*
     1747                                                 * Any failures inside foreign content should return the part of
     1748                                                 * the post processed up until the entrance of the foreign content.
     1749                                                 * This is necessary because it’s only inside foreign content that
     1750                                                 * the self-closing flag indicates a self-closing element.
     1751                                                 *
     1752                                                 * While the HTML Processor can enter into SVG and MATH and track
     1753                                                 * when they close, it’s substantially more complicated and requires
     1754                                                 * considerable accounting. To avoid all of that, and to accept the
     1755                                                 * kind of content that is nominal and safe, track only when the
     1756                                                 * next tag _could_ lead to implicit changing of the parsing namespace
     1757                                                 * or insertion mode.
     1758                                                 */
     1759                                                if (
     1760                                                        'html' !== $namespace &&
     1761                                                        $this->could_escape_foreign_content(
     1762                                                                $is_in_mathml_text_integration_point,
     1763                                                                $is_in_svg_html_integration_point
     1764                                                        )
     1765                                                ) {
     1766                                                        break 2;
     1767                                                }
     1768
     1769                                                if ( $skip_token ) {
     1770                                                        break;
     1771                                                }
     1772
     1773                                                $tag_name = strtolower( $token_name );
     1774
     1775                                                // Skip unallowed elements by tag name
     1776                                                if ( ! isset( $this->allowed_html[ $tag_name ] ) ) {
     1777                                                        break;
     1778                                                }
     1779
     1780                                                if ( $is_closer ) {
     1781                                                        $output .= "</{$tag_name}>";
     1782                                                        break;
     1783                                                }
     1784
     1785                                                $is_special_atomic_element = (
     1786                                                        'html' === $namespace &&
     1787                                                        in_array( $token_name, $special_atomic_elements, true )
     1788                                                );
     1789
     1790                                                $expects_closer = ! (
     1791                                                        'html' === $namespace
     1792                                                                ? ( WP_HTML_Processor::is_void( $token_name ) || $is_special_atomic_element )
     1793                                                                : $has_self_closing_flag
     1794                                                );
     1795
     1796                                                $self_closer = ( 'html' !== $namespace && $has_self_closing_flag ) ? ' /' : '';
     1797                                                $closing_tag = $is_special_atomic_element ? "</{$tag_name}>" : '';
     1798
     1799                                                $attribute_names    = $this->get_attribute_names_with_prefix( '' );
     1800                                                $element_attributes = $this->allowed_html[ $tag_name ];
     1801
     1802                                                // Check for required attributes.
     1803                                                $required_attributes = array();
     1804                                                if ( is_array( $element_attributes ) ) {
     1805                                                        foreach ( $element_attributes as $name => $spec ) {
     1806                                                                if ( true === ( $spec['required'] ?? false ) ) {
     1807                                                                        $required_attributes[ $name ] = true;
     1808                                                                }
     1809                                                        }
     1810                                                }
     1811
     1812                                                /*
     1813                                                 * Allow `data-*` attributes.
     1814                                                 *
     1815                                                 * When specifying `$allowed_html`, the attribute name should be set as
     1816                                                 * `data-*` (not to be mixed with the HTML 4.0 `data` attribute, see
     1817                                                 * https://www.w3.org/TR/html40/struct/objects.html#adef-data).
     1818                                                 *
     1819                                                 * Note: the attribute name should only contain `A-Za-z0-9_-` chars.
     1820                                                 */
     1821                                                if ( ! empty( $element_attributes['data-*'] ) ) {
     1822                                                        if ( is_array( $attribute_names ) ) {
     1823                                                                foreach ( $attribute_names as $name ) {
     1824                                                                        if (
     1825                                                                                1 === preg_match( '/^data-[a-z0-9_-]+$/', $name ) &&
     1826                                                                                (
     1827                                                                                        ! isset( $element_attributes[ $name ] ) ||
     1828                                                                                        '' === $element_attributes[ $name ]
     1829                                                                                )
     1830                                                                        ) {
     1831                                                                                $element_attributes[ $name ] = $element_attributes['data-*'];
     1832                                                                        }
     1833                                                                }
     1834                                                        }
     1835
     1836                                                        unset( $element_attributes['data-*'] );
     1837                                                }
     1838
     1839                                                $tag_maker = new self(
     1840                                                        "<{$tag_name}{$self_closer}>{$closing_tag}",
     1841                                                        $this->specified_allowed_html,
     1842                                                        $this->allowed_html,
     1843                                                        $this->allowed_protocols,
     1844                                                        $this->uri_attributes
     1845                                                );
     1846                                                $tag_maker->change_parsing_namespace( $namespace );
     1847                                                $tag_maker->next_token();
     1848                                                if ( is_array( $attribute_names ) ) {
     1849                                                        foreach ( $attribute_names as $name ) {
     1850                                                                $spec = $element_attributes[ $name ] ?? null;
     1851
     1852                                                                // This attribute is not specified, thus not allowed. Skip it.
     1853                                                                if ( null === $spec || '' === $spec ) {
     1854                                                                        continue;
     1855                                                                }
     1856
     1857                                                                $raw_value = $this->get_attribute( $name );
     1858                                                                $value     = is_string( $raw_value ) ? $raw_value : '';
     1859
     1860                                                                // Process the style attribute through CSS sanitization.
     1861                                                                if ( 'style' === $name ) {
     1862                                                                        if ( ! is_string( $raw_value ) ) {
     1863                                                                                continue;
     1864                                                                        }
     1865
     1866                                                                        $value = safecss_filter_attr( $value );
     1867                                                                        if ( '' === trim( $value ) ) {
     1868                                                                                continue;
     1869                                                                        }
     1870                                                                }
     1871
     1872                                                                $is_url_ish = in_array( strtolower( $name ), $this->uri_attributes, true );
     1873                                                                if ( $is_url_ish ) {
     1874                                                                        $value = wp_kses_bad_protocol( $value, $this->allowed_protocols );
     1875                                                                }
     1876
     1877                                                                /*
     1878                                                                 * Process the remaining attributes according to their policies.
     1879                                                                 *
     1880                                                                 * Non-array values for the attribute specification are assumed
     1881                                                                 * to be `true`, thus permitting the attribute.
     1882                                                                 */
     1883                                                                if ( is_array( $spec ) ) {
     1884                                                                        foreach ( $spec as $property => $constraint ) {
     1885                                                                                $vless = true === $raw_value ? 'y' : 'n';
     1886
     1887                                                                                if ( ! wp_kses_check_attr_val( $value, $vless, $property, $constraint ) ) {
     1888                                                                                        continue 2;
     1889                                                                                }
     1890                                                                        }
     1891                                                                }
     1892
     1893                                                                $did_set = ( true === $raw_value && '' === $value )
     1894                                                                        ? $tag_maker->set_attribute( $name, true )
     1895                                                                        : $tag_maker->set_attribute( $name, $value );
     1896
     1897                                                                if ( $did_set ) {
     1898                                                                        unset( $required_attributes[ $name ] );
     1899                                                                }
     1900                                                        }
     1901                                                }
     1902
     1903                                                $needs_special_newline = 'html' === $namespace && ( 'PRE' === $token_name || 'LISTING' === $token_name );
     1904
     1905                                                if ( ! empty( $required_attributes ) ) {
     1906                                                        if ( ! $expects_closer ) {
     1907                                                                break;
     1908                                                        }
     1909
     1910                                                        /*
     1911                                                         * Since this processor cannot track nesting of HTML elements
     1912                                                         * generally, leave opening tags when required attributes are
     1913                                                         * missing, but strip them of their attributes.
     1914                                                         */
     1915                                                        $output .= "<{$tag_name}>";
     1916                                                        if ( $needs_special_newline ) {
     1917                                                                $special_newline_at = strlen( $output );
     1918                                                        }
     1919                                                        break;
     1920                                                }
     1921
     1922                                                if ( $is_special_atomic_element ) {
     1923                                                        if ( 'TITLE' === $token_name || 'TEXTAREA' === $token_name ) {
     1924                                                                /*
     1925                                                                 * RCDATA nodes can be safely escaped, but this must be
     1926                                                                 * done after enqueing the update to avoid double-escaping.
     1927                                                                 */
     1928                                                                $tag_maker->set_modifiable_text( $text );
     1929                                                                $tag_maker->lexical_updates['modifiable text']->text = strtr(
     1930                                                                        $tag_maker->lexical_updates['modifiable text']->text,
     1931                                                                        array(
     1932                                                                                "\x00" => "\u{FFFD}",
     1933                                                                                "\r"   => '&#xD;',
     1934                                                                        )
     1935                                                                );
     1936                                                        } elseif ( ! self::contains_a_block_delimiter( $text ) ) {
     1937                                                                // Other nodes not containing a block delimiter are safe.
     1938                                                                $tag_maker->set_modifiable_text( $text );
     1939                                                        } else {
     1940                                                                /*
     1941                                                                 * But RAWTEXT and SCRIPT cannot be generally escaped, so reject
     1942                                                                 * updates which would include something that could be misparsed
     1943                                                                 * as a block comment delimiter.
     1944                                                                 */
     1945                                                                $tag_maker->set_modifiable_text( '' );
     1946                                                        }
     1947                                                }
     1948
     1949                                                $output .= $tag_maker->get_updated_html();
     1950                                                if ( $needs_special_newline ) {
     1951                                                        $special_newline_at = strlen( $output );
     1952                                                }
     1953
     1954                                                break;
     1955                                }
     1956
     1957                                // Re-enter the HTML namespace.
     1958                                if ( 'html' !== $namespace ) {
     1959                                        if ( $has_self_closing_flag ) {
     1960                                                array_pop( $this->foreign_content_stack );
     1961
     1962                                                if ( 'math' === $namespace && 'ANNOTATION-XML' === $token_name ) {
     1963                                                        --$this->math_annotation_xml_depth;
     1964                                                }
     1965                                        }
     1966
     1967                                        if ( empty( $this->foreign_content_stack ) ) {
     1968                                                $is_in_mathml_text_integration_point = false;
     1969                                                $is_in_svg_html_integration_point    = false;
     1970                                                $this->change_parsing_namespace( 'html' );
     1971                                                $foreign_content_starts_at = PHP_INT_MAX;
     1972                                                $foreign_closed_blocks     = array();
     1973                                        } elseif ( '#tag' === $token_type && ! $has_self_closing_flag ) {
     1974                                                switch ( $token_name ) {
     1975                                                        case 'MI':
     1976                                                        case 'MN':
     1977                                                        case 'MO':
     1978                                                        case 'MS':
     1979                                                        case 'MTEXT':
     1980                                                                $is_in_mathml_text_integration_point = ! $is_closer && 'math' === $namespace;
     1981                                                                break;
     1982
     1983                                                        case 'DESC':
     1984                                                        case 'FOREIGNOBJECT':
     1985                                                        case 'TITLE':
     1986                                                                $is_in_svg_html_integration_point = ! $is_closer && 'svg' === $namespace;
     1987                                                                break;
     1988                                                }
     1989                                        }
     1990                                }
     1991
     1992                                if ( 'TEMPLATE' === $token_name && $template_depth > 0 && $is_closer && 'html' === $namespace ) {
     1993                                        --$template_depth;
     1994                                }
     1995                        }
     1996
     1997                        /*
     1998                         * While there might have been an incomplete token in the output stream,
     1999                         * there is no need to render it to the output. They would disappear on
     2000                         * their own in a browser if they ended the document, but here they do
     2001                         * not end the document; instead, they are likely being inserted into an
     2002                         * existing document, where the incomplete token might mess with the rest
     2003                         * of the page’s HTML structure.
     2004                         */
     2005
     2006                        $sanitized = substr( $output, 0, $foreign_content_starts_at );
     2007
     2008                        // Templates should be contained within blocks, so close them first.
     2009                        if ( $template_depth > 0 && isset( $this->allowed_html['template'] ) ) {
     2010                                $sanitized .= str_repeat( '</template>', $template_depth );
     2011                        }
     2012
     2013                        // Close any remaining-open blocks ensure isolation of block content.
     2014                        foreach ( $foreign_closed_blocks as $block_name ) {
     2015                                $sanitized .= "<!-- /wp:{$block_name} -->";
     2016                        }
     2017
     2018                        for ( $i = count( $open_blocks ) - 1; $i >= 0; $i-- ) {
     2019                                // Skip blocks that were opened when inside truncated foreign content.
     2020                                if ( $open_blocks_at[ $i ] >= $foreign_content_starts_at ) {
     2021                                        continue;
     2022                                }
     2023
     2024                                $block_name = $open_blocks[ $i ];
     2025                                $sanitized .= "<!-- /wp:{$block_name} -->";
     2026                        }
     2027
     2028                        return $sanitized;
     2029                }
     2030        };
     2031
     2032        return $processor->sanitize();
    9712033}
    9722034
  • trunk/tests/phpunit/tests/admin/includesTemplate.php

    r63943 r64233  
    351351                        'disallowed tag in before_section'        => array(
    352352                                array(
    353                                         'before_section' => '<div class="video-settings-section"><iframe src="https://www.wordpress.org/" />',
     353                                        'before_section' => '<div class="video-settings-section"><iframe src="https://www.wordpress.org/"></iframe>',
    354354                                        'after_section'  => '</div><!-- end of the test section -->',
    355355                                ),
    … …  
    358358                                        'title'          => 'Section title',
    359359                                        'callback'       => '__return_false',
    360                                         'before_section' => '<div class="video-settings-section"><iframe src="https://www.wordpress.org/" />',
     360                                        'before_section' => '<div class="video-settings-section"><iframe src="https://www.wordpress.org/"></iframe>',
    361361                                        'after_section'  => '</div><!-- end of the test section -->',
    362362                                        'section_class'  => '',
    … …  
    368368                                array(
    369369                                        'before_section' => '<div class="video-settings-section">',
    370                                         'after_section'  => '</div><iframe src="https://www.wordpress.org/" />',
     370                                        'after_section'  => '</div><iframe src="https://www.wordpress.org/"></iframe>',
    371371                                ),
    372372                                array(
    … …  
    375375                                        'callback'       => '__return_false',
    376376                                        'before_section' => '<div class="video-settings-section">',
    377                                         'after_section'  => '</div><iframe src="https://www.wordpress.org/" />',
     377                                        'after_section'  => '</div><iframe src="https://www.wordpress.org/"></iframe>',
    378378                                        'section_class'  => '',
    379379                                ),
  • trunk/tests/phpunit/tests/block-bindings/postMetaSource.php

    r61605 r64233  
    261261                $content = $this->get_modified_post_content( '<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"tests_unsafe_html_field"}}}}} --><p>Fallback value</p><!-- /wp:paragraph -->' );
    262262
    263                 $this->assertSame(
    264                         '<p class="wp-block-paragraph">alert(&#8220;Unsafe HTML&#8221;)</p>',
    265                         $content,
     263                $this->assertEqualHTML(
     264                        '<p class="wp-block-paragraph"></p>',
     265                        $content,
     266                        '<body>',
    266267                        'The post content should not include the script tag.'
    267268                );
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r64225 r64233  
    194194                                        return '<script>alert("Unsafe HTML")</script>';
    195195                                },
    196                                 '<p class="wp-block-paragraph">alert("Unsafe HTML")</p>',
     196                                '<p class="wp-block-paragraph"></p>',
    197197                        ),
    198198                        'symbols and numbers should be rendered correctly' => array(
    … …  
    235235                $result        = $block->render();
    236236
    237                 $this->assertSame(
     237                $this->assertEqualHTML(
    238238                        $expected,
    239239                        trim( $result ),
     240                        '<body>',
    240241                        'The block content should be updated with the value returned by the source.'
    241242                );
  • trunk/tests/phpunit/tests/block-bindings/termDataSource.php

    r64112 r64233  
    357357                );
    358358
    359                 $this->assertSame(
    360                         '<p>Allowed HTML</p> alert("xss"); &amp; "quotes"',
     359                $this->assertEqualHTML(
     360                        '<p>Allowed HTML</p> &amp; "quotes"',
    361361                        _block_bindings_term_data_get_value( array( 'field' => 'description' ), $block ),
     362                        '<body>',
    362363                        'Term description should be sanitized with wp_kses_post(), stripping disallowed tags like <script>.'
    363364                );
  • trunk/tests/phpunit/tests/customize/manager.php

    r64116 r64233  
    13711371                // User saved as one who cannot bypass content_save_pre filter.
    13721372                $this->assertStringNotContainsString( '<script>', get_option( 'custom_html_2' ) );
    1373                 $this->assertStringContainsString( 'WordPress', get_option( 'custom_html_2' ) );
     1373                $this->assertStringNotContainsString( 'WordPress', get_option( 'custom_html_2' ) );
    13741374
    13751375                // User saved as one who also cannot bypass content_save_pre filter.
    13761376                $this->assertStringNotContainsString( '<script>', get_option( 'custom_html_3' ) );
    1377                 $this->assertStringContainsString( 'WordPress', get_option( 'custom_html_3' ) );
     1377                $this->assertStringNotContainsString( 'WordPress', get_option( 'custom_html_3' ) );
    13781378        }
    13791379
    … …  
    20052005                do_action( 'customize_register', $wp_customize );
    20062006                $wp_customize->save_changeset_post( array( 'status' => 'publish' ) );
    2007                 $this->assertSame( 'Unfilteredevil', get_option( 'scratchpad' ) );
     2007                $this->assertSame( 'Unfiltered', get_option( 'scratchpad' ) );
    20082008
    20092009                // Attempt publishing scratchpad as anonymous user when changeset was set by privileged user.
    … …  
    20222022                $wp_customize = null;
    20232023                unset( $GLOBALS['wp_actions']['customize_register'] );
    2024                 $this->assertSame( 'Unfilteredevil', apply_filters( 'content_save_pre', 'Unfiltered<script>evil</script>' ) );
     2024                $this->assertSame( 'Unfiltered', apply_filters( 'content_save_pre', 'Unfiltered<script>evil</script>' ) );
    20252025                wp_publish_post( $changeset_post_id ); // @todo If wp_update_post() is used here, then kses will corrupt the post_content.
    20262026                $this->assertSame( 'Unfiltered<script>evil</script>', get_option( 'scratchpad' ) );
  • trunk/tests/phpunit/tests/customize/nav-menu-item-setting.php

    r63604 r64233  
    589589                        'position'         => -123,
    590590                        'type'             => 'customb',
    591                         'title'            => current_user_can( 'unfiltered_html' ) ? '\o/ o\'o Hi<script>unfilteredHtml()</script>' : '\o/ o\'o HiunfilteredHtml()',
     591                        'title'            => current_user_can( 'unfiltered_html' ) ? '\o/ o\'o Hi<script>unfilteredHtml()</script>' : '\o/ o\'o Hi',
    592592                        'url'              => '',
    593593                        'target'           => 'onclick',
    594                         'attr_title'       => current_user_can( 'unfiltered_html' ) ? '\o/ o\'o <b>bolded</b><script>unfilteredHtml()</script>' : '\o/ o\'o <b>bolded</b>unfilteredHtml()',
    595                         'description'      => current_user_can( 'unfiltered_html' ) ? '\o/ o\'o <b>Hello world</b><script>unfilteredHtml()</script>' : '\o/ o\'o <b>Hello world</b>unfilteredHtml()',
     594                        'attr_title'       => current_user_can( 'unfiltered_html' ) ? '\o/ o\'o <b>bolded</b><script>unfilteredHtml()</script>' : '\o/ o\'o <b>bolded</b>',
     595                        'description'      => current_user_can( 'unfiltered_html' ) ? '\o/ o\'o <b>Hello world</b><script>unfilteredHtml()</script>' : '\o/ o\'o <b>Hello world</b>',
    596596                        'classes'          => 'hello  inject',
    597597                        'xfn'              => 'hello  inject',
  • trunk/tests/phpunit/tests/formatting/sanitizeTextField.php

    r57987 r64233  
    2121                        $expected_multiline = $expected;
    2222                }
    23                 $this->assertSame( $expected_oneline, sanitize_text_field( $str ) );
     23                $this->assertEqualHTML( $expected_oneline, sanitize_text_field( $str ) );
    2424                $this->assertSameIgnoreEOL( $expected_multiline, sanitize_textarea_field( $str ) );
    2525        }
    … …  
    5656                                "foo <\ndiv\n> bar",
    5757                                array(
    58                                         'oneline'   => 'foo &lt; div > bar',
     58                                        'oneline'   => 'foo &lt; div &gt; bar',
    5959                                        'multiline' => "foo &lt;\ndiv\n> bar",
    6060                                ),
  • trunk/tests/phpunit/tests/functions/wpTriggerError.php

    r59107 r64233  
    111111                                'function_name'    => 'some_function',
    112112                                'message'          => '<script>alert("expected the function name and message")</script>',
    113                                 'expected_message' => 'some_function(): alert("expected the function name and message")',
     113                                'expected_message' => 'some_function(): ',
    114114                        ),
    115115                );
  • trunk/tests/phpunit/tests/icons/wpRestIconsController.php

    r64224 r64233  
    376376                $this->assertSame( 'Arrow Left', $data['label'] );
    377377                $this->assertNotEmpty( $data['content'] );
    378                 $this->assertStringStartsWith(
    379                         '<svg xmlns="',
    380                         $data['content'],
    381                         'Icon content should match the actual SVG asset'
    382                 );
     378
     379                $processor = new WP_HTML_Tag_Processor( $data['content'] );
     380                $this->assertTrue( $processor->next_token() );
     381                $this->assertSame( 'SVG', $processor->get_tag(), 'Icon content should match the actual SVG asset' );
     382                $this->assertSame( 'http://www.w3.org/2000/svg', $processor->get_attribute( 'xmlns' ), 'Icon content should match the actual SVG asset' );
    383383        }
    384384
  • trunk/tests/phpunit/tests/kses.php

    r64093 r64233  
    107107
    108108                return $data;
     109        }
     110
     111        /**
     112         * Ensures that repeated runs return the same result.
     113         *
     114         * @ticket 65984
     115         *
     116         * @dataProvider data_wp_kses_idempotent_inputs
     117         *
     118         * @param string $input                 Process this HTML.
     119         * @param array|null $allowed_html      Optional. If provided, will be passed into `wp_kses()`.
     120         *                                      Default is to rely on the WordPress defaults.
     121         * @param array|null $allowed_protocols Optional. If provided, will be passed into `wp_kses()`.
     122         *                                      Default is to rely on the WordPress defaults.
     123         * @return void
     124         */
     125        public function test_wp_kses_is_idempotent( string $input, ?array $allowed_html = null, ?array $allowed_protocols = null ): void {
     126                $output = wp_kses( $input, $allowed_html, $allowed_protocols );
     127
     128                $this->assertSame(
     129                        $output,
     130                        wp_kses( $output, $allowed_html, $allowed_protocols ),
     131                        'Should have produced the same output after running the given input back through `wp_kses()`'
     132                );
     133        }
     134
     135        /**
     136         * Data provider.
     137         *
     138         * @return array[]
     139         */
     140        public static function data_wp_kses_idempotent_inputs(): array {
     141                return array(
     142                        array(
     143                                '<div>a < b</div>',
     144                        ),
     145                        array(
     146                                '<div id="a < b">',
     147                                array(
     148                                        'div' => array(
     149                                                'id' => true,
     150                                        ),
     151                                ),
     152                        ),
     153                );
    109154        }
    110155
    … …  
    365410                        switch ( $attack->name ) {
    366411                                case 'XSS Locator':
    367                                         $this->assertSame( '\';alert(String.fromCharCode(88,83,83))//\\\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\\";alert(String.fromCharCode(88,83,83))//--&gt;"&gt;\'&gt;alert(String.fromCharCode(88,83,83))=&amp;{}', $result );
     412                                        $this->assertEqualHTML( '\';alert(String.fromCharCode(88,83,83))//\\\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\\";alert(String.fromCharCode(88,83,83))//--&gt;"&gt;\'&gt;=&amp;{}', $result );
    368413                                        break;
    369414                                case 'XSS Quick Test':
    370                                         $this->assertSame( '\'\';!--"=&amp;{()}', $result );
     415                                        $this->assertEqualHTML( '\'\';!--"=&amp;{()}', $result );
    371416                                        break;
    372417                                case 'SCRIPT w/Alert()':
    373                                         $this->assertSame( "alert('XSS')", $result );
     418                                        $this->assertEqualHTML( "alert('XSS')", $result );
    374419                                        break;
    375420                                case 'SCRIPT w/Char Code':
    376                                         $this->assertSame( 'alert(String.fromCharCode(88,83,83))', $result );
     421                                        $this->assertEqualHTML( 'alert(String.fromCharCode(88,83,83))', $result );
    377422                                        break;
    378423                                case 'IMG STYLE w/expression':
    379                                         $this->assertSame( 'exp/*', $result );
     424                                        $this->assertEqualHTML( 'exp/*', $result );
    380425                                        break;
    381426                                case 'List-style-image':
    382                                         $this->assertSame( 'li {list-style-image: url("javascript:alert(\'XSS\')");}XSS', $result );
     427                                        $this->assertEqualHTML( 'li {list-style-image: url("javascript:alert(\'XSS\')");}XSS', $result );
    383428                                        break;
    384429                                case 'STYLE':
    385                                         $this->assertSame( "alert('XSS');", $result );
     430                                        $this->assertEqualHTML( "alert('XSS');", $result );
    386431                                        break;
    387432                                case 'STYLE w/background-image':
    388                                         $this->assertSame( '.XSS{background-image:url("javascript:alert(\'XSS\')");}<A></A>', $result );
     433                                        $this->assertEqualHTML( '<A></A>', $result );
    389434                                        break;
    390435                                case 'STYLE w/background':
    391                                         $this->assertSame( 'BODY{background:url("javascript:alert(\'XSS\')")}', $result );
     436                                        $this->assertEqualHTML( 'BODY{background:url("javascript:alert(\'XSS\')")}', $result );
    392437                                        break;
    393438                                case 'Remote Stylesheet 2':
    394                                         $this->assertSame( "@import'http://ha.ckers.org/xss.css';", $result );
     439                                        $this->assertEqualHTML( "@import'http://ha.ckers.org/xss.css';", $result );
    395440                                        break;
    396441                                case 'Remote Stylesheet 3':
    397                                         $this->assertSame( '&lt;META HTTP-EQUIV=&quot;Link&quot; Content=&quot;; REL=stylesheet"&gt;', $result );
     442                                        $this->assertEqualHTML( '&lt;META HTTP-EQUIV=&quot;Link&quot; Content=&quot;; REL=stylesheet"&gt;', $result );
    398443                                        break;
    399444                                case 'Remote Stylesheet 4':
    400                                         $this->assertSame( 'BODY{-moz-binding:url("http://ha.ckers.org/xssmoz.xml#xss")}', $result );
     445                                        $this->assertEqualHTML( 'BODY{-moz-binding:url("http://ha.ckers.org/xssmoz.xml#xss")}', $result );
    401446                                        break;
    402447                                case 'XML data island w/CDATA':
    403                                         $this->assertSame( '&lt;![CDATA[]]&gt;', $result );
     448                                        $this->assertEqualHTML( ']]>', $result );
    404449                                        break;
    405450                                case 'XML data island w/comment':
    406                                         $this->assertSame( "<I><B>&lt;IMG SRC=&quot;javas<!-- -->cript:alert('XSS')\"&gt;</B></I>", $result );
     451                                        $this->assertEqualHTML( '<I><B></B></I>', $result );
    407452                                        break;
    408453                                case 'XML HTML+TIME':
    409                                         $this->assertSame( '&lt;t:set attributeName=&quot;innerHTML&quot; to=&quot;XSSalert(\'XSS\')"&gt;', $result );
     454                                        $this->assertEqualHTML( '', $result );
    410455                                        break;
    411456                                case 'Commented-out Block':
    412                                         $this->assertSame( "<!--[if gte IE 4]&gt;-->\nalert('XSS');", $result );
     457                                        $this->assertEqualHTML( "<!--[if gte IE 4]>\n&lt;SCRIPT>alert('XSS');&lt;/SCRIPT>\n&lt;![endif]-->", $result );
    413458                                        break;
    414459                                case 'Cookie Manipulation':
    415                                         $this->assertSame( '&lt;META HTTP-EQUIV=&quot;Set-Cookie&quot; Content=&quot;USERID=alert(\'XSS\')"&gt;', $result );
     460                                        $this->assertEqualHTML( '&lt;META HTTP-EQUIV=&quot;Set-Cookie&quot; Content=&quot;USERID=alert(\'XSS\')"&gt;', $result );
    416461                                        break;
    417462                                case 'SSI':
    418                                         $this->assertSame( '&lt;!--#exec cmd=&quot;/bin/echo &#039;<!--#exec cmd="/bin/echo \'=http://ha.ckers.org/xss.js&gt;\'"-->', $result );
     463                                        $this->assertEqualHTML( '<!--#exec cmd="/bin/echo \'&lt;SCRIPT SRC\'"--><!--#exec cmd="/bin/echo \'=http://ha.ckers.org/xss.js>&lt;/SCRIPT>\'"-->', $result );
    419464                                        break;
    420465                                case 'PHP':
    421                                         $this->assertSame( '&lt;? echo(&#039;alert("XSS")\'); ?&gt;', $result );
     466                                        $this->assertEqualHTML( 'alert("XSS")\'); ?&gt;', $result );
    422467                                        break;
    423468                                case 'UTF-7 Encoding':
    424                                         $this->assertSame( '+ADw-SCRIPT+AD4-alert(\'XSS\');+ADw-/SCRIPT+AD4-', $result );
     469                                        $this->assertEqualHTML( '+ADw-SCRIPT+AD4-alert(\'XSS\');+ADw-/SCRIPT+AD4-', $result );
    425470                                        break;
    426471                                case 'Escaping JavaScript escapes':
    427                                         $this->assertSame( '\";alert(\'XSS\');//', $result );
     472                                        $this->assertEqualHTML( '\";alert(\'XSS\');//', $result );
    428473                                        break;
    429474                                case 'STYLE w/broken up JavaScript':
    430                                         $this->assertSame( '@im\port\'\ja\vasc\ript:alert("XSS")\';', $result );
     475                                        $this->assertEqualHTML( '@im\port\'\ja\vasc\ript:alert("XSS")\';', $result );
    431476                                        break;
    432477                                case 'Null Chars 2':
    433                                         $this->assertSame( '&amp;alert("XSS")', $result );
     478                                        $this->assertEqualHTML( '&amp;alert("XSS")', $result );
    434479                                        break;
    435480                                case 'No Closing Script Tag':
    436                                         $this->assertSame( '&lt;SCRIPT SRC=http://ha.ckers.org/xss.js', $result );
     481                                        $this->assertEqualHTML( '&lt;SCRIPT SRC=http://ha.ckers.org/xss.js', $result );
    437482                                        break;
    438483                                case 'Half-Open HTML/JavaScript':
    439                                         $this->assertSame( '&lt;IMG SRC=&quot;javascript:alert(&#039;XSS&#039;)&quot;', $result );
     484                                        $this->assertEqualHTML( '&lt;IMG SRC=&quot;javascript:alert(&#039;XSS&#039;)&quot;', $result );
    440485                                        break;
    441486                                case 'Double open angle brackets':
    442                                         $this->assertSame( '&lt;IFRAME SRC=http://ha.ckers.org/scriptlet.html &lt;', $result );
     487                                        $this->assertEqualHTML( '&lt;IFRAME SRC=http://ha.ckers.org/scriptlet.html &lt;', $result );
    443488                                        break;
    444489                                case 'Extraneous Open Brackets':
    445                                         $this->assertSame( '&lt;alert("XSS");//&lt;', $result );
     490                                        $this->assertSame( '&lt;', $result );
    446491                                        break;
    447492                                case 'Malformed IMG Tags':
    448                                         $this->assertSame( 'alert("XSS")"&gt;', $result );
     493                                        $this->assertEqualHTML( '">', $result );
    449494                                        break;
    450495                                case 'No Quotes/Semicolons':
    451                                         $this->assertSame( "a=/XSS/\nalert(a.source)", $result );
     496                                        $this->assertEqualHTML( "a=/XSS/\nalert(a.source)", $result );
    452497                                        break;
    453498                                case 'Evade Regex Filter 1':
    454                                         $this->assertSame( '" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     499                                        $this->assertEqualHTML( '" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    455500                                        break;
    456501                                case 'Evade Regex Filter 4':
    457                                         $this->assertSame( '\'" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     502                                        $this->assertEqualHTML( '\'" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    458503                                        break;
    459504                                case 'Evade Regex Filter 5':
    460                                         $this->assertSame( '` SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     505                                        $this->assertEqualHTML( '` SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    461506                                        break;
    462507                                case 'Filter Evasion 1':
    463                                         $this->assertSame( 'document.write("&lt;SCRI&quot;);PT SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     508                                        $this->assertEqualHTML( 'PT SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    464509                                        break;
    465510                                case 'Filter Evasion 2':
    466                                         $this->assertSame( '\'&gt;" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     511                                        $this->assertEqualHTML( '\'&gt;" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    467512                                        break;
    468513                                default:
    … …  
    855900                global $allowedposttags;
    856901
    857                 return $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) );
     902                // It also must explicitly escape the C0 control characters.
     903                $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) );
    858904        }
    859905
    … …  
    876922                                'hello world. WORDPRESS KSES./',
    877923                        ),
     924                        /*
     925                         * When decoding HTML, all "\r\n" grapheme clusters are converted into "\n" and
     926                         * then any remaining "\r" characters are also converted into "\n". This is why
     927                         * the two yield different outputs after normalization depending on the order in
     928                         * which they appear together.
     929                         */
    878930                        array(
    879931                                "\t\r\n word \n\r\t",
    880                                 "\t\r\n word \n\r\t",
     932                                "\t\n word \n\n\t",
    881933                        ),
    882934                );
    … …  
    892944                global $allowedposttags;
    893945
    894                 return $this->assertEqualHTML( $expected, wp_kses( $content, $allowedposttags ) );
     946                $with_style = array_merge(
     947                        $allowedposttags,
     948                        array(
     949                                'style' => array(
     950                                        'type' => true,
     951                                ),
     952                        )
     953                );
     954
     955                return $this->assertEqualHTML( $expected, wp_kses( $content, $with_style ) );
    895956        }
    896957
    … …  
    931992                        array(
    932993                                '<style type="text/css">div {background-image:\\0}</style>',
    933                                 'div {background-image:\\0}',
     994                                '<style type="text/css">div {background-image:\\0}</style>',
    934995                        ),
    935996                );
    … …  
    23712432                        'invalid value for type'                  => array(
    23722433                                '<object type="application/exe" data="https://' . WP_TESTS_DOMAIN . '/foo.exe" />',
    2373                                 '',
     2434                                '<object>',
    23742435                        ),
    23752436                        'multiple type attributes, last invalid'  => array(
    … …  
    23872448                        'multiple type attributes, first invalid' => array(
    23882449                                '<object type="application/exe" type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2389                                 '',
     2450                                '<object>',
    23902451                        ),
    23912452                        'multiple type attributes, first upper case and invalid' => array(
    23922453                                '<object TYPE="application/exe" type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2393                                 '',
     2454                                '<object>',
    23942455                        ),
    23952456                        'multiple type attributes, first invalid, last uppercase' => array(
    23962457                                '<object type="application/exe" TYPE="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2397                                 '',
     2458                                '<object>',
    23982459                        ),
    23992460                        'multiple object tags, last invalid'      => array(
    24002461                                '<object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" /><object type="application/exe" data="https://' . WP_TESTS_DOMAIN . '/foo.exe" />',
    2401                                 '<object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
     2462                                '<object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" /><object>',
    24022463                        ),
    24032464                        'multiple object tags, first invalid'     => array(
    24042465                                '<object type="application/exe" data="https://' . WP_TESTS_DOMAIN . '/foo.exe" /><object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2405                                 '<object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
     2466                                '<object><object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    24062467                        ),
    24072468                        'type attribute with partially incorrect value' => array(
    24082469                                '<object type="application/pdfa" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2409                                 '',
     2470                                '<object>',
    24102471                        ),
    24112472                        'type attribute with empty value'         => array(
    24122473                                '<object type="" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2413                                 '',
     2474                                '<object>',
    24142475                        ),
    24152476                        'type attribute with no value'            => array(
    24162477                                '<object type data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2417                                 '',
     2478                                '<object>',
    24182479                        ),
    24192480                        'no type attribute'                       => array(
    24202481                                '<object data="https://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2421                                 '',
     2482                                '<object>',
    24222483                        ),
    24232484                        'different protocol in url'               => array(
    … …  
    24272488                        'query string on url'                     => array(
    24282489                                '<object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf?lol=.pdf" />',
    2429                                 '',
     2490                                '<object>',
    24302491                        ),
    24312492                        'fragment on url'                         => array(
    24322493                                '<object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.pdf#lol.pdf" />',
    2433                                 '',
     2494                                '<object>',
    24342495                        ),
    24352496                        'wrong extension'                         => array(
    24362497                                '<object type="application/pdf" data="https://' . WP_TESTS_DOMAIN . '/foo.php" />',
    2437                                 '',
     2498                                '<object>',
    24382499                        ),
    24392500                        'protocol-relative url'                   => array(
    24402501                                '<object type="application/pdf" data="//' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2441                                 '',
     2502                                '<object>',
    24422503                        ),
    24432504                        'unsupported protocol'                    => array(
    24442505                                '<object type="application/pdf" data="ftp://' . WP_TESTS_DOMAIN . '/foo.pdf" />',
    2445                                 '',
     2506                                '<object>',
    24462507                        ),
    24472508                        'relative url'                            => array(
    24482509                                '<object type="application/pdf" data="/cat/foo.pdf" />',
    2449                                 '',
     2510                                '<object>',
    24502511                        ),
    24512512                        'url with port number-like path'          => array(
    … …  
    24862547                        'url with wrong port number'             => array(
    24872548                                '<object type="application/pdf" data="http://example.org:3333/cat/foo.pdf" />',
    2488                                 '',
     2549                                '<object>',
    24892550                        ),
    24902551                        'url without port number'                => array(
    24912552                                '<object type="application/pdf" data="http://example.org/cat/foo.pdf" />',
    2492                                 '',
     2553                                '<object>',
    24932554                        ),
    24942555                );
    … …  
    25542615
    25552616        /**
    2556          * Ensures that `wp_kses()` preserves various kinds of HTML comments, both valid and invalid.
     2617         * Ensures that `wp_kses()` preserves various kinds of HTML comments;
     2618         * specifically well-formed comments and “funky comments.”
    25572619         *
    25582620         * @ticket 61009
    … …  
    25822644                        'Normative HTML comment'            => array( 'before<!-- this is a comment -->after', 'before<!-- this is a comment -->after' ),
    25832645                        'Closing tag with invalid tag name' => array( 'before<//not a tag>after', 'before<//not a tag>after' ),
    2584                         'Incorrectly opened comment (Markup declaration)' => array( 'before<!also not a tag>after', 'before<!also not a tag>after' ),
     2646                        'Incorrectly opened comment (Markup declaration)' => array( 'before<!also not a tag>after', 'beforeafter' ),
    25852647                );
    25862648        }
  • trunk/tests/phpunit/tests/link/getThePrivacyPolicyLink.php

    r64092 r64233  
    110110
    111111                $this->assertStringEndsWith(
    112                         '><strong class="privacy">Privacy</strong> <em class="policy">Policy</em> <b class="bold">Bold</b> <i class="italic">Italic</i> <span class="page-title">Page</span> alert("test")</a>',
     112                        '><strong class="privacy">Privacy</strong> <em class="policy">Policy</em> <b class="bold">Bold</b> <i class="italic">Italic</i> <span class="page-title">Page</span> </a>',
    113113                        $actual_link
    114114                );
  • trunk/tests/phpunit/tests/media.php

    r64059 r64233  
    47384738
    47394739                $html = get_the_block_template_html();
    4740                 $this->assertSame( '<div class="wp-site-blocks"><div class="entry-content wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">' . $expected_content . '</div></div>', $html );
     4740                $this->assertEqualHTML( '<div class="wp-site-blocks"><div class="entry-content wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">' . $expected_content . '</div></div>', $html );
    47414741        }
    47424742
    … …  
    48084808
    48094809                $html = get_the_block_template_html();
    4810                 $this->assertSame( '<div class="wp-site-blocks">' . $expected_featured_image . ' <div class="entry-content wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">' . $expected_content . '</div></div>', $html );
     4810                $this->assertEqualHTML( '<div class="wp-site-blocks">' . $expected_featured_image . ' <div class="entry-content wp-block-post-content is-layout-flow wp-block-post-content-is-layout-flow">' . $expected_content . '</div></div>', $html );
    48114811        }
    48124812
    … …  
    48674867
    48684868                $html = get_the_block_template_html();
    4869                 $this->assertSame( '<div class="wp-site-blocks">' . $expected_template_content . '</div>', $html );
     4869                $this->assertEqualHTML( '<div class="wp-site-blocks">' . $expected_template_content . '</div>', $html );
    48704870        }
    48714871
    … …  
    59805980                remove_shortcode( 'full_image' );
    59815981
    5982                 $this->assertSame( $expected_content, $content );
     5982                $this->assertEqualHTML( $expected_content, $content );
    59835983        }
    59845984
    … …  
    61346134                unregister_block_type( 'core/full-image-shortcode' );
    61356135
    6136                 $this->assertSame( $expected_content, $content );
     6136                $this->assertEqualHTML( $expected_content, $content );
    61376137        }
    61386138
  • trunk/tests/phpunit/tests/post/output.php

    r61605 r64233  
    148148                $this->assertNull( the_post() );
    149149
    150                 $this->assertSame( strip_ws( $expected ), strip_ws( get_echo( 'the_content' ) ) );
     150                $this->assertEqualHTML( strip_ws( $expected ), strip_ws( get_echo( 'the_content' ) ) );
    151151
    152152                kses_remove_filters();
  • trunk/tests/phpunit/tests/post/wpPublishPost.php

    r54090 r64233  
    8989                $post_id = wp_insert_post(
    9090                        array(
    91                                 'post_title' => '<script>Test</script>',
     91                                'post_title' => 'Talking about: <script>Test</script>',
    9292                        )
    9393                );
    9494                $post    = get_post( $post_id );
    95                 $this->assertSame( '<script>Test</script>', $post->post_title );
     95                $this->assertSame( 'Talking about: <script>Test</script>', $post->post_title );
    9696                $this->assertSame( 'draft', $post->post_status );
    9797
    … …  
    108108
    109109                $post = get_post( $post->ID );
    110                 $this->assertSame( 'Test', $post->post_title );
     110                $this->assertSame( 'Talking about: ', $post->post_title );
    111111        }
    112112
  • trunk/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportGroupHtml.php

    r58164 r64233  
    175175                                        'scripts' => array(
    176176                                                'name'  => 'Script tags are not allowed.',
    177                                                 'value' => '<script>Testing that script tags are stripped.</script>',
     177                                                'value' => 'Before<script>Testing that script tags are stripped.</script>After',
    178178                                        ),
    179179                                        'images'  => array(
    … …  
    188188
    189189                $this->assertStringNotContainsString( $data['items'][0]['scripts']['value'], $actual );
    190                 $this->assertStringContainsString( '<td>Testing that script tags are stripped.</td>', $actual );
     190                $this->assertStringContainsString( '<td>BeforeAfter</td>', $actual );
    191191
    192192                $this->assertStringNotContainsString( $data['items'][0]['images']['value'], $actual );
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r64118 r64233  
    18531853                                array(
    18541854                                        'title'       => array(
    1855                                                 'raw'      => 'div <strong>strong</strong> oh noes',
    1856                                                 'rendered' => 'div <strong>strong</strong> oh noes',
     1855                                                'raw'      => 'div <strong>strong</strong> ',
     1856                                                'rendered' => 'div <strong>strong</strong>',
    18571857                                        ),
    18581858                                        'description' => array(
    1859                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    1860                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     1859                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     1860                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    18611861                                        ),
    18621862                                        'caption'     => array(
    1863                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    1864                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     1863                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     1864                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    18651865                                        ),
    18661866                                ),
    … …  
    19071907                                array(
    19081908                                        'title'       => array(
    1909                                                 'raw'      => 'div <strong>strong</strong> oh noes',
    1910                                                 'rendered' => 'div <strong>strong</strong> oh noes',
     1909                                                'raw'      => 'div <strong>strong</strong> ',
     1910                                                'rendered' => 'div <strong>strong</strong>',
    19111911                                        ),
    19121912                                        'description' => array(
    1913                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    1914                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     1913                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     1914                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    19151915                                        ),
    19161916                                        'caption'     => array(
    1917                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    1918                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     1917                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     1918                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    19191919                                        ),
    19201920                                )
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r63912 r64233  
    31483148                                array(
    31493149                                        'content'           => array(
    3150                                                 'raw'      => 'div <strong>strong</strong> oh noes',
    3151                                                 'rendered' => '<p>div <strong>strong</strong> oh noes</p>',
     3150                                                'raw'      => 'div <strong>strong</strong> ',
     3151                                                'rendered' => '<p>div <strong>strong</strong> </p>',
    31523152                                        ),
    31533153                                        'author_name'       => 'div strong',
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r64039 r64233  
    46674667                                array(
    46684668                                        'title'   => array(
    4669                                                 'raw'      => 'div <strong>strong</strong> oh noes',
    4670                                                 'rendered' => 'div <strong>strong</strong> oh noes',
     4669                                                'raw'      => 'div <strong>strong</strong> ',
     4670                                                'rendered' => 'div <strong>strong</strong>',
    46714671                                        ),
    46724672                                        'content' => array(
    4673                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    4674                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     4673                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     4674                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    46754675                                        ),
    46764676                                        'excerpt' => array(
    4677                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    4678                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     4677                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     4678                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    46794679                                        ),
    46804680                                ),
    … …  
    47194719                                array(
    47204720                                        'title'   => array(
    4721                                                 'raw'      => 'div <strong>strong</strong> oh noes',
    4722                                                 'rendered' => 'div <strong>strong</strong> oh noes',
     4721                                                'raw'      => 'div <strong>strong</strong> ',
     4722                                                'rendered' => 'div <strong>strong</strong>',
    47234723                                        ),
    47244724                                        'content' => array(
    4725                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    4726                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     4725                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     4726                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    47274727                                        ),
    47284728                                        'excerpt' => array(
    4729                                                 'raw'      => '<div>div</div> <strong>strong</strong> oh noes',
    4730                                                 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
     4729                                                'raw'      => '<div>div</div> <strong>strong</strong> ',
     4730                                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> </p>",
    47314731                                        ),
    47324732                                )
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r63616 r64233  
    11051105                                array(
    11061106                                        'name'        => 'div strong',
    1107                                         'description' => 'div <strong>strong</strong> oh noes',
     1107                                        'description' => 'div <strong>strong</strong> ',
    11081108                                )
    11091109                        );
    … …  
    11171117                                array(
    11181118                                        'name'        => 'div strong',
    1119                                         'description' => 'div <strong>strong</strong> oh noes',
     1119                                        'description' => 'div <strong>strong</strong> ',
    11201120                                )
    11211121                        );
    … …  
    11501150                        array(
    11511151                                'name'        => 'div strong',
    1152                                 'description' => 'div <strong>strong</strong> oh noes',
     1152                                'description' => 'div <strong>strong</strong> ',
    11531153                        )
    11541154                );
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r64070 r64233  
    23892389                                        'last_name'   => 'div strong',
    23902390                                        'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
    2391                                         'description' => 'div <strong>strong</strong> oh noes',
     2391                                        'description' => 'div <strong>strong</strong> ',
    23922392                                        'nickname'    => 'div strong',
    23932393                                        'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
    … …  
    24122412                                        'last_name'   => 'div strong',
    24132413                                        'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
    2414                                         'description' => 'div <strong>strong</strong> oh noes',
     2414                                        'description' => 'div <strong>strong</strong> ',
    24152415                                        'nickname'    => 'div strong',
    24162416                                        'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
    … …  
    24712471                                'last_name'   => 'div strong',
    24722472                                'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
    2473                                 'description' => 'div <strong>strong</strong> oh noes',
     2473                                'description' => 'div <strong>strong</strong> ',
    24742474                                'nickname'    => 'div strong',
    24752475                                'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  • trunk/tests/phpunit/tests/rest-api/rest-widgets-controller.php

    r64074 r64233  
    12801280                if ( is_multisite() ) {
    12811281                        $this->assertSame(
    1282                                 '<div class="textwidget">alert(1)</div>',
     1282                                '<div class="textwidget"></div>',
    12831283                                $this->update_text_widget_with_raw_html( '<script>alert(1)</script>' )
    12841284                        );
  • trunk/tests/phpunit/tests/rest-api/wpRestUrlDetailsController.php

    r64077 r64233  
    756756                $method = $this->get_reflective_method( 'get_description' );
    757757                $actual = $method->invoke( $controller, $meta_elements );
    758                 $this->assertSame( $expected, $actual );
     758                $this->assertEqualHTML( $expected, $actual );
    759759        }
    760760
  • trunk/tests/phpunit/tests/widgets/wpWidgetMediaImage.php

    r59120 r64233  
    242242                        $result,
    243243                        array(
    244                                 'caption' => '"&gt;<i />',
    245                         )
     244                                'caption' => '"&gt;<i>',
     245                        ),
    246246                );
    247247
Note: See TracChangeset for help on using the changeset viewer.