Skip to content

Commit

Permalink
properly escape html to avoid invalid utf8 causing XSS attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Aug 16, 2011
1 parent 66c3e31 commit a19ee5c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Expand Up @@ -20,7 +20,7 @@ def html_escape(s)
if s.html_safe?
s
else
s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }.html_safe
s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;").html_safe
end
end

Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -8,10 +8,17 @@
require 'active_support/time'
require 'active_support/core_ext/kernel/reporting'
require 'active_support/core_ext/string/strip'
require 'active_support/core_ext/string/output_safety'

class StringInflectionsTest < Test::Unit::TestCase
include InflectorTestCases

def test_erb_escape
string = [192, 60].pack('CC')
expected = 192.chr + "&lt;"
assert_equal expected, ERB::Util.html_escape(string)
end

def test_strip_heredoc_on_an_empty_string
assert_equal '', ''.strip_heredoc
end
Expand Down

0 comments on commit a19ee5c

Please sign in to comment.