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

ensure protocol processing happens on data attributes #207

Merged
merged 1 commit into from
Jan 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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