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 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
|
|
|
|
2017-05-09 12:14:24 +00:00
|
|
|
# Allow data-math-style attribute in order to support LaTeX formatting
|
|
|
|
whitelist[:attributes]['code'] = %w(data-math-style)
|
|
|
|
whitelist[:attributes]['pre'] = %w(data-math-style)
|
|
|
|
|
2016-09-28 11:46:11 +00:00
|
|
|
# Allow html5 details/summary elements
|
|
|
|
whitelist[:elements].push('details')
|
|
|
|
whitelist[:elements].push('summary')
|
|
|
|
|
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
|
2016-09-21 13:55:04 +00:00
|
|
|
whitelist[:transformers].push(self.class.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
|
2016-09-21 13:55:04 +00:00
|
|
|
whitelist[:transformers].push(self.class.remove_rel)
|
2015-05-29 23:01:12 +00:00
|
|
|
|
2015-04-27 22:56:37 +00:00
|
|
|
whitelist
|
|
|
|
end
|
|
|
|
|
2016-09-21 13:55:04 +00:00
|
|
|
class << self
|
|
|
|
def remove_unsafe_links
|
|
|
|
lambda do |env|
|
|
|
|
node = env[:node]
|
2015-10-02 03:40:29 +00:00
|
|
|
|
2016-09-21 13:55:04 +00:00
|
|
|
return unless node.name == 'a'
|
|
|
|
return unless node.has_attribute?('href')
|
2015-10-02 03:40:29 +00:00
|
|
|
|
2016-09-21 13:55:04 +00:00
|
|
|
begin
|
|
|
|
uri = Addressable::URI.parse(node['href'])
|
|
|
|
uri.scheme = uri.scheme.strip.downcase if uri.scheme
|
2016-03-14 20:07:51 +00:00
|
|
|
|
2016-09-21 13:55:04 +00:00
|
|
|
node.remove_attribute('href') if UNSAFE_PROTOCOLS.include?(uri.scheme)
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
node.remove_attribute('href')
|
|
|
|
end
|
2015-10-02 03:40:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-21 13:55:04 +00:00
|
|
|
def remove_rel
|
|
|
|
lambda do |env|
|
|
|
|
if env[:node_name] == 'a'
|
|
|
|
env[:node].remove_attribute('rel')
|
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
end
|
|
|
|
end
|
2015-05-08 16:17:54 +00:00
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|