1
0

request.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. # frozen_string_literal: true
  2. require 'ipaddr'
  3. require 'socket'
  4. require 'resolv'
  5. # Use our own timeout class to avoid using HTTP.rb's timeout block
  6. # around the Socket#open method, since we use our own timeout blocks inside
  7. # that method
  8. #
  9. # Also changes how the read timeout behaves so that it is cumulative (closer
  10. # to HTTP::Timeout::Global, but still having distinct timeouts for other
  11. # operation types)
  12. class PerOperationWithDeadline < HTTP::Timeout::PerOperation
  13. READ_DEADLINE = 30
  14. def initialize(*args)
  15. super
  16. @read_deadline = options.fetch(:read_deadline, READ_DEADLINE)
  17. end
  18. def connect(socket_class, host, port, nodelay = false)
  19. @socket = socket_class.open(host, port)
  20. @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
  21. end
  22. # Reset deadline when the connection is re-used for different requests
  23. def reset_counter
  24. @deadline = nil
  25. end
  26. # Read data from the socket
  27. def readpartial(size, buffer = nil)
  28. @deadline ||= Process.clock_gettime(Process::CLOCK_MONOTONIC) + @read_deadline
  29. timeout = false
  30. loop do
  31. result = @socket.read_nonblock(size, buffer, exception: false)
  32. return :eof if result.nil?
  33. remaining_time = @deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
  34. raise HTTP::TimeoutError, "Read timed out after #{@read_timeout} seconds" if timeout
  35. raise HTTP::TimeoutError, "Read timed out after a total of #{@read_deadline} seconds" if remaining_time <= 0
  36. return result if result != :wait_readable
  37. # marking the socket for timeout. Why is this not being raised immediately?
  38. # it seems there is some race-condition on the network level between calling
  39. # #read_nonblock and #wait_readable, in which #read_nonblock signalizes waiting
  40. # for reads, and when waiting for x seconds, it returns nil suddenly without completing
  41. # the x seconds. In a normal case this would be a timeout on wait/read, but it can
  42. # also mean that the socket has been closed by the server. Therefore we "mark" the
  43. # socket for timeout and try to read more bytes. If it returns :eof, it's all good, no
  44. # timeout. Else, the first timeout was a proper timeout.
  45. # This hack has to be done because io/wait#wait_readable doesn't provide a value for when
  46. # the socket is closed by the server, and HTTP::Parser doesn't provide the limit for the chunks.
  47. timeout = true unless @socket.to_io.wait_readable([remaining_time, @read_timeout].min)
  48. end
  49. end
  50. end
  51. class Request
  52. REQUEST_TARGET = '(request-target)'
  53. # We enforce a 5s timeout on DNS resolving, 5s timeout on socket opening
  54. # and 5s timeout on the TLS handshake, meaning the worst case should take
  55. # about 15s in total
  56. TIMEOUT = { connect_timeout: 5, read_timeout: 10, write_timeout: 10, read_deadline: 30 }.freeze
  57. include RoutingHelper
  58. def initialize(verb, url, **options)
  59. raise ArgumentError if url.blank?
  60. @verb = verb
  61. @url = Addressable::URI.parse(url).normalize
  62. @http_client = options.delete(:http_client)
  63. @allow_local = options.delete(:allow_local)
  64. @full_path = !options.delete(:omit_query_string)
  65. @options = options.merge(socket_class: use_proxy? || @allow_local ? ProxySocket : Socket)
  66. @options = @options.merge(timeout_class: PerOperationWithDeadline, timeout_options: TIMEOUT)
  67. @options = @options.merge(proxy_url) if use_proxy?
  68. @headers = {}
  69. raise Mastodon::HostValidationError, 'Instance does not support hidden service connections' if block_hidden_service?
  70. set_common_headers!
  71. set_digest! if options.key?(:body)
  72. end
  73. def on_behalf_of(actor, sign_with: nil)
  74. raise ArgumentError, 'actor must not be nil' if actor.nil?
  75. @actor = actor
  76. @keypair = sign_with.present? ? OpenSSL::PKey::RSA.new(sign_with) : @actor.keypair
  77. self
  78. end
  79. def add_headers(new_headers)
  80. @headers.merge!(new_headers)
  81. self
  82. end
  83. def perform
  84. begin
  85. response = http_client.request(@verb, @url.to_s, @options.merge(headers: headers))
  86. rescue => e
  87. raise e.class, "#{e.message} on #{@url}", e.backtrace[0]
  88. end
  89. begin
  90. # If we are using a persistent connection, we have to
  91. # read every response to be able to move forward at all.
  92. # However, simply calling #to_s or #flush may not be safe,
  93. # as the response body, if malicious, could be too big
  94. # for our memory. So we use the #body_with_limit method
  95. response.body_with_limit if http_client.persistent?
  96. yield response if block_given?
  97. ensure
  98. http_client.close unless http_client.persistent?
  99. end
  100. end
  101. def headers
  102. (@actor ? @headers.merge('Signature' => signature) : @headers).without(REQUEST_TARGET)
  103. end
  104. class << self
  105. def valid_url?(url)
  106. begin
  107. parsed_url = Addressable::URI.parse(url)
  108. rescue Addressable::URI::InvalidURIError
  109. return false
  110. end
  111. %w(http https).include?(parsed_url.scheme) && parsed_url.host.present?
  112. end
  113. def http_client
  114. HTTP.use(:auto_inflate).follow(max_hops: 3)
  115. end
  116. end
  117. private
  118. def set_common_headers!
  119. @headers[REQUEST_TARGET] = request_target
  120. @headers['User-Agent'] = Mastodon::Version.user_agent
  121. @headers['Host'] = @url.host
  122. @headers['Date'] = Time.now.utc.httpdate
  123. @headers['Accept-Encoding'] = 'gzip' if @verb != :head
  124. end
  125. def set_digest!
  126. @headers['Digest'] = "SHA-256=#{Digest::SHA256.base64digest(@options[:body])}"
  127. end
  128. def request_target
  129. if @url.query.nil? || !@full_path
  130. "#{@verb} #{@url.path}"
  131. else
  132. "#{@verb} #{@url.path}?#{@url.query}"
  133. end
  134. end
  135. def signature
  136. algorithm = 'rsa-sha256'
  137. signature = Base64.strict_encode64(@keypair.sign(OpenSSL::Digest.new('SHA256'), signed_string))
  138. "keyId=\"#{key_id}\",algorithm=\"#{algorithm}\",headers=\"#{signed_headers.keys.join(' ').downcase}\",signature=\"#{signature}\""
  139. end
  140. def signed_string
  141. signed_headers.map { |key, value| "#{key.downcase}: #{value}" }.join("\n")
  142. end
  143. def signed_headers
  144. @headers.without('User-Agent', 'Accept-Encoding')
  145. end
  146. def key_id
  147. ActivityPub::TagManager.instance.key_uri_for(@actor)
  148. end
  149. def http_client
  150. @http_client ||= Request.http_client
  151. end
  152. def use_proxy?
  153. proxy_url.present?
  154. end
  155. def proxy_url
  156. if hidden_service? && Rails.configuration.x.http_client_hidden_proxy.present?
  157. Rails.configuration.x.http_client_hidden_proxy
  158. else
  159. Rails.configuration.x.http_client_proxy
  160. end
  161. end
  162. def block_hidden_service?
  163. !Rails.configuration.x.access_to_hidden_service && hidden_service?
  164. end
  165. def hidden_service?
  166. /\.(onion|i2p)$/.match?(@url.host)
  167. end
  168. module ClientLimit
  169. def truncated_body(limit = 1.megabyte)
  170. if charset.nil?
  171. encoding = Encoding::BINARY
  172. else
  173. begin
  174. encoding = Encoding.find(charset)
  175. rescue ArgumentError
  176. encoding = Encoding::BINARY
  177. end
  178. end
  179. contents = String.new(encoding: encoding)
  180. while (chunk = readpartial)
  181. contents << chunk
  182. chunk.clear
  183. break if contents.bytesize > limit
  184. end
  185. contents
  186. end
  187. def body_with_limit(limit = 1.megabyte)
  188. require_limit_not_exceeded!(limit)
  189. contents = truncated_body(limit)
  190. raise Mastodon::LengthValidationError, "Body size exceeds limit of #{limit}" if contents.bytesize > limit
  191. contents
  192. end
  193. def require_limit_not_exceeded!(limit)
  194. raise Mastodon::LengthValidationError, "Content-Length #{content_length} exceeds limit of #{limit}" if content_length.present? && content_length > limit
  195. end
  196. end
  197. if ::HTTP::Response.methods.include?(:body_with_limit) && !Rails.env.production?
  198. abort 'HTTP::Response#body_with_limit is already defined, the monkey patch will not be applied'
  199. else
  200. class ::HTTP::Response
  201. include Request::ClientLimit
  202. end
  203. end
  204. class Socket < TCPSocket
  205. class << self
  206. def open(host, *args)
  207. outer_e = nil
  208. port = args.first
  209. addresses = []
  210. begin
  211. addresses = [IPAddr.new(host)]
  212. rescue IPAddr::InvalidAddressError
  213. Resolv::DNS.open do |dns|
  214. dns.timeouts = 5
  215. addresses = dns.getaddresses(host)
  216. addresses = addresses.filter { |addr| addr.is_a?(Resolv::IPv6) }.take(2) + addresses.filter { |addr| !addr.is_a?(Resolv::IPv6) }.take(2)
  217. end
  218. end
  219. socks = []
  220. addr_by_socket = {}
  221. addresses.each do |address|
  222. check_private_address(address, host)
  223. sock = ::Socket.new(address.is_a?(Resolv::IPv6) ? ::Socket::AF_INET6 : ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0)
  224. sockaddr = ::Socket.pack_sockaddr_in(port, address.to_s)
  225. sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
  226. sock.connect_nonblock(sockaddr)
  227. # If that hasn't raised an exception, we somehow managed to connect
  228. # immediately, close pending sockets and return immediately
  229. socks.each(&:close)
  230. return sock
  231. rescue IO::WaitWritable
  232. socks << sock
  233. addr_by_socket[sock] = sockaddr
  234. rescue => e
  235. outer_e = e
  236. end
  237. until socks.empty?
  238. _, available_socks, = IO.select(nil, socks, nil, Request::TIMEOUT[:connect_timeout])
  239. if available_socks.nil?
  240. socks.each(&:close)
  241. raise HTTP::TimeoutError, "Connect timed out after #{Request::TIMEOUT[:connect_timeout]} seconds"
  242. end
  243. available_socks.each do |sock|
  244. socks.delete(sock)
  245. begin
  246. sock.connect_nonblock(addr_by_socket[sock])
  247. rescue Errno::EISCONN
  248. # Do nothing
  249. rescue => e
  250. sock.close
  251. outer_e = e
  252. next
  253. end
  254. socks.each(&:close)
  255. return sock
  256. end
  257. end
  258. if outer_e
  259. raise outer_e
  260. else
  261. raise SocketError, "No address for #{host}"
  262. end
  263. end
  264. alias new open
  265. def check_private_address(address, host)
  266. addr = IPAddr.new(address.to_s)
  267. return if Rails.env.development? || Rails.configuration.x.private_address_exceptions.any? { |range| range.include?(addr) }
  268. raise Mastodon::PrivateNetworkAddressError, host if PrivateAddressCheck.private_address?(addr)
  269. end
  270. end
  271. end
  272. class ProxySocket < Socket
  273. class << self
  274. def check_private_address(_address, _host)
  275. # Accept connections to private addresses as HTTP proxies will usually
  276. # be on local addresses
  277. nil
  278. end
  279. end
  280. end
  281. private_constant :ClientLimit, :Socket, :ProxySocket
  282. end