diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-02-07 08:45:29 -0800 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-02-07 08:45:29 -0800 |
commit | 641359e54aa34291d9bf1361c4979080a471862d (patch) | |
tree | 14e9094539abe6c460f7d4ad3e443e313b91a4ce /actionpack | |
parent | c3a371ad7a06afdbb703f69cf039443922c6082b (diff) | |
parent | dd09811fa6214a130fdc2de1d4c00b4337cb15f9 (diff) | |
download | rails-641359e54aa34291d9bf1361c4979080a471862d.tar.gz rails-641359e54aa34291d9bf1361c4979080a471862d.tar.bz2 rails-641359e54aa34291d9bf1361c4979080a471862d.zip |
Merge pull request #2632 from gsterndale/replace_trusted_proxies
Trusted proxies are configurable
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/remote_ip.rb | 10 | ||||
-rw-r--r-- | actionpack/test/dispatch/request_test.rb | 15 |
2 files changed, 19 insertions, 6 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/remote_ip.rb b/actionpack/lib/action_dispatch/middleware/remote_ip.rb index 030ccb2017..70dc2ff531 100644 --- a/actionpack/lib/action_dispatch/middleware/remote_ip.rb +++ b/actionpack/lib/action_dispatch/middleware/remote_ip.rb @@ -18,11 +18,13 @@ module ActionDispatch def initialize(app, check_ip_spoofing = true, custom_proxies = nil) @app = app @check_ip = check_ip_spoofing - if custom_proxies - custom_regexp = Regexp.new(custom_proxies) - @proxies = Regexp.union(TRUSTED_PROXIES, custom_regexp) + @proxies = case custom_proxies + when Regexp + custom_proxies + when nil + TRUSTED_PROXIES else - @proxies = TRUSTED_PROXIES + Regexp.union(TRUSTED_PROXIES, custom_proxies) end end diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index 5b3d38c48c..1b4b00ea9e 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -94,8 +94,8 @@ class RequestTest < ActiveSupport::TestCase assert_equal '127.0.0.1', request.remote_ip end - test "remote ip with user specified trusted proxies" do - @trusted_proxies = /^67\.205\.106\.73$/i + test "remote ip with user specified trusted proxies String" do + @trusted_proxies = "67.205.106.73" request = stub_request 'REMOTE_ADDR' => '67.205.106.73', 'HTTP_X_FORWARDED_FOR' => '3.4.5.6' @@ -120,6 +120,17 @@ class RequestTest < ActiveSupport::TestCase assert_equal '3.4.5.6', request.remote_ip end + test "remote ip with user specified trusted proxies Regexp" do + @trusted_proxies = /^67\.205\.106\.73$/i + + request = stub_request 'REMOTE_ADDR' => '67.205.106.73', + 'HTTP_X_FORWARDED_FOR' => '3.4.5.6' + assert_equal '3.4.5.6', request.remote_ip + + request = stub_request 'HTTP_X_FORWARDED_FOR' => '9.9.9.9, 3.4.5.6, 10.0.0.1, 67.205.106.73' + assert_equal '10.0.0.1', request.remote_ip + end + test "domains" do request = stub_request 'HTTP_HOST' => 'www.rubyonrails.org' assert_equal "rubyonrails.org", request.domain |