Browse files

Don't use 'unknown' in HTTP_X_FORWARDED_FOR

  • Loading branch information...
1 parent b433e48 commit fa324e371df263563af2943d092033ec81a5ab24 @manveru manveru committed Jun 9, 2010
Showing with 7 additions and 1 deletion.
  1. +1 −1 lib/rack/request.rb
  2. +6 −0 test/spec_rack_request.rb
View
2 lib/rack/request.rb
@@ -253,7 +253,7 @@ def accept_encoding
def ip
if addr = @env['HTTP_X_FORWARDED_FOR']
- addr.split(',').first.strip
+ (addr.split(',').grep(/\d\./).first || @env['REMOTE_ADDR']).to_s.strip
else
@env['REMOTE_ADDR']
end
View
6 test/spec_rack_request.rb
@@ -512,6 +512,12 @@
'HTTP_X_FORWARDED_FOR' => '234.234.234.234,212.212.212.212'
res.body.should.equal '234.234.234.234'
+
+ res = mock.get '/',
+ 'REMOTE_ADDR' => '123.123.123.123',
+ 'HTTP_X_FORWARDED_FOR' => 'unknown,234.234.234.234,212.212.212.212'
+
+ res.body.should.equal '234.234.234.234'
end
class MyRequest < Rack::Request

0 comments on commit fa324e3

Please sign in to comment.