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

Allow using use tag in sanitized html #5984

Merged
merged 2 commits into from
Dec 4, 2024
Merged

Allow using use tag in sanitized html #5984

merged 2 commits into from
Dec 4, 2024

Conversation

jorg-vr
Copy link
Contributor

@jorg-vr jorg-vr commented Dec 3, 2024

This pull request adds the possibility to use use tags in html that will pass through our sanitizer.
Looking at the code in the underlying library I deem it safe to use and I have added testcases that should warn us if support in that library changes.

  • Tests were added

@jorg-vr jorg-vr added the bug Something isn't working label Dec 3, 2024
@jorg-vr jorg-vr self-assigned this Dec 3, 2024
@jorg-vr
Copy link
Contributor Author

jorg-vr commented Dec 3, 2024

Ok this just drove me nearly insane. I think the best solution is to allow use.

The below code comes from the PermitScrubber which is used to sanitize our html. This method is performed on every attribute, even when we have explicitly allowed it.

As you can see it is more complex then expected.
All URI's are validated separately and removed if not fitting their standards. (This removes SVG data url's from images)
All SVG atributes that can reference external sources are validated if the attribute references anything dangerous
For href attributes, everything that is not an id is removed <= this one is the relevant one for our case

def scrub_attribute(node, attr_node)
  attr_name = if attr_node.namespace
    "#{attr_node.namespace.prefix}:#{attr_node.node_name}"
  else
    attr_node.node_name
  end

  return if Loofah::HTML5::SafeList::ATTR_VAL_IS_URI.include?(attr_name) && Loofah::HTML5::Scrub.scrub_uri_attribute(attr_node)

  if Loofah::HTML5::SafeList::SVG_ATTR_VAL_ALLOWS_REF.include?(attr_name)
    Loofah::HTML5::Scrub.scrub_attribute_that_allows_local_ref(attr_node)
  end

  if Loofah::HTML5::SafeList::SVG_ALLOW_LOCAL_HREF.include?(node.name) && attr_name == "xlink:href" && attr_node.value =~ /^\s*[^#\s].*/m
    attr_node.remove
  end

  node.remove_attribute(attr_node.name) if attr_name == "src" && attr_node.value !~ /[^[:space:]]/

  Loofah::HTML5::Scrub.force_correct_attribute_escaping! node
end

So I stick to my point that we don't want to be implementing this ourselves. So I wouldn't overwrite this code to allow SVG's inside img tags. But I would allow use tags as the dangerous cases are clearly resolved by this code.

@bmesuere @pdawyndt

@jorg-vr jorg-vr changed the title Add failing test Allow using use tag in sanitized html Dec 3, 2024
@jorg-vr jorg-vr marked this pull request as ready for review December 3, 2024 15:56
@jorg-vr jorg-vr requested a review from bmesuere as a code owner December 3, 2024 15:56
@pdawyndt
Copy link
Contributor

pdawyndt commented Dec 3, 2024

@jorg-vr my use case for using the use tag in SVG is to reference the id of a defs tag in the same attribute (#id) so if that could be supported than we don't even need to base64 detour and would be OK for me

Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The pull request modifies the ApplicationHelper module by updating the sanitize method to include the <use> tag as an allowed SVG tag, while removing the <style> tag. This change affects how SVG content is sanitized in the application. Correspondingly, the ApplicationHelperTest class is updated to enhance testing of the sanitize method, particularly focusing on the <use> tag and its attributes. The overall structure and logic of the helper methods and tests remain unchanged.

Changes

File Change Summary
app/helpers/application_helper.rb Updated sanitize method to allow <use> tag in SVG and disallow <style> tag.
test/helpers/application_helper_test.rb Enhanced test cases for sanitize to check for <use> elements and their attributes; expanded SVG validation.

Possibly related PRs

  • Add polyline as allowed svg tag #5853: The changes in this PR also modify the sanitize method in app/helpers/application_helper.rb, specifically updating the allowed SVG tags, which is directly related to the changes made in the main PR.
  • Add img as allowed tag to sanitizer #5983: This PR also alters the sanitize method in app/helpers/application_helper.rb by adding a new allowed tag, which relates to the overall sanitization logic being modified in the main PR.

Suggested labels

enhancement

Suggested reviewers

  • bmesuere

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
test/helpers/application_helper_test.rb (1)

87-87: Consider adding positive test cases for safe use tags.

While the current tests verify that dangerous attributes are filtered, it would be beneficial to add assertions that confirm safe use tags are preserved. This ensures the sanitizer isn't overly aggressive.

 assert_no_match(/xlink:href/, clean_html)
+# Verify safe use tags are preserved
+assert_match(/<use href="#diamond"/, clean_html)
+assert_match(/<use x="50" y="50"/, clean_html)
app/helpers/application_helper.rb (1)

Add href and xlink:href attributes to the safelist for SVG use tag functionality

Based on the test file, specifically the "sanitize helper should allow a selection of svg tags" test, both href and xlink:href attributes are required for proper SVG use tag functionality. The test expects these attributes to be preserved when they reference valid SVG elements (e.g., #diamond), while the security test confirms they should be stripped when containing JavaScript URLs.

  • Add href and xlink:href to the allowed attributes list to support proper SVG use tag functionality
🔗 Analysis chain

Line range hint 138-142: Verify attribute requirements for SVG use tag.

While the tag is now allowed, please verify if any additional SVG-specific attributes are required for the use tag to function properly (e.g., href, xlink:href).


Based on the test file output, I can see that both href and xlink:href attributes are being used with the SVG use tag, but these attributes are not included in the current safelist. Let me verify the test expectations to ensure these attributes are actually required.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for use tag attributes in the codebase to ensure we haven't missed any required attributes

# Search for use tag usage patterns
rg -A 2 "<use\s+[^>]+>" 

# Search for SVG-related tests or documentation
fd -e md -e txt . | xargs rg -l "svg|use tag" 

Length of output: 904


Script:

#!/bin/bash
# Check the test file to understand the test expectations
cat test/helpers/application_helper_test.rb

Length of output: 6510

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3cad0b6 and 00cea2c.

📒 Files selected for processing (2)
  • app/helpers/application_helper.rb (1 hunks)
  • test/helpers/application_helper_test.rb (2 hunks)
🔇 Additional comments (2)
test/helpers/application_helper_test.rb (2)

77-79: LGTM! Good security test coverage.

The test cases appropriately verify that both xlink:href and href attributes with JavaScript URLs are filtered out, while allowing safe URLs.


136-137: LGTM! Comprehensive test coverage for SVG use cases.

The test cases properly verify that use tags work with both xlink:href and href attributes when referencing local IDs, which is the intended use case.

@jorg-vr
Copy link
Contributor Author

jorg-vr commented Dec 3, 2024

@pdawyndt your usecase would be supported by this pr

@bmesuere bmesuere changed the title Allow using use tag in sanitized html Allow using use tag in sanitized html Dec 3, 2024
@jorg-vr jorg-vr merged commit f97d88e into main Dec 4, 2024
14 checks passed
@jorg-vr jorg-vr deleted the fix/svg-images branch December 4, 2024 08:31
@pdawyndt
Copy link
Contributor

pdawyndt commented Dec 4, 2024

Confirmation that everything works on my side. Thanks for taking this up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants