Browse files

Making the IP Spoofing check in AbstractRequest#remote_ip configurable.

Certain groups of web proxies do not set these values properly.  Notably,
proxies for cell phones, which often do not set the remote IP information
correctly (not surprisingly, since the clients do not have an IP address).

Allowing this to be configurable makes it possible for developers to choose
to ignore this simple spoofing check, when a significant amount of their
traffic would result in false positives anyway.

Signed-off-by: Michael Koziarski <>

[#1200 state:committed]
  • Loading branch information...
Darren Boyd authored and NZKoz committed Nov 22, 2008
1 parent 97403ad commit 0a4a5f3129a137fc357e8444a08b135f0ad4fbe8
@@ -1,5 +1,7 @@
*2.3.0 [Edge]*
* Allow users to opt out of the spoofing checks in Request#remote_ip. Useful for sites whose traffic regularly triggers false positives. [Darren Boyd]
* Deprecated formatted_polymorphic_url. [Jeremy Kemper]
* Added the option to declare an asset_host as an object that responds to call (see for an example) [DHH]
@@ -327,6 +327,10 @@ class Base
# sets it to <tt>:authenticity_token</tt> by default.
cattr_accessor :request_forgery_protection_token
# Controls the IP Spoofing check when determining the remote IP.
@@ip_spoofing_check = true
cattr_accessor :ip_spoofing_check
# Indicates whether or not optimise the generated named
# route helper methods
cattr_accessor :optimise_named_routes
@@ -218,7 +218,7 @@ def remote_ip
remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',')
if @env.include? 'HTTP_CLIENT_IP'
if remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP'])
if ActionController::Base.ip_spoofing_check && remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP'])
# We don't know which came from the proxy, and which from the user
IP spoofing attack?!
@@ -66,6 +66,15 @@ def test_remote_ip
assert_match /HTTP_X_FORWARDED_FOR=",,,"/, e.message
assert_match /HTTP_CLIENT_IP=""/, e.message
# turn IP Spoofing detection off.
# This is useful for sites that are aimed at non-IP clients. The typical
# example is WAP. Since the cellular network is not IP based, it's a
# leap of faith to assume that their proxies are ever going to set the
ActionController::Base.ip_spoofing_check = false
assert_equal('', @request.remote_ip(true))
ActionController::Base.ip_spoofing_check = true
@request.env['HTTP_X_FORWARDED_FOR'] = ','
assert_equal '', @request.remote_ip(true)

1 comment on commit 0a4a5f3


This comment has been minimized.

Show comment
Hide comment

yob Dec 2, 2008


yob commented on 0a4a5f3 Dec 2, 2008


Please sign in to comment.