Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5616: text_helper_strict_sanitize_patch.2.diff

File text_helper_strict_sanitize_patch.2.diff, 2.8 kB (added by kamens@gmail.com, 4 years ago)

Patch for 5616

  • actionpack/test/template/text_helper_test.rb

    old new  
    194194    result = sanitize(raw) 
    195195    assert_equal %{href="javascript:bang" <a name='hello'>foo</a>, <span>bar</span>}, result 
    196196  end 
     197 
     198  def test_sanitize_javascript_href_newlines 
     199    raw = %{href="j\nava\nscript:bang" <a href="j\nava\nscrip\nt:bang" name="hello">foo</a>, <span href="jav\nascript:bang">bar</span>} 
     200    result = sanitize(raw) 
     201    assert_equal %{href="j\nava\nscript:bang" <a name='hello'>foo</a>, <span>bar</span>}, result 
     202  end 
     203 
     204  def test_sanitize_javascript_style_newlines 
     205    raw = %{style="javascript:bang" <a style='background:url("javascr\ni\npt:bang")' name="hello">foo</a>, <span style="background:url('jav\nascript:bang')">bar</span>} 
     206    result = sanitize(raw) 
     207         assert_equal %{style="javascript:bang" <a name='hello'>foo</a>, <span>bar</span>}, result 
     208  end 
    197209   
    198210  def test_cycle_class 
    199211    value = Cycle.new("one", 2, "3") 
  • actionpack/lib/action_view/helpers/text_helper.rb

    old new  
    174174      # Sanitizes the given HTML by making form and script tags into regular 
    175175      # text, and removing all "onxxx" attributes (so that arbitrary Javascript 
    176176      # cannot be executed). Also removes href attributes that start with 
    177       # "javascript:". 
     177      # "javascript:" and style attributes that contain "javascript:" 
     178                # (since IE6 has been known to execute javascript: within inline styles). 
    178179      # 
    179180      # Returns the sanitized text. 
    180181      def sanitize(html) 
     
    192193                else 
    193194                  if node.closing != :close 
    194195                    node.attributes.delete_if { |attr,v| attr =~ VERBOTEN_ATTRS } 
    195                     if node.attributes["href"] =~ /^javascript:/i 
     196                                                  # Do not allow any href attribute w/ "javascript:" 
     197                                                  # Even if there are newlines somewhere in the "javascript:" string, IE6 will execute 
     198                    if node.attributes["href"] =~ /^[\s]*j[\r\n]*a[\r\n]*v[\r\n]*a[\r\n]*s[\r\n]*c[\r\n]*r[\r\n]*i[\r\n]*p[\r\n]*t[\r\n]*:/i  
    196199                      node.attributes.delete "href" 
    197200                    end 
     201                                                  # Remove any style attributes containing "javascript:" 
     202                                             if node.attributes["style"] =~ /j[\r\n]*a[\r\n]*v[\r\n]*a[\r\n]*s[\r\n]*c[\r\n]*r[\r\n]*i[\r\n]*p[\r\n]*t[\r\n]*:/i  
     203                                               node.attributes.delete "style" 
     204                                             end 
    198205                  end 
    199206                  node.to_s 
    200207                end