REST API: Reject non-string custom CSS in the global styles controller - #12549
REST API: Reject non-string custom CSS in the global styles controller#12549ramonjd wants to merge 1 commit into
Conversation
`styles.css` has no type constraint in the global styles request schema, so a REST consumer can PUT an array (or any non-string value). That value reaches `strlen()` in `validate_custom_css()` and throws an uncaught `TypeError` on PHP 8+. Guard `validate_custom_css()` against non-string input, returning a `rest_custom_css_invalid_type` `WP_Error` with a 400 status instead of a fatal. Adds a controller test that PUTs `styles.css` as an array and asserts the 400 response, plus unit tests covering array, integer, boolean, null, and object values. Props ramonjd. Fixes #65640. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
andrewserong
left a comment
There was a problem hiding this comment.
This LGTM and matches the GB PR. As mentioned on the other PR, I think throwing a 400 in this case is the right behaviour as any consumer should be following the theme.json schema for this, where css is defined as a string. And throwing a 400 is safer than trying to infer a value from differently shaped data.
styles.csshas no type constraint in the global styles request schema, so a REST consumer can PUT an array (or any non-string value). That value reachesstrlen()invalidate_custom_css()and throws an uncaughtTypeErroron PHP 8+.Guard
validate_custom_css()against non-string input, returning arest_custom_css_invalid_typeWP_Errorwith a 400 status instead of a fatal.Adds a controller test that PUTs
styles.cssas an array and asserts the 400 response, plus unit tests covering array, integer, boolean, null, and object values.Backports WordPress/gutenberg#80338
Trac ticket: https://core.trac.wordpress.org/ticket/65640