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

feat: support empty HTML5 data attributes #216

Merged
merged 1 commit into from Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
# Changelog

## next / unreleased

### Features

* Support empty HTML5 data attributes. [[#215](https://github.com/flavorjones/loofah/issues/215)]


## 2.11.0 / 2021-07-31

### Features
Expand Down
7 changes: 5 additions & 2 deletions lib/loofah/html5/scrub.rb
Expand Up @@ -10,6 +10,7 @@ module Scrub
CRASS_SEMICOLON = { node: :semicolon, raw: ";" }
CSS_IMPORTANT = '!important'
CSS_PROPERTY_STRING_WITHOUT_EMBEDDED_QUOTES = /\A(["'])?[^"']+\1\z/
DATA_ATTRIBUTE_NAME = /\Adata-[\w-]+\z/

class << self
def allowed_element?(element_name)
Expand All @@ -25,7 +26,7 @@ def scrub_attributes(node)
attr_node.node_name
end

if attr_name =~ /\Adata-[\w-]+\z/
if attr_name =~ DATA_ATTRIBUTE_NAME
next
end

Expand Down Expand Up @@ -62,7 +63,9 @@ def scrub_attributes(node)
scrub_css_attribute(node)

node.attribute_nodes.each do |attr_node|
node.remove_attribute(attr_node.name) if attr_node.value !~ /[^[:space:]]/
if attr_node.value !~ /[^[:space:]]/ && attr_node.name !~ DATA_ATTRIBUTE_NAME
node.remove_attribute(attr_node.name)
end
end

force_correct_attribute_escaping!(node)
Expand Down
15 changes: 10 additions & 5 deletions test/html5/test_sanitizer.rb
Expand Up @@ -102,18 +102,23 @@ def assert_completes_in_reasonable_time(&block)

def test_should_allow_data_attributes
input = "<p data-foo='foo'>foo <bad>bar</bad> baz</p>"

output = "<p data-foo='foo'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"
htmloutput = "<p data-foo='foo'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"

check_sanitization(input, htmloutput, output, output)
check_sanitization(input, output, output, output)
end

def test_should_allow_multi_word_data_attributes
input = "<p data-foo-bar-id='11'>foo <bad>bar</bad> baz</p>"
output = htmloutput = "<p data-foo-bar-id='11'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"
output = "<p data-foo-bar-id='11'>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"

check_sanitization(input, htmloutput, output, output)
check_sanitization(input, output, output, output)
end

def test_should_allow_empty_data_attributes
input = "<p data-foo data-bar="">foo <bad>bar</bad> baz</p>"
output = "<p data-foo data-bar=''>foo &lt;bad&gt;bar&lt;/bad&gt; baz</p>"

check_sanitization(input, output, output, output)
end

def test_should_allow_contenteditable
Expand Down