Skip to content

Commit

Permalink
Cloudflare header CF-Connecting-IP
Browse files Browse the repository at this point in the history
  • Loading branch information
vanodevium committed Oct 6, 2023
1 parent c2ae4ed commit 81de5bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions httpbin/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func getClientIP(r *http.Request) string {
}

// Special case Cloudflare for the value directly.
if cfClientIP := r.Header.Get("CF-Connecting-IP"); cfClientIP != "" {
return cfClientIP
if clientIP := r.Header.Get("CF-Connecting-IP"); clientIP != "" {
return clientIP
}

// Try to pull a reasonable value from the X-Forwarded-For header, if
Expand Down
10 changes: 10 additions & 0 deletions httpbin/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ func TestGetClientIP(t *testing.T) {
},
want: "9.9.9.9",
},
"custom cloudflare header take precedence": {
given: &http.Request{
Header: makeHeaders(map[string]string{
"CF-Connecting-IP": "9.9.9.9",
"X-Forwarded-For": "1.1.1.1,2.2.2.2,3.3.3.3",
}),
RemoteAddr: "0.0.0.0",
},
want: "9.9.9.9",
},
"x-forwarded-for is parsed": {
given: &http.Request{
Header: makeHeaders(map[string]string{
Expand Down

0 comments on commit 81de5bf

Please sign in to comment.