| ... | @@ -11,6 +11,7 @@ module Banzai |
... | @@ -11,6 +11,7 @@ module Banzai |
|
|
# Extends HTML::Pipeline::SanitizationFilter with common rules.
|
|
# Extends HTML::Pipeline::SanitizationFilter with common rules.
|
|
|
class BaseSanitizationFilter < HTML::Pipeline::SanitizationFilter
|
|
class BaseSanitizationFilter < HTML::Pipeline::SanitizationFilter
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
extend Gitlab::Utils::SanitizeNodeLink
|
|
|
|
|
|
|
|
UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze
|
|
UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze
|
|
|
|
|
|
| ... | @@ -40,7 +41,7 @@ module Banzai |
... | @@ -40,7 +41,7 @@ module Banzai |
|
|
# Allow any protocol in `a` elements
|
|
# Allow any protocol in `a` elements
|
|
|
# and then remove links with unsafe protocols
|
|
# and then remove links with unsafe protocols
|
|
|
whitelist[:protocols].delete('a')
|
|
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
|
|
# Remove `rel` attribute from `a` elements
|
|
|
whitelist[:transformers].push(self.class.remove_rel)
|
|
whitelist[:transformers].push(self.class.remove_rel)
|
| ... | @@ -54,35 +55,6 @@ module Banzai |
... | @@ -54,35 +55,6 @@ module Banzai |
|
|
end
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
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
|
|
def remove_rel
|
|
|
lambda do |env|
|
|
lambda do |env|
|
|
|
if env[:node_name] == 'a'
|
|
if env[:node_name] == 'a'
|
| ... | |
... | |
| ... | | ... | |