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-11 02:29:07 -04:00
|
|
|
def validate!(url, ports: [], schemes: [], allow_localhost: false, allow_local_network: true, ascii_only: false, enforce_user: false, enforce_sanitization: false)
|
2018-03-28 13:27:16 -04:00
|
|
|
return true 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-01-07 12:55:21 -05:00
|
|
|
validate_html_tags!(uri) if enforce_sanitization
|
|
|
|
|
2018-04-02 11:13:23 -04:00
|
|
|
# Allow imports from the GitLab instance itself but only from the configured ports
|
|
|
|
return true if internal?(uri)
|
2018-03-13 18:38:25 -04:00
|
|
|
|
2019-01-07 12:55:21 -05:00
|
|
|
port = get_port(uri)
|
2019-04-11 02:29:07 -04:00
|
|
|
validate_scheme!(uri.scheme, schemes)
|
2018-06-01 07:43:53 -04:00
|
|
|
validate_port!(port, ports) if ports.any?
|
2018-06-11 09:29:37 -04:00
|
|
|
validate_user!(uri.user) if enforce_user
|
2018-04-02 11:13:23 -04:00
|
|
|
validate_hostname!(uri.hostname)
|
2018-12-05 15:14:09 -05:00
|
|
|
validate_unicode_restriction!(uri) if ascii_only
|
2018-03-28 13:27:16 -04:00
|
|
|
|
2018-04-02 11:13:23 -04:00
|
|
|
begin
|
2018-11-26 03:45:38 -05:00
|
|
|
addrs_info = Addrinfo.getaddrinfo(uri.hostname, port, nil, :STREAM).map do |addr|
|
|
|
|
addr.ipv6_v4mapped? ? addr.ipv6_to_ipv4 : addr
|
|
|
|
end
|
2017-11-07 03:30:38 -05:00
|
|
|
rescue SocketError
|
2018-04-02 11:13:23 -04:00
|
|
|
return true
|
2017-03-15 16:09:08 -04:00
|
|
|
end
|
|
|
|
|
2018-04-02 11:13:23 -04:00
|
|
|
validate_localhost!(addrs_info) unless allow_localhost
|
2018-09-06 00:41:59 -04:00
|
|
|
validate_loopback!(addrs_info) unless allow_localhost
|
2018-04-02 11:13:47 -04:00
|
|
|
validate_local_network!(addrs_info) unless allow_local_network
|
2018-08-11 06:38:21 -04:00
|
|
|
validate_link_local!(addrs_info) unless allow_local_network
|
2018-04-02 11:13:23 -04:00
|
|
|
|
2018-03-28 13:27:16 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def blocked_url?(*args)
|
|
|
|
validate!(*args)
|
|
|
|
|
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-01-07 12:55:21 -05:00
|
|
|
def get_port(uri)
|
|
|
|
uri.port || uri.default_port
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_html_tags!(uri)
|
|
|
|
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)
|
|
|
|
raise Addressable::URI::InvalidURIError if multiline?(url)
|
|
|
|
|
|
|
|
Addressable::URI.parse(url)
|
|
|
|
rescue Addressable::URI::InvalidURIError, URI::InvalidURIError
|
|
|
|
raise BlockedUrlError, 'URI is invalid'
|
|
|
|
end
|
|
|
|
|
|
|
|
def multiline?(url)
|
|
|
|
CGI.unescape(url.to_s) =~ /\n|\r/
|
|
|
|
end
|
|
|
|
|
2018-06-01 07:43:53 -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-04-11 02:29:07 -04:00
|
|
|
def validate_scheme!(scheme, schemes)
|
|
|
|
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
|
|
|
|
|
|
|
|
def validate_user!(value)
|
|
|
|
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
|
|
|
|
|
2018-04-02 11:13:23 -04:00
|
|
|
def validate_hostname!(value)
|
|
|
|
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
|
|
|
|
|
2018-12-05 15:14:09 -05:00
|
|
|
def validate_unicode_restriction!(uri)
|
|
|
|
return if uri.to_s.ascii_only?
|
|
|
|
|
|
|
|
raise BlockedUrlError, "URI must be ascii only #{uri.to_s.dump}"
|
|
|
|
end
|
|
|
|
|
2018-04-02 11:13:23 -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
|
|
|
|
|
2018-09-06 00:41:59 -04:00
|
|
|
def validate_loopback!(addrs_info)
|
|
|
|
return unless addrs_info.any? { |addr| addr.ipv4_loopback? || addr.ipv6_loopback? }
|
|
|
|
|
|
|
|
raise BlockedUrlError, "Requests to loopback addresses are not allowed"
|
|
|
|
end
|
|
|
|
|
2018-04-02 11:13:23 -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
|
|
|
|
|
2018-08-11 06:38:21 -04:00
|
|
|
def validate_link_local!(addrs_info)
|
|
|
|
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
|
|
|
|
|
|
|
|
def config
|
|
|
|
Gitlab.config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|