Skip to content

Block Supports: Strip custom CSS from blocks for users without edit_css capability#11347

Open
glendaviesnz wants to merge 7 commits intoWordPress:trunkfrom
glendaviesnz:trac-64771
Open

Block Supports: Strip custom CSS from blocks for users without edit_css capability#11347
glendaviesnz wants to merge 7 commits intoWordPress:trunkfrom
glendaviesnz:trac-64771

Conversation

@glendaviesnz
Copy link
Copy Markdown

Adds capability-gated CSS stripping so that when a user without edit_css saves a post, any style.css attributes are surgically removed from block comments using WP_Block_Parser::next_token().

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@glendaviesnz glendaviesnz self-assigned this Mar 24, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 24, 2026

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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @gclapps0612-cmd.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Props glendaviesnz, ramonopoly, shailu25, westonruter.

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

@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.

Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
@ramonjd
Copy link
Copy Markdown
Member

ramonjd commented Apr 17, 2026

This is working according to the test steps in WordPress/gutenberg#76650 for me (minus the editor warning of course, which is part of the GB packages). Code matches the GB filters and tests.

I'll rebase for final review.

Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php
Comment thread tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php Outdated
@ramonjd
Copy link
Copy Markdown
Member

ramonjd commented Apr 17, 2026

You can also test that author's can't post block CSS to the REST API :

wp.apiRequest( {
	path: '/wp/v2/posts/<YOUR_POST_ID>',
	method: 'POST',
	contentType: 'application/json',
	processData: false,
	data: JSON.stringify( {
		content:
			'<!-- wp:paragraph {"style":{"css":"color:green"}} --><p class="has-custom-css">Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p><!-- /wp:paragraph -->',
	} ),
} ).done( console.log ).fail( console.error );

Comment thread src/wp-includes/block-supports/custom-css.php Outdated
$this->assertArrayNotHasKey( 'css', $blocks[0]['attrs']['style'] ?? array(), 'style.css should be stripped even from slashed content.' );
}

/**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I added these tests to cover that the filters are added, which I think is the core guarantee of this feature?

Comment thread src/wp-includes/block-supports/custom-css.php
Comment thread src/wp-includes/block-supports/custom-css.php
Comment thread src/wp-includes/block-supports/custom-css.php
Comment thread src/wp-includes/block-supports/custom-css.php Outdated
Comment thread src/wp-includes/block-supports/custom-css.php
…ss capability

Add capability-gated CSS stripping so that when a user without `edit_css`
saves a post, any `style.css` attributes are surgically removed from block
comments using `WP_Block_Parser::next_token()`.

Props TODO.
See #64771.
glendaviesnz and others added 6 commits April 19, 2026 14:56
Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
…rser::next_token()`. Improved logic in `wp_strip_custom_css_from_blocks` to access token data directly from the `$next_token` array. Adjusted filter priorities in `wp_custom_css_kses_init_filters` and `wp_custom_css_force_filtered_html_on_import_filter` to ensure correct execution order during content processing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants