diff --git a/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go b/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go index a3c30457d..2de60b1f5 100644 --- a/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go +++ b/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go @@ -437,10 +437,15 @@ func (t Transport) buildEnv(r *http.Request) (envVars, error) { // Add all HTTP headers to env variables for field, val := range r.Header { + switch http.CanonicalHeaderKey(field) { + // HTTP_HOST comes from r.Host, the host the request was routed on: + // over HTTP/2, net/http keeps a "host" field distinct from :authority. + case "Host": + continue // HTTPoxy mitigation: client-supplied Proxy header must not // be trusted for setting the HTTP_PROXY environment variable. // See https://httpoxy.org for details. - if http.CanonicalHeaderKey(field) == "Proxy" { + case "Proxy": continue } diff --git a/modules/caddyhttp/reverseproxy/fastcgi/fastcgi_test.go b/modules/caddyhttp/reverseproxy/fastcgi/fastcgi_test.go index 78aef091b..1a7037abc 100644 --- a/modules/caddyhttp/reverseproxy/fastcgi/fastcgi_test.go +++ b/modules/caddyhttp/reverseproxy/fastcgi/fastcgi_test.go @@ -487,6 +487,28 @@ func TestBuildEnvDropsProxyHeader(t *testing.T) { }) } +// Over HTTP/2, net/http keeps a "host" field sent next to :authority in +// r.Header; it must not replace HTTP_HOST, which comes from the routed r.Host. +func TestBuildEnvIgnoresHostHeaderField(t *testing.T) { + t.Parallel() + + for _, casing := range []string{"Host", "host"} { + t.Run("casing "+casing, func(t *testing.T) { + r := httptest.NewRequest(http.MethodGet, "http://example.com/index.php", nil) + r.Header = http.Header{casing: []string{"admin.example.com"}} + ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, caddy.NewReplacer()) + ctx = context.WithValue(ctx, caddyhttp.VarsCtxKey, map[string]any{}) + ctx = context.WithValue(ctx, caddyhttp.OriginalRequestCtxKey, *r) + + env, err := Transport{}.buildEnv(r.WithContext(ctx)) + require.NoError(t, err) + + assert.Equal(t, "example.com", env["HTTP_HOST"]) + assert.Equal(t, "example.com", env["SERVER_NAME"]) + }) + } +} + // A request path containing dot segments must not let the client select a // different script than the one every path matcher in the chain evaluated. // Matchers judge the cleaned path (see caddyhttp.MatchPath), so the transport