Skip to content

Commit

Permalink
ensure protocol processing happens on data attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer committed Jan 11, 2021
1 parent 4f6858f commit fac1a2e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/sanitize/transformers/clean_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,15 @@ def call(env)
unless attr_allowlist.include?(attr_name)
# The attribute isn't allowed.

if allow_data_attributes && attr_name.start_with?('data-')
# Arbitrary data attributes are allowed. If this is a data
# attribute, continue.
next if attr_name =~ REGEX_DATA_ATTR
# Arbitrary data attributes are allowed. If this is a data
# attribute, continue.
unless allow_data_attributes && attr_name.start_with?('data-') &&
attr_name =~ REGEX_DATA_ATTR
# Either the attribute isn't a data attribute or arbitrary data
# attributes aren't allowed. Remove the attribute.
attr.unlink
next
end

# Either the attribute isn't a data attribute or arbitrary data
# attributes aren't allowed. Remove the attribute.
attr.unlink
next
end

# The attribute is allowed.
Expand Down
16 changes: 16 additions & 0 deletions test/test_clean_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,22 @@
}).must_equal "<a>Text</a>"
end

it 'should sanitize protocols in data attributes even if data attributes are generically allowed' do
input = '<a data-url="mailto:someone@example.com">Text</a>'

Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => [:data]},
:protocols => {'a' => {'data-url' => ['https']}}
}).must_equal "<a>Text</a>"

Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => [:data]},
:protocols => {'a' => {'data-url' => ['mailto']}}
}).must_equal input
end

it 'should prevent `<meta>` tags from being used to set a non-UTF-8 charset' do
Sanitize.document('<html><head><meta charset="utf-8"></head><body>Howdy!</body></html>',
:elements => %w[html head meta body],
Expand Down

0 comments on commit fac1a2e

Please sign in to comment.