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 "<nil>". Return an empty string instead, completing #2718.

Also document that over a unix socket RemoteIPHeaders are consulted
regardless of SetTrustedProxies, including after SetTrustedProxies(nil).
This commit is contained in:
takumireid 2026-09-26 00:27:24 +09:00
parent 1bd0ecf2d3
commit 9aff161d86
3 changed files with 36 additions and 0 deletions

View File

@ -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
// "<nil>" produced by net.IP(nil).String().
if remoteIP == nil {
return ""
}
return remoteIP.String()
}

View File

@ -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 "<nil>" (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")

6
gin.go
View File

@ -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()