Class: ActionDispatch::RemoteIp::GetIp
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Inherits: | Object |
| Defined in: | actionpack/lib/action_dispatch/middleware/remote_ip.rb |
Overview
The GetIp class exists as a way to defer processing of the request data into an actual IP address. If the ActionDispatch::Request#remote_ip method is called, this class will calculate the value and then memoize it.
Class Method Summary
- .new(req, check_ip, proxies) ⇒ GetIp constructor
Instance Method Summary
-
#calculate_ip
Sort through the various IP address headers, looking for the IP most likely to be the address of the actual remote client making this request.
-
#to_s
Memoizes the value returned by #calculate_ip and returns it for
::ActionDispatch::Requestto use. - #check_ip(client_ips, forwarded_ips) private
- #first_non_proxy(ips) private
- #ips_from(header) private
- #raise_spoofing_error private
- #sanitize_ips(ips) private
Constructor Details
.new(req, check_ip, proxies) ⇒ GetIp
Instance Method Details
#calculate_ip
Sort through the various IP address headers, looking for the IP most likely to be the address of the actual remote client making this request.
REMOTE_ADDR will be correct if the request is made directly against the Ruby
process, on e.g. Heroku. When the request is proxied by another server like
HAProxy or NGINX, the IP address that made the original request will be put in
an X-Forwarded-For header. If there are multiple proxies, that header may
contain a list of IPs. Other proxy services set the Client-Ip header
instead, so we check that too.
As discussed in this post about ::Rails IP
Spoofing,
while the first IP in the list is likely to be the "originating" IP, it
could also have been set by the client maliciously.
In order to find the first address that is (probably) accurate, we take the list of IPs, remove known and trusted proxies, and then take the last address left, which was presumably set by one of those proxies.
# File 'actionpack/lib/action_dispatch/middleware/remote_ip.rb', line 136
def calculate_ip # Set by the Rack web server, this is a single value. remote_addr = sanitize_ips(ips_from(@req.remote_addr)).last # Could be a CSV list and/or repeated headers that were concatenated. client_ips = sanitize_ips(ips_from(@req.client_ip)).reverse! forwarded_ips = sanitize_ips(@req.forwarded_for || []).reverse! # `Client-Ip`, `Forwarded`, and `X-Forwarded-For` should not, generally, all # be set. If several of them are set, it means that either: # # 1) This request passed through proxies with incompatible IP header # conventions. # # 2) The client passed the headers the proxy servers weren't using # themselves. # # Either way, there is no way for us to determine which header is the right one # after the fact. Since we have no idea, if we are concerned about IP spoofing # we need to give up and explode. (If you're not concerned about IP spoofing you # can turn the `ip_spoofing_check` option off.) check_ip(client_ips, forwarded_ips) if @check_ip # We assume these things about the IP headers: # # - X-Forwarded-For will be a list of IPs, one per proxy, or blank # - Client-Ip is propagated from the outermost proxy, or is blank # - REMOTE_ADDR will be the IP that made the request to Rack ips = forwarded_ips + client_ips ips.compact! ips << remote_addr # If every single IP option is in the trusted list, return the IP that's # furthest away first_non_proxy(ips) || ips[-2] || ips.last end
#check_ip(client_ips, forwarded_ips) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/remote_ip.rb', line 196
def check_ip(client_ips, forwarded_ips) if client_ips.last && forwarded_ips.last && !forwarded_ips.include?(client_ips.last) # We don't know which came from the proxy, and which from the user raise_spoofing_error end if @req.get_header("HTTP_FORWARDED") && @req.x_forwarded_for x_forwarded_ips = XForwardedForParser.new("HTTP_X_FORWARDED_FOR" => @req.x_forwarded_for).forwarded_for || [] x_forwarded_client = first_non_proxy(sanitize_ips(x_forwarded_ips).reverse!) forwarded_client = first_non_proxy(forwarded_ips) # Compare strings, not parsed addresses, so the client cannot choose how its # own address is spelled. if x_forwarded_client && x_forwarded_client != forwarded_client raise_spoofing_error end end end
#first_non_proxy(ips) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/remote_ip.rb', line 235
def first_non_proxy(ips) # :doc: ips.find do |raw_ip| return unless raw_ip ip = @parsed_ips&.[](raw_ip) || IPAddr.new(raw_ip) @proxies.none? do |proxy| if proxy.is_a?(IPAddr) proxy.include?(ip) else proxy === raw_ip end end end end
#ips_from(header) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/remote_ip.rb', line 180
def ips_from(header) # :doc: return [] unless header # Split the comma-separated list into an array of strings. header.strip.split(/[,\s]+/) end
#raise_spoofing_error (private)
# File 'actionpack/lib/action_dispatch/middleware/remote_ip.rb', line 186
def raise_spoofing_error = +"IP spoofing attack?! " \ "HTTP_CLIENT_IP=#{@req.client_ip.inspect} " \ "HTTP_X_FORWARDED_FOR=#{@req.x_forwarded_for.inspect}" if forwarded = @req.get_header("HTTP_FORWARDED") << " HTTP_FORWARDED=#{forwarded.inspect}" end raise IpSpoofAttackError, end
#sanitize_ips(ips) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/remote_ip.rb', line 215
def sanitize_ips(ips) # :doc: ips.select! do |ip| # Only return IPs that are valid according to the IPAddr#new method. addr = IPAddr.new(ip) # We want to make sure nobody is sneaking a netmask in, which can only be # given after a slash. if ip.include?("/") range = addr.to_range next false unless range.begin == range.end end # Keep the parsed address for #first_non_proxy, by identity since the # same strings are checked there. (@parsed_ips ||= {}.compare_by_identity)[ip] = addr true rescue ArgumentError nil end ips end
#to_s
Memoizes the value returned by #calculate_ip and returns it for
::ActionDispatch::Request to use.
# File 'actionpack/lib/action_dispatch/middleware/remote_ip.rb', line 175
def to_s @ip ||= calculate_ip end