module Amber::Router::ClientAddress

Overview

Decides the IP address of the client behind a request. HTTP::Request#client_ip and Amber::Pipe::ClientIp call it.

When the immediate peer is not a trusted proxy, the client is the peer and every forwarding header is ignored, because whoever opened the connection could have written them.

When the peer is a trusted proxy, Amber reads the chain of hops that the proxies appended, from the one header family they write (server.trusted_proxy_headers): X-Forwarded-For by default, or the for= parameters of RFC 7239 Forwarded. The other family is ignored, because a proxy passes it through from the client unchanged. Amber walks the chain from the right, skipping hops that are themselves trusted proxies, and returns the first address that is not. Entries to the left of that address were written by the client or by an untrusted hop and are never read, so a spoofed leftmost entry cannot win.

A hop that is not an IP address (unknown, an obfuscated identifier, or garbage) ends the walk: nothing to its left can be verified, so the client is the nearest trusted hop that was verified. When every hop is trusted, the farthest one is the client.

Defined in:

amber/router/client_address.cr

Constant Summary

X_FORWARDED_FOR = "X-Forwarded-For"

Class Method Summary

Class Method Detail

def self.resolve(request : HTTP::Request, trusted_proxies : Amber::Support::TrustedProxies, list_of_header_names : Array(String) | Nil = nil) : Socket::IPAddress | Nil #

Returns the client address of request, or nil when no IP address is known (a request built in memory, or a UNIX socket peer with no usable forwarding header). list_of_header_names replaces the default Forwarded / X-Forwarded-For lookup with the first of those headers that is present.


[View source]