Commit 1165c5f5 authored by Alessio Caiazza's avatar Alessio Caiazza

Merge branch 'cat-workhorse-host-rewrite-to-x-forwarded' into 'master'

Include X-Forwarded-Host when proxying and rewriting Host in Workhorse

See merge request gitlab-org/gitlab!84368
parents c6f54adf 8506644a
......@@ -57,6 +57,14 @@ func NewProxy(myURL *url.URL, version string, roundTripper http.RoundTripper, op
previousDirector := p.reverseProxy.Director
p.reverseProxy.Director = func(request *http.Request) {
previousDirector(request)
// send original host along for the upstream
// to know it's being proxied under a different Host
// (for redirects and other stuff that depends on this)
request.Header.Set("X-Forwarded-Host", request.Host)
request.Header.Set("Forwarded", fmt.Sprintf("host=%s", request.Host))
// override the Host with the target
request.Host = request.URL.Host
}
}
......
......@@ -41,6 +41,8 @@ func TestProxyRequest(t *testing.T) {
require.Equal(t, "test", r.Header.Get("Custom-Header"), "custom header")
require.Equal(t, testVersion, r.Header.Get("Gitlab-Workhorse"), "version header")
require.Equal(t, inboundURL.Host, r.Host, "sent host header")
require.Empty(t, r.Header.Get("X-Forwarded-Host"), "X-Forwarded-Host header")
require.Empty(t, r.Header.Get("Forwarded"), "Forwarded header")
require.Regexp(
t,
......@@ -78,6 +80,8 @@ func TestProxyWithForcedTargetHostHeader(t *testing.T) {
urlRegexp := regexp.MustCompile(fmt.Sprintf(`%s\z`, inboundURL.Path))
ts := testhelper.TestServerWithHandler(urlRegexp, func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, tsUrl.Host, r.Host, "upstream host header")
require.Equal(t, inboundURL.Host, r.Header.Get("X-Forwarded-Host"), "X-Forwarded-Host header")
require.Equal(t, fmt.Sprintf("host=%s", inboundURL.Host), r.Header.Get("Forwarded"), "Forwarded header")
_, err := w.Write([]byte(`ok`))
require.NoError(t, err, "write ok response")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment