Fix Trusted Header Parsing for Cloudflare (#20359)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
19098e93fc
Коммит
182536b87d
@@ -101,9 +101,9 @@ func GetIPAddress(r *http.Request, trustedProxyIPHeader []string) string {
|
||||
for _, proxyHeader := range trustedProxyIPHeader {
|
||||
header := r.Header.Get(proxyHeader)
|
||||
if header != "" {
|
||||
addresses := strings.Fields(header)
|
||||
addresses := strings.Split(header, ",")
|
||||
if len(addresses) > 0 {
|
||||
address = strings.TrimRight(addresses[0], ",")
|
||||
address = strings.TrimSpace(addresses[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -152,6 +152,17 @@ func TestGetIPAddress(t *testing.T) {
|
||||
}
|
||||
|
||||
assert.Equal(t, "10.1.0.1", GetIPAddress(&httpRequest10, []string{"X-Real-Ip", "X-Forwarded-For"}))
|
||||
|
||||
// Test with multiple IPs in the X-Forwarded-For with no spaces
|
||||
httpRequest11 := http.Request{
|
||||
Header: http.Header{
|
||||
"X-Forwarded-For": []string{"10.0.0.1,10.0.0.2,10.0.0.3"},
|
||||
"X-Real-Ip": []string{"10.1.0.1"},
|
||||
},
|
||||
RemoteAddr: "10.2.0.1:12345",
|
||||
}
|
||||
|
||||
assert.Equal(t, "10.0.0.1", GetIPAddress(&httpRequest11, []string{"X-Forwarded-For"}))
|
||||
}
|
||||
|
||||
func TestRemoveStringFromSlice(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user