2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-15 16:09:08 -04:00
|
|
|
require 'resolv'
|
2018-11-26 03:45:38 -05:00
|
|
|
require 'ipaddress'
|
2017-03-15 16:09:08 -04:00
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
class UrlBlocker
|
2018-03-28 13:27:16 -04:00
|
|
|
BlockedUrlError = Class.new(StandardError)
|
2017-03-18 13:56:05 -04:00
|
|
|
|
2018-03-28 13:27:16 -04:00
|
|
|
class << self
|
2019-04-21 06:03:26 -04:00
|
|
|
# Validates the given url according to the constraints specified by arguments.
|
|
|
|
#
|
|
|
|
# ports - Raises error if the given URL port does is not between given ports.
|
2020-03-31 14:07:42 -04:00
|
|
|
# allow_localhost - Raises error if URL resolves to a localhost IP address and argument is false.
|
|
|
|
# allow_local_network - Raises error if URL resolves to a link-local address and argument is false.
|
2019-04-21 06:03:26 -04:00
|
|
|
# ascii_only - Raises error if URL has unicode characters and argument is true.
|
|
|
|
# enforce_user - Raises error if URL user doesn't start with alphanumeric characters and argument is true.
|
|
|
|
# enforce_sanitization - Raises error if URL includes any HTML/CSS/JS tags and argument is true.
|
|
|
|
#
|
|
|
|
# Returns an array with [<uri>, <original-hostname>].
|
2019-05-29 12:43:07 -04:00
|
|
|
# rubocop:disable Metrics/ParameterLists
|
|
|
|
def validate!(
|
|
|
|
url,
|
|
|
|
ports: [],
|
|
|
|
schemes: [],
|
|
|
|
allow_localhost: false,
|
|
|
|
allow_local_network: true,
|
|
|
|
ascii_only: false,
|
|
|
|
enforce_user: false,
|
|
|
|
enforce_sanitization: false,
|
|
|
|
dns_rebind_protection: true)
|
|
|
|
# rubocop:enable Metrics/ParameterLists
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
return [nil, nil] if url.nil?
|
2017-03-15 16:09:08 -04:00
|
|
|
|
2018-11-28 14:06:44 -05:00
|
|
|
# Param url can be a string, URI or Addressable::URI
|
|
|
|
uri = parse_url(url)
|
2017-03-15 16:09:08 -04:00
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
validate_uri(
|
|
|
|
uri: uri,
|
|
|
|
schemes: schemes,
|
|
|
|
ports: ports,
|
|
|
|
enforce_sanitization: enforce_sanitization,
|
|
|
|
enforce_user: enforce_user,
|
|
|
|
ascii_only: ascii_only
|
|
|
|
)
|
2019-01-07 12:55:21 -05:00
|
|
|
|
2019-09-13 09:26:31 -04:00
|
|
|
address_info = get_address_info(uri, dns_rebind_protection)
|
2019-07-12 03:04:44 -04:00
|
|
|
return [uri, nil] unless address_info
|
2017-03-15 16:09:08 -04:00
|
|
|
|
2019-07-24 13:59:38 -04:00
|
|
|
ip_address = ip_address(address_info)
|
2021-02-01 19:09:14 -05:00
|
|
|
return [uri, nil] if domain_allowed?(uri)
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
protected_uri_with_hostname = enforce_uri_hostname(ip_address, uri, dns_rebind_protection)
|
2019-05-29 12:43:07 -04:00
|
|
|
|
2021-02-01 19:09:14 -05:00
|
|
|
return protected_uri_with_hostname if ip_allowed?(ip_address, port: get_port(uri))
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
# Allow url from the GitLab instance itself but only for the configured hostname and ports
|
2019-05-29 12:43:07 -04:00
|
|
|
return protected_uri_with_hostname if internal?(uri)
|
2019-04-21 06:03:26 -04:00
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
validate_local_request(
|
|
|
|
address_info: address_info,
|
|
|
|
allow_localhost: allow_localhost,
|
|
|
|
allow_local_network: allow_local_network
|
|
|
|
)
|
2018-04-02 11:13:23 -04:00
|
|
|
|
2019-05-29 12:43:07 -04:00
|
|
|
protected_uri_with_hostname
|
2018-03-28 13:27:16 -04:00
|
|
|
end
|
|
|
|
|
2020-10-27 08:08:33 -04:00
|
|
|
def blocked_url?(url, **kwargs)
|
|
|
|
validate!(url, **kwargs)
|
2018-03-28 13:27:16 -04:00
|
|
|
|
2017-03-15 16:09:08 -04:00
|
|
|
false
|
2018-03-28 13:27:16 -04:00
|
|
|
rescue BlockedUrlError
|
|
|
|
true
|
2017-03-15 16:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
# Returns the given URI with IP address as hostname and the original hostname respectively
|
|
|
|
# in an Array.
|
|
|
|
#
|
|
|
|
# It checks whether the resolved IP address matches with the hostname. If not, it changes
|
|
|
|
# the hostname to the resolved IP address.
|
|
|
|
#
|
|
|
|
# The original hostname is used to validate the SSL, given in that scenario
|
|
|
|
# we'll be making the request to the IP address, instead of using the hostname.
|
2019-09-13 09:26:31 -04:00
|
|
|
def enforce_uri_hostname(ip_address, uri, dns_rebind_protection)
|
|
|
|
return [uri, nil] unless dns_rebind_protection && ip_address && ip_address != uri.hostname
|
2019-04-21 06:03:26 -04:00
|
|
|
|
2019-09-13 09:26:31 -04:00
|
|
|
new_uri = uri.dup
|
|
|
|
new_uri.hostname = ip_address
|
|
|
|
[new_uri, uri.hostname]
|
2019-04-21 06:03:26 -04:00
|
|
|
end
|
|
|
|
|
2019-07-24 13:59:38 -04:00
|
|
|
def ip_address(address_info)
|
|
|
|
address_info.first&.ip_address
|
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_uri(uri:, schemes:, ports:, enforce_sanitization:, enforce_user:, ascii_only:)
|
|
|
|
validate_html_tags(uri) if enforce_sanitization
|
|
|
|
|
|
|
|
return if internal?(uri)
|
|
|
|
|
|
|
|
validate_scheme(uri.scheme, schemes)
|
|
|
|
validate_port(get_port(uri), ports) if ports.any?
|
|
|
|
validate_user(uri.user) if enforce_user
|
|
|
|
validate_hostname(uri.hostname)
|
|
|
|
validate_unicode_restriction(uri) if ascii_only
|
|
|
|
end
|
|
|
|
|
2019-09-13 09:26:31 -04:00
|
|
|
def get_address_info(uri, dns_rebind_protection)
|
2021-02-17 07:09:26 -05:00
|
|
|
Addrinfo.getaddrinfo(uri.hostname, get_port(uri), nil, :STREAM).map do |addr|
|
2019-07-12 03:04:44 -04:00
|
|
|
addr.ipv6_v4mapped? ? addr.ipv6_to_ipv4 : addr
|
|
|
|
end
|
|
|
|
rescue SocketError
|
2019-09-13 09:26:31 -04:00
|
|
|
# If the dns rebinding protection is not enabled or the domain
|
2020-11-10 07:08:57 -05:00
|
|
|
# is allowed we avoid the dns rebinding checks
|
2020-10-29 02:08:45 -04:00
|
|
|
return if domain_allowed?(uri) || !dns_rebind_protection
|
2019-09-05 02:07:17 -04:00
|
|
|
|
2019-07-02 12:38:23 -04:00
|
|
|
# In the test suite we use a lot of mocked urls that are either invalid or
|
|
|
|
# don't exist. In order to avoid modifying a ton of tests and factories
|
|
|
|
# we allow invalid urls unless the environment variable RSPEC_ALLOW_INVALID_URLS
|
|
|
|
# is not true
|
|
|
|
return if Rails.env.test? && ENV['RSPEC_ALLOW_INVALID_URLS'] == 'true'
|
|
|
|
|
|
|
|
# If the addr can't be resolved or the url is invalid (i.e http://1.1.1.1.1)
|
|
|
|
# we block the url
|
|
|
|
raise BlockedUrlError, "Host cannot be resolved or invalid"
|
2019-10-03 17:07:29 -04:00
|
|
|
rescue ArgumentError => error
|
|
|
|
# Addrinfo.getaddrinfo errors if the domain exceeds 1024 characters.
|
|
|
|
raise unless error.message.include?('hostname too long')
|
|
|
|
|
|
|
|
raise BlockedUrlError, "Host is too long (maximum is 1024 characters)"
|
2019-07-12 03:04:44 -04:00
|
|
|
end
|
|
|
|
|
2019-07-24 13:59:38 -04:00
|
|
|
def validate_local_request(
|
|
|
|
address_info:,
|
|
|
|
allow_localhost:,
|
|
|
|
allow_local_network:)
|
2019-07-12 03:04:44 -04:00
|
|
|
return if allow_local_network && allow_localhost
|
|
|
|
|
|
|
|
unless allow_localhost
|
|
|
|
validate_localhost(address_info)
|
|
|
|
validate_loopback(address_info)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless allow_local_network
|
|
|
|
validate_local_network(address_info)
|
|
|
|
validate_link_local(address_info)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-07 12:55:21 -05:00
|
|
|
def get_port(uri)
|
|
|
|
uri.port || uri.default_port
|
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_html_tags(uri)
|
2019-01-07 12:55:21 -05:00
|
|
|
uri_str = uri.to_s
|
|
|
|
sanitized_uri = ActionController::Base.helpers.sanitize(uri_str, tags: [])
|
|
|
|
if sanitized_uri != uri_str
|
|
|
|
raise BlockedUrlError, 'HTML/CSS/JS tags are not allowed'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-28 14:06:44 -05:00
|
|
|
def parse_url(url)
|
2021-10-20 23:12:55 -04:00
|
|
|
Addressable::URI.parse(url).tap do |parsed_url|
|
|
|
|
raise Addressable::URI::InvalidURIError if multiline_blocked?(parsed_url)
|
|
|
|
end
|
2018-11-28 14:06:44 -05:00
|
|
|
rescue Addressable::URI::InvalidURIError, URI::InvalidURIError
|
|
|
|
raise BlockedUrlError, 'URI is invalid'
|
|
|
|
end
|
|
|
|
|
2021-10-20 23:12:55 -04:00
|
|
|
def multiline_blocked?(parsed_url)
|
|
|
|
url = parsed_url.to_s
|
|
|
|
|
|
|
|
return true if url =~ /\n|\r/
|
|
|
|
# Google Cloud Storage uses a multi-line, encoded Signature query string
|
|
|
|
return false if %w(http https).include?(parsed_url.scheme&.downcase)
|
|
|
|
|
|
|
|
CGI.unescape(url) =~ /\n|\r/
|
2018-11-28 14:06:44 -05:00
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_port(port, ports)
|
2018-04-02 11:13:23 -04:00
|
|
|
return if port.blank?
|
|
|
|
# Only ports under 1024 are restricted
|
|
|
|
return if port >= 1024
|
2018-06-01 07:43:53 -04:00
|
|
|
return if ports.include?(port)
|
2018-04-02 11:13:23 -04:00
|
|
|
|
2018-06-01 07:43:53 -04:00
|
|
|
raise BlockedUrlError, "Only allowed ports are #{ports.join(', ')}, and any over 1024"
|
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_scheme(scheme, schemes)
|
2019-04-11 02:29:07 -04:00
|
|
|
if scheme.blank? || (schemes.any? && !schemes.include?(scheme))
|
|
|
|
raise BlockedUrlError, "Only allowed schemes are #{schemes.join(', ')}"
|
2018-06-01 07:43:53 -04:00
|
|
|
end
|
2018-04-02 11:13:23 -04:00
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_user(value)
|
2018-04-02 11:13:23 -04:00
|
|
|
return if value.blank?
|
|
|
|
return if value =~ /\A\p{Alnum}/
|
2017-03-15 16:09:08 -04:00
|
|
|
|
2018-04-02 11:13:23 -04:00
|
|
|
raise BlockedUrlError, "Username needs to start with an alphanumeric character"
|
2017-03-15 16:09:08 -04:00
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_hostname(value)
|
2018-04-02 11:13:23 -04:00
|
|
|
return if value.blank?
|
2018-11-26 03:45:38 -05:00
|
|
|
return if IPAddress.valid?(value)
|
2018-04-02 11:13:23 -04:00
|
|
|
return if value =~ /\A\p{Alnum}/
|
2017-08-08 13:36:24 -04:00
|
|
|
|
2018-11-26 03:45:38 -05:00
|
|
|
raise BlockedUrlError, "Hostname or IP address invalid"
|
2018-04-02 11:13:23 -04:00
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_unicode_restriction(uri)
|
2018-12-05 15:14:09 -05:00
|
|
|
return if uri.to_s.ascii_only?
|
|
|
|
|
|
|
|
raise BlockedUrlError, "URI must be ascii only #{uri.to_s.dump}"
|
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_localhost(addrs_info)
|
2018-11-26 03:45:38 -05:00
|
|
|
local_ips = ["::", "0.0.0.0"]
|
2018-04-02 11:13:23 -04:00
|
|
|
local_ips.concat(Socket.ip_address_list.map(&:ip_address))
|
|
|
|
|
|
|
|
return if (local_ips & addrs_info.map(&:ip_address)).empty?
|
|
|
|
|
|
|
|
raise BlockedUrlError, "Requests to localhost are not allowed"
|
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_loopback(addrs_info)
|
2018-09-06 00:41:59 -04:00
|
|
|
return unless addrs_info.any? { |addr| addr.ipv4_loopback? || addr.ipv6_loopback? }
|
|
|
|
|
|
|
|
raise BlockedUrlError, "Requests to loopback addresses are not allowed"
|
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_local_network(addrs_info)
|
2018-11-26 03:45:38 -05:00
|
|
|
return unless addrs_info.any? { |addr| addr.ipv4_private? || addr.ipv6_sitelocal? || addr.ipv6_unique_local? }
|
2018-04-02 11:13:23 -04:00
|
|
|
|
|
|
|
raise BlockedUrlError, "Requests to the local network are not allowed"
|
2017-08-08 13:36:24 -04:00
|
|
|
end
|
|
|
|
|
2019-07-12 03:04:44 -04:00
|
|
|
def validate_link_local(addrs_info)
|
2018-08-11 06:38:21 -04:00
|
|
|
netmask = IPAddr.new('169.254.0.0/16')
|
|
|
|
return unless addrs_info.any? { |addr| addr.ipv6_linklocal? || netmask.include?(addr.ip_address) }
|
|
|
|
|
|
|
|
raise BlockedUrlError, "Requests to the link local network are not allowed"
|
|
|
|
end
|
|
|
|
|
2017-03-15 16:09:08 -04:00
|
|
|
def internal?(uri)
|
|
|
|
internal_web?(uri) || internal_shell?(uri)
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_web?(uri)
|
2018-11-28 13:37:12 -05:00
|
|
|
uri.scheme == config.gitlab.protocol &&
|
|
|
|
uri.hostname == config.gitlab.host &&
|
2017-03-15 16:09:08 -04:00
|
|
|
(uri.port.blank? || uri.port == config.gitlab.port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_shell?(uri)
|
2018-11-28 13:37:12 -05:00
|
|
|
uri.scheme == 'ssh' &&
|
|
|
|
uri.hostname == config.gitlab_shell.ssh_host &&
|
2017-03-15 16:09:08 -04:00
|
|
|
(uri.port.blank? || uri.port == config.gitlab_shell.ssh_port)
|
|
|
|
end
|
|
|
|
|
2020-10-29 02:08:45 -04:00
|
|
|
def domain_allowed?(uri)
|
|
|
|
Gitlab::UrlBlockers::UrlAllowlist.domain_allowed?(uri.normalized_host, port: get_port(uri))
|
2019-07-24 13:59:38 -04:00
|
|
|
end
|
|
|
|
|
2020-10-29 02:08:45 -04:00
|
|
|
def ip_allowed?(ip_address, port: nil)
|
|
|
|
Gitlab::UrlBlockers::UrlAllowlist.ip_allowed?(ip_address, port: port)
|
2019-07-24 13:59:38 -04:00
|
|
|
end
|
|
|
|
|
2017-03-15 16:09:08 -04:00
|
|
|
def config
|
|
|
|
Gitlab.config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|