Skip to content

Commit 55e3325

Browse files
committed
Editor: Guard against non-string className in wp_render_elements_class_name() block render filter callback.
The `wp_render_elements_class_name()` function reads the `className` block attribute and passes it straight to `preg_match()`. While `className` is expected to be a string, malformed or corrupted stored block data can hold another type, such as an array, which triggers a fatal `TypeError` on PHP 8+. Guard against this by returning the block content unchanged when `className` is not a string. While here, align the implementation with `wp_render_custom_css_class_name()` from r62359: replace the regular expression with a `str_contains()` short-circuit followed by an HTML-spec-compliant `strtok()` walk over the class tokens. This also corrects a latent matching bug: the previous `\bwp-elements-\S+\b` pattern treated the hyphen as a word boundary, so a class such as `my-wp-elements-foo` was erroneously matched. Tokenizing the attribute first ensures only a standalone `wp-elements-*` class is applied. Add regression tests for the non-string and substring-prefix cases, and resolve PHPStan errors at rule level 10 (`missingType.iterableValue`, `offsetAccess.nonOffsetAccessible`, `argument.type`) by adding an array shape to the `@phpstan-param` tag. Developed in #12028 and WordPress/gutenberg#78841. Follow-up to r58074, r62359. Props aaronrobertshaw, andrewserong, mukesh27, westonruter. Fixes #65379. git-svn-id: https://develop.svn.wordpress.org/trunk@62475 602fd350-edb4-49c9-b593-d223f7449a82
1 parent dbf0e21 commit 55e3325

3 files changed

Lines changed: 93 additions & 19 deletions

File tree

‎src/wp-includes/block-supports/custom-css.php‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,31 @@ function wp_enqueue_block_custom_css() {
9898
* } $block
9999
*/
100100
function wp_render_custom_css_class_name( $block_content, $block ) {
101-
$class_name_attr = $block['attrs']['className'] ?? null;
102-
103-
if ( ! is_string( $class_name_attr ) || ! str_contains( $class_name_attr, 'wp-custom-css-' ) ) {
101+
$class_name_attr = $block['attrs']['className'] ?? null;
102+
$class_name_prefix = 'wp-custom-css-';
103+
if ( ! is_string( $class_name_attr ) || ! str_contains( $class_name_attr, $class_name_prefix ) ) {
104104
return $block_content;
105105
}
106106

107107
// Parse out the 'wp-custom-css-*' class name added by wp_render_custom_css_support_styles().
108-
$custom_class_name = null;
109-
$token_delimiter = " \t\f\r\n";
110-
$class_token = strtok( $class_name_attr, $token_delimiter );
108+
$matched_class_name = null;
109+
$token_delimiter = " \t\f\r\n";
110+
$class_token = strtok( $class_name_attr, $token_delimiter );
111111
while ( false !== $class_token ) {
112-
if ( str_starts_with( $class_token, 'wp-custom-css-' ) ) {
113-
$custom_class_name = $class_token;
112+
if ( str_starts_with( $class_token, $class_name_prefix ) ) {
113+
$matched_class_name = $class_token;
114114
break;
115115
}
116116
$class_token = strtok( $token_delimiter );
117117
}
118-
if ( null === $custom_class_name ) {
118+
if ( null === $matched_class_name ) {
119119
return $block_content;
120120
}
121121

122122
$tags = new WP_HTML_Tag_Processor( $block_content );
123-
124123
if ( $tags->next_tag() ) {
125124
$tags->add_class( 'has-custom-css' );
126-
$tags->add_class( $custom_class_name );
125+
$tags->add_class( $matched_class_name );
127126
}
128127

129128
return $tags->get_updated_html();

‎src/wp-includes/block-supports/elements.php‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,43 @@ function wp_render_elements_support_styles( $parsed_block ) {
237237
* @see wp_render_elements_support_styles
238238
* @since 6.6.0
239239
*
240-
* @param string $block_content Rendered block content.
241-
* @param array $block Block object.
242-
* @return string Filtered block content.
240+
* @param string $block_content Rendered block content.
241+
* @param array $block Block object.
242+
* @return string Filtered block content.
243+
*
244+
* @phpstan-param array{
245+
* attrs: array{
246+
* className?: string,
247+
* ...
248+
* },
249+
* ...
250+
* } $block
243251
*/
244252
function wp_render_elements_class_name( $block_content, $block ) {
245-
$class_string = $block['attrs']['className'] ?? '';
246-
preg_match( '/\bwp-elements-\S+\b/', $class_string, $matches );
253+
$class_name_attr = $block['attrs']['className'] ?? null;
254+
$class_name_prefix = 'wp-elements-';
255+
if ( ! is_string( $class_name_attr ) || ! str_contains( $class_name_attr, $class_name_prefix ) ) {
256+
return $block_content;
257+
}
247258

248-
if ( empty( $matches ) ) {
259+
// Parse out the 'wp-elements-*' class name.
260+
$matched_class_name = null;
261+
$token_delimiter = " \t\f\r\n";
262+
$class_token = strtok( $class_name_attr, $token_delimiter );
263+
while ( false !== $class_token ) {
264+
if ( str_starts_with( $class_token, $class_name_prefix ) ) {
265+
$matched_class_name = $class_token;
266+
break;
267+
}
268+
$class_token = strtok( $token_delimiter );
269+
}
270+
if ( null === $matched_class_name ) {
249271
return $block_content;
250272
}
251273

252274
$tags = new WP_HTML_Tag_Processor( $block_content );
253-
254275
if ( $tags->next_tag() ) {
255-
$tags->add_class( $matches[0] );
276+
$tags->add_class( $matched_class_name );
256277
}
257278

258279
return $tags->get_updated_html();

‎tests/phpunit/tests/block-supports/wpRenderElementsSupport.php‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,60 @@ public function test_elements_block_support_class( $color_settings, $elements_st
105105
);
106106
}
107107

108+
/**
109+
* Tests that a non-string `className` attribute does not cause a fatal
110+
* error and the block content is returned unmodified.
111+
*
112+
* Block attributes such as `className` are always expected to be strings,
113+
* however invalid stored data can result in other types being present. The
114+
* render filter should fail gracefully rather than passing an array to
115+
* `preg_match()`.
116+
*
117+
* @ticket 65379
118+
*
119+
* @covers ::wp_render_elements_class_name
120+
*/
121+
public function test_elements_block_support_class_with_non_string_class_name(): void {
122+
$block = array(
123+
'blockName' => 'core/paragraph',
124+
'attrs' => array(
125+
'className' => array( '0', '1' ),
126+
),
127+
);
128+
129+
$block_content = "<p class=\"0 1\">Test</p>\n";
130+
131+
$this->assertSame(
132+
$block_content,
133+
wp_render_elements_class_name( $block_content, $block ), // @phpstan-ignore argument.type (Intentionally passing bad attrs array.)
134+
'Block content should be returned unchanged when className is not a string'
135+
);
136+
}
137+
138+
/**
139+
* Tests that a 'my-wp-elements-*' class name is skipped from processing.
140+
*
141+
* @ticket 65379
142+
*
143+
* @covers ::wp_render_elements_class_name
144+
*/
145+
public function test_elements_block_support_class_with_invalid_elements_prefix(): void {
146+
$block = array(
147+
'blockName' => 'core/paragraph',
148+
'attrs' => array(
149+
'className' => 'my-wp-elements-foo',
150+
),
151+
);
152+
153+
$block_content = "<p>Test</p>\n";
154+
155+
$this->assertSame(
156+
$block_content,
157+
wp_render_elements_class_name( $block_content, $block ),
158+
'Block content should be returned unchanged when className lacks a class with the expected prefix'
159+
);
160+
}
161+
108162
/**
109163
* Data provider.
110164
*

0 commit comments

Comments
 (0)