Make WordPress Core

Opened 9 hours ago

Last modified 9 hours ago

#65379 new defect (bug)

Elements: Guard against non-string className in render filter

Reported by: aaronrobertshaw's profile aaronrobertshaw Owned by:
Milestone: 7.1 Priority: normal
Severity: normal Version: trunk
Component: Editor Keywords: gutenberg-merge has-patch has-unit-tests
Focuses: Cc:

Description

This ticket tracks the backport of PHP files for the following Gutenberg update:

https://github.com/WordPress/gutenberg/pull/78841

Adds a safeguard in elements block support rendering to prevent fatal errors when a block’s className attribute is not a string. The backport updates wp_render_elements_class_name() to bail early and return block content unchanged for invalid className values, and includes a PHPUnit test covering this regression case.

Change History (1)

This ticket was mentioned in PR #12028 on WordPress/wordpress-develop by @aaronrobertshaw.


9 hours ago
#1

  • Keywords has-patch has-unit-tests added

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

This PR brings the changes from the following Gutenberg PR to core:

WordPress/gutenberg#78841

## Description
Hardens the elements block support render filter against invalid block attribute data.

wp_render_elements_class_name() currently reads attrs.className and passes it into preg_match(). While className is expected to be a string, malformed or corrupted stored block data can contain other types (for example, an array), which can trigger a fatal TypeError in PHP 8+.

This backport adds a defensive type check so that when className is not a string, the function returns the original block content unchanged instead of attempting regex matching.

A regression test is also added to ensure non-string className values do not cause fatals and continue to fail gracefully.

## Testing

  • Run the PHPUnit tests for elements block support: wpRenderElementsSupport.php
  • Verify the new regression test passes: test_elements_block_support_class_with_non_string_class_name
  • Verify existing tests in the same file continue to pass.
Note: See TracTickets for help on using tickets.