You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.9 KiB

  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
  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(:per_operation, 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. addresses.each do |address|
  154. begin
  155. check_private_address(address)
  156. sock = ::Socket.new(address.is_a?(Resolv::IPv6) ? ::Socket::AF_INET6 : ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0)
  157. sockaddr = ::Socket.pack_sockaddr_in(port, address.to_s)
  158. sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
  159. begin
  160. sock.connect_nonblock(sockaddr)
  161. rescue IO::WaitWritable
  162. if IO.select(nil, [sock], nil, Request::TIMEOUT[:connect])
  163. begin
  164. sock.connect_nonblock(sockaddr)
  165. rescue Errno::EISCONN
  166. # Yippee!
  167. rescue
  168. sock.close
  169. raise
  170. end
  171. else
  172. sock.close
  173. raise HTTP::TimeoutError, "Connect timed out after #{Request::TIMEOUT[:connect]} seconds"
  174. end
  175. end
  176. return sock
  177. rescue => e
  178. outer_e = e
  179. end
  180. end
  181. if outer_e
  182. raise outer_e
  183. else
  184. raise SocketError, "No address for #{host}"
  185. end
  186. end
  187. alias new open
  188. def check_private_address(address)
  189. raise Mastodon::HostValidationError if PrivateAddressCheck.private_address?(IPAddr.new(address.to_s))
  190. end
  191. end
  192. end
  193. class ProxySocket < Socket
  194. class << self
  195. def check_private_address(_address)
  196. # Accept connections to private addresses as HTTP proxies will usually
  197. # be on local addresses
  198. nil
  199. end
  200. end
  201. end
  202. private_constant :ClientLimit, :Socket, :ProxySocket
  203. end