-
-
Notifications
You must be signed in to change notification settings - Fork 7
Closed
Description
Problem
rest.RealIP extracts client IP using the rightmost IP in the X-Forwarded-For chain (RFC 7239 security model). This doesn't work with Cloudflare and similar CDNs.
With Cloudflare, the X-Forwarded-For header looks like:
X-Forwarded-For: <original-client>, <cloudflare-edge-server>
The rightmost IP is a Cloudflare edge server which:
- Varies per request - Cloudflare routes through different edges
- Is not the actual client IP
Example
Same client making requests gets different "client IPs":
172.71.203.9 (Cloudflare edge)
172.70.231.89 (different edge)
104.23.190.60 (another edge)
Should have been: 203.0.113.195 (actual client) for all requests.
Proposed Solution
Add RealIPFromCDN middleware with CDN-friendly header priority:
X-Real-IP- trusted proxy (reproxy/nginx) sets this to actual clientCF-Connecting-IP- Cloudflare's dedicated header for original clientX-Forwarded-For(leftmost public IP) - original client in CDN chainRemoteAddr- fallback for direct connections
Key: filter out private/loopback IPs from headers before accepting.
API
// new middleware
func RealIPFromCDN(next http.Handler) http.Handler
// new function in realip package
func GetFromCDN(r *http.Request) (string, error)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels