rack_attack.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 authenticated_user_id
  16. authenticated_token&.resource_owner_id
  17. end
  18. def unauthenticated?
  19. !authenticated_user_id
  20. end
  21. def api_request?
  22. path.start_with?('/api')
  23. end
  24. def web_request?
  25. !api_request?
  26. end
  27. def paging_request?
  28. params['page'].present? || params['min_id'].present? || params['max_id'].present? || params['since_id'].present?
  29. end
  30. end
  31. Rack::Attack.safelist('allow from localhost') do |req|
  32. req.remote_ip == '127.0.0.1' || req.remote_ip == '::1'
  33. end
  34. Rack::Attack.blocklist('deny from blocklist') do |req|
  35. IpBlock.blocked?(req.remote_ip)
  36. end
  37. throttle('throttle_authenticated_api', limit: 300, period: 5.minutes) do |req|
  38. req.authenticated_user_id if req.api_request?
  39. end
  40. throttle('throttle_unauthenticated_api', limit: 300, period: 5.minutes) do |req|
  41. req.remote_ip if req.api_request? && req.unauthenticated?
  42. end
  43. throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req|
  44. req.authenticated_user_id if req.post? && req.path.match?('^/api/v\d+/media')
  45. end
  46. throttle('throttle_media_proxy', limit: 30, period: 10.minutes) do |req|
  47. req.remote_ip if req.path.start_with?('/media_proxy')
  48. end
  49. throttle('throttle_api_sign_up', limit: 5, period: 30.minutes) do |req|
  50. req.remote_ip if req.post? && req.path == '/api/v1/accounts'
  51. end
  52. throttle('throttle_authenticated_paging', limit: 300, period: 15.minutes) do |req|
  53. req.authenticated_user_id if req.paging_request?
  54. end
  55. throttle('throttle_unauthenticated_paging', limit: 300, period: 15.minutes) do |req|
  56. req.remote_ip if req.paging_request? && req.unauthenticated?
  57. end
  58. API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog/.freeze
  59. API_DELETE_STATUS_REGEX = /\A\/api\/v1\/statuses\/[\d]+/.freeze
  60. throttle('throttle_api_delete', limit: 30, period: 30.minutes) do |req|
  61. req.authenticated_user_id if (req.post? && req.path.match?(API_DELETE_REBLOG_REGEX)) || (req.delete? && req.path.match?(API_DELETE_STATUS_REGEX))
  62. end
  63. throttle('throttle_sign_up_attempts/ip', limit: 25, period: 5.minutes) do |req|
  64. if req.post? && req.path == '/auth'
  65. addr = req.remote_ip
  66. addr = IPAddr.new(addr) if addr.is_a?(String)
  67. addr = addr.mask(64) if addr.ipv6?
  68. addr.to_s
  69. end
  70. end
  71. throttle('throttle_password_resets/ip', limit: 25, period: 5.minutes) do |req|
  72. req.remote_ip if req.post? && req.path == '/auth/password'
  73. end
  74. throttle('throttle_password_resets/email', limit: 5, period: 30.minutes) do |req|
  75. req.params.dig('user', 'email').presence if req.post? && req.path == '/auth/password'
  76. end
  77. throttle('throttle_email_confirmations/ip', limit: 25, period: 5.minutes) do |req|
  78. req.remote_ip if req.post? && %w(/auth/confirmation /api/v1/emails/confirmations).include?(req.path)
  79. end
  80. throttle('throttle_email_confirmations/email', limit: 5, period: 30.minutes) do |req|
  81. if req.post? && req.path == '/auth/password'
  82. req.params.dig('user', 'email').presence
  83. elsif req.post? && req.path == '/api/v1/emails/confirmations'
  84. req.authenticated_user_id
  85. end
  86. end
  87. throttle('throttle_login_attempts/ip', limit: 25, period: 5.minutes) do |req|
  88. req.remote_ip if req.post? && req.path == '/auth/sign_in'
  89. end
  90. throttle('throttle_login_attempts/email', limit: 25, period: 1.hour) do |req|
  91. req.session[:attempt_user_id] || req.params.dig('user', 'email').presence if req.post? && req.path == '/auth/sign_in'
  92. end
  93. self.throttled_responder = lambda do |request|
  94. now = Time.now.utc
  95. match_data = request.env['rack.attack.match_data']
  96. headers = {
  97. 'Content-Type' => 'application/json',
  98. 'X-RateLimit-Limit' => match_data[:limit].to_s,
  99. 'X-RateLimit-Remaining' => '0',
  100. 'X-RateLimit-Reset' => (now + (match_data[:period] - now.to_i % match_data[:period])).iso8601(6),
  101. }
  102. [429, headers, [{ error: I18n.t('errors.429') }.to_json]]
  103. end
  104. end