2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-26 14:24:53 +05:30
|
|
|
# This class is part of the Gitlab::HTTP wrapper. Depending on the value
|
2019-10-12 21:52:04 +05:30
|
|
|
# of the global setting allow_local_requests_from_web_hooks_and_services this adapter
|
2018-03-26 14:24:53 +05:30
|
|
|
# will allow/block connection to internal IPs and/or urls.
|
|
|
|
#
|
2018-12-13 13:39:08 +05:30
|
|
|
# This functionality can be overridden by providing the setting the option
|
2018-03-26 14:24:53 +05:30
|
|
|
# allow_local_requests = true in the request. For example:
|
|
|
|
# Gitlab::HTTP.get('http://www.gitlab.com', allow_local_requests: true)
|
|
|
|
#
|
|
|
|
# This option will take precedence over the global setting.
|
|
|
|
module Gitlab
|
2019-06-05 12:25:43 +05:30
|
|
|
class HTTPConnectionAdapter < HTTParty::ConnectionAdapter
|
2021-04-17 20:07:23 +05:30
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
|
|
|
override :connection
|
2018-03-26 14:24:53 +05:30
|
|
|
def connection
|
2021-04-17 20:07:23 +05:30
|
|
|
@uri, hostname = validate_url!(uri)
|
|
|
|
|
2019-06-05 12:25:43 +05:30
|
|
|
super.tap do |http|
|
|
|
|
http.hostname_override = hostname if hostname
|
|
|
|
end
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
def validate_url!(url)
|
|
|
|
Gitlab::UrlBlocker.validate!(url, allow_local_network: allow_local_requests?,
|
|
|
|
allow_localhost: allow_local_requests?,
|
|
|
|
dns_rebind_protection: dns_rebind_protection?)
|
|
|
|
rescue Gitlab::UrlBlocker::BlockedUrlError => e
|
|
|
|
raise Gitlab::HTTP::BlockedUrlError, "URL '#{url}' is blocked: #{e.message}"
|
|
|
|
end
|
|
|
|
|
2018-03-26 14:24:53 +05:30
|
|
|
def allow_local_requests?
|
|
|
|
options.fetch(:allow_local_requests, allow_settings_local_requests?)
|
|
|
|
end
|
|
|
|
|
2019-06-05 12:25:43 +05:30
|
|
|
def dns_rebind_protection?
|
|
|
|
return false if Gitlab.http_proxy_env?
|
|
|
|
|
|
|
|
Gitlab::CurrentSettings.dns_rebinding_protection_enabled?
|
|
|
|
end
|
|
|
|
|
2018-03-26 14:24:53 +05:30
|
|
|
def allow_settings_local_requests?
|
2019-10-12 21:52:04 +05:30
|
|
|
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
|
2018-03-26 14:24:53 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|