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 duplicate entry for X-Forwarded-For header #32039

Merged
merged 2 commits into from
Sep 18, 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
12 changes: 8 additions & 4 deletions lib/httplib/reverseproxy/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (rw *HeaderRewriter) Rewrite(req *http.Request) {
// Set X-Real-IP header if it is not set to the IP address of the client making the request.
maybeSetXRealIP(req)

// Set X-Forwarded-Proto header if it is not set to the scheme of the request.
maybeSetForwardedProto(req)
// Set X-Forwarded-* headers if it is not set to the scheme of the request.
maybeSetForwarded(req)

if xfPort := req.Header.Get(XForwardedPort); xfPort == "" {
req.Header.Set(XForwardedPort, forwardedPort(req))
Expand Down Expand Up @@ -104,9 +104,13 @@ func maybeSetXRealIP(req *http.Request) {
}
}

// maybeSetForwardedProto sets X-Forwarded-Proto header if it is not set to the
// maybeSetForwarded sets X-Forwarded-* headers if it is not set to the
// scheme of the request.
func maybeSetForwardedProto(req *http.Request) {
func maybeSetForwarded(req *http.Request) {
// We need to delete the value because httputil.ReverseProxy
// appends to the existing value.
req.Header.Del(XForwardedFor)

if req.Header.Get(XForwardedProto) != "" {
return
}
Expand Down
9 changes: 6 additions & 3 deletions lib/httplib/reverseproxy/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ func TestRewriter(t *testing.T) {
expected http.Header
}{
{
desc: "set x-real-ip",
reqHeaders: http.Header{},
desc: "set x-real-ip",
reqHeaders: http.Header{
XForwardedFor: []string{"1.2.3.4"},
},
tlsReq: true,
hostReq: "teleport.dev:3543",
remoteAddr: "1.2.3.4:1234",
Expand All @@ -105,7 +107,8 @@ func TestRewriter(t *testing.T) {
{
desc: "trust x-real-ip",
reqHeaders: http.Header{
XRealIP: []string{"5.6.7.8"},
XRealIP: []string{"5.6.7.8"},
XForwardedFor: []string{"1.2.3.4"},
},
tlsReq: false,
hostReq: "teleport.dev:3543",
Expand Down
Loading