debian-mirror-gitlab/lib/banzai/filter/sanitization_filter.rb

119 lines
3.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-12-23 02:04:40 +05:30
module Banzai
module Filter
2015-09-11 14:41:01 +05:30
# Sanitize HTML
#
# Extends HTML::Pipeline::SanitizationFilter with a custom whitelist.
class SanitizationFilter < HTML::Pipeline::SanitizationFilter
2018-11-08 19:23:39 +05:30
include Gitlab::Utils::StrongMemoize
2016-06-02 11:05:42 +05:30
UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze
2017-09-10 17:25:29 +05:30
TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/
2016-06-02 11:05:42 +05:30
2015-09-11 14:41:01 +05:30
def whitelist
2018-11-08 19:23:39 +05:30
strong_memoize(:whitelist) do
customize_whitelist(super.deep_dup)
end
2015-09-11 14:41:01 +05:30
end
private
def customize_whitelist(whitelist)
2018-06-27 16:04:02 +05:30
# Allow table alignment; we whitelist specific text-align values in a
2017-09-10 17:25:29 +05:30
# transformer below
2015-09-11 14:41:01 +05:30
whitelist[:attributes]['th'] = %w(style)
whitelist[:attributes]['td'] = %w(style)
2018-06-27 16:04:02 +05:30
whitelist[:css] = { properties: ['text-align'] }
2015-09-11 14:41:01 +05:30
# Allow span elements
whitelist[:elements].push('span')
2017-09-10 17:25:29 +05:30
# 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)
2017-08-17 22:00:37 +05:30
# Allow html5 details/summary elements
whitelist[:elements].push('details')
whitelist[:elements].push('summary')
2016-04-02 18:10:28 +05:30
# Allow abbr elements with title attribute
whitelist[:elements].push('abbr')
whitelist[:attributes]['abbr'] = %w(title)
2018-03-17 18:26:18 +05:30
# Disallow `name` attribute globally, allow on `a`
2017-09-10 17:25:29 +05:30
whitelist[:attributes][:all].delete('name')
2018-03-17 18:26:18 +05:30
whitelist[:attributes]['a'].push('name')
2017-09-10 17:25:29 +05:30
2015-11-26 14:37:03 +05:30
# Allow any protocol in `a` elements...
whitelist[:protocols].delete('a')
2016-06-02 11:05:42 +05:30
# ...but then remove links with unsafe protocols
2016-09-29 09:46:39 +05:30
whitelist[:transformers].push(self.class.remove_unsafe_links)
2015-11-26 14:37:03 +05:30
2015-09-11 14:41:01 +05:30
# Remove `rel` attribute from `a` elements
2016-09-29 09:46:39 +05:30
whitelist[:transformers].push(self.class.remove_rel)
2015-09-11 14:41:01 +05:30
2017-09-10 17:25:29 +05:30
# Remove any `style` properties not required for table alignment
whitelist[:transformers].push(self.class.remove_unsafe_table_style)
2015-09-11 14:41:01 +05:30
whitelist
end
2016-09-29 09:46:39 +05:30
class << self
def remove_unsafe_links
lambda do |env|
node = env[:node]
2015-11-26 14:37:03 +05:30
2016-09-29 09:46:39 +05:30
return unless node.name == 'a'
return unless node.has_attribute?('href')
2015-11-26 14:37:03 +05:30
2016-09-29 09:46:39 +05:30
begin
2018-03-17 18:26:18 +05:30
node['href'] = node['href'].strip
2016-09-29 09:46:39 +05:30
uri = Addressable::URI.parse(node['href'])
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
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-29 09:46:39 +05:30
rescue Addressable::URI::InvalidURIError
node.remove_attribute('href')
end
2015-11-26 14:37:03 +05:30
end
end
2016-09-29 09:46:39 +05:30
def remove_rel
lambda do |env|
if env[:node_name] == 'a'
env[:node].remove_attribute('rel')
end
2015-09-11 14:41:01 +05:30
end
end
2017-09-10 17:25:29 +05:30
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-09-11 14:41:01 +05:30
end
end
end
end