Skip to content

Commit

Permalink
Make Request#remote_ip return nil when HTTP_X_FORWARDED_FOR is empty
Browse files Browse the repository at this point in the history
If HTTP_X_FORWARDED_FOR only contains whitespace, don't try to extract a
list of IP addresses from it.
  • Loading branch information
dasch committed Dec 27, 2011
1 parent e0774e4 commit cd2136a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/request.rb
Expand Up @@ -225,7 +225,7 @@ def remote_ip
not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES} not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES}
return not_trusted_addrs.first unless not_trusted_addrs.empty? return not_trusted_addrs.first unless not_trusted_addrs.empty?
end end
remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',') remote_ips = @env['HTTP_X_FORWARDED_FOR'].present? && @env['HTTP_X_FORWARDED_FOR'].split(',')


if @env.include? 'HTTP_CLIENT_IP' if @env.include? 'HTTP_CLIENT_IP'
if ActionController::Base.ip_spoofing_check && remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP']) if ActionController::Base.ip_spoofing_check && remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP'])
Expand Down
3 changes: 3 additions & 0 deletions actionpack/test/controller/request_test.rb
Expand Up @@ -20,6 +20,9 @@ def test_remote_ip
'HTTP_X_FORWARDED_FOR' => '3.4.5.6' 'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
assert_equal '1.2.3.4', request.remote_ip assert_equal '1.2.3.4', request.remote_ip


request = stub_request 'HTTP_X_FORWARDED_FOR' => ''
assert_nil request.remote_ip

request = stub_request 'REMOTE_ADDR' => '127.0.0.1', request = stub_request 'REMOTE_ADDR' => '127.0.0.1',
'HTTP_X_FORWARDED_FOR' => '3.4.5.6' 'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
assert_equal '3.4.5.6', request.remote_ip assert_equal '3.4.5.6', request.remote_ip
Expand Down

0 comments on commit cd2136a

Please sign in to comment.