wppaste
WordPress

WP_REST_Global_Styles_Controller::validate_custom_css( mixed $css ): true|WP_Error

Since
6.2.0, 6.4.0, 7.0.0, 7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php:683
Validate style.css as valid CSS.

Description

Currently just checks that CSS will not break an HTML STYLE tag.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$cssmixed
CSS to validate.

Return value

true|WP_Error
True if the input was validated, otherwise WP_Error.

Performance profile

How much work a call to WP_REST_Global_Styles_Controller::validate_custom_css() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
12–51

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 85.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_REST_Global_Styles_Controller::validate_custom_css()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Global_Styles_Controller::validate_custom_css() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_string($css) && !$at12strcspn()
!is_string($css)12__()
is_string($css) && $at && $remaining_strlen41strcspn(), substr_compare(), __(), esc_html(), sprintf()
is_string($css) && $at && !$remaining_strlen51strcspn(), substr_compare(), strspn(), __(), esc_html(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8512–515
8.58512–515
8.48512–5159 fewer instructions than PHP 8.3
8.39412–575
8.29412–575
8.19412–575
7.49412–575

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 3

Used by · 1

Source code

	protected function validate_custom_css( $css ) {		if ( ! is_string( $css ) ) {			return new WP_Error(				'rest_custom_css_invalid_type',				__( 'CSS must be a string.' ),				array( 'status' => 400 )			);		} 		$length = strlen( $css );		for (			$at = strcspn( $css, '<' );			$at < $length;			$at += strcspn( $css, '<', ++$at )		) {			$remaining_strlen = $length - $at;			/**			 * Custom CSS text is expected to render inside an HTML STYLE element.			 * A STYLE closing tag must not appear within the CSS text because it			 * would close the element prematurely.			 *			 * The text must also *not* end with a partial closing tag (e.g., `<`,			 * `</`, … `</style`) because subsequent styles which are concatenated			 * could complete it, forming a valid `</style>` tag.			 *			 * Example:			 *			 *     $style_a = 'p { font-weight: bold; </sty';			 *     $style_b = 'le> gotcha!';			 *     $combined = "{$style_a}{$style_b}";			 *			 *     $style_a = 'p { font-weight: bold; </style';			 *     $style_b = 'p > b { color: red; }';			 *     $combined = "{$style_a}\n{$style_b}";			 *			 * Note how in the second example, both of the style contents are benign			 * when analyzed on their own. The first style was likely the result of			 * improper truncation, while the second is perfectly sound. It was only			 * through concatenation that these two styles combined to form content			 * that would have broken out of the containing STYLE element, thus			 * corrupting the page and potentially introducing security issues.			 *			 * @link https://html.spec.whatwg.org/multipage/parsing.html#rawtext-end-tag-name-state			 */			$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;	}

Changelog

Introduced in 6.2.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.1.0
Parameter $css retyped from string to mixed.verified against source
7.1.0
Rejects non-string values with a WP_Error instead of a fatal error.from the docblock
7.0.0
Only restricts contents which risk prematurely closing the STYLE element, either through a STYLE end tag or a prefix of one which might become a full end tag when combined with the contents of other styles.from the docblock
6.4.0
Changed method visibility to protected.from the docblock
6.2.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.