Go package for extracting a client IP address from net/http requests with an explicit trust model for reverse proxies.
The exported header constants in this package use lower-case values so they can
be used directly with ExtractFrom and gRPC metadata. Header matching remains
case-insensitive.
- Proxy headers are disabled by default.
- Only
Request.RemoteAddris used unless proxy support is explicitly enabled. - The default extractor is permissive: malformed proxy headers are ignored rather than turning the whole request into an error.
-
ProxiesDenied- Default mode.
- Ignores
forwarded,x-forwarded-for, and other proxy headers. - Safest choice unless the service is known to sit behind trusted reverse proxies.
-
ProxiesAllowedList- Trusts proxy headers only when
Request.RemoteAddrbelongs to a configured allow-list. - Supports both individual IP addresses and CIDR ranges.
- Recommended for production behind load balancers or reverse proxies.
- Trusts proxy headers only when
-
ProxiesAllowedAll- Trusts proxy headers from any source.
- Unsafe unless an upstream component strips and rewrites forwarding headers.
- Kept explicit via
WithUnsafeTrustAllProxies().
-
Permissive mode:
- Ignores malformed proxy headers.
- Falls back to safer sources when possible.
- Good default for typical production handlers.
-
Strict mode:
- Returns typed errors for malformed
forwarded, invalid IP tokens, conflicting trusted headers, untrusted proxy usage, and invalid configuration. - Returns
ErrAmbiguousHeaderwhen multiple physical keys normalize to the same configured logical header, such asX-Forwarded-Forandx-forwarded-for. - Useful for diagnostics, ingress validation, and security-sensitive services.
- Returns typed errors for malformed
-
Standardized:
forwardedis parsed according to RFC 7239, including quoted strings, escaping, IPv6-in-brackets, obfuscated identifiers,unknown, multiple header fields, and HTTP list semantics.
-
De-facto:
x-forwarded-foris parsed as a comma-separated proxy chain.x-real-ip,x-client-ip,cf-connecting-ip,fastly-client-ip,true-client-ip, andx-cluster-client-ipare treated as single-IP headers.
-
Ambiguous / legacy:
x-forwardedandforwarded-forare treated as single-IP headers only.- They remain lower priority than
forwardedandx-forwarded-for.
- Header matching is case-insensitive.
- In strict mode, multiple physical keys that normalize to the same logical header are rejected with
ErrAmbiguousHeader. - In permissive mode,
ExtractFromprefers the exact lower-case key when present. Otherwise it prefers the lexicographically smallest key spelling.