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

Simpler and actually correct x-forwarded-for fix #641

Merged
merged 2 commits into from
Dec 20, 2024
Merged
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
9 changes: 5 additions & 4 deletions proxyfilters/forwardedfor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proxyfilters
import (
"net"
"net/http"
"strings"

"github.com/getlantern/proxy/v3/filters"
)
Expand All @@ -17,10 +16,12 @@ const (
var AddForwardedFor = filters.FilterFunc(func(cs *filters.ConnectionState, req *http.Request, next filters.Next) (*http.Response, *filters.ConnectionState, error) {
if req.Method != http.MethodConnect {
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
if prior, ok := req.Header[xForwardedFor]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
// Proxies are supposed to actually overwrite previous values, as they
// can be maliciously set by the client.
req.Header.Set(xForwardedFor, clientIP)
} else {
// If we can't parse the client IP, we should remove the header.
req.Header.Del(xForwardedFor)
}
}
return next(cs, req)
Expand Down
Loading