Skip to content

Commit

Permalink
fix X-Forwarded-For HTTP header not getting passed in app access requ…
Browse files Browse the repository at this point in the history
…ests (#44579)

* fix X-Forwarded-For HTTP header not getting passed in app access requests

* Use `XForwardedFor` constant

Co-authored-by: Zac Bergquist <[email protected]>

---------

Co-authored-by: Andrew LeFevre <Andrew LeFevre>
Co-authored-by: Zac Bergquist <[email protected]>
  • Loading branch information
capnspacehook and zmb3 authored Jul 24, 2024
1 parent de63b02 commit 7edc922
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/httplib/reverseproxy/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ func maybeSetXRealIP(req *http.Request) {
// maybeSetForwarded sets X-Forwarded-* headers if it is not set to the
// scheme of the request.
func maybeSetForwarded(req *http.Request) {
// We need to delete the value because httputil.ReverseProxy
// appends to the existing value.
req.Header.Del(XForwardedFor)
// Set X-Forwarded-For since net/http/httputil.ReverseProxy won't
// do this when Rewrite is set.
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
req.Header.Set(XForwardedFor, clientIP)
}

if req.Header.Get(XForwardedProto) != "" {
return
Expand Down
5 changes: 5 additions & 0 deletions lib/httplib/reverseproxy/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestRewriter(t *testing.T) {
hostReq: "teleport.dev:3543",
remoteAddr: "1.2.3.4:1234",
expected: http.Header{
XForwardedFor: []string{"1.2.3.4"},
XForwardedHost: []string{"teleport.dev:3543"},
XForwardedPort: []string{"3543"},
XForwardedProto: []string{"https"},
Expand All @@ -117,6 +118,7 @@ func TestRewriter(t *testing.T) {
hostReq: "teleport.dev:3543",
remoteAddr: "1.2.3.4:1234",
expected: http.Header{
XForwardedFor: []string{"1.2.3.4"},
XForwardedHost: []string{"teleport.dev:3543"},
XForwardedPort: []string{"3543"},
XForwardedProto: []string{"http"},
Expand All @@ -133,6 +135,7 @@ func TestRewriter(t *testing.T) {
hostReq: "teleport.dev",
remoteAddr: "1.2.3.4:1234",
expected: http.Header{
XForwardedFor: []string{"1.2.3.4"},
XForwardedHost: []string{"teleport.dev"},
XForwardedPort: []string{"80"},
XForwardedProto: []string{"http"},
Expand All @@ -141,9 +144,11 @@ func TestRewriter(t *testing.T) {
},
},
}

rewriter := NewHeaderRewriter()
// set hostname to make sure it's the same in all tests.
rewriter.Hostname = hostname

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
Expand Down

0 comments on commit 7edc922

Please sign in to comment.