diff options
author | Sam Aarons <samaarons@gmail.com> | 2014-08-20 15:42:32 -0700 |
---|---|---|
committer | Sam Aarons <samaarons@gmail.com> | 2014-08-21 18:38:08 -0700 |
commit | f9a84bb2361d7ec2147ca9e104f66ceb159a4039 (patch) | |
tree | 3bee67962e0b841dd415a149b765e49b7bcf7ac5 /railties | |
parent | a57f7b57976c74dd76c911f867b76197119546e6 (diff) | |
download | rails-f9a84bb2361d7ec2147ca9e104f66ceb159a4039.tar.gz rails-f9a84bb2361d7ec2147ca9e104f66ceb159a4039.tar.bz2 rails-f9a84bb2361d7ec2147ca9e104f66ceb159a4039.zip |
Refactor ActionDispatch::RemoteIp
Refactored IP address checking in ActionDispatch::RemoteIp to rely on
the IPAddr class instead of the unwieldly regular expression to match
IP addresses. This commit keeps the same api but allows users to pass
IPAddr objects to config.action_dispatch.trusted_proxies in addition
to passing strings and regular expressions.
Example:
# config/environments/production.rb
config.action_dispatch.trusted_proxies = IPAddr.new('4.8.15.0/16')
Diffstat (limited to 'railties')
-rw-r--r-- | railties/test/application/middleware/remote_ip_test.rb | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/railties/test/application/middleware/remote_ip_test.rb b/railties/test/application/middleware/remote_ip_test.rb index 946b82eeb3..97d5b5c698 100644 --- a/railties/test/application/middleware/remote_ip_test.rb +++ b/railties/test/application/middleware/remote_ip_test.rb @@ -1,3 +1,4 @@ +require 'ipaddr' require 'isolation/abstract_unit' require 'active_support/key_generator' @@ -53,12 +54,25 @@ module ApplicationTests end end + test "remote_ip works with HTTP_X_FORWARDED_FOR" do + make_basic_app + assert_equal "4.2.42.42", remote_ip("REMOTE_ADDR" => "1.1.1.1", "HTTP_X_FORWARDED_FOR" => "4.2.42.42") + end + test "the user can set trusted proxies" do make_basic_app do |app| app.config.action_dispatch.trusted_proxies = /^4\.2\.42\.42$/ end - assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "4.2.42.42,1.1.1.1") + assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "1.1.1.1", "HTTP_X_FORWARDED_FOR" => "4.2.42.42") + end + + test "the user can set trusted proxies with an IPAddr argument" do + make_basic_app do |app| + app.config.action_dispatch.trusted_proxies = IPAddr.new('4.2.42.0/24') + end + + assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "1.1.1.1", "HTTP_X_FORWARDED_FOR" => "10.0.0.0,4.2.42.42") end end end |