我正在我的 Rails 应用程序中实现 Kickstarter 的 Rack-attack。
白名单/黑名单过滤工作正常,但我在使用 Allow2Ban 锁定影响我的登录(设计)页面的 IP 地址时遇到问题。注意:我在本地测试这个并且已经从白名单中删除了 localhost。
# Lockout IP addresses that are hammering your login page.
# After 3 requests in 1 minute, block all requests from that IP for 1 hour.
Rack::Attack.blacklist('allow2ban login scrapers') do |req|
# `filter` returns false value if request is to your login page (but still
# increments the count) so request below the limit are not blocked until
# they hit the limit. At that point, filter will return true and block.
Rack::Attack::Allow2Ban.filter(req.ip, :maxretry => 3, :findtime => 1.minute, :bantime => 1.hour) do
# The count for the IP is incremented if the return value is truthy.
req.path == '/sign_in' and req.post?
end
end
在Rack-attack文档中,明确指出了节流功能需要缓存,即:
Rack::Attack.throttle('req/ip', :limit => 5, :period => 1.second) do |req| )
,但它没有为 Allow2Ban 说明这一点。任何人都知道 Allow2Ban 是否需要缓存,或者我是否在设计登录页面上使用上面的代码错误地实现了
最佳答案
是的,Allow2Ban 和 Fail2Ban 绝对需要检查(在 https://github.com/kickstarter/rack-attack/blob/master/lib/rack/attack/fail2ban.rb 中您可以看到如何以及为什么)。 顺便提一句。我建议使用 Redis 作为缓存,因为即使您使用多个应用程序节点,它也能确保您的应用程序阻止一个 IP 地址。如果您在多应用程序节点场景中使用 Rails 缓存,您的过滤器将按实例进行管理,这不是我所假设的。
关于ruby-on-rails-4 - rails 4 中的机架攻击和 Allow2Ban 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22721561/