Skip to content

Commit

Permalink
feat: special case CloudFlare client IP addrs (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanodevium authored Oct 6, 2023
1 parent 8cd8f9c commit e3c4f8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions httpbin/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func getClientIP(r *http.Request) string {
if clientIP := r.Header.Get("Fly-Client-IP"); clientIP != "" {
return clientIP
}
if clientIP := r.Header.Get("CF-Connecting-IP"); clientIP != "" {
return clientIP
}

// Try to pull a reasonable value from the X-Forwarded-For header, if
// present, by taking the first entry in a comma-separated list of IPs.
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 e3c4f8d

Please sign in to comment.