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
|
2017-08-31 15:27:34 +00:00
|
|
|
TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/
|
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
|
|
|
|
2018-06-15 12:05:26 +00:00
|
|
|
# Allow table alignment; we whitelist specific text-align values in a
|
2017-08-31 15:27:34 +00:00
|
|
|
# transformer below
|
2015-05-29 23:01:12 +00:00
|
|
|
whitelist[:attributes]['th'] = %w(style)
|
|
|
|
whitelist[:attributes]['td'] = %w(style)
|
2018-06-15 12:05:26 +00:00
|
|
|
whitelist[:css] = { properties: ['text-align'] }
|
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)
|
|
|
|
|
2017-09-22 15:51:09 +00:00
|
|
|
# Disallow `name` attribute globally, allow on `a`
|
2017-08-30 17:34:34 +00:00
|
|
|
whitelist[:attributes][:all].delete('name')
|
2017-09-22 15:51:09 +00:00
|
|
|
whitelist[:attributes]['a'].push('name')
|
2017-08-30 17:34:34 +00:00
|
|
|
|
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
|
|
|
|
2017-08-31 15:27:34 +00:00
|
|
|
# Remove any `style` properties not required for table alignment
|
|
|
|
whitelist[:transformers].push(self.class.remove_unsafe_table_style)
|
|
|
|
|
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
|
2017-09-28 14:03:20 +00:00
|
|
|
node['href'] = node['href'].strip
|
2016-09-21 13:55:04 +00:00
|
|
|
uri = Addressable::URI.parse(node['href'])
|
2016-03-14 20:07:51 +00:00
|
|
|
|
2017-10-17 10:12:24 +00:00
|
|
|
return unless uri.scheme
|
|
|
|
|
|
|
|
# Remove all invalid scheme characters before checking against the
|
|
|
|
# list of unsafe protocols.
|
|
|
|
#
|
|
|
|
# See https://tools.ietf.org/html/rfc3986#section-3.1
|
|
|
|
scheme = uri.scheme
|
|
|
|
.strip
|
|
|
|
.downcase
|
|
|
|
.gsub(/[^A-Za-z0-9\+\.\-]+/, '')
|
|
|
|
|
|
|
|
node.remove_attribute('href') if UNSAFE_PROTOCOLS.include?(scheme)
|
2016-09-21 13:55:04 +00:00
|
|
|
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
|
2017-08-31 15:27:34 +00:00
|
|
|
|
|
|
|
def remove_unsafe_table_style
|
|
|
|
lambda do |env|
|
|
|
|
node = env[:node]
|
|
|
|
|
|
|
|
return unless node.name == 'th' || node.name == 'td'
|
|
|
|
return unless node.has_attribute?('style')
|
|
|
|
|
|
|
|
if node['style'] =~ TABLE_ALIGNMENT_PATTERN
|
|
|
|
node['style'] = "text-align: #{$~[:alignment]}"
|
|
|
|
else
|
|
|
|
node.remove_attribute('style')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-08 16:17:54 +00:00
|
|
|
end
|
2015-04-27 22:56:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|