Skip to content
Browse files

fix output safety issue with select options

  • Loading branch information...
1 parent c67ff97 commit 5b4082fddf3412aef6c085fbb2a13fd3bbc75f4e @lest lest committed with tenderlove
View
6 actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -596,13 +596,13 @@ def to_time_zone_select_tag(priority_zones, options, html_options)
private
def add_options(option_tags, options, value = nil)
if options[:include_blank]
- option_tags = "<option value=\"\">#{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=\"\">#{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
end
View
9 actionpack/test/template/form_options_helper_test.rb
@@ -432,7 +432,7 @@ def @post.to_param; 108; end
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')
@@ -536,6 +536,13 @@ def test_select_with_index_option
)
end
+ def test_select_escapes_options
+ assert_dom_equal(
+ '<select id="post_title" name="post[title]">&lt;script&gt;alert(1)&lt;/script&gt;</select>',
+ select('post', 'title', '<script>alert(1)</script>')
+ )
+ end
+
def test_select_with_selected_nil
@post = Post.new
@post.category = "<mus>"

1 comment on commit 5b4082f

@rubys

This change causes the AWDwR tests to fail: http://intertwingly.net/projects/AWDwR3/checkdepot-30/section-23.5.html

Before this change, the output looked like:

<select id='user_country' name='user[country]'><option value='Afghanistan'>Afghanistan</option>
<option value='Aland Islands'>Aland Islands</option>
...
<option selected='selected' value='United States'>United States</option>

After this change, the output looks like this:

<select name='user[country]' id='user_country'>&lt;option value=&quot;Afghanistan&quot;&gt;Afghanistan&lt;/option&gt;
&lt;option value=&quot;Aland Islands&quot;&gt;Aland Islands&lt;/option&gt;
....
&lt;option value=&quot;United States&quot; selected=&quot;selected&quot;&gt;United States&lt;/option&gt;
Please sign in to comment.
Something went wrong with that request. Please try again.