]> cat aescling's git repositories - mastodon.git/blob - app/lib/request.rb
Fix Ruby 2.7 support (#12831)
[mastodon.git] / app / lib / request.rb
1 # frozen_string_literal: true
2
3 require 'ipaddr'
4 require 'socket'
5 require 'resolv'
6
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
9 # that method
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
14 end
15 end
16
17 class Request
18 REQUEST_TARGET = '(request-target)'
19
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
22 # about 15s in total
23 TIMEOUT = { connect: 5, read: 10, write: 10 }.freeze
24
25 include RoutingHelper
26
27 def initialize(verb, url, **options)
28 raise ArgumentError if url.blank?
29
30 @verb = verb
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?
35 @headers = {}
36
37 raise Mastodon::HostValidationError, 'Instance does not support hidden service connections' if block_hidden_service?
38
39 set_common_headers!
40 set_digest! if options.key?(:body)
41 end
42
43 def on_behalf_of(account, key_id_format = :uri, sign_with: nil)
44 raise ArgumentError, 'account must not be nil' if account.nil?
45
46 @account = account
47 @keypair = sign_with.present? ? OpenSSL::PKey::RSA.new(sign_with) : @account.keypair
48 @key_id_format = key_id_format
49
50 self
51 end
52
53 def add_headers(new_headers)
54 @headers.merge!(new_headers)
55 self
56 end
57
58 def perform
59 begin
60 response = http_client.public_send(@verb, @url.to_s, @options.merge(headers: headers))
61 rescue => e
62 raise e.class, "#{e.message} on #{@url}", e.backtrace[0]
63 end
64
65 begin
66 response = response.extend(ClientLimit)
67
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?
74
75 yield response if block_given?
76 rescue => e
77 raise e.class, e.message, e.backtrace[0]
78 ensure
79 http_client.close unless http_client.persistent?
80 end
81 end
82
83 def headers
84 (@account ? @headers.merge('Signature' => signature) : @headers).without(REQUEST_TARGET)
85 end
86
87 class << self
88 def valid_url?(url)
89 begin
90 parsed_url = Addressable::URI.parse(url)
91 rescue Addressable::URI::InvalidURIError
92 return false
93 end
94
95 %w(http https).include?(parsed_url.scheme) && parsed_url.host.present?
96 end
97
98 def http_client
99 HTTP.use(:auto_inflate).timeout(TIMEOUT.dup).follow(max_hops: 2)
100 end
101 end
102
103 private
104
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
111 end
112
113 def set_digest!
114 @headers['Digest'] = "SHA-256=#{Digest::SHA256.base64digest(@options[:body])}"
115 end
116
117 def signature
118 algorithm = 'rsa-sha256'
119 signature = Base64.strict_encode64(@keypair.sign(OpenSSL::Digest::SHA256.new, signed_string))
120
121 "keyId=\"#{key_id}\",algorithm=\"#{algorithm}\",headers=\"#{signed_headers.keys.join(' ').downcase}\",signature=\"#{signature}\""
122 end
123
124 def signed_string
125 signed_headers.map { |key, value| "#{key.downcase}: #{value}" }.join("\n")
126 end
127
128 def signed_headers
129 @headers.without('User-Agent', 'Accept-Encoding')
130 end
131
132 def key_id
133 case @key_id_format
134 when :acct
135 @account.to_webfinger_s
136 when :uri
137 [ActivityPub::TagManager.instance.uri_for(@account), '#main-key'].join
138 end
139 end
140
141 def http_client
142 @http_client ||= Request.http_client
143 end
144
145 def use_proxy?
146 Rails.configuration.x.http_client_proxy.present?
147 end
148
149 def block_hidden_service?
150 !Rails.configuration.x.access_to_hidden_service && /\.(onion|i2p)$/.match(@url.host)
151 end
152
153 module ClientLimit
154 def body_with_limit(limit = 1.megabyte)
155 raise Mastodon::LengthValidationError if content_length.present? && content_length > limit
156
157 if charset.nil?
158 encoding = Encoding::BINARY
159 else
160 begin
161 encoding = Encoding.find(charset)
162 rescue ArgumentError
163 encoding = Encoding::BINARY
164 end
165 end
166
167 contents = String.new(encoding: encoding)
168
169 while (chunk = readpartial)
170 contents << chunk
171 chunk.clear
172
173 raise Mastodon::LengthValidationError if contents.bytesize > limit
174 end
175
176 contents
177 end
178 end
179
180 class Socket < TCPSocket
181 class << self
182 def open(host, *args)
183 outer_e = nil
184 port = args.first
185
186 addresses = []
187 begin
188 addresses = [IPAddr.new(host)]
189 rescue IPAddr::InvalidAddressError
190 Resolv::DNS.open do |dns|
191 dns.timeouts = 5
192 addresses = dns.getaddresses(host).take(2)
193 end
194 end
195
196 socks = []
197 addr_by_socket = {}
198
199 addresses.each do |address|
200 begin
201 check_private_address(address)
202
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)
205
206 sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
207
208 sock.connect_nonblock(sockaddr)
209
210 # If that hasn't raised an exception, we somehow managed to connect
211 # immediately, close pending sockets and return immediately
212 socks.each(&:close)
213 return sock
214 rescue IO::WaitWritable
215 socks << sock
216 addr_by_socket[sock] = sockaddr
217 rescue => e
218 outer_e = e
219 end
220 end
221
222 until socks.empty?
223 _, available_socks, = IO.select(nil, socks, nil, Request::TIMEOUT[:connect])
224
225 if available_socks.nil?
226 socks.each(&:close)
227 raise HTTP::TimeoutError, "Connect timed out after #{Request::TIMEOUT[:connect]} seconds"
228 end
229
230 available_socks.each do |sock|
231 socks.delete(sock)
232
233 begin
234 sock.connect_nonblock(addr_by_socket[sock])
235 rescue Errno::EISCONN
236 rescue => e
237 sock.close
238 outer_e = e
239 next
240 end
241
242 socks.each(&:close)
243 return sock
244 end
245 end
246
247 if outer_e
248 raise outer_e
249 else
250 raise SocketError, "No address for #{host}"
251 end
252 end
253
254 alias new open
255
256 def check_private_address(address)
257 raise Mastodon::HostValidationError if PrivateAddressCheck.private_address?(IPAddr.new(address.to_s))
258 end
259 end
260 end
261
262 class ProxySocket < Socket
263 class << self
264 def check_private_address(_address)
265 # Accept connections to private addresses as HTTP proxies will usually
266 # be on local addresses
267 nil
268 end
269 end
270 end
271
272 private_constant :ClientLimit, :Socket, :ProxySocket
273 end
This page took 0.137884 seconds and 4 git commands to generate.