Skip to content

Commit fdacff0

Browse files
osavchenkoaldas
authored andcommitted
Split XFF header only by comma
1 parent 1c24ab8 commit fdacff0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

context.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ func (c *context) RealIP() string {
276276
}
277277
// Fall back to legacy behavior
278278
if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
279-
i := strings.IndexAny(ip, ", ")
279+
i := strings.IndexAny(ip, ",")
280280
if i > 0 {
281-
return ip[:i]
281+
return strings.TrimSpace(ip[:i])
282282
}
283283
return ip
284284
}

context_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,14 @@ func TestContext_RealIP(t *testing.T) {
888888
},
889889
"127.0.0.1",
890890
},
891+
{
892+
&context{
893+
request: &http.Request{
894+
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1,127.0.1.1"}},
895+
},
896+
},
897+
"127.0.0.1",
898+
},
891899
{
892900
&context{
893901
request: &http.Request{

0 commit comments

Comments
 (0)