From 2d02199e1581db8dc84361803950b1697f493fc0 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sun, 23 Sep 2007 00:11:08 +0000 Subject: Secure #sanitize, #strip_tags, and #strip_links helpers against xss attacks. Closes #8877. [Rick, lifofifo, Jacques Distler] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7589 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/helpers/text_helper.rb | 136 ++++++++++++++++------ 1 file changed, 98 insertions(+), 38 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index e7a6303154..af6f6e4bb8 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -324,63 +324,118 @@ module ActionView # # strip_links('Blog: Visit.') # # => Blog: Visit - def strip_links(text) - text.gsub(/(.*?)<\/a>/mi, '\1') + def strip_links(html) + # Stupid firefox treats 'something' as link! + if html.index(" and ') - # # => <script> do_nasty_stuff() </script> + # <%= sanitize @article.body %> + # + # You can add or remove tags/attributes if you want to customize it a bit. See ActionView::Base for full docs on the + # available options. You can add tags/attributes for single uses of #sanitize by passing either the :attributes or :tags options: # - # sanitize('Click here for $100') - # # => Click here for $100 + # Normal Use # - # sanitize('Click here!!!') - # # => Click here!!! + # <%= sanitize @article.body %> # - # sanitize('') - # # => - def sanitize(html) - # only do this if absolutely necessary - if html.index("<") + # Custom Use + # + # <%= sanitize @article.body, :tags => %w(table tr td), :attributes => %w(id class style) + # + # Add table tags + # + # Rails::Initializer.run do |config| + # config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td' + # end + # + # Remove tags + # + # Rails::Initializer.run do |config| + # config.after_initialize do + # ActionView::Base.sanitized_allowed_tags.delete 'div' + # end + # end + # + # Change allowed attributes + # + # Rails::Initializer.run do |config| + # config.action_view.sanitized_allowed_attributes = 'id', 'class', 'style' + # end + # + def sanitize(html, options = {}) + return html if html.blank? || !html.include?('<') + attrs = options.key?(:attributes) ? Set.new(options[:attributes]).merge(sanitized_allowed_attributes) : sanitized_allowed_attributes + tags = options.key?(:tags) ? Set.new(options[:tags] ).merge(sanitized_allowed_tags) : sanitized_allowed_tags + returning [] do |new_text| tokenizer = HTML::Tokenizer.new(html) - new_text = "" - + parent = [] while token = tokenizer.next node = HTML::Node.parse(nil, 0, 0, token, false) new_text << case node when HTML::Tag - if VERBOTEN_TAGS.include?(node.name) - node.to_s.gsub(/[\n]?/m, "") + strip_tags(text.gsub(/[\n]?/m, "")) # Recurse - handle all dirty nested tags else html # already plain text end @@ -574,6 +629,11 @@ module ActionView end end end + + def contains_bad_protocols?(attr_name, value) + sanitized_uri_attributes.include?(attr_name) && + (value =~ /(^[^\/:]*):|(�*58)|(p)|(%|%)3A/ && !sanitized_allowed_protocols.include?(value.split(sanitized_protocol_separator).first)) + end end end end -- cgit v1.2.3