Make WordPress Core

Changeset 65875


Ignore:
Timestamp:
12/03/2026 04:25:13 AM (16 hours ago)
Author:
dmsnell
Message:

KSES: Adjust restrictions for non-DATA text content.

Contents of IFRAME elements are not rendered in a browser, and while they are benign in terms of being parsed as RAWTEXT, it is prudent to empty them from untrusted content sources.

On the other hand, NOEMBED, NOFRAMES, and XMP (while all are deprecated) do render in browsers. To preserve fallback text in these elements, it’s reasonable to replace angle brackets with visually-similar characters to maintain as much of the plaintext rendering as is possible without making it easy for downstream parsers to misidentify content as markup.

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

Follow-up to r64233.

Props dmsnell, jonsurrell.
Fixes #66917, see #66208.

File:
1 edited

Legend:

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

    r65866 r65875  
    20192019
    20202020                                                if ( $is_special_atomic_element ) {
    2021                                                         if ( 'TITLE' === $token_name || 'TEXTAREA' === $token_name ) {
    2022                                                                 /*
    2023                                                                  * RCDATA nodes can be safely escaped, but this must be
    2024                                                                  * done after enqueuing the update to avoid double-escaping.
    2025                                                                  */
    2026                                                                 $tag_maker->set_modifiable_text( $text );
    2027                                                                 $tag_maker->lexical_updates['modifiable text']->text = strtr(
    2028                                                                         $tag_maker->lexical_updates['modifiable text']->text,
    2029                                                                         array(
    2030                                                                                 "\x00" => "\u{FFFD}",
    2031                                                                                 "\r"   => '
',
    2032                                                                         )
    2033                                                                 );
    2034                                                         } elseif ( ! self::contains_a_block_delimiter( $text ) ) {
    2035                                                                 // Other nodes not containing a block delimiter are safe.
    2036                                                                 $tag_maker->set_modifiable_text( $text );
    2037                                                         } else {
    2038                                                                 /*
    2039                                                                  * But RAWTEXT and SCRIPT cannot be generally escaped, so reject
    2040                                                                  * updates which would include something that could be misparsed
    2041                                                                  * as a block comment delimiter.
    2042                                                                  */
    2043                                                                 $tag_maker->set_modifiable_text( '' );
     2021                                                        switch ( $token_name ) {
     2022                                                                case 'IFRAME':
     2023                                                                        /*
     2024                                                                         * IFRAME contents are ignored, so there is no reason to allow
     2025                                                                         * them from untrusted inputs.
     2026                                                                         */
     2027                                                                        $tag_maker->set_modifiable_text( '' );
     2028                                                                        break;
     2029
     2030                                                                case 'TEXTAREA':
     2031                                                                case 'TITLE':
     2032                                                                        /*
     2033                                                                         * RCDATA nodes can be safely escaped, but this must be
     2034                                                                         * done after enqueuing the update to avoid double-escaping.
     2035                                                                         */
     2036                                                                        $tag_maker->set_modifiable_text( $text );
     2037                                                                        $tag_maker->lexical_updates['modifiable text']->text = strtr(
     2038                                                                                $tag_maker->lexical_updates['modifiable text']->text,
     2039                                                                                array(
     2040                                                                                        "\x00" => "\u{FFFD}",
     2041                                                                                        "\r"   => '
',
     2042                                                                                )
     2043                                                                        );
     2044                                                                        break;
     2045
     2046                                                                case 'NOEMBED':
     2047                                                                case 'NOFRAMES':
     2048                                                                case 'XMP':
     2049                                                                        /*
     2050                                                                         * These elements are obsolete and their contents are not
     2051                                                                         * unescaped; therefore make a visual change to prevent
     2052                                                                         * downstream parsers from interpreting content as markup.
     2053                                                                         * The alternative is rejecting the elements entirely, which
     2054                                                                         * is the default behavior unless they are specifically allowed.
     2055                                                                         *
     2056                                                                         * Replacing angle brackets with U+27E8 and U+27E9 is a practical
     2057                                                                         * content degradation that will mostly preserve the contents
     2058                                                                         * perceptually while retaining as much as is safely possible.
     2059                                                                         */
     2060                                                                        $text = strtr(
     2061                                                                                $text,
     2062                                                                                array(
     2063                                                                                        '<' => '⟨',
     2064                                                                                        '>' => '⟩',
     2065                                                                                )
     2066                                                                        );
     2067                                                                        $tag_maker->set_modifiable_text( $text );
     2068                                                                        break;
     2069
     2070                                                                default:
     2071                                                                        if ( self::contains_a_block_delimiter( $text ) ) {
     2072                                                                                /*
     2073                                                                                 * Reject special atomic elements whose contents contain the syntax
     2074                                                                                 * for a block comment delimiter to prevent accidental parses. There
     2075                                                                                 * is no reliable way to escape their content, so rejecting it is a
     2076                                                                                 * more fitting approach for untrusted inputs.
     2077                                                                                 */
     2078                                                                                $tag_maker->set_modifiable_text( '' );
     2079                                                                        } else {
     2080                                                                                // Otherwise consider the contents safe to pass through.
     2081                                                                                $tag_maker->set_modifiable_text( $text );
     2082                                                                        }
    20442083                                                        }
    20452084                                                }
Note: See TracChangeset for help on using the changeset viewer.