机架攻击阻止列表不起作用



我在rails 5中使用gemrack-attack和gemgeoip。一切都设置得很好,但由于某种原因,我无法让阻止列表阻止国家代码。我设置了一个闪光警报,以确保通过了正确的国家代码,并且是("CA"(。"CA"也在阻止列表中,但我的应用程序并没有阻止我访问该网站。

我哪里错了?

def get_ip
@ip = request.ip
@country = GeoIp.geolocation("#{@ip}", :precision => :country)
country_code = @country[:country_code]

Rack::Attack.safelist('allow from localhost and select countries') do |req|
'127.0.0.1' || '::1' == req.ip || 'US' == req.country_code 
end
Rack::Attack.blocklist('block specific countries') do |req|
"CA" == req.country_code
end
end

所有东西都在你的安全清单上。看到这个街区了吗?

Rack::Attack.safelist('allow from localhost and select countries') do |req|
'127.0.0.1' || '::1' == req.ip || 'US' == req.country_code 
flash[:error] = "#{req.country_code}"
end

最后一条可执行语句是flash[:error]的赋值,它返回字符串"#{req.country_code}",任何字符串都是"truthy",因此块返回true。

更改语句的顺序,以便最后一个语句返回正确的true/false值。

Rack::Attack.safelist('allow from localhost and select countries') do |req|
flash[:error] = "#{req.country_code}"
'127.0.0.1' || '::1' == req.ip || 'US' == req.country_code 
end

你的阻止列表块也有同样的问题。。。确保实际测试是最后一个执行的语句,否则您将始终为true。

编辑

但我刚刚注意到你的保险箱里还有所有的东西。。。"127.0.0.1"是一个字符串,字符串总是真实的,所以在…中

'127.0.0.1' || ... 

由于字符串为true,因此永远不会对其余部分求值。

也许你想要这个。。。

Rack::Attack.safelist('allow from localhost and select countries') do |req|
flash[:error] = "#{req.country_code}"
'127.0.0.1' == req.ip || '::1' == req.ip || 'US' == req.country_code 
end

最新更新