↓ Skip to main content
  1. Writing/

Behind Cloudflare, a Fly app's client IP is a Cloudflare edge

··

Put Cloudflare in front of a Fly app, and the client IP Fly hands you is a Cloudflare edge. Fly’s docs say so for Fly-Client-IP, and the last hop in x-forwarded-for goes the same way. A rate limit that reads that last hop lumps together everyone who comes through the same Cloudflare edge.

The client is the hop Cloudflare appended before its own. remote_ip reads x-forwarded-for right to left and skips the proxies you give it, so give it Cloudflare’s published ranges:

@cloudflare ~w(173.245.48.0/20 103.21.244.0/22 [...the rest of cloudflare.com/ips...])

defp forwarded_for(headers, proxies),
  do: RemoteIp.from(headers, headers: ["x-forwarded-for"], proxies: proxies)

A client can forge hops on the left, but remote_ip stops at the first hop that isn’t Cloudflare:

assert ClientIP.from_conn(conn("198.51.100.9, 203.0.113.7, 172.70.1.2")) == "203.0.113.7"

Anyone can send cf-ipcountry and cf-ipcity too, so believe them only when the request came through Cloudflare, which is when skipping Cloudflare’s ranges changes the answer:

def via_cloudflare?(headers),
  do: forwarded_for(headers, @cloudflare) != forwarded_for(headers, [])
Glenn Roberts
Written by
Glenn Roberts
Hands-on engineer and former CTO. I help companies build with AI.