2015-04-27 22:56:37 +00:00
|
|
|
require 'html/pipeline/filter'
|
|
|
|
require 'html/pipeline/sanitization_filter'
|
|
|
|
|
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
|
|
|
|
def whitelist
|
2015-05-29 23:01:12 +00:00
|
|
|
# Descriptions are more heavily sanitized, allowing only a few elements.
|
|
|
|
# See http://git.io/vkuAN
|
2015-10-14 17:27:23 +00:00
|
|
|
if context[:inline_sanitization]
|
2015-05-29 23:01:12 +00:00
|
|
|
whitelist = LIMITED
|
2015-06-02 17:41:12 +00:00
|
|
|
whitelist[:elements] -= %w(pre code img ol ul li)
|
2015-05-29 23:01:12 +00:00
|
|
|
else
|
|
|
|
whitelist = super
|
|
|
|
end
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
# ...but then remove links with the `javascript` protocol
|
|
|
|
whitelist[:transformers].push(remove_javascript_links)
|
|
|
|
|
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
|
|
|
|
|
2015-10-02 03:40:29 +00:00
|
|
|
def remove_javascript_links
|
|
|
|
lambda do |env|
|
|
|
|
node = env[:node]
|
|
|
|
|
|
|
|
return unless node.name == 'a'
|
|
|
|
return unless node.has_attribute?('href')
|
|
|
|
|
|
|
|
if node['href'].start_with?('javascript', ':javascript')
|
|
|
|
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
|