]>
cat aescling's git repositories - mastodon.git/blob - app/lib/request.rb
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 raise e
.class, e
.message
, e
.backtrace
[0]
79 http_client
.close
unless http_client
.persistent
?
84 (@account ? @headers.merge('Signature' => signature
) : @headers).without(REQUEST_TARGET
)
90 parsed_url
= Addressable
::URI.parse(url
)
91 rescue Addressable
::URI::InvalidURIError
95 %w(http https
).include?(parsed_url
.scheme
) && parsed_url
.host
.present
?
99 HTTP
.use(:auto_inflate).timeout(TIMEOUT
.dup
).follow(max_hops
: 2)
105 def set_common_headers!
106 @headers[REQUEST_TARGET
] = "#{@verb} #{@url.path}"
107 @headers['User-Agent'] = Mastodon
::Version.user_agent
108 @headers['Host'] = @url.host
109 @headers['Date'] = Time
.now
.utc
.httpdate
110 @headers['Accept-Encoding'] = 'gzip' if @verb !
= :head
114 @headers['Digest'] = "SHA-256=#{Digest::SHA256.base64digest(@options[:body])}"
118 algorithm
= 'rsa-sha256'
119 signature
= Base64
.strict_encode64(@keypair.sign(OpenSSL
::Digest::SHA256.new
, signed_string
))
121 "keyId=\"#{key_id}\",algorithm=\"#{algorithm}\",headers=\"#{signed_headers.keys.join(' ').downcase}\",signature=\"#{signature}\""
125 signed_headers
.map
{ |key
, value
| "#{key.downcase}: #{value}" }.join("\n")
129 @headers.without('User-Agent', 'Accept-Encoding')
135 @account.to_webfinger_s
137 [ActivityPub
::TagManager.instance
.uri_for(@account), '#main-key'].join
142 @http_client ||= Request
.http_client
146 Rails
.configuration
.x
.http_client_proxy
.present
?
149 def block_hidden_service
?
150 !Rails
.configuration
.x
.access_to_hidden_service
&& /\.(onion|i2p)$/.match(@url.host
)
154 def body_with_limit(limit
= 1.megabyte
)
155 raise Mastodon
::LengthValidationError if content_length
.present
? && content_length
> limit
158 encoding
= Encoding
::BINARY
161 encoding
= Encoding
.find(charset
)
163 encoding
= Encoding
::BINARY
167 contents
= String
.new(encoding
: encoding
)
169 while (chunk
= readpartial
)
173 raise Mastodon
::LengthValidationError if contents
.bytesize
> limit
180 class Socket
< TCPSocket
182 def open(host
, *args
)
188 addresses
= [IPAddr
.new(host
)]
189 rescue IPAddr
::InvalidAddressError
190 Resolv
::DNS.open
do |dns
|
192 addresses
= dns
.getaddresses(host
).take(2)
199 addresses
.each
do |address
|
201 check_private_address(address
)
203 sock
= ::Socket.new(address
.is_a
?(Resolv
::IPv6) ? ::Socket::AF_INET6 : ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0)
204 sockaddr
= ::Socket.pack_sockaddr_in(port
, address
.to_s
)
206 sock
.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
208 sock
.connect_nonblock(sockaddr
)
210 # If that hasn't raised an exception, we somehow managed to connect
211 # immediately, close pending sockets and return immediately
214 rescue IO
::WaitWritable
216 addr_by_socket
[sock
] = sockaddr
223 _
, available_socks
, = IO
.select(nil, socks
, nil, Request
::TIMEOUT[:connect])
225 if available_socks
.nil?
227 raise HTTP
::TimeoutError, "Connect timed out after #{Request::TIMEOUT[:connect]} seconds"
230 available_socks
.each
do |sock
|
234 sock
.connect_nonblock(addr_by_socket
[sock
])
235 rescue Errno
::EISCONN
250 raise SocketError
, "No address for #{host}"
256 def check_private_address(address
)
257 raise Mastodon
::HostValidationError if PrivateAddressCheck
.private_address
?(IPAddr
.new(address
.to_s
))
262 class ProxySocket
< Socket
264 def check_private_address(_address
)
265 # Accept connections to private addresses as HTTP proxies will usually
266 # be on local addresses
272 private_constant
:ClientLimit, :Socket, :ProxySocket
This page took 0.137884 seconds and 4 git commands to generate.