mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
fix output safety issue with select options
This commit is contained in:
committed by
Aaron Patterson
parent
b80d8f7111
commit
7b73913701
@@ -616,13 +616,13 @@ module ActionView
|
||||
private
|
||||
def add_options(option_tags, options, value = nil)
|
||||
if options[:include_blank]
|
||||
option_tags = "<option value=\"\">#{ERB::Util.html_escape(options[:include_blank]) if options[:include_blank].kind_of?(String)}</option>\n" + option_tags
|
||||
option_tags = content_tag('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags
|
||||
end
|
||||
if value.blank? && options[:prompt]
|
||||
prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select')
|
||||
option_tags = "<option value=\"\">#{ERB::Util.html_escape(prompt)}</option>\n" + option_tags
|
||||
option_tags = content_tag('option', prompt, :value => '') + "\n" + option_tags
|
||||
end
|
||||
option_tags.html_safe
|
||||
option_tags
|
||||
end
|
||||
|
||||
def select_content_tag(option_tags, options, html_options)
|
||||
|
||||
@@ -495,7 +495,7 @@ class FormOptionsHelperTest < ActionView::TestCase
|
||||
|
||||
def test_select_under_fields_for_with_string_and_given_prompt
|
||||
@post = Post.new
|
||||
options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>"
|
||||
options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>".html_safe
|
||||
|
||||
output_buffer = fields_for :post, @post do |f|
|
||||
concat f.select(:category, options, :prompt => 'The prompt')
|
||||
@@ -651,6 +651,13 @@ class FormOptionsHelperTest < ActionView::TestCase
|
||||
)
|
||||
end
|
||||
|
||||
def test_select_escapes_options
|
||||
assert_dom_equal(
|
||||
'<select id="post_title" name="post[title]"><script>alert(1)</script></select>',
|
||||
select('post', 'title', '<script>alert(1)</script>')
|
||||
)
|
||||
end
|
||||
|
||||
def test_select_with_selected_nil
|
||||
@post = Post.new
|
||||
@post.category = "<mus>"
|
||||
|
||||
Reference in New Issue
Block a user