Skip to content

Commit 8f544bc

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 2efddad commit 8f544bc

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

actionview/lib/action_view/helpers/tag_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def tag_option(key, value, escape)
189189
else
190190
value = escape ? ERB::Util.unwrapped_html_escape(value) : value
191191
end
192-
%(#{key}="#{value}")
192+
%(#{key}="#{value.gsub(/"/, '"'.freeze)}")
193193
end
194194
end
195195
end

actionview/test/template/tag_helper_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ def test_tag_honors_html_safe_with_escaped_array_class
150150
assert_equal '<p class="song> play&gt;" />', str
151151
end
152152

153+
def test_tag_does_not_honor_html_safe_double_quotes_as_attributes
154+
assert_dom_equal '<p title="&quot;">content</p>',
155+
content_tag('p', "content", title: '"'.html_safe)
156+
end
157+
158+
def test_data_tag_does_not_honor_html_safe_double_quotes_as_attributes
159+
assert_dom_equal '<p data-title="&quot;">content</p>',
160+
content_tag('p', "content", data: { title: '"'.html_safe })
161+
end
162+
153163
def test_skip_invalid_escaped_attributes
154164
['&1;', '&#1dfa3;', '& #123;'].each do |escaped|
155165
assert_equal %(<a href="#{escaped.gsub(/&/, '&amp;')}" />), tag('a', :href => escaped)
@@ -177,6 +187,6 @@ def test_aria_attributes
177187
def test_link_to_data_nil_equal
178188
div_type1 = content_tag(:div, 'test', { 'data-tooltip' => nil })
179189
div_type2 = content_tag(:div, 'test', { data: {tooltip: nil} })
180-
assert_dom_equal div_type1, div_type2
190+
assert_dom_equal div_type1, div_type2
181191
end
182192
end

0 commit comments

Comments
 (0)