rack_attack.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # frozen_string_literal: true
  2. require 'doorkeeper/grape/authorization_decorator'
  3. class Rack::Attack
  4. class Request
  5. def authenticated_token
  6. return @token if defined?(@token)
  7. @token = Doorkeeper::OAuth::Token.authenticate(
  8. Doorkeeper::Grape::AuthorizationDecorator.new(self),
  9. *Doorkeeper.configuration.access_token_methods
  10. )
  11. end
  12. def remote_ip
  13. @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s
  14. end
  15. def throttleable_remote_ip
  16. @throttleable_remote_ip ||= begin
  17. ip = IPAddr.new(remote_ip)
  18. if ip.ipv6?
  19. ip.mask(64)
  20. else
  21. ip
  22. end
  23. end.to_s
  24. end
  25. def authenticated_user_id
  26. authenticated_token&.resource_owner_id
  27. end
  28. def unauthenticated?
  29. !authenticated_user_id
  30. end
  31. def api_request?
  32. path.start_with?('/api')
  33. end
  34. def path_matches?(other_path)
  35. /\A#{Regexp.escape(other_path)}(\..*)?\z/ =~ path
  36. end
  37. def web_request?
  38. !api_request?
  39. end
  40. def paging_request?
  41. params['page'].present? || params['min_id'].present? || params['max_id'].present? || params['since_id'].present?
  42. end
  43. end
  44. Rack::Attack.safelist('allow from localhost') do |req|
  45. req.remote_ip == '127.0.0.1' || req.remote_ip == '::1'
  46. end
  47. Rack::Attack.blocklist('deny from blocklist') do |req|
  48. IpBlock.blocked?(req.remote_ip)
  49. end
  50. throttle('throttle_authenticated_api', limit: 300, period: 5.minutes) do |req|
  51. req.authenticated_user_id if req.api_request?
  52. end
  53. throttle('throttle_unauthenticated_api', limit: 300, period: 5.minutes) do |req|
  54. req.throttleable_remote_ip if req.api_request? && req.unauthenticated?
  55. end
  56. throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req|
  57. req.authenticated_user_id if req.post? && req.path.match?(/\A\/api\/v\d+\/media\z/i)
  58. end
  59. throttle('throttle_media_proxy', limit: 30, period: 10.minutes) do |req|
  60. req.throttleable_remote_ip if req.path.start_with?('/media_proxy')
  61. end
  62. throttle('throttle_api_sign_up', limit: 5, period: 30.minutes) do |req|
  63. req.throttleable_remote_ip if req.post? && req.path == '/api/v1/accounts'
  64. end
  65. throttle('throttle_authenticated_paging', limit: 300, period: 15.minutes) do |req|
  66. req.authenticated_user_id if req.paging_request?
  67. end
  68. throttle('throttle_unauthenticated_paging', limit: 300, period: 15.minutes) do |req|
  69. req.throttleable_remote_ip if req.paging_request? && req.unauthenticated?
  70. end
  71. API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog\z/.freeze
  72. API_DELETE_STATUS_REGEX = /\A\/api\/v1\/statuses\/[\d]+\z/.freeze
  73. throttle('throttle_api_delete', limit: 30, period: 30.minutes) do |req|
  74. req.authenticated_user_id if (req.post? && req.path.match?(API_DELETE_REBLOG_REGEX)) || (req.delete? && req.path.match?(API_DELETE_STATUS_REGEX))
  75. end
  76. throttle('throttle_sign_up_attempts/ip', limit: 25, period: 5.minutes) do |req|
  77. req.throttleable_remote_ip if req.post? && req.path_matches?('/auth')
  78. end
  79. throttle('throttle_password_resets/ip', limit: 25, period: 5.minutes) do |req|
  80. req.throttleable_remote_ip if req.post? && req.path_matches?('/auth/password')
  81. end
  82. throttle('throttle_password_resets/email', limit: 5, period: 30.minutes) do |req|
  83. req.params.dig('user', 'email').presence if req.post? && req.path_matches?('/auth/password')
  84. end
  85. throttle('throttle_email_confirmations/ip', limit: 25, period: 5.minutes) do |req|
  86. req.throttleable_remote_ip if req.post? && (req.path_matches?('/auth/confirmation') || req.path == '/api/v1/emails/confirmations')
  87. end
  88. throttle('throttle_email_confirmations/email', limit: 5, period: 30.minutes) do |req|
  89. if req.post? && req.path_matches?('/auth/password')
  90. req.params.dig('user', 'email').presence
  91. elsif req.post? && req.path == '/api/v1/emails/confirmations'
  92. req.authenticated_user_id
  93. end
  94. end
  95. throttle('throttle_login_attempts/ip', limit: 25, period: 5.minutes) do |req|
  96. req.throttleable_remote_ip if req.post? && req.path_matches?('/auth/sign_in')
  97. end
  98. throttle('throttle_login_attempts/email', limit: 25, period: 1.hour) do |req|
  99. req.session[:attempt_user_id] || req.params.dig('user', 'email').presence if req.post? && req.path_matches?('/auth/sign_in')
  100. end
  101. self.throttled_responder = lambda do |request|
  102. now = Time.now.utc
  103. match_data = request.env['rack.attack.match_data']
  104. headers = {
  105. 'Content-Type' => 'application/json',
  106. 'X-RateLimit-Limit' => match_data[:limit].to_s,
  107. 'X-RateLimit-Remaining' => '0',
  108. 'X-RateLimit-Reset' => (now + (match_data[:period] - now.to_i % match_data[:period])).iso8601(6),
  109. }
  110. [429, headers, [{ error: I18n.t('errors.429') }.to_json]]
  111. end
  112. end