trusted_proxies and the real client IP
How the WAF decides who a request is from, and why that decision needs a trust boundary. In short: behind Cloudflare or another CDN, either list the proxies in trusted_proxies and name the header in client_ip_header, or let Caddy resolve the client with its global trusted_proxies and set client_ip_source caddy. Without either, forwarding headers are ignored and the WAF sees the proxy. Implementation: clientip.go.
The problem
Every IP-based control needs the real client address. When the WAF is the edge, that is simply the connection's peer (r.RemoteAddr). Behind a reverse proxy or CDN it is not: the peer is the proxy, and the client is carried in a forwarding header such as X-Forwarded-For.
That header is set by whoever is upstream — including, if the WAF is reachable directly, the client itself. Trusting it blindly lets anyone send X-Forwarded-For: 1.2.3.4 and impersonate any address, slipping past the GeoIP, ASN and REMOTE_IP checks. So a forwarding header is only trustworthy when it comes from a peer you actually trust.
The model
By default trusted_proxies is empty: the WAF uses the peer address and ignores every forwarding header. A client reaching it directly cannot change its apparent IP.
Configure trusted_proxies with the addresses of your proxy or CDN edges. Then, and only when the immediate peer is one of them, the WAF resolves the real client:
- If
client_ip_headeris set (e.g.CF-Connecting-IP), its value is used. - Otherwise
X-Forwarded-Foris walked right-to-left, and the first address that is not itself a trusted proxy is the client — so a chainclient, edge-lb, cdnresolves toclient.
If the peer is not trusted, forwarding headers are ignored entirely — header or no header.
Directives
| Directive | Value | Default | Meaning |
|---|---|---|---|
trusted_proxies | <entry> [<entry> …] | unset | Peers allowed to speak for their clients: bare IPs, CIDR ranges, or the token private_ranges. Repeatable. Empty = ignore forwarding headers, use the peer. |
client_ip_header | <name> | unset | A single-IP header (CF-Connecting-IP, True-Client-IP, X-Real-IP, …) read for the client IP once the peer is trusted, instead of X-Forwarded-For. |
client_ip_source | waf or caddy | waf | waf: resolve the client IP with the two directives above. caddy: use the client IP Caddy's server already resolved with its global trusted_proxies and client_ip_headers options, which accept IP source modules. See Let Caddy resolve the client IP. |
What uses the resolved client IP
| Subsystem | Address used |
|---|---|
| Rate limiter | Resolved client IP — so a CDN's edges don't collapse into one bucket |
| GeoIP country filter | Resolved client IP |
| ASN blocking | Resolved client IP |
REMOTE_IP rule target | Resolved client IP (a bare IP, no port) |
| IP blacklist | Peer plus every X-Forwarded-For hop (unchanged) |
| IP whitelist | Peer only, never a forwarding header |
The last two are deliberate asymmetries. The blacklist checks every hop because for blocking, consulting more addresses can only ever block more, never less. The whitelist uses only the peer because for allowing, honouring a client-supplied header would let anyone exempt themselves — see IP whitelist.
Cloudflare
Cloudflare terminates the connection, so the peer is always a Cloudflare edge IP and the real client is in CF-Connecting-IP:
waf {
rule_file rules.json
trusted_proxies 173.245.48.0/20 103.21.244.0/22 108.162.192.0/18 # Cloudflare's published ranges
client_ip_header CF-Connecting-IP
}A static list has to be kept in sync with Cloudflare's published ranges (https://www.cloudflare.com/ips/) by hand. To have the ranges fetched and refreshed for you, let Caddy resolve the client IP instead, as below.
Let Caddy resolve the client IP
Caddy's own server options trusted_proxies and client_ip_headers resolve the client IP before any handler runs, and trusted_proxies accepts IP source modules that fetch and refresh provider ranges, for example cloudflare or list. With client_ip_source caddy the WAF uses that resolved IP, so it agrees with Caddy's logs and matchers and needs no list of its own:
{
servers {
trusted_proxies cloudflare
client_ip_headers CF-Connecting-IP
}
}
example.com {
route {
waf {
rule_file /etc/caddy/rules.json
client_ip_source caddy
}
reverse_proxy localhost:8080
}
}Build Caddy with the source module next to caddy-waf, for example xcaddy build --with github.com/fabriziosalmi/caddy-waf --with github.com/WeidiDeng/caddy-cloudflare-ip (see Build Caddy with plugins). The static source needs no module: trusted_proxies static 173.245.48.0/20 ....
- With
client_ip_source caddy,trusted_proxiesandclient_ip_headerinside thewafblock are rejected at startup: they would be ignored, and the config would read as if they applied. - When Caddy does not trust the peer, its client IP is the peer address, so a spoofed
CF-Connecting-IPfrom outside Cloudflare changes nothing. - The IP blacklist and IP whitelist keep the behaviour in the table above.
Generic reverse proxy
Behind an nginx / HAProxy / your-own edge that appends to X-Forwarded-For, trust that edge's address — no client_ip_header needed, the client is taken from the header:
waf {
rule_file rules.json
trusted_proxies 10.0.0.0/8 # your internal proxy network
}IMPORTANT
Migration
Earlier versions trusted X-Forwarded-For unconditionally for the GeoIP and ASN checks. That was spoofable whenever the WAF was reachable directly. The default is now secure: forwarding headers are ignored unless the peer is a configured trusted proxy.
If you run behind a CDN or reverse proxy and rely on filtering by the real client's country or ASN — or on per-client rate limiting, or REMOTE_IP rules — set trusted_proxies (and, for header-based CDNs, client_ip_header). Without it those controls now judge the proxy's address instead of the client's.
WARNING
trusted_proxies is a trust boundary: anything you list can set a request's apparent client IP. List only addresses you control or trust — your own edges, your CDN's published ranges. Never 0.0.0.0/0, and never a range wider than your actual proxy fleet.