Skip to content

Conversation

@sirreal
Copy link
Member

@sirreal sirreal commented Dec 29, 2025

  • Allow arbitrary text in Customizer custom CSS.
  • Protect the CSS data from mangling by KSES HTML filters.

Under some circumstances KSES would run post content filters and change the resulting content like this:

 @property --animate {
-  syntax: "<custom-ident>";
+  syntax: "";
   inherits: true;
   initial-value: false;
 }

This depends on #10656 to ensure styles output is safely printed in the HTML (merged here).

Trac ticket: https://core.trac.wordpress.org/ticket/64418


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

github-actions bot commented Dec 29, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple trivial suggestions, but otherwise this looks great.

@sirreal
Copy link
Member Author

sirreal commented Jan 8, 2026

#10641 includes some protection to ensure bad style tag contents are not saved (including </style> or ending in something that could become </style> after concatenation).

This change should be fine because of the work in #10656. Is it worth including that additional safety here?

On one hand, this change should be perfectly safe now.

On the other hand, there's not a very good reason to allow possible style tag closers inside of style tags. It will be safely escaped by the HTML API, but I wonder whether it's more helpful to prevent it from ever being stored.

@westonruter
Copy link
Member

In other words, to restore the validate method but to change this:

if ( preg_match( '#</?\w+#', $css ) ) {

To be rather:

if ( false !== stripos( $css, '</style' ) ) {

Right? That seems fine to me. To be more conservative, and only make it even looser if we find this is actually needed.

@sirreal
Copy link
Member Author

sirreal commented Jan 9, 2026

The check I've proposed is a bit more complicated. It also covers text that ends in partial style close tags <, </, … </style. This should ensure that even when concatenated with other text in a style tag, it cannot produce a style close tag. I do believe that these customizer styles are concatenated with other styles by the global styles system, so this check may be worth having.

The HTML API should still protect against broken style tag contents, so I'm not sure whether this protection is worth bringing here as well.

code sample
	protected function validate_custom_css( $css ) {
		$length = strlen( $css );
		for (
			$at = strcspn( $css, '<' );
			$at < $length;
			$at += strcspn( $css, '<', ++$at )
		) {
			$remaining_strlen         = $length - $at;
			$possible_style_close_tag = 0 === substr_compare( $css, '</style', $at, min( 7, $remaining_strlen ), true );
			if ( $possible_style_close_tag ) {
				if ( $remaining_strlen < 8 ) {
					return new WP_Error(
						'rest_custom_css_illegal_markup',
						sprintf(
							/* translators: %s is the CSS that was provided. */
							__( 'The CSS must not end in "%s".' ),
							esc_html( substr( $css, $at ) )
						),
						array( 'status' => 400 )
					);
				}


				if ( 1 === strspn( $css, " \t\f\r\n/>", $at + 7, 1 ) ) {
					return new WP_Error(
						'rest_custom_css_illegal_markup',
						sprintf(
							/* translators: %s is the CSS that was provided. */
							__( 'The CSS must not contain "%s".' ),
							esc_html( substr( $css, $at, 8 ) )
						),
						array( 'status' => 400 )
					);
				}
			}
		}


		return true;
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants