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

Fix x-forwarded-for header processing for ws connections #860

Merged
merged 2 commits into from
May 6, 2022
Merged
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion proxy/http_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ func addHeaders(r *http.Request, cfg config.Proxy, stripPath string) error {
// http proxy which sets it.
ws := r.Header.Get("Upgrade") == "websocket"
if ws {
r.Header.Set("X-Forwarded-For", remoteIP)
targetHeader := []string{remoteIP}
sourceHeader := r.Header.Get("X-Forwarded-For")
if sourceHeader != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some subtle things going on here. It would be good to mirror the functionality in the standard library:

https://github.com/golang/go/blob/master/src/net/http/httputil/reverseproxy.go#L296

The Get() call on the header only grabs the first element if there is more than one.

targetHeader = append([]string{sourceHeader}, targetHeader...)
}
r.Header.Set("X-Forwarded-For", strings.Join(targetHeader, ","))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Down here, these should be joined by

", "

as it is done in the standdard library here:

https://github.com/golang/go/blob/master/src/net/http/httputil/reverseproxy.go#L303

}

// Issue #133: Setting the X-Forwarded-Proto header to
Expand Down