fix(hub): check X-Real-IP header to resolve agent's real source IP (#1973)

This commit is contained in:
Sven van Ginkel
2026-08-14 16:13:26 +02:00
committed by GitHub
parent bc55e249c4
commit 6607d4c0d6
2 changed files with 21 additions and 0 deletions

View File

@@ -317,6 +317,9 @@ func getRealIP(r *http.Request) string {
if ip := r.Header.Get("CF-Connecting-IP"); ip != "" {
return ip
}
if ip := r.Header.Get("X-Real-IP"); ip != "" {
return ip
}
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
// X-Forwarded-For can contain a comma-separated list: "client_ip, proxy1, proxy2"
// Take the first one

View File

@@ -1796,6 +1796,24 @@ func TestGetRealIP(t *testing.T) {
remoteAddr: "127.0.0.1:12345",
expectedIP: "192.168.1.8",
},
{
name: "X-Real-IP header",
headers: map[string]string{"X-Real-IP": "10.8.0.4"},
remoteAddr: "172.21.0.1:12345",
expectedIP: "10.8.0.4",
},
{
name: "X-Real-IP takes precedence over X-Forwarded-For",
headers: map[string]string{"X-Real-IP": "10.8.0.4", "X-Forwarded-For": "10.8.0.5"},
remoteAddr: "172.21.0.1:12345",
expectedIP: "10.8.0.4",
},
{
name: "CF-Connecting-IP takes precedence over X-Real-IP",
headers: map[string]string{"CF-Connecting-IP": "1.2.3.4", "X-Real-IP": "10.8.0.4"},
remoteAddr: "172.21.0.1:12345",
expectedIP: "1.2.3.4",
},
}
for _, tc := range testCases {