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

Added a helper function for wp_kses to sanitize a SVG #137

Closed
wants to merge 3 commits into from

Conversation

bmarshall511
Copy link
Contributor

Description of the Change

Added a helper function used for wp_kses() to return an array of allowed HTML tags & attributes.

Closes #115

How to test the Change

Example usage:

echo wp_kses('<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 25"><path fill="currentcolor" d="M17.525 9.302H14v-2c0-1.032.084-1.682 1.563-1.682h1.868V2.44a26.065 26.065 0 0 0-2.738-.138C11.98 2.302 10 3.959 10 7v2.3H7v4h3v9h4V13.3l3.066-.001.459-3.996Z"/></svg>', \SafeSvg\SafeSvgTags\safe_svg_tags::kses_allowed_html())

Changelog Entry

Added - Added the kses_allowed_html helper function for wp_kses to sanitize SVG images

Credits

Props @bmarshall511

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@bmarshall511 bmarshall511 requested a review from a team as a code owner July 20, 2023 20:02
@bmarshall511 bmarshall511 self-assigned this Jul 20, 2023
@bmarshall511 bmarshall511 requested review from dkotter and removed request for a team July 20, 2023 20:02
Copy link
Collaborator

@darylldoyle darylldoyle left a comment

Choose a reason for hiding this comment

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

I have a few concerns with this PR.

  1. I'm not sure what this solves. KSES cannot be used as an SVG sanitisation method, see here. Is it just to help with output? If so, why not use wp_kses_post()?
  2. There is a list of SVG elements for KSES but no listed attributes which means we'll allow them all. Within the SVG specification, the SVG attributes pose more of a threat to an XSS vulnerability than the elements do.
  3. To keep this in line with the svg-sanitiser library, this list will need to be kept up to date with each release of the underlying library. This will likely be labour-intensive.
  4. The Safe SVG plugin includes filters that allow engineers to extend the list of allowed elements 1 and attributes 2. This KSES filter doesn't take that into account and therefore could cause the allowed elements/attributes to be stripped by late escaping.

Whilst I think it's great to push this plugin forward, I think we need to consider how we can ensure we're not overcomplicating things and to always be cognizant of any potential security impacts.

If you're interested in further reading, the below links might be interesting:

  1. The core trac ticket to get SVG support added
  2. An old blog post from me on SVG security
  3. My WordCamp London 2018 talk about SVG security
  4. Mario Heiderich's, The Image that called me presentation

@@ -74,6 +74,14 @@ add_filter( 'svg_allowed_tags', function ( $tags ) {
} );
```

### Can `wp_kses` be used with a helper to sanitize an SVG?
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue: kses will not sanitise SVGs, so we either need to change this title, or re-think the use-case for this filter.

*/
public function test_kses_allowed_html() {
$allowed_html = SafeSvg\SafeSvgTags\safe_svg_tags::kses_allowed_html();
$this->assertIsArray( $allowed_html );
Copy link
Collaborator

Choose a reason for hiding this comment

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

thought (non-blocking): This feels like adding a test just to have a test. I'd prefer it if we were either verifying that the correct items were stripped from an SVG, or that the correct params/attributes are included.

@bmarshall511
Copy link
Contributor Author

@darylldoyle Totally get where you're coming from. This was actually in response to this issue: #115. In a few projects that I've been dabbling in recently, we've been using something similar within the theme to sanitize SVG output, so thought it might be a pretty cool solution for that request.

You've made some solid points, especially about KSES not being suitable for SVG sanitization. And I agree, maintaining the SVG elements list in line with the svg-sanitiser library might be quite a handful.

I hear ya about the fact that the KSES filter doesn't consider the extendibility of the list of allowed elements/attributes. This could indeed lead to some elements/attributes being stripped off due to late escaping. And for sure, the last thing we want is to overcomplicate things and neglect potential security implications.

Gonna check out those resources you've shared - seems like some really useful stuff there. I'm all about that SVG security knowledge, so much appreciated for that! How about for now I close this & can be used as reference for the #115 issue?

@darylldoyle
Copy link
Collaborator

@bmarshall511 I appreciate your willingness to understand this! I hope the shared resources are helpful 🙂

In regards to #115, I think it'd be useful for Safe SVG to provide a helper function which wraps these lines:

https://github.com/10up/safe-svg/blob/develop/safe-svg.php#L277-L283

That would give people a way to output SVGs whilst ensuring they're sanitised. There's something to say for sanitising it to appease a linter. If the SVG is bundled with a theme, do we need to sanitise it on output? We should know what's in that file. We mainly need to sanitise files coming from third parties.

It would make sense if we were outputting SVGs from an API or other third-party integration.

I'm happy for you to close this issue and move the discussion to #115 if you like.

@jeffpaul jeffpaul added this to the Future Release milestone Aug 16, 2023
@jeffpaul jeffpaul modified the milestones: Future Release, 2.2.1 Oct 10, 2023
@dkotter dkotter removed this from the 2.2.1 milestone Oct 19, 2023
@jeffpaul jeffpaul deleted the feature/115-wp-kses-helper-function branch November 2, 2023 16:28
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.

It would be beneficial if you could use safe-svg for wp_kses and other similar purposes.
4 participants