Skip to content

Commit

Permalink
Split XFF header only by comma
Browse files Browse the repository at this point in the history
  • Loading branch information
osavchenko committed May 20, 2021
1 parent 2acb24a commit 04f8493
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions context.go
Expand Up @@ -276,9 +276,9 @@ func (c *context) RealIP() string {
}
// Fall back to legacy behavior
if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
i := strings.IndexAny(ip, ", ")
i := strings.IndexAny(ip, ",")
if i > 0 {
return ip[:i]
return strings.TrimSpace(ip[:i])
}
return ip
}
Expand Down
8 changes: 8 additions & 0 deletions context_test.go
Expand Up @@ -888,6 +888,14 @@ func TestContext_RealIP(t *testing.T) {
},
"127.0.0.1",
},
{
&context{
request: &http.Request{
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1,127.0.1.1"}},
},
},
"127.0.0.1",
},
{
&context{
request: &http.Request{
Expand Down

0 comments on commit 04f8493

Please sign in to comment.