1
0

request.rb 7.7 KB

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