2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-13 18:38:25 -04:00
|
|
|
# This class is part of the Gitlab::HTTP wrapper. Depending on the value
|
2019-07-26 06:21:52 -04:00
|
|
|
# of the global setting allow_local_requests_from_web_hooks_and_services this adapter
|
2018-03-13 18:38:25 -04:00
|
|
|
# will allow/block connection to internal IPs and/or urls.
|
|
|
|
#
|
2018-10-30 06:53:01 -04:00
|
|
|
# This functionality can be overridden by providing the setting the option
|
2018-03-13 18:38:25 -04:00
|
|
|
# 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-04-21 06:03:26 -04:00
|
|
|
class HTTPConnectionAdapter < HTTParty::ConnectionAdapter
|
2021-03-09 13:09:41 -05:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
|
|
|
override :connection
|
2018-03-13 18:38:25 -04:00
|
|
|
def connection
|
2021-03-09 13:09:41 -05:00
|
|
|
@uri, hostname = validate_url!(uri)
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
super.tap do |http|
|
|
|
|
http.hostname_override = hostname if hostname
|
|
|
|
end
|
2018-03-13 18:38:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-03-09 13:09:41 -05:00
|
|
|
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-13 18:38:25 -04:00
|
|
|
def allow_local_requests?
|
|
|
|
options.fetch(:allow_local_requests, allow_settings_local_requests?)
|
|
|
|
end
|
|
|
|
|
2019-05-29 12:43:07 -04:00
|
|
|
def dns_rebind_protection?
|
|
|
|
return false if Gitlab.http_proxy_env?
|
|
|
|
|
|
|
|
Gitlab::CurrentSettings.dns_rebinding_protection_enabled?
|
|
|
|
end
|
|
|
|
|
2018-03-13 18:38:25 -04:00
|
|
|
def allow_settings_local_requests?
|
2019-07-26 06:21:52 -04:00
|
|
|
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
|
2018-03-13 18:38:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|