fastcgi: take HTTP_HOST from the routed host only (#8355)

This commit is contained in:
Kévin Dunglas 2026-11-04 20:32:20 +01:00 committed by GitHub
parent cf1b1121e7
commit bd56cc1748
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View file

@ -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
}

View file

@ -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