request.rb 9.4 KB

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