Commit 2fc695462d for wordpress.org
commit 2fc695462d03d8c182bc7fe36688b5b64d2b8252
Author: dmsnell <dmsnell@git.wordpress.org>
Date: Wed Dec 2 21:08:52 2026 +0000
KSES: Avoid creating ignored special atomic elements.
There are specific HTML constructions where tags are ignored, and in which cases the “special atomic elements” would never be created because their starting tag is ignored. `wp_kses()` should ensure that these aren’t created when a browser would ignore them.
This patch detects these situations and prevents the creation of the elements when their starting tag would be ignored.
This patch was created with the help of Claude Code.
Developed in: https://github.com/WordPress/wordpress-develop/pull/16410
Discussed in: https://core.trac.wordpress.org/ticket/66916
Follow-up to r64233.
Props dmsnell, jonsurrell, westonruter.
Fixes #66916, see #66208.
Built from https://develop.svn.wordpress.org/trunk@65866
git-svn-id: http://core.svn.wordpress.org/trunk@64895 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/html-api/class-wp-html-processor.php b/wp-includes/html-api/class-wp-html-processor.php
index dd4469d527..d2700aa03c 100644
--- a/wp-includes/html-api/class-wp-html-processor.php
+++ b/wp-includes/html-api/class-wp-html-processor.php
@@ -1056,6 +1056,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
}
if ( self::PROCESS_NEXT_NODE === $node_to_process ) {
+ $this->inserts_special_atomic_elements = $this->get_inserted_special_atomic_elements();
parent::next_token();
if ( WP_HTML_Tag_Processor::STATE_TEXT_NODE === $this->parser_state ) {
parent::subdivide_text_appropriately();
@@ -1567,6 +1568,49 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
return str_replace( "\x00", "\u{FFFD}", $text );
}
+ /**
+ * Indicates which special atomic elements the tree builder would insert from their
+ * start tags as the next token.
+ *
+ * Only two situations ignore any of these start tags outside of foreign content: the
+ * "in column group" insertion mode when the current node is not a COLGROUP element,
+ * as inside a TEMPLATE element, and the insertion modes for a FRAMESET, which only
+ * insert NOFRAMES elements. Both depend on nothing but the insertion mode and the
+ * current node, so this is a constant-time check.
+ *
+ * @since 7.2.0
+ *
+ * @see WP_HTML_Tag_Processor::$inserts_special_atomic_elements
+ *
+ * @return bool|'NOFRAMES' `true` if all of them are inserted, `false` if none are,
+ * or the name of the only one which is inserted.
+ */
+ private function get_inserted_special_atomic_elements() {
+ switch ( $this->state->insertion_mode ) {
+ /*
+ * > Anything else
+ * > If the current node is not a colgroup element, then this is a parse error;
+ * > ignore the token.
+ */
+ case WP_HTML_Processor_State::INSERTION_MODE_IN_COLUMN_GROUP:
+ return $this->state->stack_of_open_elements->current_node_is( 'COLGROUP' );
+
+ /*
+ * > A start tag whose tag name is "noframes"
+ * > Process the token using the rules for the "in head" insertion mode.
+ * >
+ * > Anything else
+ * > Parse error. Ignore the token.
+ */
+ case WP_HTML_Processor_State::INSERTION_MODE_IN_FRAMESET:
+ case WP_HTML_Processor_State::INSERTION_MODE_AFTER_FRAMESET:
+ case WP_HTML_Processor_State::INSERTION_MODE_AFTER_AFTER_FRAMESET:
+ return 'NOFRAMES';
+ }
+
+ return true;
+ }
+
/**
* Parses next element in the 'initial' insertion mode.
*
diff --git a/wp-includes/html-api/class-wp-html-tag-processor.php b/wp-includes/html-api/class-wp-html-tag-processor.php
index 34429f2b31..96718851cf 100644
--- a/wp-includes/html-api/class-wp-html-tag-processor.php
+++ b/wp-includes/html-api/class-wp-html-tag-processor.php
@@ -567,6 +567,24 @@ class WP_HTML_Tag_Processor {
*/
private $parsing_namespace = 'html';
+ /**
+ * Whether the tree builder would currently insert special atomic elements from their
+ * start tags: `true` for all of them, `false` for none, or the name of the only one.
+ *
+ * The tokenizer only reads such an element's contents as text once the tree builder
+ * inserts it. Where the tree builder ignores the start tag, the contents remain markup.
+ *
+ * Example:
+ *
+ * // Only NOFRAMES elements are inserted, as in a FRAMESET.
+ * 'NOFRAMES';
+ *
+ * @since 7.2.0
+ *
+ * @var bool|'NOFRAMES'
+ */
+ protected $inserts_special_atomic_elements = true;
+
/**
* What kind of syntax token became an HTML comment.
*
@@ -1087,6 +1105,11 @@ class WP_HTML_Tag_Processor {
return true;
}
+ // Leave the contents of an ignored special atomic element to be parsed as markup.
+ if ( true !== $this->inserts_special_atomic_elements && $tag_name !== $this->inserts_special_atomic_elements ) {
+ return true;
+ }
+
/*
* There are certain elements whose children are not DATA but are instead
* RCDATA or RAWTEXT. These cannot contain other elements, and the contents
diff --git a/wp-includes/kses.php b/wp-includes/kses.php
index a9cb15b90e..0c88d9ab52 100644
--- a/wp-includes/kses.php
+++ b/wp-includes/kses.php
@@ -1141,6 +1141,21 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
*/
private $math_annotation_xml_depth = 0;
+ /**
+ * Tracks, for each TEMPLATE element open in the output, whether a browser parses
+ * its contents in the "in column group" insertion mode, which ignores the start
+ * tags of special atomic elements.
+ *
+ * Each entry is `null` until the first start tag written into the template decides
+ * it: a COL element starts a column group, while most other start tags start other
+ * insertion modes, none of which ignore these start tags.
+ *
+ * @since 7.2.0
+ *
+ * @var array<bool|null>
+ */
+ private $template_column_groups = array();
+
/**
* Constructor function, used internally.
*
@@ -1381,6 +1396,53 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
return $processor->next_delimiter();
}
+ /**
+ * Finds the next token, first telling the tokenizer which special atomic elements a
+ * browser would insert from their start tags, given the output written so far.
+ *
+ * Where a browser ignores one of these start tags, it parses what follows as markup
+ * instead of as the element's text, so it must be sanitized as markup.
+ *
+ * @since 7.2.0
+ * @access private
+ *
+ * @return bool Whether a token was parsed.
+ */
+ public function next_token(): bool {
+ $this->inserts_special_atomic_elements = true !== end( $this->template_column_groups );
+
+ return parent::next_token();
+ }
+
+ /**
+ * Records how a start tag written to the output affects the parsing of special
+ * atomic elements in a browser.
+ *
+ * @since 7.2.0
+ * @access private
+ *
+ * @param string $token_name Name of the start tag written to the output.
+ * @param string $token_namespace Namespace of the start tag.
+ * @phpstan-param non-falsy-string $token_name
+ * @phpstan-param non-falsy-string $token_namespace
+ */
+ private function note_written_start_tag( string $token_name, string $token_namespace ): void {
+ $innermost = count( $this->template_column_groups ) - 1;
+ if ( $innermost < 0 || null !== $this->template_column_groups[ $innermost ] ) {
+ return;
+ }
+
+ // These start tags are processed without deciding how the template contents are parsed.
+ if (
+ 'html' === $token_namespace &&
+ in_array( $token_name, array( 'BASE', 'BASEFONT', 'BGSOUND', 'LINK', 'META', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEMPLATE', 'TITLE' ), true )
+ ) {
+ return;
+ }
+
+ $this->template_column_groups[ $innermost ] = 'html' === $token_namespace && 'COL' === $token_name;
+ }
+
/**
* Returns a sanitized copy of the input HTML.
*
@@ -1433,6 +1495,15 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
break;
}
+ /*
+ * Output should only ever render inside a BODY element or its
+ * descendants, where FRAMESET is ignored. Prevent untrusted
+ * sources from creating these tags to avoid further confusion.
+ */
+ if ( 'FRAMESET' === $token_name && 'html' === $namespace ) {
+ continue;
+ }
+
/*
* PLAINTEXT is unique in that it changes the tokenization. Everything in
* a document following the PLAINTEXT opening tag is interpreted as character
@@ -1555,6 +1626,7 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
if ( 'TEMPLATE' === $token_name && 'html' === $namespace && ! $is_closer ) {
++$template_depth;
+ $this->template_column_groups[] = null;
}
$skip_token = (
@@ -1790,6 +1862,15 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
in_array( $token_name, $special_atomic_elements, true )
);
+ // A browser ignores this start tag, so leave it out; its contents were parsed as markup.
+ if (
+ $is_special_atomic_element &&
+ true !== $this->inserts_special_atomic_elements &&
+ $token_name !== $this->inserts_special_atomic_elements
+ ) {
+ break;
+ }
+
$expects_closer = ! (
'html' === $namespace
? ( WP_HTML_Processor::is_void( $token_name ) || $is_special_atomic_element )
@@ -1929,6 +2010,7 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
* missing, but strip them of their attributes.
*/
$output .= "<{$tag_name}>";
+ $this->note_written_start_tag( $token_name, $namespace );
if ( $needs_special_newline ) {
$special_newline_at = strlen( $output );
}
@@ -1963,6 +2045,7 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
}
$output .= $tag_maker->get_updated_html();
+ $this->note_written_start_tag( $token_name, $namespace );
if ( $needs_special_newline ) {
$special_newline_at = strlen( $output );
}
@@ -2007,6 +2090,7 @@ function wp_sanitize_html_kses( $content, $allowed_html, $allowed_protocols = ar
if ( 'TEMPLATE' === $token_name && $template_depth > 0 && $is_closer && 'html' === $namespace ) {
--$template_depth;
+ array_pop( $this->template_column_groups );
}
}
diff --git a/wp-includes/version.php b/wp-includes/version.php
index d984ff6330..a367a73069 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.3-alpha-65865';
+$wp_version = '7.3-alpha-65866';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.