Skip to content

Commit

Permalink
Merge pull request #380 from LeReverandNox/x-forwarded-host-support
Browse files Browse the repository at this point in the history
Set X-Forwarded-Host header if not present.
  • Loading branch information
magiconair authored Nov 30, 2017
2 parents 83f7b2c + 88c76ff commit 96adb21
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions proxy/http_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
50 changes: 50 additions & 0 deletions proxy/http_headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand All @@ -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"},
},
"",
},
Expand All @@ -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"},
},
"",
},
Expand All @@ -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"},
},
"",
},
Expand All @@ -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{""},
},
"",
},
Expand Down

0 comments on commit 96adb21

Please sign in to comment.