debian-mirror-gitlab/lib/gitlab/http_connection_adapter.rb

71 lines
2.4 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2022-04-04 11:22:00 +05:30
# This class is part of the Gitlab::HTTP wrapper. It handles local requests and header timeouts
2018-03-26 14:24:53 +05:30
#
2022-04-04 11:22:00 +05:30
# 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-26 14:24:53 +05:30
#
2022-04-04 11:22:00 +05:30
# 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-26 14:24:53 +05:30
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)
2022-04-04 11:22:00 +05:30
http = super
http.hostname_override = hostname if hostname
2022-06-21 17:19:12 +05:30
gitlab_http = Gitlab::NetHttpAdapter.new(http.address, http.port)
2022-04-04 11:22:00 +05:30
2022-06-21 17:19:12 +05:30
http.instance_variables.each do |variable|
gitlab_http.instance_variable_set(variable, http.instance_variable_get(variable))
2019-06-05 12:25:43 +05:30
end
2022-04-04 11:22:00 +05:30
2022-06-21 17:19:12 +05:30
gitlab_http
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?,
2022-05-07 20:08:51 +05:30
allow_object_storage: allow_object_storage?,
2021-04-17 20:07:23 +05:30
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
2022-05-07 20:08:51 +05:30
def allow_object_storage?
options.fetch(:allow_object_storage, false)
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