Skip to content
This repository
Browse code

GetIp#to_s should never return nil. That's icky.

  • Loading branch information...
commit d743954792ccf5975a11ee88cdd690e8f1915728 1 parent c7ab43f
André Arko indirect authored
5 actionpack/lib/action_dispatch/http/request.rb
@@ -155,10 +155,9 @@ def ip
155 155 @ip ||= super
156 156 end
157 157
158   - # Originating IP address, usually set by the RemoteIp middleware.
  158 + # Originating IP address from the RemoteIp middleware.
159 159 def remote_ip
160   - # Coerce the remote_ip object into a string, because to_s could return nil
161   - @remote_ip ||= @env["action_dispatch.remote_ip"].to_s || ip
  160 + @remote_ip ||= @env["action_dispatch.remote_ip"]
162 161 end
163 162
164 163 # Returns the unique request id, which is based off either the X-Request-Id header that can
9 actionpack/lib/action_dispatch/middleware/remote_ip.rb
@@ -55,7 +55,10 @@ def calculate_ip
55 55 "HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect}"
56 56 end
57 57
58   - client_ip || forwarded_ips.last || remote_addrs.first
  58 + not_proxy = client_ip || forwarded_ips.last || remote_addrs.first
  59 +
  60 + # Return first REMOTE_ADDR if there are no other options
  61 + not_proxy || ips_from('REMOTE_ADDR', :all).first
59 62 end
60 63
61 64 def to_s
@@ -66,9 +69,9 @@ def to_s
66 69
67 70 protected
68 71
69   - def ips_from(header)
  72 + def ips_from(header, allow_proxies = false)
70 73 ips = @env[header] ? @env[header].strip.split(/[,\s]+/) : []
71   - ips.reject{|ip| ip =~ @middleware.proxies }
  74 + allow_proxies ? ips : ips.reject{|ip| ip =~ @middleware.proxies }
72 75 end
73 76 end
74 77

0 comments on commit d743954

Please sign in to comment.
Something went wrong with that request. Please try again.