Skip to content
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

Jetpack Tweaks: Decode child selectors > in Custom CSS #746

Merged
merged 2 commits into from
Apr 1, 2022

Conversation

ryelle
Copy link
Contributor

@ryelle ryelle commented Mar 29, 2022

Fixes #678 — The > in CSS is encoded as > when output on WordCamp sites, which is invalid CSS. This fix is very simple, just replace the > with > again on output. The sanitization already strips out any HTML-like tags, so this should not introduce any security issues.

Props @megmorsie, @CdrMarks.

How to test the changes in this Pull Request:

  1. Open the Additional CSS panel in the Customizer
  2. Add CSS with an angle bracket, like
    body > div {
    	background: lightcoral;
    }
  3. It should apply correctly in the customizer, so any divs that are direct children of body should have a pink background now.
  4. Save and exit the customizer, view the site
  5. It should still have the background on the live site
  6. Alternately, view https://yoursite.wordcamp.org/?custom-css and the CSS should be there, with the > not encoded.

I also added two simple tests, you can see it fail if you disable the filter, and pass again after the filter is enabled.

@ryelle ryelle added [Component] Themes & Customization Custom WC Themes, compatibility with core themes, and theme-adjacent customizations to sites [Component] Jetpack Customizations to Jetpack labels Mar 29, 2022
@ryelle ryelle requested review from iandunn and coreymckrill March 29, 2022 21:14
@ryelle ryelle self-assigned this Mar 29, 2022
Copy link
Contributor

@coreymckrill coreymckrill left a comment

Choose a reason for hiding this comment

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

👍 Works on the sandbox. Took a lot of effort to get the tests to run locally 😅 but that looks good too. One minor comment, but not a blocker.

* @return string
*/
function filter_custom_css( $css ) {
return str_replace( '>', '>', $css );
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any other characters this could happen to? Would it make sense to run something like html_entity_decode() here instead of a simple string replace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was hesitant to do that at first because I'm not totally sure why it's encoded, if there would be a security implication to decoding everything. Looking at the different selectors available, no others are encoded, but it would be possible to add anything to an attribute selector, which could get encoded (like a[href*='a=1&b=2], would become a[href*='a=1&b=2]). I'm not sure if supporting that edge case is worth any security issue (but… maybe there is no issue here?)

I updated the test to check against almost all the selectors available.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to merge this as-is, if other selectors become a problem we can make the switch then.

@ryelle ryelle merged commit d0e409c into production Apr 1, 2022
@ryelle ryelle deleted the update/custom-css-encoding branch April 1, 2022 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Component] Jetpack Customizations to Jetpack [Component] Themes & Customization Custom WC Themes, compatibility with core themes, and theme-adjacent customizations to sites
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom CSS outputting HTML entities instead of correct characters
2 participants