struct Amber::Support::TrustedProxies

Overview

The set of peers Amber believes when they forward a client's scheme or address, and the one header family those peers write.

The peers are the parsed server.trusted_proxies networks, plus every UNIX socket peer when Amber itself listens on a UNIX socket (server.unix_socket). A UNIX socket connection is local by construction, and the socket file's permissions decide who may open it, so a proxy that reaches Amber that way is as trustworthy as one on loopback.

The header family comes from server.trusted_proxy_headers. A proxy rewrites only the family it manages and passes the other one through from the client unchanged, so Amber reads exactly one family: X-Forwarded-For / X-Forwarded-Proto by default, or RFC 7239 Forwarded when reads_forwarded_header is true.

networks = ["127.0.0.0/8"].map { |notation| Amber::Support::TrustedProxyNetwork.parse(notation) }
trusted_proxies = Amber::Support::TrustedProxies.new(networks, trusts_unix_socket_peers: false)
trusted_proxies.trusts?(Socket::IPAddress.new("127.0.0.1", 0)) # => true

Defined in:

amber/support/trusted_proxies.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(list_of_networks : Slice(TrustedProxyNetwork), *, trusts_unix_socket_peers : Bool, reads_forwarded_header : Bool = false) #

[View source]
def self.new(list_of_networks : Array(TrustedProxyNetwork), *, trusts_unix_socket_peers : Bool, reads_forwarded_header : Bool = false) : self #

Builds the set from list_of_networks, copying it into a read-only slice.


[View source]

Instance Method Detail

def list_of_networks : Slice(TrustedProxyNetwork) #

The trusted networks. The slice is read-only: writing to it raises.


[View source]
def reads_forwarded_header? : Bool #

[View source]
def trusts?(peer : Socket::Address | Nil) : Bool #

Returns true when peer is a trusted proxy. A request without a peer address (built in memory rather than read from a socket) never is.


[View source]
def trusts_unix_socket_peers? : Bool #

[View source]