Make WordPress Core

Opened 4 weeks ago

Closed 4 weeks ago

#66286 closed enhancement (fixed)

Global Styles: Ignore theme.json values of unexpected types

Reported by: ramonopoly Owned by: ramonopoly
Priority: normal Milestone: 7.2
Component: Themes Version: 6.2
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description (last modified by ramonopoly)

Tracks the backport of ​https://github.com/WordPress/gutenberg/pull/84523 and ​https://github.com/WordPress/gutenberg/pull/84529

AI generated content (the main suspect) creating custom CSS for theme.json can cause PHP fatals.

process_blocks_custom_css() tries to explode an non-string ($css).

Reported on a live site:

`
PHP Fatal error: Uncaught TypeError: explode(): Argument #2 ($string) must be of type string, array given in lib/class-wp-theme-json-gutenberg.php:2085
#1 WP_Theme_JSON_Gutenberg::process_blocks_custom_css(Array, 'a:where(:not(.w...')
#2 WP_Theme_JSON_Gutenberg->get_styles_for_block(Array)
`

Change History (3)

This ticket was mentioned in ​PR #14190 on ​WordPress/wordpress-develop by ​@ramonopoly.


4 weeks ago
#1

  • Keywords has-patch has-unit-tests added

Backports ​https://github.com/WordPress/gutenberg/pull/84523.

What

Stops WP_Theme_JSON from throwing fatal TypeErrors when theme.json or global styles data contains values of the wrong type.

Why

The Gutenberg report came from a live site where every front-end page fatalled:

PHP Fatal error: Uncaught TypeError: explode(): Argument #2 ($string) must be of type string, array given
#1 WP_Theme_JSON_Gutenberg::process_blocks_custom_css(Array, 'a:where(:not(.w...')
#2 WP_Theme_JSON_Gutenberg->get_styles_for_block(Array)

sanitize() keeps schema leaves such as css and spacingScale whatever their type, so an array css value reaches explode() in process_blocks_custom_css(). The same gap fatals in a few other places.

What changed

Values of unexpected types are now ignored instead of crashing:

  • css that isn't a string produces no output (the reported crash).
  • ref that isn't a string is ignored.
  • A spacingScale that isn't an array, or a spacingScale.unit that isn't a string, is ignored.
  • Shared block style variations that aren't arrays, and blockTypes items that aren't strings, are skipped.

Valid data is unaffected.

Each case has a test. All nine new cases fail on trunk with a TypeError.

#2 @ramonopoly
4 weeks ago

  • Description modified (diff)

#3 @ramonopoly
4 weeks ago

  • Owner set to ramonopoly
  • Resolution → fixed
  • Status new → closed

In 64309:

Editor: Ignore theme.json values of unexpected types

This commit prevents WP_Theme_JSON from throwing fatal TypeErrors when theme.json or global styles data contains values of the wrong type.

Those values could be user- or AI-generated.

The checks in this commit cover spacing scale arrays, block variation selectors, and custom block CSS processing.

The motivation arose from a real-life PHP fatal where WP_Theme_JSON::process_blocks_custom_css attempted to explode a non-string.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14190

Props ramonopoly, aaronrobertshaw, andrewserong.

Fixes #66286.

Note: See TracTickets for help on using tickets.