| ... | ... | @@ -7,8 +7,8 @@ module Banzai |
|
|
|
# Extends HTML::Pipeline::SanitizationFilter with a custom whitelist.
|
|
|
|
class SanitizationFilter < HTML::Pipeline::SanitizationFilter
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
extend Gitlab::Utils::SanitizeNodeLink
|
|
|
|
|
|
|
|
UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze
|
|
|
|
TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/.freeze
|
|
|
|
|
|
|
|
def whitelist
|
| ... | ... | @@ -51,7 +51,7 @@ module Banzai |
|
|
|
# Allow any protocol in `a` elements
|
|
|
|
# and then remove links with unsafe protocols
|
|
|
|
whitelist[:protocols].delete('a')
|
|
|
|
whitelist[:transformers].push(self.class.remove_unsafe_links)
|
|
|
|
whitelist[:transformers].push(self.class.method(:remove_unsafe_links))
|
|
|
|
|
|
|
|
# Remove `rel` attribute from `a` elements
|
|
|
|
whitelist[:transformers].push(self.class.remove_rel)
|
| ... | ... | @@ -69,35 +69,6 @@ module Banzai |
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def remove_unsafe_links
|
|
|
|
lambda do |env|
|
|
|
|
node = env[:node]
|
|
|
|
|
|
|
|
return unless node.name == 'a'
|
|
|
|
return unless node.has_attribute?('href')
|
|
|
|
|
|
|
|
begin
|
|
|
|
node['href'] = node['href'].strip
|
|
|
|
uri = Addressable::URI.parse(node['href'])
|
|
|
|
|
|
|
|
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)
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
node.remove_attribute('href')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_rel
|
|
|
|
lambda do |env|
|
|
|
|
if env[:node_name] == 'a'
|
| ... | ... | |
| ... | ... | |