Skip to content

Commit

Permalink
Merge pull request rails#254 from wjordan/rescue_ip_spoof
Browse files Browse the repository at this point in the history
Rescue ActionDispatch::RemoteIp::IpSpoofAttackError
  • Loading branch information
gsamokovarov authored Mar 30, 2018
2 parents d858b05 + 456ae92 commit 266b121
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/web_console/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def from_whitelisted_ip?
# Determines the remote IP using our much stricter whitelist.
def strict_remote_ip
GetSecureIp.new(self, whitelisted_ips).to_s
rescue ActionDispatch::RemoteIp::IpSpoofAttackError
'[Spoofed]'
end

# Returns whether the request is acceptable.
Expand Down
6 changes: 6 additions & 0 deletions test/web_console/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class RequestTest < ActiveSupport::TestCase
assert_not req.from_whitelisted_ip?
end

test '#from_whitelisted_ip? is falsy for spoofed IPs' do
req = request('http://example.com', 'HTTP_CLIENT_IP' => '127.0.0.1', 'HTTP_X_FORWARDED_FOR' => '127.0.0.0')

assert_not req.from_whitelisted_ip?
end

test '#acceptable? is truthy for current version' do
req = xhr('http://example.com', 'HTTP_ACCEPT' => "#{Mime[:web_console_v2]}")

Expand Down
7 changes: 7 additions & 0 deletions test/web_console/whiny_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class WhinyRequestTest < ActiveSupport::TestCase
assert_not req.from_whitelisted_ip?
end

test '#from_whitelisted_ip? is falsy for spoofed IPs' do
WebConsole.logger.expects(:info)
req = request('http://example.com', 'HTTP_CLIENT_IP' => '127.0.0.1', 'HTTP_X_FORWARDED_FOR' => '127.0.0.0')

assert_not req.from_whitelisted_ip?
end

private

def request(*args)
Expand Down

0 comments on commit 266b121

Please sign in to comment.