From 9aff161d86b9fe037107ef280062af19eb656ca1 Mon Sep 17 00:00:00 2001 From: takumireid Date: Sat, 26 Sep 2026 00:27:24 +0900 Subject: [PATCH] fix(context): return empty ClientIP over unix socket without a forwarded header On a unix socket, ClientIP() always trusts the connection but never sets remoteIP, so a request without X-Forwarded-For / X-Real-IP returned the literal "". Return an empty string instead, completing #2718. Also document that over a unix socket RemoteIPHeaders are consulted regardless of SetTrustedProxies, including after SetTrustedProxies(nil). --- context.go | 12 ++++++++++++ context_test.go | 18 ++++++++++++++++++ gin.go | 6 ++++++ 3 files changed, 36 insertions(+) diff --git a/context.go b/context.go index a6add8a6..d27c6d49 100644 --- a/context.go +++ b/context.go @@ -994,6 +994,10 @@ func (c *Context) ShouldBindBodyWithPlain(obj any) error { // If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-IP]). // If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy, // the remote IP (coming from Request.RemoteAddr) is returned. +// +// When gin is listening on a unix socket the connection is always considered +// trusted, so Engine.RemoteIPHeaders are consulted regardless of +// Engine.SetTrustedProxies (including after SetTrustedProxies(nil)). func (c *Context) ClientIP() string { // Check if we're running on a trusted platform, continue running backwards if error if c.engine.TrustedPlatform != "" { @@ -1042,6 +1046,14 @@ func (c *Context) ClientIP() string { } } } + + // remoteIP can be nil here only on a unix socket connection that carried + // no usable forwarding header (see the "always trust" branch above, which + // does not populate remoteIP). Return an empty string rather than the + // "" produced by net.IP(nil).String(). + if remoteIP == nil { + return "" + } return remoteIP.String() } diff --git a/context_test.go b/context_test.go index b82e04d5..134f995a 100644 --- a/context_test.go +++ b/context_test.go @@ -2300,6 +2300,24 @@ func TestContextClientIP(t *testing.T) { assert.Empty(t, c.ClientIP()) } +// Over a unix socket without a usable forwarding header, ClientIP() must return +// an empty string instead of "" (net.IP(nil).String()). See #2718. +func TestContextClientIPUnixSocketWithoutHeaders(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest(http.MethodGet, "/", nil) + addr := &net.UnixAddr{Net: "unix", Name: "@"} + c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), http.LocalAddrContextKey, addr)) + c.Request.RemoteAddr = addr.String() + + assert.Empty(t, c.ClientIP()) + + c.Request.Header.Set("X-Real-IP", "5.6.7.8") + assert.Equal(t, "5.6.7.8", c.ClientIP()) + + c.Request.Header.Set("X-Forwarded-For", "1.2.3.4") + assert.Equal(t, "1.2.3.4", c.ClientIP()) +} + func resetContextForClientIPTests(c *Context) { c.Request.Header.Set("X-Real-IP", " 10.10.10.10 ") c.Request.Header.Set("X-Forwarded-For", " 20.20.20.20, 30.30.30.30") diff --git a/gin.go b/gin.go index a1607275..27ab9b87 100644 --- a/gin.go +++ b/gin.go @@ -471,6 +471,12 @@ func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) { // by default. If you want to disable this feature, use // Engine.SetTrustedProxies(nil), then Context.ClientIP() will // return the remote address directly. +// +// Note: this does not apply when gin serves requests over a unix socket. +// In that case ClientIP() always treats the connection as trusted and still +// consults Engine.RemoteIPHeaders, even after SetTrustedProxies(nil). To make +// ClientIP() ignore those headers for a unix socket, disable +// Engine.ForwardedByClientIP (or set Engine.RemoteIPHeaders to nil). func (engine *Engine) SetTrustedProxies(trustedProxies []string) error { engine.trustedProxies = trustedProxies return engine.parseTrustedProxies()