Skip to content
This repository
Browse code

fix output safety issue with select options

  • Loading branch information...
commit 1be2bbec310ffe94061cca7ba0e3c1a478af03af 1 parent da7d0a2
Sergey Nartimov authored February 20, 2012 tenderlove committed February 20, 2012
6  actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -595,13 +595,13 @@ def to_time_zone_select_tag(priority_zones, options, html_options)
595 595
       private
596 596
         def add_options(option_tags, options, value = nil)
597 597
           if options[:include_blank]
598  
-            option_tags = "<option value=\"\">#{ERB::Util.html_escape(options[:include_blank]) if options[:include_blank].kind_of?(String)}</option>\n" + option_tags
  598
+            option_tags = content_tag('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags
599 599
           end
600 600
           if value.blank? && options[:prompt]
601 601
             prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select')
602  
-            option_tags = "<option value=\"\">#{ERB::Util.html_escape(prompt)}</option>\n" + option_tags
  602
+            option_tags = content_tag('option', prompt, :value => '') + "\n" + option_tags
603 603
           end
604  
-          option_tags.html_safe
  604
+          option_tags
605 605
         end
606 606
     end
607 607
 
9  actionpack/test/template/form_options_helper_test.rb
@@ -452,7 +452,7 @@ def @post.to_param; 108; end
452 452
 
453 453
   def test_select_under_fields_for_with_string_and_given_prompt
454 454
     @post = Post.new
455  
-    options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>"
  455
+    options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>".html_safe
456 456
 
457 457
     output_buffer = fields_for :post, @post do |f|
458 458
       concat f.select(:category, options, :prompt => 'The prompt')
@@ -556,6 +556,13 @@ def test_select_with_index_option
556 556
     )
557 557
   end
558 558
 
  559
+  def test_select_escapes_options
  560
+    assert_dom_equal(
  561
+      '<select id="post_title" name="post[title]">&lt;script&gt;alert(1)&lt;/script&gt;</select>',
  562
+      select('post', 'title', '<script>alert(1)</script>')
  563
+    )
  564
+  end
  565
+
559 566
   def test_select_with_selected_nil
560 567
     @post = Post.new
561 568
     @post.category = "<mus>"

0 notes on commit 1be2bbe

Please sign in to comment.
Something went wrong with that request. Please try again.