0
- PROTOCOL_ATTRIBUTES =
%w(src href)
0
+ PROTOCOL_ATTRIBUTES =
Set.new %w(src href)
0
PROTOCOL_SEPARATOR = /:|(�*58)|(p)|(%|%)3A/
0
- mattr_reader :tags, :attributes, :protocols
0
- @@tags = %w(strong em b i p code pre tt output samp kbd var sub sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dt dd abbr acronym)
0
- 'img' => %w(src width height alt),
0
- 'blockquote' => %w(cite),
0
- 'del' => %w(cite datetime),
0
- 'ins' => %w(cite datetime),
0
- nil => %w(title class) }
0
- @@protocols = %w(ed2k ftp http https irc mailto news gopher nntp telnet webcal xmpp callto feed)
0
- tags.push(*attributes.keys).uniq!
0
- ::WhiteListHelper.tags
0
- def white_listed_attributes
0
- ::WhiteListHelper.attributes
0
+ [:bad_tags, :tags, :attributes, :protocols].each do |attr|
0
+ klass = class << self; self; end
0
+ klass.send(:define_method, "#{attr}=") { |value| class_variable_set("@@#{attr}", Set.new(value)) }
0
+ define_method("white_listed_#{attr}") { ::WhiteListHelper.send(attr) }
0
- def white_listed_protocols
0
- ::WhiteListHelper.protocols
0
- def white_list(html, options = {})
0
+ # This White Listing helper will html encode all tags and strip all attributes that aren't specifically allowed.
0
+ # It also strips href/src tags with invalid protocols, like javascript: especially. It does its best to counter any
0
+ # tricks that hackers may use, like throwing in unicode/ascii/hex values to get past the javascript: filters. Check out
0
+ # the extensive test suite.
0
+ # <%= white_list @article.body %>
0
+ # You can add or remove tags/attributes if you want to customize it a bit.
0
+ # WhiteListHelper.tags.merge %w(table td th)
0
+ # WhiteListHelper.tags.delete 'div'
0
+ # Change allowed attributes
0
+ # WhiteListHelper.attributes.merge %w(id class style)
0
+ # white_list accepts a block for custom tag escaping. Shown below is the default block that white_list uses if none is given.
0
+ # The block is called for all bad tags, and every text node. node is an instance of HTML::Node (either HTML::Tag or HTML::Text).
0
+ # bad is nil for text nodes inside good tags, or is the tag name of the bad tag.
0
+ # <%= white_list(@article.body) { |node, bad| white_listed_bad_tags.include?(bad) ? nil : node.to_s.gsub(/</, '<') } %>
0
+ def white_list(html, options = {}, &block)
0
return html if html.blank? || !html.include?('<')
0
- (options[:attributes] ||= {}).update(white_listed_attributes)
0
- (options[:tags] ||= []).push(*options[:attributes].keys).push(*white_listed_tags).uniq!
0
+ attrs = Set.new(options[:attributes]).merge(white_listed_attributes)
0
+ tags = Set.new(options[:tags] ).merge(white_listed_tags)
0
+ block ||= lambda { |node, bad| white_listed_bad_tags.include?(bad) ? nil : node.to_s.gsub(/</, '<') }
0
returning [] do |new_text|
0
tokenizer = HTML::Tokenizer.new(html)
0
while token = tokenizer.next
0
node = HTML::Node.parse(nil, 0, 0, token, false)
0
- unless (options[:tags]).include?(node.name)
0
- node.to_s.gsub(/</, "<")
0
+ unless tags.include?(node.name)
0
if node.closing != :close
0
- attributes = (options[:attributes][nil] || []).push(*(options[:attributes][node.name] || []))
0
node.attributes.delete_if do |attr_name, value|
0
- !attr
ibutes.include?(attr_name) || (PROTOCOL_ATTRIBUTES.include?(attr_name) && contains_bad_protocols?(value))
0
+ !attr
s.include?(attr_name) || (PROTOCOL_ATTRIBUTES.include?(attr_name) && contains_bad_protocols?(value))
0
-
node.to_s.gsub(/</, "<")0
def contains_bad_protocols?(value)
0
value =~ PROTOCOL_SEPARATOR && !white_listed_protocols.include?(value.split(PROTOCOL_SEPARATOR).first)
0
\ No newline at end of file
0
+WhiteListHelper.bad_tags = %w(script)
0
+WhiteListHelper.tags = %w(strong em b i p code pre tt output samp kbd var sub sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dt dd abbr acronym a img blockquote del ins fieldset legend)
0
+WhiteListHelper.attributes = %w(href src width height alt cite datetime title class)
0
+WhiteListHelper.protocols = %w(ed2k ftp http https irc mailto news gopher nntp telnet webcal xmpp callto feed)
0
\ No newline at end of file
Comments
No one has commented yet.