2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-02 20:19:28 -05:00
|
|
|
# This class is part of the Gitlab::HTTP wrapper. It handles local requests and header timeouts
|
2018-03-13 18:38:25 -04:00
|
|
|
#
|
2022-02-02 20:19:28 -05:00
|
|
|
# 1. Local requests
|
|
|
|
# Depending on the value of the global setting allow_local_requests_from_web_hooks_and_services,
|
|
|
|
# this adapter will allow/block connection to internal IPs and/or urls.
|
2018-03-13 18:38:25 -04:00
|
|
|
#
|
2022-02-02 20:19:28 -05:00
|
|
|
# This functionality can be overridden by providing the setting the option
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 2. Header timeouts
|
|
|
|
# When the use_read_total_timeout option is used, that means the receiver
|
|
|
|
# of the HTTP request cannot be trusted. Gitlab::BufferedIo will be used,
|
|
|
|
# to read header data. It is a modified version of Net::BufferedIO that
|
|
|
|
# raises a timeout error if reading header data takes too much time.
|
|
|
|
|
2018-03-13 18:38:25 -04:00
|
|
|
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)
|
|
|
|
|
2022-02-02 20:19:28 -05:00
|
|
|
http = super
|
|
|
|
http.hostname_override = hostname if hostname
|
|
|
|
|
2022-03-29 11:09:53 -04:00
|
|
|
gitlab_http = Gitlab::NetHttpAdapter.new(http.address, http.port)
|
2022-02-02 20:19:28 -05:00
|
|
|
|
2022-03-29 11:09:53 -04:00
|
|
|
http.instance_variables.each do |variable|
|
|
|
|
gitlab_http.instance_variable_set(variable, http.instance_variable_get(variable))
|
2019-04-21 06:03:26 -04:00
|
|
|
end
|
2022-02-02 20:19:28 -05:00
|
|
|
|
2022-03-29 11:09:53 -04:00
|
|
|
gitlab_http
|
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?,
|
2022-03-16 23:08:05 -04:00
|
|
|
allow_object_storage: allow_object_storage?,
|
2021-03-09 13:09:41 -05:00
|
|
|
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
|
|
|
|
|
2022-03-16 23:08:05 -04:00
|
|
|
def allow_object_storage?
|
|
|
|
options.fetch(:allow_object_storage, false)
|
|
|
|
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
|