1 # frozen_string_literal: true
7 # Monkey-patch the HTTP.rb timeout class to avoid using a timeout block
8 # around the Socket#open method, since we use our own timeout blocks inside
10 class HTTP
::Timeout::PerOperation
11 def connect(socket_class
, host
, port
, nodelay
= false)
12 @socket = socket_class
.open(host
, port
)
13 @socket.setsockopt(Socket
::IPPROTO_TCP, Socket
::TCP_NODELAY, 1) if nodelay
18 REQUEST_TARGET
= '(request-target)'
20 # We enforce a 5s timeout on DNS resolving, 5s timeout on socket opening
21 # and 5s timeout on the TLS handshake, meaning the worst case should take
23 TIMEOUT
= { connect
: 5, read
: 10, write
: 10 }.freeze
27 def initialize(verb
, url
, **options
)
28 raise ArgumentError
if url
.blank
?
31 @url = Addressable
::URI.parse(url
).normalize
32 @http_client = options
.delete(:http_client)
33 @options = options
.merge(socket_class
: use_proxy
? ? ProxySocket
: Socket
)
34 @options = @options.merge(Rails
.configuration
.x
.http_client_proxy
) if use_proxy
?
37 raise Mastodon
::HostValidationError, 'Instance does not support hidden service connections' if block_hidden_service
?
40 set_digest!
if options
.key
?(:body)
43 def on_behalf_of(account
, key_id_format
= :uri, sign_with
: nil)
44 raise ArgumentError
, 'account must not be nil' if account
.nil?
47 @keypair = sign_with
.present
? ? OpenSSL
::PKey::RSA.new(sign_with
) : @account.keypair
48 @key_id_format = key_id_format
53 def add_headers(new_headers
)
54 @headers.merge!
(new_headers
)
60 response
= http_client
.public_send(@verb, @url.to_s
, @options.merge(headers
: headers
))
62 raise e
.class, "#{e.message} on #{@url}", e
.backtrace
[0]
66 response
= response
.extend(ClientLimit
)
68 # If we are using a persistent connection, we have to
69 # read every response to be able to move forward at all.
70 # However, simply calling #to_s or #flush may not be safe,
71 # as the response body, if malicious, could be too big
72 # for our memory. So we use the #body_with_limit method
73 response
.body_with_limit
if http_client
.persistent
?
75 yield response
if block_given
?
77 http_client
.close
unless http_client
.persistent
?
82 (@account ? @headers.merge('Signature' => signature
) : @headers).without(REQUEST_TARGET
)
88 parsed_url
= Addressable
::URI.parse(url
)
89 rescue Addressable
::URI::InvalidURIError
93 %w(http https
).include?(parsed_url
.scheme
) && parsed_url
.host
.present
?
97 HTTP
.use(:auto_inflate).timeout(TIMEOUT
.dup
).follow(max_hops
: 2)
103 def set_common_headers!
104 @headers[REQUEST_TARGET
] = "#{@verb} #{@url.path}"
105 @headers['User-Agent'] = Mastodon
::Version.user_agent
106 @headers['Host'] = @url.host
107 @headers['Date'] = Time
.now
.utc
.httpdate
108 @headers['Accept-Encoding'] = 'gzip' if @verb !
= :head
112 @headers['Digest'] = "SHA-256=#{Digest::SHA256.base64digest(@options[:body])}"
116 algorithm
= 'rsa-sha256'
117 signature
= Base64
.strict_encode64(@keypair.sign(OpenSSL
::Digest.new('SHA256'), signed_string
))
119 "keyId=\"#{key_id}\",algorithm=\"#{algorithm}\",headers=\"#{signed_headers.keys.join(' ').downcase}\",signature=\"#{signature}\""
123 signed_headers
.map
{ |key
, value
| "#{key.downcase}: #{value}" }.join("\n")
127 @headers.without('User-Agent', 'Accept-Encoding')
133 @account.to_webfinger_s
135 [ActivityPub
::TagManager.instance
.uri_for(@account), '#main-key'].join
140 @http_client ||= Request
.http_client
144 Rails
.configuration
.x
.http_client_proxy
.present
?
147 def block_hidden_service
?
148 !Rails
.configuration
.x
.access_to_hidden_service
&& /\.(onion|i2p)$/.match
?(@url.host
)
152 def body_with_limit(limit
= 1.megabyte
)
153 raise Mastodon
::LengthValidationError if content_length
.present
? && content_length
> limit
156 encoding
= Encoding
::BINARY
159 encoding
= Encoding
.find(charset
)
161 encoding
= Encoding
::BINARY
165 contents
= String
.new(encoding
: encoding
)
167 while (chunk
= readpartial
)
171 raise Mastodon
::LengthValidationError if contents
.bytesize
> limit
178 class Socket
< TCPSocket
180 def open(host
, *args
)
186 addresses
= [IPAddr
.new(host
)]
187 rescue IPAddr
::InvalidAddressError
188 Resolv
::DNS.open
do |dns
|
190 addresses
= dns
.getaddresses(host
).take(2)
197 addresses
.each
do |address
|
199 check_private_address(address
)
201 sock
= ::Socket.new(address
.is_a
?(Resolv
::IPv6) ? ::Socket::AF_INET6 : ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0)
202 sockaddr
= ::Socket.pack_sockaddr_in(port
, address
.to_s
)
204 sock
.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
206 sock
.connect_nonblock(sockaddr
)
208 # If that hasn't raised an exception, we somehow managed to connect
209 # immediately, close pending sockets and return immediately
212 rescue IO
::WaitWritable
214 addr_by_socket
[sock
] = sockaddr
221 _
, available_socks
, = IO
.select(nil, socks
, nil, Request
::TIMEOUT[:connect])
223 if available_socks
.nil?
225 raise HTTP
::TimeoutError, "Connect timed out after #{Request::TIMEOUT[:connect]} seconds"
228 available_socks
.each
do |sock
|
232 sock
.connect_nonblock(addr_by_socket
[sock
])
233 rescue Errno
::EISCONN
249 raise SocketError
, "No address for #{host}"
255 def check_private_address(address
)
256 addr
= IPAddr
.new(address
.to_s
)
257 return if private_address_exceptions
.any
? { |range
| range
.include?(addr
) }
258 raise Mastodon
::HostValidationError if PrivateAddressCheck
.private_address
?(addr
)
261 def private_address_exceptions
262 @private_address_exceptions = begin
263 (ENV['ALLOWED_PRIVATE_ADDRESSES'] || '').split(',').map
{ |addr
| IPAddr
.new(addr
) }
269 class ProxySocket
< Socket
271 def check_private_address(_address
)
272 # Accept connections to private addresses as HTTP proxies will usually
273 # be on local addresses
279 private_constant
:ClientLimit, :Socket, :ProxySocket