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.

133 lines
4.1 KiB

  1. # frozen_string_literal: true
  2. # Implemented according to HTTP signatures (Draft 6)
  3. # <https://tools.ietf.org/html/draft-cavage-http-signatures-06>
  4. module SignatureVerification
  5. extend ActiveSupport::Concern
  6. def signed_request?
  7. request.headers['Signature'].present?
  8. end
  9. def signature_verification_failure_reason
  10. return @signature_verification_failure_reason if defined?(@signature_verification_failure_reason)
  11. end
  12. def signed_request_account
  13. return @signed_request_account if defined?(@signed_request_account)
  14. unless signed_request?
  15. @signature_verification_failure_reason = 'Request not signed'
  16. @signed_request_account = nil
  17. return
  18. end
  19. if request.headers['Date'].present? && !matches_time_window?
  20. @signature_verification_failure_reason = 'Signed request date outside acceptable time window'
  21. @signed_request_account = nil
  22. return
  23. end
  24. raw_signature = request.headers['Signature']
  25. signature_params = {}
  26. raw_signature.split(',').each do |part|
  27. parsed_parts = part.match(/([a-z]+)="([^"]+)"/i)
  28. next if parsed_parts.nil? || parsed_parts.size != 3
  29. signature_params[parsed_parts[1]] = parsed_parts[2]
  30. end
  31. if incompatible_signature?(signature_params)
  32. @signature_verification_failure_reason = 'Incompatible request signature'
  33. @signed_request_account = nil
  34. return
  35. end
  36. account_stoplight = Stoplight("source:#{request.ip}") { account_from_key_id(signature_params['keyId']) }
  37. .with_fallback { nil }
  38. .with_threshold(1)
  39. .with_cool_off_time(5.minutes.seconds)
  40. account = account_stoplight.run
  41. if account.nil?
  42. @signature_verification_failure_reason = "Public key not found for key #{signature_params['keyId']}"
  43. @signed_request_account = nil
  44. return
  45. end
  46. signature = Base64.decode64(signature_params['signature'])
  47. compare_signed_string = build_signed_string(signature_params['headers'])
  48. if account.keypair.public_key.verify(OpenSSL::Digest::SHA256.new, signature, compare_signed_string)
  49. @signed_request_account = account
  50. @signed_request_account
  51. elsif account.possibly_stale?
  52. account = account.refresh!
  53. if account.keypair.public_key.verify(OpenSSL::Digest::SHA256.new, signature, compare_signed_string)
  54. @signed_request_account = account
  55. @signed_request_account
  56. else
  57. @signature_verification_failure_reason = "Verification failed for #{account.username}@#{account.domain} #{account.uri}"
  58. @signed_request_account = nil
  59. end
  60. else
  61. @signature_verification_failure_reason = "Verification failed for #{account.username}@#{account.domain} #{account.uri}"
  62. @signed_request_account = nil
  63. end
  64. end
  65. def request_body
  66. @request_body ||= request.raw_post
  67. end
  68. private
  69. def build_signed_string(signed_headers)
  70. signed_headers = 'date' if signed_headers.blank?
  71. signed_headers.downcase.split(' ').map do |signed_header|
  72. if signed_header == Request::REQUEST_TARGET
  73. "#{Request::REQUEST_TARGET}: #{request.method.downcase} #{request.path}"
  74. elsif signed_header == 'digest'
  75. "digest: #{body_digest}"
  76. else
  77. "#{signed_header}: #{request.headers[to_header_name(signed_header)]}"
  78. end
  79. end.join("\n")
  80. end
  81. def matches_time_window?
  82. begin
  83. time_sent = Time.httpdate(request.headers['Date'])
  84. rescue ArgumentError
  85. return false
  86. end
  87. (Time.now.utc - time_sent).abs <= 12.hours
  88. end
  89. def body_digest
  90. "SHA-256=#{Digest::SHA256.base64digest(request_body)}"
  91. end
  92. def to_header_name(name)
  93. name.split(/-/).map(&:capitalize).join('-')
  94. end
  95. def incompatible_signature?(signature_params)
  96. signature_params['keyId'].blank? ||
  97. signature_params['signature'].blank?
  98. end
  99. def account_from_key_id(key_id)
  100. if key_id.start_with?('acct:')
  101. ResolveAccountService.new.call(key_id.gsub(/\Aacct:/, ''))
  102. elsif !ActivityPub::TagManager.instance.local_uri?(key_id)
  103. account = ActivityPub::TagManager.instance.uri_to_resource(key_id, Account)
  104. account ||= ActivityPub::FetchRemoteKeyService.new.call(key_id, id: false)
  105. account
  106. end
  107. end
  108. end