1
0

request.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. class HTTP::Timeout::PerOperation
  9. def connect(socket_class, host, port, nodelay = false)
  10. @socket = socket_class.open(host, port)
  11. @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
  12. end
  13. end
  14. class Request
  15. REQUEST_TARGET = '(request-target)'
  16. # We enforce a 5s timeout on DNS resolving, 5s timeout on socket opening
  17. # and 5s timeout on the TLS handshake, meaning the worst case should take
  18. # about 15s in total
  19. TIMEOUT = { connect: 5, read: 10, write: 10 }.freeze
  20. include RoutingHelper
  21. def initialize(verb, url, **options)
  22. raise ArgumentError if url.blank?
  23. @verb = verb
  24. @url = Addressable::URI.parse(url).normalize
  25. @http_client = options.delete(:http_client)
  26. @options = options.merge(socket_class: use_proxy? ? ProxySocket : Socket)
  27. @options = @options.merge(Rails.configuration.x.http_client_proxy) if use_proxy?
  28. @headers = {}
  29. raise Mastodon::HostValidationError, 'Instance does not support hidden service connections' if block_hidden_service?
  30. set_common_headers!
  31. set_digest! if options.key?(:body)
  32. end
  33. def on_behalf_of(account, key_id_format = :uri, sign_with: nil)
  34. raise ArgumentError, 'account must not be nil' if account.nil?
  35. @account = account
  36. @keypair = sign_with.present? ? OpenSSL::PKey::RSA.new(sign_with) : @account.keypair
  37. @key_id_format = key_id_format
  38. self
  39. end
  40. def add_headers(new_headers)
  41. @headers.merge!(new_headers)
  42. self
  43. end
  44. def perform
  45. begin
  46. response = http_client.public_send(@verb, @url.to_s, @options.merge(headers: headers))
  47. rescue => e
  48. raise e.class, "#{e.message} on #{@url}", e.backtrace[0]
  49. end
  50. begin
  51. response = response.extend(ClientLimit)
  52. # If we are using a persistent connection, we have to
  53. # read every response to be able to move forward at all.
  54. # However, simply calling #to_s or #flush may not be safe,
  55. # as the response body, if malicious, could be too big
  56. # for our memory. So we use the #body_with_limit method
  57. response.body_with_limit if http_client.persistent?
  58. yield response if block_given?
  59. ensure
  60. http_client.close unless http_client.persistent?
  61. end
  62. end
  63. def headers
  64. (@account ? @headers.merge('Signature' => signature) : @headers).without(REQUEST_TARGET)
  65. end
  66. class << self
  67. def valid_url?(url)
  68. begin
  69. parsed_url = Addressable::URI.parse(url)
  70. rescue Addressable::URI::InvalidURIError
  71. return false
  72. end
  73. %w(http https).include?(parsed_url.scheme) && parsed_url.host.present?
  74. end
  75. def http_client
  76. HTTP.use(:auto_inflate).timeout(TIMEOUT.dup).follow(max_hops: 2)
  77. end
  78. end
  79. private
  80. def set_common_headers!
  81. @headers[REQUEST_TARGET] = "#{@verb} #{@url.path}"
  82. @headers['User-Agent'] = Mastodon::Version.user_agent
  83. @headers['Host'] = @url.host
  84. @headers['Date'] = Time.now.utc.httpdate
  85. @headers['Accept-Encoding'] = 'gzip' if @verb != :head
  86. end
  87. def set_digest!
  88. @headers['Digest'] = "SHA-256=#{Digest::SHA256.base64digest(@options[:body])}"
  89. end
  90. def signature
  91. algorithm = 'rsa-sha256'
  92. signature = Base64.strict_encode64(@keypair.sign(OpenSSL::Digest::SHA256.new, signed_string))
  93. "keyId=\"#{key_id}\",algorithm=\"#{algorithm}\",headers=\"#{signed_headers.keys.join(' ').downcase}\",signature=\"#{signature}\""
  94. end
  95. def signed_string
  96. signed_headers.map { |key, value| "#{key.downcase}: #{value}" }.join("\n")
  97. end
  98. def signed_headers
  99. @headers.without('User-Agent', 'Accept-Encoding')
  100. end
  101. def key_id
  102. case @key_id_format
  103. when :acct
  104. @account.to_webfinger_s
  105. when :uri
  106. [ActivityPub::TagManager.instance.uri_for(@account), '#main-key'].join
  107. end
  108. end
  109. def http_client
  110. @http_client ||= Request.http_client
  111. end
  112. def use_proxy?
  113. Rails.configuration.x.http_client_proxy.present?
  114. end
  115. def block_hidden_service?
  116. !Rails.configuration.x.access_to_hidden_service && /\.(onion|i2p)$/.match(@url.host)
  117. end
  118. module ClientLimit
  119. def body_with_limit(limit = 1.megabyte)
  120. raise Mastodon::LengthValidationError if content_length.present? && content_length > limit
  121. if charset.nil?
  122. encoding = Encoding::BINARY
  123. else
  124. begin
  125. encoding = Encoding.find(charset)
  126. rescue ArgumentError
  127. encoding = Encoding::BINARY
  128. end
  129. end
  130. contents = String.new(encoding: encoding)
  131. while (chunk = readpartial)
  132. contents << chunk
  133. chunk.clear
  134. raise Mastodon::LengthValidationError if contents.bytesize > limit
  135. end
  136. contents
  137. end
  138. end
  139. class Socket < TCPSocket
  140. class << self
  141. def open(host, *args)
  142. outer_e = nil
  143. port = args.first
  144. addresses = []
  145. begin
  146. addresses = [IPAddr.new(host)]
  147. rescue IPAddr::InvalidAddressError
  148. Resolv::DNS.open do |dns|
  149. dns.timeouts = 5
  150. addresses = dns.getaddresses(host).take(2)
  151. end
  152. end
  153. socks = []
  154. addr_by_socket = {}
  155. addresses.each do |address|
  156. begin
  157. check_private_address(address)
  158. sock = ::Socket.new(address.is_a?(Resolv::IPv6) ? ::Socket::AF_INET6 : ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0)
  159. sockaddr = ::Socket.pack_sockaddr_in(port, address.to_s)
  160. sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
  161. sock.connect_nonblock(sockaddr)
  162. # If that hasn't raised an exception, we somehow managed to connect
  163. # immediately, close pending sockets and return immediately
  164. socks.each(&:close)
  165. return sock
  166. rescue IO::WaitWritable
  167. socks << sock
  168. addr_by_socket[sock] = sockaddr
  169. rescue => e
  170. outer_e = e
  171. end
  172. end
  173. until socks.empty?
  174. _, available_socks, = IO.select(nil, socks, nil, Request::TIMEOUT[:connect])
  175. if available_socks.nil?
  176. socks.each(&:close)
  177. raise HTTP::TimeoutError, "Connect timed out after #{Request::TIMEOUT[:connect]} seconds"
  178. end
  179. available_socks.each do |sock|
  180. socks.delete(sock)
  181. begin
  182. sock.connect_nonblock(addr_by_socket[sock])
  183. rescue Errno::EISCONN
  184. rescue => e
  185. sock.close
  186. outer_e = e
  187. next
  188. end
  189. socks.each(&:close)
  190. return sock
  191. end
  192. end
  193. if outer_e
  194. raise outer_e
  195. else
  196. raise SocketError, "No address for #{host}"
  197. end
  198. end
  199. alias new open
  200. def check_private_address(address)
  201. raise Mastodon::HostValidationError if PrivateAddressCheck.private_address?(IPAddr.new(address.to_s))
  202. end
  203. end
  204. end
  205. class ProxySocket < Socket
  206. class << self
  207. def check_private_address(_address)
  208. # Accept connections to private addresses as HTTP proxies will usually
  209. # be on local addresses
  210. nil
  211. end
  212. end
  213. end
  214. private_constant :ClientLimit, :Socket, :ProxySocket
  215. end