2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-04-27 22:56:37 +00:00
|
|
|
# Sanitize HTML
|
|
|
|
#
|
|
|
|
# Extends HTML::Pipeline::SanitizationFilter with a custom whitelist.
|
|
|
|
class SanitizationFilter < HTML::Pipeline::SanitizationFilter
|
2016-03-14 20:07:51 +00:00
|
|
|
UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze
|
2016-02-24 01:40:31 +00:00
|
|
|
|
2015-04-27 22:56:37 +00:00
|
|
|
def whitelist
|
2016-09-21 13:46:31 +00:00
|
|
|
whitelist = super
|
2015-05-29 23:01:12 +00:00
|
|
|
|
|
|
|
customize_whitelist(whitelist)
|
|
|
|
|
|
|
|
whitelist
|
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def customized?(transformers)
|
|
|
|
transformers.last.source_location[0] == __FILE__
|
|
|
|
end
|
|
|
|
|
|
|
|
def customize_whitelist(whitelist)
|
2015-05-21 00:55:11 +00:00
|
|
|
# Only push these customizations once
|
2015-05-29 23:01:12 +00:00
|
|
|
return if customized?(whitelist[:transformers])
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Allow code highlighting
|
|
|
|
whitelist[:attributes]['pre'] = %w(class)
|
|
|
|
whitelist[:attributes]['span'] = %w(class)
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Allow table alignment
|
|
|
|
whitelist[:attributes]['th'] = %w(style)
|
|
|
|
whitelist[:attributes]['td'] = %w(style)
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Allow span elements
|
|
|
|
whitelist[:elements].push('span')
|
2015-04-27 22:56:37 +00:00
|
|
|
|
2016-01-22 12:47:14 +00:00
|
|
|
# Allow abbr elements with title attribute
|
|
|
|
whitelist[:elements].push('abbr')
|
|
|
|
whitelist[:attributes]['abbr'] = %w(title)
|
|
|
|
|
2015-10-02 03:40:29 +00:00
|
|
|
# Allow any protocol in `a` elements...
|
|
|
|
whitelist[:protocols].delete('a')
|
|
|
|
|
2016-02-21 22:33:35 +00:00
|
|
|
# ...but then remove links with unsafe protocols
|
|
|
|
whitelist[:transformers].push(remove_unsafe_links)
|
2015-10-02 03:40:29 +00:00
|
|
|
|
2015-05-29 23:01:12 +00:00
|
|
|
# Remove `rel` attribute from `a` elements
|
|
|
|
whitelist[:transformers].push(remove_rel)
|
|
|
|
|
|
|
|
# Remove `class` attribute from non-highlight spans
|
|
|
|
whitelist[:transformers].push(clean_spans)
|
2015-05-08 16:17:54 +00:00
|
|
|
|
2015-04-27 22:56:37 +00:00
|
|
|
whitelist
|
|
|
|
end
|
|
|
|
|
2016-02-21 22:33:35 +00:00
|
|
|
def remove_unsafe_links
|
2015-10-02 03:40:29 +00:00
|
|
|
lambda do |env|
|
|
|
|
node = env[:node]
|
|
|
|
|
|
|
|
return unless node.name == 'a'
|
|
|
|
return unless node.has_attribute?('href')
|
|
|
|
|
2016-03-14 20:07:51 +00:00
|
|
|
begin
|
|
|
|
uri = Addressable::URI.parse(node['href'])
|
2016-05-07 17:56:08 +00:00
|
|
|
uri.scheme = uri.scheme.strip.downcase if uri.scheme
|
2016-03-14 20:07:51 +00:00
|
|
|
|
|
|
|
node.remove_attribute('href') if UNSAFE_PROTOCOLS.include?(uri.scheme)
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
2015-10-02 03:40:29 +00:00
|
|
|
node.remove_attribute('href')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-27 22:56:37 +00:00
|
|
|
def remove_rel
|
|
|
|
lambda do |env|
|
|
|
|
if env[:node_name] == 'a'
|
|
|
|
env[:node].remove_attribute('rel')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-08 16:17:54 +00:00
|
|
|
|
|
|
|
def clean_spans
|
|
|
|
lambda do |env|
|
2015-09-05 21:50:47 +00:00
|
|
|
node = env[:node]
|
2015-05-08 16:17:54 +00:00
|
|
|
|
2015-09-05 21:50:47 +00:00
|
|
|
return unless node.name == 'span'
|
|
|
|
return unless node.has_attribute?('class')
|
|
|
|
|
|
|
|
unless has_ancestor?(node, 'pre')
|
|
|
|
node.remove_attribute('class')
|
2015-05-08 16:17:54 +00:00
|
|
|
end
|
2015-09-05 21:50:47 +00:00
|
|
|
|
2015-09-10 20:06:24 +00:00
|
|
|
{ node_whitelist: [node] }
|
2015-05-08 16:17:54 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|