-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Fix problem with encoding of css entities when post with block level custom css is edited by user without unfiltered_html #11104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
glendaviesnz
wants to merge
2
commits into
WordPress:trunk
Choose a base branch
from
glendaviesnz:fix/block-custom-css-bug
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2077,6 +2077,17 @@ function _filter_block_content_callback( $matches ) { | |
| function filter_block_kses( $block, $allowed_html, $allowed_protocols = array() ) { | ||
| $block['attrs'] = filter_block_kses_value( $block['attrs'], $allowed_html, $allowed_protocols, $block ); | ||
|
|
||
| // Per-block custom CSS (attrs.style.css) may contain & and > as valid | ||
| // CSS selectors. wp_kses() entity-encodes these because it treats the | ||
| // value as HTML. Decode them after KSES has already stripped any | ||
| // dangerous HTML tags, so the CSS round-trips correctly through | ||
| // serialize_block_attributes(). | ||
| if ( isset( $block['attrs']['style']['css'] ) ) { | ||
| $block['attrs']['style']['css'] = undo_block_custom_css_kses_entities( | ||
| $block['attrs']['style']['css'] | ||
| ); | ||
| } | ||
|
|
||
| if ( is_array( $block['innerBlocks'] ) ) { | ||
| foreach ( $block['innerBlocks'] as $i => $inner_block ) { | ||
| $block['innerBlocks'][ $i ] = filter_block_kses( $inner_block, $allowed_html, $allowed_protocols ); | ||
|
|
@@ -2124,6 +2135,40 @@ function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = ar | |
| return $value; | ||
| } | ||
|
|
||
| /** | ||
| * Decodes HTML entities in per-block custom CSS that were incorrectly | ||
| * introduced by wp_kses() during the block KSES filtering pipeline. | ||
| * | ||
| * Per-block custom CSS (stored in attrs.style.css) may contain & and > | ||
| * as valid CSS selectors (nesting and child combinator). When wp_kses() | ||
| * processes this CSS string as if it were HTML, it entity-encodes these | ||
| * characters (&, >). If the block is then re-serialized via | ||
| * serialize_block_attributes(), the entity's ampersand is escaped again | ||
| * (\u0026amp;), producing a double-encoded value that corrupts the CSS | ||
| * on subsequent editor loads. | ||
| * | ||
| * This reverses only the specific named entities that wp_kses() may | ||
| * introduce, intentionally narrower than wp_specialchars_decode() to | ||
| * avoid decoding numeric/hex references that KSES intentionally preserved. | ||
| * | ||
| * @since 7.0 | ||
| * | ||
| * @param string $value Per-block custom CSS string potentially containing | ||
| * KSES-introduced entities. | ||
| * @return string CSS string with KSES-introduced entities decoded. | ||
| */ | ||
| function undo_block_custom_css_kses_entities( $value ) { | ||
| if ( ! is_string( $value ) || false === strpos( $value, '&' ) ) { | ||
| return $value; | ||
| } | ||
|
|
||
| return str_replace( | ||
| array( '&', '>', '"', ''' ), | ||
| array( '&', '>', '"', "'" ), | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will not doubt need to account for other values here, but we can work out exactly what needs to be covered once there is some agreement on the best way to solve this problem - I imagine there will be a smarter solution - so didn't spend too much time finessing this one. |
||
| $value | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Sanitizes the value of the Template Part block's `tagName` attribute. | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting up a fix!
I was wondering: what is the important sanitization step for block CSS attributes?
wp_kses()treats the CSS string as HTML. But should it?I'm wondering if an alternative solution would be to target the
cssattribute and run it throughwp_strip_all_tagsrather thanwp_kses.Or running through something similar (or reuse this same validation in a helper) that @sirreal and @dmsnell worked on in
WP_REST_Global_Styles_Controller::validate_custom_css()for https://core.trac.wordpress.org/ticket/64418There, for users without
unfiltered_html,&and>in block custom CSS were being double-encoded by KSES + JSON, so the CSS broke.(Sorry Jon and Dennis - you've become my default go-to brains trust for this stuff 😄 )