Skip to content

Commit

Permalink
fix protocol checking in sanitization [CVE-2013-1857]
Browse files Browse the repository at this point in the history
Conflicts:
	actionpack/lib/action_controller/vendor/html-scanner/html/sanitizer.rb
  • Loading branch information
tenderlove committed Mar 16, 2013
1 parent a7d252b commit 735bb98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WhiteListSanitizer < Sanitizer

# A regular expression of the valid characters used to separate protocols like
# the ':' in 'http://foo.com'
self.protocol_separator = /:|(&#0*58)|(&#x70)|(%|&#37;)3A/
self.protocol_separator = /:|(&#0*58)|(&#x70)|(&#x0*3a)|(%|&#37;)3A/i

# Specifies a Set of HTML attributes that can have URIs.
self.uri_attributes = Set.new(%w(href src cite action longdesc xlink:href lowsrc))
Expand Down Expand Up @@ -171,7 +171,7 @@ def process_attributes_for(node, options)

def contains_bad_protocols?(attr_name, value)
uri_attributes.include?(attr_name) &&
(value =~ /(^[^\/:]*):|(&#0*58)|(&#x70)|(%|&#37;)3A/ && !allowed_protocols.include?(value.split(protocol_separator).first.downcase))
(value =~ /(^[^\/:]*):|(&#0*58)|(&#x70)|(&#x0*3a)|(%|&#37;)3A/i && !allowed_protocols.include?(value.split(protocol_separator).first.downcase.strip))
end
end
end
10 changes: 10 additions & 0 deletions actionpack/test/template/html-scanner/sanitizer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def test_should_block_script_tag
%(<IMG SRC="jav&#x0A;ascript:alert('XSS');">),
%(<IMG SRC="jav&#x0D;ascript:alert('XSS');">),
%(<IMG SRC=" &#14; javascript:alert('XSS');">),
%(<IMG SRC="javascript&#x3a;alert('XSS');">),
%(<IMG SRC=`javascript:alert("RSnake says, 'XSS'")`>)].each_with_index do |img_hack, i|
define_method "test_should_not_fall_for_xss_image_hack_#{i+1}" do
assert_sanitized img_hack, "<img>"
Expand Down Expand Up @@ -281,6 +282,15 @@ def test_should_sanitize_neverending_attribute
assert_sanitized "<span class=\"\\", "<span class=\"\\\">"
end

def test_x03a
assert_sanitized %(<a href="javascript&#x3a;alert('XSS');">), "<a>"
assert_sanitized %(<a href="javascript&#x003a;alert('XSS');">), "<a>"
assert_sanitized %(<a href="http&#x3a;//legit">), %(<a href="http://legit">)
assert_sanitized %(<a href="javascript&#x3A;alert('XSS');">), "<a>"
assert_sanitized %(<a href="javascript&#x003A;alert('XSS');">), "<a>"
assert_sanitized %(<a href="http&#x3A;//legit">), %(<a href="http://legit">)
end

protected
def assert_sanitized(input, expected = nil)
@sanitizer ||= HTML::WhiteListSanitizer.new
Expand Down

0 comments on commit 735bb98

Please sign in to comment.