2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2016-04-21 18:03:37 +00:00
|
|
|
# HTML Filter to modify the attributes of external links
|
2015-05-27 19:39:08 +00:00
|
|
|
class ExternalLinkFilter < HTML::Pipeline::Filter
|
2019-01-14 22:57:54 +00:00
|
|
|
SCHEMES = ['http', 'https', nil].freeze
|
2019-09-10 08:11:43 +00:00
|
|
|
RTLO = "\u202E"
|
|
|
|
ENCODED_RTLO = '%E2%80%AE'
|
2017-05-02 22:25:58 +00:00
|
|
|
|
2015-05-27 19:39:08 +00:00
|
|
|
def call
|
2016-10-12 05:12:31 +00:00
|
|
|
links.each do |node|
|
2019-01-14 22:57:54 +00:00
|
|
|
# URI.parse does stricter checking on the url than Addressable,
|
|
|
|
# such as on `mailto:` links. Since we've been using it, do an
|
|
|
|
# initial parse for validity and then use Addressable
|
|
|
|
# for IDN support, etc
|
2019-02-20 23:51:55 +00:00
|
|
|
uri = uri_strict(node_src(node))
|
2019-01-14 22:57:54 +00:00
|
|
|
if uri
|
2019-02-20 23:51:55 +00:00
|
|
|
node.set_attribute(node_src_attribute(node), uri.to_s)
|
|
|
|
addressable_uri = addressable_uri(node_src(node))
|
2019-01-14 22:57:54 +00:00
|
|
|
else
|
|
|
|
addressable_uri = nil
|
|
|
|
end
|
2016-10-12 05:12:31 +00:00
|
|
|
|
2019-01-14 22:57:54 +00:00
|
|
|
unless internal_url?(addressable_uri)
|
|
|
|
punycode_autolink_node!(addressable_uri, node)
|
|
|
|
sanitize_link_text!(node)
|
|
|
|
add_malicious_tooltip!(addressable_uri, node)
|
|
|
|
add_nofollow!(addressable_uri, node)
|
2016-10-12 05:12:31 +00:00
|
|
|
end
|
2015-05-27 19:39:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-20 23:51:55 +00:00
|
|
|
# if this is a link to a proxied image, then `src` is already the correct
|
|
|
|
# proxied url, so work with the `data-canonical-src`
|
|
|
|
def node_src_attribute(node)
|
|
|
|
node['data-canonical-src'] ? 'data-canonical-src' : 'href'
|
|
|
|
end
|
|
|
|
|
|
|
|
def node_src(node)
|
|
|
|
node[node_src_attribute(node)]
|
|
|
|
end
|
|
|
|
|
2019-01-14 22:57:54 +00:00
|
|
|
def uri_strict(href)
|
2017-05-02 22:25:58 +00:00
|
|
|
URI.parse(href)
|
2017-05-18 15:47:18 +00:00
|
|
|
rescue URI::Error
|
2017-05-02 22:25:58 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-01-14 22:57:54 +00:00
|
|
|
def addressable_uri(href)
|
|
|
|
Addressable::URI.parse(href)
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2016-10-12 05:12:31 +00:00
|
|
|
def links
|
|
|
|
query = 'descendant-or-self::a[@href and not(@href = "")]'
|
|
|
|
doc.xpath(query)
|
|
|
|
end
|
|
|
|
|
2018-12-11 16:20:06 +00:00
|
|
|
def internal_url?(uri)
|
|
|
|
return false if uri.nil?
|
2022-02-02 18:15:35 +00:00
|
|
|
# Relative URLs miss a hostname AND a scheme
|
|
|
|
return true if !uri.hostname && !uri.scheme
|
2016-10-12 05:12:31 +00:00
|
|
|
|
2018-12-11 16:20:06 +00:00
|
|
|
uri.hostname == internal_url.hostname
|
2016-10-12 05:12:31 +00:00
|
|
|
end
|
|
|
|
|
2015-05-27 19:39:08 +00:00
|
|
|
def internal_url
|
2017-05-02 22:25:58 +00:00
|
|
|
@internal_url ||= URI.parse(Gitlab.config.gitlab.url)
|
2015-05-27 19:39:08 +00:00
|
|
|
end
|
2019-01-14 22:57:54 +00:00
|
|
|
|
|
|
|
# Only replace an autolink with an IDN with it's punycode
|
|
|
|
# version if we need emailable links. Otherwise let it
|
|
|
|
# be shown normally and the tooltips will show the
|
|
|
|
# punycode version.
|
|
|
|
def punycode_autolink_node!(uri, node)
|
|
|
|
return unless uri
|
|
|
|
return unless context[:emailable_links]
|
|
|
|
|
2019-02-20 23:51:55 +00:00
|
|
|
unencoded_uri_str = Addressable::URI.unencode(node_src(node))
|
2019-01-14 22:57:54 +00:00
|
|
|
|
|
|
|
if unencoded_uri_str == node.content && idn?(uri)
|
|
|
|
node.content = uri.normalize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# escape any right-to-left (RTLO) characters in link text
|
|
|
|
def sanitize_link_text!(node)
|
|
|
|
node.inner_html = node.inner_html.gsub(RTLO, ENCODED_RTLO)
|
|
|
|
end
|
|
|
|
|
|
|
|
# If the domain is an international domain name (IDN),
|
|
|
|
# let's expose with a tooltip in case it's intended
|
|
|
|
# to be malicious. This is particularly useful for links
|
|
|
|
# where the link text is not the same as the actual link.
|
|
|
|
# We will continue to show the unicode version of the domain
|
|
|
|
# in autolinked link text, which could contain emojis, etc.
|
|
|
|
#
|
|
|
|
# Also show the tooltip if the url contains the RTLO character,
|
|
|
|
# as this is an indicator of a malicious link
|
|
|
|
def add_malicious_tooltip!(uri, node)
|
|
|
|
if idn?(uri) || has_encoded_rtlo?(uri)
|
|
|
|
node.add_class('has-tooltip')
|
|
|
|
node.set_attribute('title', uri.normalize)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_nofollow!(uri, node)
|
|
|
|
if SCHEMES.include?(uri&.scheme)
|
2021-12-16 09:15:29 +00:00
|
|
|
license = true if node.attribute('rel')&.value == 'license'
|
2019-01-14 22:57:54 +00:00
|
|
|
node.set_attribute('rel', 'nofollow noreferrer noopener')
|
2021-12-16 09:15:29 +00:00
|
|
|
node.kwattr_append('rel', 'license') if license
|
2019-01-14 22:57:54 +00:00
|
|
|
node.set_attribute('target', '_blank')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def idn?(uri)
|
|
|
|
uri&.normalized_host&.start_with?('xn--')
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_encoded_rtlo?(uri)
|
|
|
|
uri&.to_s&.include?(ENCODED_RTLO)
|
|
|
|
end
|
2015-05-27 19:39:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|