2017-03-15 16:09:08 -04:00
|
|
|
require 'resolv'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
class UrlBlocker
|
|
|
|
class << self
|
|
|
|
# Used to specify what hosts and port numbers should be prohibited for project
|
|
|
|
# imports.
|
|
|
|
VALID_PORTS = [22, 80, 443].freeze
|
|
|
|
|
|
|
|
def blocked_url?(url)
|
2017-03-18 13:56:05 -04:00
|
|
|
return false if url.nil?
|
|
|
|
|
2017-03-15 16:09:08 -04:00
|
|
|
blocked_ips = ["127.0.0.1", "::1", "0.0.0.0"]
|
|
|
|
blocked_ips.concat(Socket.ip_address_list.map(&:ip_address))
|
|
|
|
|
|
|
|
begin
|
|
|
|
uri = Addressable::URI.parse(url)
|
|
|
|
# Allow imports from the GitLab instance itself but only from the configured ports
|
|
|
|
return false if internal?(uri)
|
|
|
|
|
|
|
|
return true if blocked_port?(uri.port)
|
2017-08-08 13:36:24 -04:00
|
|
|
return true if blocked_user_or_hostname?(uri.user)
|
|
|
|
return true if blocked_user_or_hostname?(uri.hostname)
|
2017-03-15 16:09:08 -04:00
|
|
|
|
2017-11-07 03:30:38 -05:00
|
|
|
server_ips = Addrinfo.getaddrinfo(uri.hostname, 80, nil, :STREAM).map(&:ip_address)
|
2017-03-15 16:09:08 -04:00
|
|
|
return true if (blocked_ips & server_ips).any?
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
return true
|
2017-11-07 03:30:38 -05:00
|
|
|
rescue SocketError
|
|
|
|
return false
|
2017-03-15 16:09:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def blocked_port?(port)
|
|
|
|
return false if port.blank?
|
|
|
|
|
|
|
|
port < 1024 && !VALID_PORTS.include?(port)
|
|
|
|
end
|
|
|
|
|
2017-08-08 13:36:24 -04:00
|
|
|
def blocked_user_or_hostname?(value)
|
|
|
|
return false if value.blank?
|
|
|
|
|
|
|
|
value !~ /\A\p{Alnum}/
|
|
|
|
end
|
|
|
|
|
2017-03-15 16:09:08 -04:00
|
|
|
def internal?(uri)
|
|
|
|
internal_web?(uri) || internal_shell?(uri)
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_web?(uri)
|
|
|
|
uri.hostname == config.gitlab.host &&
|
|
|
|
(uri.port.blank? || uri.port == config.gitlab.port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_shell?(uri)
|
|
|
|
uri.hostname == config.gitlab_shell.ssh_host &&
|
|
|
|
(uri.port.blank? || uri.port == config.gitlab_shell.ssh_port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
|
|
|
Gitlab.config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|