diff --git a/proxy/http_headers.go b/proxy/http_headers.go index 43671aa23..c925d240a 100644 --- a/proxy/http_headers.go +++ b/proxy/http_headers.go @@ -67,6 +67,10 @@ func addHeaders(r *http.Request, cfg config.Proxy, stripPath string) error { r.Header.Set("X-Forwarded-Port", localPort(r)) } + if r.Header.Get("X-Forwarded-Host") == "" { + r.Header.Set("X-Forwarded-Host", r.Host) + } + if stripPath != "" { r.Header.Set("X-Forwarded-Prefix", stripPath) } diff --git a/proxy/http_headers_test.go b/proxy/http_headers_test.go index 70fd7d7cf..92bdaea79 100644 --- a/proxy/http_headers_test.go +++ b/proxy/http_headers_test.go @@ -37,6 +37,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Port": []string{"80"}, "X-Forwarded-Prefix": []string{"/foo"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -50,6 +51,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"443"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -65,6 +67,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -80,6 +83,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"443"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -94,6 +98,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -107,6 +112,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -120,6 +126,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"443"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -133,6 +140,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"443"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -146,6 +154,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -159,6 +168,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -173,6 +183,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"443"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -187,6 +198,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"443"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -201,6 +213,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"443"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -214,6 +227,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -227,6 +241,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"some value"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -240,6 +255,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"some value"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -253,6 +269,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"some value"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -272,6 +289,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"other value"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, }, "", }, @@ -285,6 +303,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"1234"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{"5.6.7.8:1234"}, }, "", }, @@ -298,6 +317,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"https"}, "X-Forwarded-Port": []string{"1234"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{"5.6.7.8:1234"}, }, "", }, @@ -311,6 +331,35 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"4444"}, "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{""}, + }, + "", + }, + + {"set X-Forwarded-Host from Host", + &http.Request{RemoteAddr: "1.2.3.4:5555", Host: "5.6.7.8:1234"}, + config.Proxy{}, + "", + http.Header{ + "Forwarded": []string{"for=1.2.3.4; proto=http"}, + "X-Forwarded-Proto": []string{"http"}, + "X-Forwarded-Port": []string{"1234"}, + "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{"5.6.7.8:1234"}, + }, + "", + }, + + {"do not overwrite X-Forwarded-Host, if present", + &http.Request{RemoteAddr: "1.2.3.4:5555", Host: "5.6.7.8:1234", Header: http.Header{"X-Forwarded-Host": {"9.10.11.12:1234"}}}, + config.Proxy{}, + "", + http.Header{ + "Forwarded": []string{"for=1.2.3.4; proto=http"}, + "X-Forwarded-Proto": []string{"http"}, + "X-Forwarded-Port": []string{"1234"}, + "X-Real-Ip": []string{"1.2.3.4"}, + "X-Forwarded-Host": []string{"9.10.11.12:1234"}, }, "", }, @@ -324,6 +373,7 @@ func TestAddHeaders(t *testing.T) { "X-Forwarded-Proto": []string{"http"}, "X-Forwarded-Port": []string{"80"}, "X-Real-Ip": []string{"6.6.6.6"}, + "X-Forwarded-Host": []string{""}, }, "", },