mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-22 02:43:36 +08:00
Merge 6fe0058c3ef2093430dae5b243071b1aa8ca99c9 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646
This commit is contained in:
commit
9f659b14be
12
context.go
12
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
|
||||
// "<nil>" produced by net.IP(nil).String().
|
||||
if remoteIP == nil {
|
||||
return ""
|
||||
}
|
||||
return remoteIP.String()
|
||||
}
|
||||
|
||||
|
||||
@ -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
6
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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user