Rack::Attack test case: can't change ENV variable value using blocklist

164 Views Asked by At

Could anyone share some blocklist test cases with an ENV variable as well, I found that in the spec file, we can't change the env variable in the rails middleware.

If we set the env variable in the spec file.

stub_const('ENV', 'RACK_ATTACK_BLOCK_IP_LIST' => '1.1.1.1')

in the application.yml file, there is a setting:

RACK_ATTACK_BLOCK_IP_LIST: '2.2.2.2'

If we run the test case and monitor env values in rake_attack.rb file,we can only get the new env variable value '1.1.1.1' in side safelist block, for example :

blocklist('block_ip_list') do |req|  
    block_ip_list = ENV['RACK_ATTACK_BLOCK_IP_LIST'].try(:split, /,\s*/) || []  
    block_ip_list.include?(req.ip)
end

if we move the safe_ip_list out of the safelist block, it will still '2.2.2.2'

block_ip_list = ENV['RACK_ATTACK_BLOCK_IP_LIST'].try(:split, /,\s*/) || []
blocklist('block_ip_list') do |req| 
   block_ip_list.include?(req.ip)
end
0

There are 0 best solutions below