Skip to content

Commit f05af91

Browse files
andrewcarpentertenderlove
authored andcommitted
ensure tag/content_tag escapes " in attribute vals
Many helpers mark content as HTML-safe without escaping double quotes -- including `sanitize`. Regardless of whether or not the attribute values are HTML-escaped, we want to be sure they don't include double quotes, as that can cause XSS issues. For example: `content_tag(:div, "foo", title: sanitize('" onmouseover="alert(1);//'))` CVE-2016-6316
1 parent 7f2327f commit f05af91

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

actionview/lib/action_view/helpers/tag_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def tag_option(key, value, escape)
181181
else
182182
value = escape ? ERB::Util.unwrapped_html_escape(value) : value
183183
end
184-
%(#{key}="#{value}")
184+
%(#{key}="#{value.gsub(/"/, '"'.freeze)}")
185185
end
186186
end
187187
end

actionview/test/template/tag_helper_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ def test_tag_honors_html_safe_with_escaped_array_class
140140
assert_equal '<p class="song> play&gt;" />', str
141141
end
142142

143+
def test_tag_does_not_honor_html_safe_double_quotes_as_attributes
144+
assert_dom_equal '<p title="&quot;">content</p>',
145+
content_tag('p', "content", title: '"'.html_safe)
146+
end
147+
148+
def test_data_tag_does_not_honor_html_safe_double_quotes_as_attributes
149+
assert_dom_equal '<p data-title="&quot;">content</p>',
150+
content_tag('p', "content", data: { title: '"'.html_safe })
151+
end
152+
143153
def test_skip_invalid_escaped_attributes
144154
['&1;', '&#1dfa3;', '& #123;'].each do |escaped|
145155
assert_equal %(<a href="#{escaped.gsub(/&/, '&amp;')}" />), tag('a', :href => escaped)

0 commit comments

Comments
 (0)