Skip to content
Myr

chore(context): always trust xff headers from unix socket (#3359)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by WeidiDeng · committed by GitHub · parent 3ab698d · Browse files · 2 files+30 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+30 added−7 removed

  • other:+30 added−7 removed

2 files+30 added−7 removed

modified:context_test.go+10 added
1913 unmodified lines
1914 c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)1915 c.engine.trustedCIDRs, _ = c.engine.prepareTrustedCIDRs()1916 resetContextForClientIPTests(c)19171918 // unix address1919 addr := &net.UnixAddr{Net: "unix", Name: "@"}1920 c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), http.LocalAddrContextKey, addr))1921 c.Request.RemoteAddr = addr.String()1922 assert.Equal(t, "20.20.20.20", c.ClientIP())19231924 // reset1925 c.Request = c.Request.WithContext(context.Background())1926 resetContextForClientIPTests(c)19271928 // Legacy tests (validating that the defaults don't break the1929 // (insecure!) old behaviour)1930 assert.Equal(t, "20.20.20.20", c.ClientIP())1931
1789 unmodified lines
modified:context.go+20 added-7 removed
976 unmodified lines
977 return addr978 }979 }980981 // It also checks if the remoteIP is a trusted proxy or not.982 // In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks983 // defined by Engine.SetTrustedProxies()984 remoteIP := net.ParseIP(c.RemoteIP())985 if remoteIP == nil {986 return ""981 var (982 trusted bool983 remoteIP net.IP984 )985 // If gin is listening a unix socket, always trust it.986 localAddr, ok := c.Request.Context().Value(http.LocalAddrContextKey).(net.Addr)987 if ok && strings.HasPrefix(localAddr.Network(), "unix") {988 trusted = true989 }988 trusted := c.engine.isTrustedProxy(remoteIP)990991 // Fallback992 if !trusted {993 // It also checks if the remoteIP is a trusted proxy or not.994 // In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks995 // defined by Engine.SetTrustedProxies()996 remoteIP = net.ParseIP(c.RemoteIP())997 if remoteIP == nil {998 return ""999 }1000 trusted = c.engine.isTrustedProxy(remoteIP)1001 }10021003 if trusted && c.engine.ForwardedByClientIP && c.engine.RemoteIPHeaders != nil {1004 for _, headerName := range c.engine.RemoteIPHeaders {1005 headerValue := strings.Join(c.Request.Header.Values(headerName), ",")1006 ip, valid := c.engine.validateHeader(headerValue)
455 unmodified lines