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

Changeset 8255

Show
Ignore:
Timestamp:
12/02/07 08:45:35 (9 months ago)
Author:
nzkoz
Message:

Don't escape forward slashes with String#to_json, our unicode encoding of < and > prevent the XSS problems. [tpope] Closes #10273

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/test/template/javascript_helper_test.rb

    r8050 r8255  
    3939      page.replace_html 'header', "<h1>Greetings</h1>" 
    4040    end 
    41     assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);; return false;">Greet me!</a>), html 
     41    assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E&quot;);; return false;">Greet me!</a>), html 
    4242  end 
    4343 
     
    4646      page.replace_html 'header', "<h1>Greetings</h1>" 
    4747    end 
    48     assert_dom_equal %(<a href="#" class="updater" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);; return false;">Greet me!</a>), html 
     48    assert_dom_equal %(<a href="#" class="updater" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E&quot;);; return false;">Greet me!</a>), html 
    4949  end 
    5050 
     
    6868      page.replace_html 'header', "<h1>Greetings</h1>" 
    6969    end 
    70     assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);;" value="Greet me!" />), html 
     70    assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E&quot;);;" value="Greet me!" />), html 
    7171  end 
    7272 
     
    7575      page.replace_html 'header', "<h1>Greetings</h1>" 
    7676    end 
    77     assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);;" value="Greet me!" />), html 
     77    assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\/h1\\u003E&quot;);;" value="Greet me!" />), html 
    7878  end 
    7979 
  • trunk/actionpack/test/template/prototype_helper_test.rb

    r8050 r8255  
    304304   
    305305  def test_insert_html_with_string 
    306     assert_equal 'new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E");', 
     306    assert_equal 'new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");', 
    307307      @generator.insert_html(:top, 'element', '<p>This is a test</p>') 
    308     assert_equal 'new Insertion.Bottom("element", "\\u003Cp\u003EThis is a test\\u003C\\/p\u003E");', 
     308    assert_equal 'new Insertion.Bottom("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");', 
    309309      @generator.insert_html(:bottom, 'element', '<p>This is a test</p>') 
    310     assert_equal 'new Insertion.Before("element", "\\u003Cp\u003EThis is a test\\u003C\\/p\u003E");', 
     310    assert_equal 'new Insertion.Before("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");', 
    311311      @generator.insert_html(:before, 'element', '<p>This is a test</p>') 
    312     assert_equal 'new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C\\/p\u003E");', 
     312    assert_equal 'new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");', 
    313313      @generator.insert_html(:after, 'element', '<p>This is a test</p>') 
    314314  end 
    315315   
    316316  def test_replace_html_with_string 
    317     assert_equal 'Element.update("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E");', 
     317    assert_equal 'Element.update("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");', 
    318318      @generator.replace_html('element', '<p>This is a test</p>') 
    319319  end 
    320320   
    321321  def test_replace_element_with_string 
    322     assert_equal 'Element.replace("element", "\\u003Cdiv id=\"element\"\\u003E\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E\\u003C\\/div\\u003E");', 
     322    assert_equal 'Element.replace("element", "\\u003Cdiv id=\"element\"\\u003E\\u003Cp\\u003EThis is a test\\u003C/p\\u003E\\u003C/div\\u003E");', 
    323323      @generator.replace('element', '<div id="element"><p>This is a test</p></div>') 
    324324  end 
     
    357357   
    358358  def test_redirect_to 
    359     assert_equal 'window.location.href = "http:\\/\\/www.example.com\\/welcome";', 
     359    assert_equal 'window.location.href = "http://www.example.com/welcome";', 
    360360      @generator.redirect_to(:action => 'welcome') 
    361361  end 
     
    376376     
    377377    assert_equal <<-EOS.chomp, @generator.to_s 
    378 new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E"); 
    379 new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E"); 
     378new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); 
     379new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); 
    380380["foo", "bar"].each(Element.remove); 
    381 Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C\\/p\\u003E"); 
     381Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); 
    382382    EOS 
    383383  end 
  • trunk/activesupport/lib/active_support/json/encoders/string.rb

    r8050 r8255  
    1212        '>'    =>  '\u003E', 
    1313        '<'    =>  '\u003C', 
    14         '&'    =>  '\u0026', 
    15         '/'    =>  '\\/' 
     14        '&'    =>  '\u0026' 
    1615      } 
    1716    end 
     
    2120class String 
    2221  def to_json(options = nil) #:nodoc: 
    23     '"' + gsub(/[\010\f\n\r\t"\\><&\/]/) { |s| 
     22    '"' + gsub(/[\010\f\n\r\t"\\><&]/) { |s| 
    2423      ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s] 
    2524    }.gsub(/([\xC0-\xDF][\x80-\xBF]| 
  • trunk/activesupport/test/json/encoding_test.rb

    r8050 r8255  
    1616  StringTests   = [[ 'this is the <string>',     %("this is the \\u003Cstring\\u003E")], 
    1717                   [ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ], 
    18                    [ 'http://test.host/posts/1', %("http:\\/\\/test.host\\/posts\\/1")]] 
     18                   [ 'http://test.host/posts/1', %("http://test.host/posts/1")]] 
    1919 
    2020  ArrayTests    = [[ ['a', 'b', 'c'],          %([\"a\", \"b\", \"c\"])          ],