Repository navigation
KSES: Allow CSS colour functions in inline styles - #84213
Conversation
8b91a66 to
f9141c2
Compare
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the ⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Delete (forward) in
|
7f540f4 to
9ec6af2
Compare
aa77660 to
8acfdf5
Compare
8acfdf5 to
49d7111
Compare
3be9c4b to
b70d26e
Compare
ramonjd
left a comment
There was a problem hiding this comment.
Thanks for getting this up! I know there's a long ramp to 7.3, so I appreciate the early PR.
I'm for this change, but it'd good to understand what these CSS open up.
I'm imagining it'd be handy for theme devs, or anyone (or anything) generating global styles by hand. Are there any implications for block tools in the editor?
Soft ping to @sirreal just for awareness as it relates to some of what folks have been thinking about in relation to the kses vs valid CSS cage fight
| } | ||
| } | ||
|
|
||
| $css_test_string = preg_replace( $color_function, '0', $css_test_string ); |
There was a problem hiding this comment.
TIL that Core has always checked CSS this way, i.e., by removing each passing bit and replace it with 0.
There was a problem hiding this comment.
Yep. My understanding is so that a nested function still leaves a value for the outer check.
The shim ran at priority 10 and turned Core's false into true, so it also overrode a filter that rejected the same declaration at an earlier priority. Running it at PHP_INT_MIN makes it act like Core's own check: later filters still get the final say. Tests now reject `color: rgb(0 0 0)` from filters at priorities 5 and 10, and cover characters left over after a colour function.
Lower the nesting cap from 32 to 4, the deepest real combination of colour functions, and explain it in a comment. Run the final leftover check after the loop so the shim keeps exactly the cap. Reword the docblock to say the shim runs first. The rejection test now uses __return_false, because Core passes the filter the test string with colour functions already replaced. Test 4 levels kept and 5 dropped, and skip the drop test when Core already allows colour functions.
b70d26e to
c843479
Compare
|
Appreciate the in depth review and questions @ramonjd 👍
It doesn't open up anything beyond colour values. Only the listed colour functions are recognised, their contents have to be plain colour syntax (numbers, units, hex, keywords,
Not really. The colour pickers still save hex. Where it does show up though is:
Shadows picked in the block inspector aren't affected, since they're saved as preset references and output as |
`safecss_filter_attr()` rejected any declaration containing a color function such as `rgb()`, `hsl()` or `color-mix()`, so styles like a `box-shadow` with an `rgba()` color were stripped. Color functions are now removed from the test string, innermost first, when their contents are plain color syntax. Anything left over, such as a parenthesis, backslash, comment or unknown function, is still rejected, and the `safecss_filter_attr_allow_css` filter can still reject the declaration. Synced from WordPress/gutenberg#84213. Fixes #66387.
|
A change like this is overdue. I've been working on CSS foundations in WordPress/wordpress-develop#14781. Building on that foundation, I re-implemented None of the work is ready for any serious review at this time, but it could make this change obsolete. No promises and that doesn't block this work. I'd be very happy to land things early in the 7.3 cycle. |
Makes sense, thanks. 👍🏻 That use case alone is a good sell.
🤞🏻 Appreciate the heads up. Happy to help test stuff! |
Given it was noted the ongoing work didn't have to block this, I'll get this merged. Worst case it's a quick revert. |
`safecss_filter_attr()` rejected any declaration containing a color function such as `rgb()`, `hsl()` or `color-mix()`, so styles like a `box-shadow` with an `rgba()` color were stripped. Color functions are now removed from the test string, innermost first, when their contents are plain color syntax. Anything left over, such as a parenthesis, backslash, comment or unknown function, is still rejected, and the `safecss_filter_attr_allow_css` filter can still reject the declaration. Synced from WordPress/gutenberg#84213. Fixes #66387.

What?
safecss_filter_attr()drops any declaration whose value uses a CSS colour function. This keeps them:rgb(),rgba(),hsl(),hsla(),hwb(),lab(),lch(),oklab(),oklch(),color(),color-mix()andlight-dark().Why?
These are all dropped today:
box-shadow: 1px 1px 1px rgba(0,0,0,0.5)box-shadow: 6px 6px 9px rgb(0 0 0 / 20%), 0 0 0 1px hsl(0 0% 0%)color: oklch(70% 0.1 200)color: color-mix(in srgb, #fff 50%, #000)The style engine runs its output through the same function, so a shadow, border colour or text colour written this way never reaches the page. Gradients already have an exception (#79791).
How?
A new
safecss_filter_attr_allow_cssfilter inlib/compat/wordpress-7.3/kses.php.It only takes effect when a colour function is present and only approves one whose contents are plain colour syntax, innermost first so nested values work. It runs before other filters, so a rejection from another filter stands. Everything else is checked as before.
Core backport: Trac ticket 66387 adds the same check to
safecss_filter_attr()for WordPress 7.3.Testing Instructions
Use of AI Tools
Implemented and tested by a Claude Code agent from a predefined task.