rack_attack.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. #
  3. # Throttle password reset requests
  4. #
  5. API_V1_USERS__PASSWORD_RESET_PATH = '/api/v1/users/password_reset'.freeze
  6. Rack::Attack.throttle('limit password reset requests per username', limit: 3, period: 1.minute.to_i) do |req|
  7. if req.path == API_V1_USERS__PASSWORD_RESET_PATH && req.post?
  8. # Normalize to protect against rate limit bypasses.
  9. req.params['username'].to_s.downcase.gsub(%r{\s+}, '')
  10. end
  11. end
  12. Rack::Attack.throttle('limit password reset requests per source IP address', limit: 3, period: 1.minute.to_i) do |req|
  13. if req.path == API_V1_USERS__PASSWORD_RESET_PATH && req.post?
  14. req.ip
  15. end
  16. end
  17. #
  18. # Throttle form submit requests
  19. #
  20. API_V1_FORM_SUBMIT_PATH = '/api/v1/form_submit'.freeze
  21. form_limit_by_ip_per_hour_proc = proc { Setting.get('form_ticket_create_by_ip_per_hour') || 20 }
  22. Rack::Attack.throttle('form submits per IP and hour', limit: form_limit_by_ip_per_hour_proc, period: 1.hour.to_i) do |req|
  23. if req.path == API_V1_FORM_SUBMIT_PATH
  24. req.ip
  25. end
  26. end
  27. form_limit_by_ip_per_day_proc = proc { Setting.get('form_ticket_create_by_ip_per_day') || 240 }
  28. Rack::Attack.throttle('form submits per IP and day', limit: form_limit_by_ip_per_day_proc, period: 1.day.to_i) do |req|
  29. if req.path == API_V1_FORM_SUBMIT_PATH
  30. req.ip
  31. end
  32. end
  33. form_limit_per_day_proc = proc { Setting.get('form_ticket_create_per_day') || 5000 }
  34. Rack::Attack.throttle('form submits per day', limit: form_limit_per_day_proc, period: 1.day.to_i) do |req|
  35. if req.path == API_V1_FORM_SUBMIT_PATH
  36. req.path
  37. end
  38. end