Documentation
¶
Index ¶
- Variables
- func ParseAddr(ip string) (netip.Addr, error)
- func ParsePrefixes(ranges ...string) ([]netip.Prefix, error)
- type Chain
- type ExcludedRangeOption
- type HeaderKey
- type LeftmostNonPrivate
- type RemoteAddr
- type RightmostNonPrivate
- type RightmostTrustedCount
- type RightmostTrustedRange
- type SingleIPHeader
- type TrustedIPRange
- type TrustedIPRangeFunc
- type TrustedPeer
- type TrustedRangeOption
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidIPAddress = errors.New("invalid ip address") ErrUnspecifiedIPAddress = errors.New("unspecified ip address") ErrTrustedPeer = errors.New("trusted peer resolver") ErrRemoteAddress = errors.New("remote address resolver") ErrSingleIPHeader = errors.New("single ip header resolver") ErrLeftmostNonPrivate = errors.New("leftmost non private resolver") ErrRightmostNonPrivate = errors.New("rightmost non private resolver") ErrRightmostTrustedCount = errors.New("rightmost trusted count resolver") ErrRightmostTrustedRange = errors.New("rightmost trusted range resolver") )
Functions ¶
func ParseAddr ¶ added in v0.31.0
ParseAddr safely parses the given string into a netip.Addr. The port and any surrounding square brackets are stripped, IPv4-mapped IPv6 addresses are unmapped to their IPv4 form and IPv6 zone identifiers are preserved. It also returns an error for unspecified addresses (like "::" or "0.0.0.0"). These are nominally valid IPs, but they are never valid "real" client IPs.
The function returns the following errors: - ErrInvalidIPAddress: if the IP address cannot be parsed. - ErrUnspecifiedIPAddress: if the IP address is unspecified (e.g., "::" or "0.0.0.0").
func ParsePrefixes ¶ added in v0.31.0
ParsePrefixes converts a slice of strings with IPv4 and IPv6 addresses and CIDR ranges (prefixes) to netip.Prefix instances. Single addresses are treated as full-length prefixes and IPv4-mapped IPv6 values are normalized to their IPv4 form. Zones in addresses or ranges are not allowed and will result in an error.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain attempts to use the given resolvers in order. If the first one returns an error, the second one is tried, and so on, until a good IP is found or the resolvers are exhausted. A common use for this is if a server is both directly connected to the internet and expecting a header to check. It might be called like:
leftmost, _ := NewLeftmostNonPrivate(XForwardedForKey, 16) chain := NewChain(leftmost, NewRemoteAddr())
type ExcludedRangeOption ¶ added in v0.31.0
type ExcludedRangeOption interface {
// contains filtered or unexported methods
}
func ExcludeLinkLocal ¶
func ExcludeLinkLocal(enable bool) ExcludedRangeOption
ExcludeLinkLocal enables or disables the inclusion of link local ip ranges in the excluded ip ranges.
func ExcludeLoopback ¶
func ExcludeLoopback(enable bool) ExcludedRangeOption
ExcludeLoopback enables or disables the inclusion of loopback ip ranges in the excluded ip ranges.
func ExcludePrivateNet ¶
func ExcludePrivateNet(enable bool) ExcludedRangeOption
ExcludePrivateNet enables or disables the inclusion of private-space ip ranges in the excluded ip ranges.
type LeftmostNonPrivate ¶
type LeftmostNonPrivate struct {
// contains filtered or unexported fields
}
LeftmostNonPrivate derives the client IP from the leftmost valid and non-private/non-internal IP address in the X-Forwarded-For or Forwarded header. This resolver should be used when a valid, non-private IP closest to the client is desired. By default, loopback, link local and private net ip range are excluded. Note that this MUST NOT BE USED FOR SECURITY PURPOSES. This IP can be TRIVIALLY SPOOFED.
func NewLeftmostNonPrivate ¶
func NewLeftmostNonPrivate(key HeaderKey, limit uint, opts ...ExcludedRangeOption) (LeftmostNonPrivate, error)
NewLeftmostNonPrivate creates a LeftmostNonPrivate resolver. By default, loopback, link local and private net ip range are excluded. A reasonable limit on the number of IPs to parse must be provided to prevent excessive resource usage from adversarial headers.
func (LeftmostNonPrivate) ClientIP ¶
func (s LeftmostNonPrivate) ClientIP(c fox.RequestContext) (netip.Addr, error)
ClientIP derives the client IP using the LeftmostNonPrivate resolver. The returned netip.Addr may contain a zone identifier. If no valid IP can be derived, an error returned.
type RemoteAddr ¶
type RemoteAddr struct{}
RemoteAddr returns the client socket IP, stripped of port. This resolver should be used if the server accept direct connections, rather than through a reverse proxy.
func NewRemoteAddr ¶
func NewRemoteAddr() RemoteAddr
NewRemoteAddr that uses request remote address to get the client IP.
func (RemoteAddr) ClientIP ¶
func (s RemoteAddr) ClientIP(c fox.RequestContext) (netip.Addr, error)
ClientIP derives the client IP using the RemoteAddr resolver. The returned netip.Addr may contain a zone identifier. If no valid IP can be derived, an error is returned.
type RightmostNonPrivate ¶
type RightmostNonPrivate struct {
// contains filtered or unexported fields
}
RightmostNonPrivate derives the client IP from the rightmost valid, non-private/non-internal IP address in the X-Fowarded-For or Forwarded header. This resolver should be used when all reverse proxies between the internet and the server have private-space IP addresses. By default, loopback, link local and private net ip range are trusted.
func NewRightmostNonPrivate ¶
func NewRightmostNonPrivate(key HeaderKey, opts ...TrustedRangeOption) (RightmostNonPrivate, error)
NewRightmostNonPrivate creates a RightmostNonPrivate resolver. By default, loopback, link local and private net ip range are trusted.
func (RightmostNonPrivate) ClientIP ¶
func (s RightmostNonPrivate) ClientIP(c fox.RequestContext) (netip.Addr, error)
ClientIP derives the client IP using the RightmostNonPrivate resolver. The returned netip.Addr may contain a zone identifier. If no valid IP can be derived, an error returned.
type RightmostTrustedCount ¶
type RightmostTrustedCount struct {
// contains filtered or unexported fields
}
RightmostTrustedCount derives the client IP from the valid IP address added by the first trusted reverse proxy to the X-Forwarded-For or Forwarded header. This resolver should be used when there is a fixed number of trusted reverse proxies that are appending IP addresses to the header. It assumes the request arrived through those proxies but does not verify it: if the server is also reachable directly from the internet, wrap it in a TrustedPeer.
func NewRightmostTrustedCount ¶
func NewRightmostTrustedCount(key HeaderKey, trustedCount uint) (RightmostTrustedCount, error)
NewRightmostTrustedCount creates a RightmostTrustedCount resolver. trustedCount is the number of trusted reverse proxies. The IP returned will be the (trustedCount-1)th from the right. For example, if there's only one trusted proxy, this resolver will return the last (rightmost) IP address.
func (RightmostTrustedCount) ClientIP ¶
func (s RightmostTrustedCount) ClientIP(c fox.RequestContext) (netip.Addr, error)
ClientIP derives the client IP using the RightmostTrustedCount resolver. The returned netip.Addr may contain a zone identifier. If no valid IP can be derived, an error returned.
type RightmostTrustedRange ¶
type RightmostTrustedRange struct {
// contains filtered or unexported fields
}
RightmostTrustedRange derives the client IP from the rightmost valid IP address in the X-Forwarded-For or Forwarded header which is not in a set of trusted IP ranges. This resolver should be used when the IP ranges of the reverse proxies between the internet and the server are known.
This resolver also verifies the connecting peer (the request's RemoteAddr): if the peer is a valid IP that is not in the trusted ranges, the request did not arrive through a trusted proxy, the header is ignored and the peer itself is returned. This makes the resolver safe to use on a server that is reachable both through a trusted proxy and directly (an attacker connecting directly cannot spoof the client IP via a forged header). See ClientIP for the exact peer-handling rules.
If a third-party WAF, CDN, etc., is used, you SHOULD use a method of verifying its access to your origin that is stronger than checking its IP address (e.g., using authenticated pulls). Failure to do so can result in scenarios like: You use AWS CloudFront in front of a server you host elsewhere. An attacker creates a CF distribution that points at your origin server. The attacker uses Lambda@Edge to spoof the Host and X-Forwarded-For headers. Now your "trusted" reverse proxy is no longer trustworthy.
func NewRightmostTrustedRange ¶
func NewRightmostTrustedRange(key HeaderKey, resolver TrustedIPRange) (RightmostTrustedRange, error)
NewRightmostTrustedRange creates a RightmostTrustedRange resolver. key must be XForwardedForKey or ForwardedKey. resolver must provide all trusted reverse proxy ranges on the path to this server, private/internal or external (for example, if a third-party reverse proxy is used).
func (RightmostTrustedRange) ClientIP ¶
func (s RightmostTrustedRange) ClientIP(c fox.RequestContext) (netip.Addr, error)
ClientIP derives the client IP using the RightmostTrustedRange resolver. The returned netip.Addr may contain a zone identifier. If no valid IP can be derived, an error is returned.
The connecting peer (the request's RemoteAddr) is checked first: a valid IP outside the trusted ranges is returned as is, since the request did not arrive through a trusted proxy and the peer is the rightmost untrusted address. A trusted or unclassifiable peer (empty, a Unix domain socket peer or otherwise unparseable) falls through to the header walk.
type SingleIPHeader ¶
type SingleIPHeader struct {
// contains filtered or unexported fields
}
SingleIPHeader derives an IP address from a single-IP header. A non-exhaustive list of such single-IP headers is: X-Real-IP, CF-Connecting-IP, True-Client-IP, Fastly-Client-IP, X-Azure-ClientIP, X-Azure-SocketIP. This resolver should be used when the given header is added by a trusted reverse proxy. You must ensure that this header is not spoofable (as is possible with Akamai's use of True-Client-IP, Fastly's default use of Fastly-Client-IP, and Azure's X-Azure-ClientIP). This resolver trusts the header without checking the connecting peer: if the server is also reachable directly from the internet, wrap it in a TrustedPeer.
func NewSingleIPHeader ¶
func NewSingleIPHeader(headerName string) (SingleIPHeader, error)
NewSingleIPHeader creates a SingleIPHeader resolver that uses the headerName request header to get the client IP.
func (SingleIPHeader) ClientIP ¶
func (s SingleIPHeader) ClientIP(c fox.RequestContext) (netip.Addr, error)
ClientIP derives the client IP using the SingleIPHeader resolver. The returned netip.Addr may contain a zone identifier. If no valid IP can be derived, an error is returned.
type TrustedIPRange ¶
TrustedIPRange returns a set of trusted IP ranges. Implementations of this interface must be thread-safe as it will be invoked whenever the client IP needs to be resolved, potentially from multiple goroutines.
type TrustedIPRangeFunc ¶
The TrustedIPRangeFunc type is an adapter to allow the use of ordinary functions as TrustedIPRange. If f is a function with the appropriate signature, TrustedIPRangeFunc() is a TrustedIPRange that calls f.
func (TrustedIPRangeFunc) TrustedIPRange ¶
func (f TrustedIPRangeFunc) TrustedIPRange() ([]netip.Prefix, error)
TrustedIPRange calls f().
type TrustedPeer ¶ added in v0.32.1
type TrustedPeer struct {
// contains filtered or unexported fields
}
TrustedPeer wraps another resolver and only consults it when the connecting peer is a trusted reverse proxy. It protects header-only resolvers such as SingleIPHeader and RightmostTrustedCount, which derive the client IP purely from a header and cannot verify themselves that the request actually came through a trusted proxy. On a server that is reachable both through a proxy and directly from the internet, an attacker connecting directly could otherwise forge the header and spoof the result. RightmostTrustedRange does not need this wrapper: it already holds the trusted ranges and verifies the peer itself.
func NewTrustedPeer ¶ added in v0.32.1
func NewTrustedPeer(resolver TrustedIPRange, inner fox.ClientIPResolver) (TrustedPeer, error)
NewTrustedPeer creates a TrustedPeer resolver. resolver must provide the IP ranges of all trusted reverse proxies that connect directly to this server. inner is the resolver consulted once the peer is found to be trusted.
func (TrustedPeer) ClientIP ¶ added in v0.32.1
func (s TrustedPeer) ClientIP(c fox.RequestContext) (netip.Addr, error)
ClientIP derives the client IP using the TrustedPeer resolver. The connecting peer (the request's RemoteAddr) is classified first:
- a valid IP inside the trusted ranges is a trusted proxy: the inner resolver is consulted and its result returned.
- a valid IP outside the trusted ranges did not arrive through a trusted proxy: the header cannot be trusted and an error is returned. To also serve direct clients, chain a RemoteAddr resolver after this one with NewChain.
- anything else (empty, a Unix domain socket peer or otherwise unparseable) cannot be classified and the inner resolver is consulted as normal; a Unix socket is unreachable from the network and cannot be a spoofing vector.
type TrustedRangeOption ¶
type TrustedRangeOption interface {
// contains filtered or unexported methods
}
func TrustLinkLocal ¶
func TrustLinkLocal(enable bool) TrustedRangeOption
TrustLinkLocal enables or disables the inclusion of link local ip ranges in the trusted ip ranges.
func TrustLoopback ¶
func TrustLoopback(enable bool) TrustedRangeOption
TrustLoopback enables or disables the inclusion of loopback ip ranges in the trusted ip ranges.
func TrustPrivateNet ¶
func TrustPrivateNet(enable bool) TrustedRangeOption
TrustPrivateNet enables or disables the inclusion of private-space ip ranges in the trusted ip ranges.