Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: special case CloudFlare client IP addrs #148

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading