-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Fix HTML Block: Hide CSS/JS tabs and show warning for users without unfiltered_html capability #73749
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
Conversation
Co-authored-by: sirreal <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: sirreal <[email protected]>
sirreal
left a comment
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.
@copilot We should show a notice if a user without sufficient capabilities is editing a post with CSS/JS that would be mangled when they save it.
@youknowriad thoughts?
| // Show CSS/JS tabs only if user has unfiltered_html capability | ||
| // This prevents users from adding CSS/JS they can't actually save | ||
| const shouldShowCssTab = canUserUseUnfilteredHTML; | ||
| const shouldShowJsTab = canUserUseUnfilteredHTML; |
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.
It doesn't seem like we need two values for this, we can just use canUserUseUnfilteredHTML.
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.
Updated in eb6f24c to use canUserUseUnfilteredHTML directly throughout the code.
Co-authored-by: sirreal <[email protected]>
Co-authored-by: sirreal <[email protected]>
Added warning notice in 47f3b9f. When a user without |
| css: editedCss, | ||
| js: editedJs, | ||
| css: canUserUseUnfilteredHTML ? editedCss : '', | ||
| js: canUserUseUnfilteredHTML ? editedJs : '', |
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.
Is it possible for a user without the capability to edit a post initially created by another user with the capability? What should happen in this case.
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.
In my testing I did have a case where I authored a post without the capability, edited it as another user with the capability (adding CSS+JS), then editing again as the user without and the content gets mangled. So it's certainly possible.
| <Tabs.Tab tabId="html">HTML</Tabs.Tab> | ||
| <Tabs.Tab tabId="css">CSS</Tabs.Tab> | ||
| { shouldShowJsTab && ( | ||
| { canUserUseUnfilteredHTML && ( |
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.
Hiding the table like here is probably a good idea, but I do wonder about serialization
| className="block-library-html__modal-notice" | ||
| > | ||
| { __( | ||
| 'This block contains CSS or JavaScript that will be removed when you save because you do not have permission to use unfiltered HTML.' |
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.
I guess that message is clear enough.
youknowriad
left a comment
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.
Actually this is nice
|
@copilot can you test this PR and provide screenshots? I'd like to see the Notice rendered. You should be able to test using the playground at https://playground.wordpress.net/?gutenberg-pr=73749 if you can use a browser. |
I'm unable to access playground.wordpress.net from my environment (domain is blocked). However, based on the implementation in 47f3b9f: When the Notice appears:
Notice properties:
Testing scenario:
The Notice uses WordPress's standard warning component styling (typically yellow background with warning icon). |
|
Size Change: +70 B (0%) Total Size: 2.58 MB
ℹ️ View Unchanged
|
|
Flaky tests detected in 47f3b9f. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/19928596510
|
|
@sirreal should we merge this one? |
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @Copilot. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If 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. |
|
Yeah, I think this is OK to merge. |
What?
Fixes HTML block rendering issue where CSS/JS content displays as raw text on the frontend for users without
unfiltered_htmlcapability (authors, contributors).Why?
WordPress's kses filtering strips
<style>and<script>tags from content saved by users withoutunfiltered_htmlcapability. When authors used the CSS/JS tabs introduced in #73108, the tags were stripped but the raw text content remained, rendering visibly on the frontend instead of being applied as styles/scripts.How?
Three-part fix matching WordPress's security model:
unfiltered_htmlcapabilityChanges to
packages/block-library/src/html/modal.js:canUserUseUnfilteredHTMLto conditionally show CSS/JS tabshandleUpdate()to conditionally strip CSS/JS:css: canUserUseUnfilteredHTML ? editedCss : ''hasRestrictedContentis true (user lacks capability AND block contains CSS/JS)@wordpress/componentsThe Notice appears between the tab header and content area with:
status="warning"for yellow warning stylingisDismissible={ false }to ensure users see the messageTesting Instructions
As Administrator (has unfiltered_html):
h1 { color: blue; }<h1>Test</h1>As Author (no unfiltered_html):
<h1>Test</h1>Cross-role editing (warning notice test):
h1 { color: red; }and HTML:<h1>Test</h1>Testing Instructions for Keyboard
Navigate to HTML block using Tab, press Enter to open editor. Use Tab to navigate between HTML/CSS/JS tabs when logged in as admin. Verify CSS/JS tabs are not in tab order when logged in as author. When warning notice is present, verify it can be read by screen readers.
Screenshots or screencast
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.