Skip to content

REST API: Reject non-string custom CSS in the global styles controller - #12549

Closed
ramonjd wants to merge 1 commit into
WordPress:trunkfrom
ramonjd:add/is-string-check-custom-css
Closed

REST API: Reject non-string custom CSS in the global styles controller#12549
ramonjd wants to merge 1 commit into
WordPress:trunkfrom
ramonjd:add/is-string-check-custom-css

Conversation

@ramonjd

@ramonjd ramonjd commented Jul 16, 2026

Copy link
Copy Markdown
Member

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.

Backports WordPress/gutenberg#80338

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

`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>
@ramonjd
ramonjd requested a review from sirreal July 16, 2026 01:40
@github-actions

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd
ramonjd requested a review from glendaviesnz July 16, 2026 01:41
@ramonjd ramonjd self-assigned this Jul 16, 2026
@ramonjd ramonjd added the bug label Jul 16, 2026
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 62762
GitHub commit: 4c7af09

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants