I’m building a small Node.js service on Cloud Run and want to limit how many
requests one client IP can make. I’m trying to understand which address I can
trust, since callers can supply their own X-Forwarded-For header.
Requests go directly to Cloud Run’s default run.app URLs. I haven’t added a
load balancer, Firebase Hosting rewrite, custom domain or reverse proxy. The
test service uses gen1 in us-central1, with HTTP/1.1 to the container.
I tested both default URL forms: the one containing the project number and the
hash-based a.run.app URL. Both gave the same pattern:
Header I sent: (none)
Header received: OBSERVED_IP
Header I sent: 192.0.2.1
Header received: 192.0.2.1, OBSERVED_IP
Header I sent: 192.0.2.1, 198.51.100.2
Header received: 192.0.2.1, 198.51.100.2, OBSERVED_IP
The example addresses are deliberately fake. OBSERVED_IP replaces the actual
address in the results; I haven’t independently confirmed it is my outgoing
public IP. The socket’s remote address was an internal address.
**Is the last address in this header added by Cloud Run in a way the caller
cannot forge, and is it safe to use that address for IP-based rate limiting?
**
My test service currently requires IAM authentication. The eventual mobile
endpoint would allow requests through Cloud Run IAM and validate Firebase App
Check in the application. Does that change the answer? Are there differences
between the two default URLs, or with IPv6 or HTTP/2 clients?
I’d appreciate a documentation link or clarification of the supported behavior.
If using the last address isn’t reliable, what would you recommend for this
setup?
I found a closely related issue in Google’s Cloud Run sample repository: issue #3583.
The merged fix, PR #3586, sets app.set('trust proxy', 1) specifically for client-IP rate limiting behind Cloud Run.
This agrees with what I observed in my tests. Is that still the supported approach for direct run.app requests, including when a caller supplies their own X-Forwarded-For header?
Thanks—this looks promising, and I’d appreciate confirmation that it applies to the setup described above.
Your test results are exactly what I saw on my own service, and yes, the last address is the one Cloud Run appends, so a caller can only add entries in front of it, not behind it. For a direct run.app setup, app.set(‘trust proxy’, 1) is the right call, because it tells Express to trust that one Cloud Run hop and use the last entry as the client IP. The one thing I would watch is that it is the egress IP of the caller’s network, so a bunch of users behind the same NAT or mobile carrier will share one limit. I ended up pairing the IP limit with App Check for my mobile endpoint for that reason, since it gives you a per-device identity when the IP turns out to be too coarse.
Thanks, that’s helpful and agrees with our observations and Google’s sample fix.
Could you share a documentation reference for Cloud Run protecting the final forwarded address?
Also, could you clarify what you use as the per-device identifier? Firebase’s token documentation describes app_id and sub as the application ID, shared across installations. Are you combining App Check with another identifier?
Fair question, and I don’t have a doc reference for the direct run.app case. The container contract doesn’t say anything about X-Forwarded-For, and my answer was based on my own tests matching yours rather than a doc I can point to. On the identifier, I only paired the IP limit with App Check, I didn’t combine it with a second per-device identifier, and you’re right that the App Check token’s app_id and sub identify the app rather than the device. In my setup the IP limit was still doing the per-network work and App Check was just proving the request came from my app, so for true per-device limiting I’d need something else on top.