Type:
Class
Constants:
TRUSTED_PROXIES : %r{ ^127\.0\.0\.1$ | # localhost IPv4 ^::1$ | # localhost IPv6 ^fc00: | # private IPv6 range fc00 ^10\. | # private IPv4 range 10.x.x.x ^172\.(1[6-9]|2[0-9]|3[0-1])\.| # private IPv4 range 172.16.0.0 .. 172.31.255.255 ^192\.168\. # private IPv4 range 192.168.x.x }x

The default trusted IPs list simply includes IP addresses that are guaranteed by the IP specification to be private addresses. Those will not be the ultimate client IP in production, and so are discarded. See en.wikipedia.org/wiki/Private_network for details.

This middleware calculates the IP address of the remote client that is making the request. It does this by checking various headers that could contain the address, and then picking the last-set address that is not on the list of trusted IPs. This follows the precedent set by e.g. the Tomcat server, with reasoning explained at length by @gingerlime. A more detailed explanation of the algorithm is given at ActionDispatch::RemoteIp::GetIp#calculate_ip.

Some Rack servers concatenate repeated headers, like HTTP RFC 2616 requires. Some Rack servers simply drop preceding headers, and only report the value that was given in the last header. If you are behind multiple proxy servers (like Nginx to HAProxy to Unicorn) then you should test your Rack server to make sure your data is good.

IF YOU DON'T USE A PROXY, THIS MAKES YOU VULNERABLE TO IP SPOOFING. This middleware assumes that there is at least one proxy sitting around and setting headers with the client's remote IP address. If you don't use a proxy, because you are hosted on e.g. Heroku without SSL, any client can claim to have any IP address by setting the X-Forwarded-For header. If you care about that, then you need to explicitly drop or ignore those headers sometime before this middleware runs.

call

call(env) Instance Public methods Since the IP address may not be needed, we

2015-06-20 00:00:00
new

new(app, check_ip_spoofing = true, custom_proxies = nil) Class Public methods Create

2015-06-20 00:00:00