fastcgi: let configured env variables override request headers (#8358)

This commit is contained in:
Kévin Dunglas 2026-11-05 00:02:29 +01:00 committed by GitHub
parent c8492563ee
commit 70dca34a36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 5 deletions

View file

@ -437,11 +437,6 @@ func (t Transport) buildEnv(r *http.Request) (envVars, error) {
}
}
// Add env variables from config (with support for placeholders in values)
for key, value := range t.EnvVars {
env[key] = repl.ReplaceAll(value, "")
}
// Add all HTTP headers to env variables
for field, val := range r.Header {
switch http.CanonicalHeaderKey(field) {
@ -460,6 +455,13 @@ func (t Transport) buildEnv(r *http.Request) (envVars, error) {
header = headerNameReplacer.Replace(header)
env["HTTP_"+header] = strings.Join(val, ", ")
}
// Add env variables from config (with support for placeholders in values)
// last, so a client header cannot override an HTTP_* variable set by the operator
for key, value := range t.EnvVars {
env[key] = repl.ReplaceAll(value, "")
}
return env, nil
}

View file

@ -509,6 +509,30 @@ func TestBuildEnvIgnoresHostHeaderField(t *testing.T) {
}
}
// A client header must not override an HTTP_* variable set with the env subdirective.
func TestBuildEnvConfiguredVarsOverrideHeaders(t *testing.T) {
t.Parallel()
r := httptest.NewRequest(http.MethodGet, "http://example.com/index.php", nil)
r.Header.Set("X-Forwarded-Proto", "http")
r.Header.Set("X-Auth-User", "admin")
repl := caddy.NewReplacer()
repl.Set("http.auth.user.id", "alice")
ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
ctx = context.WithValue(ctx, caddyhttp.VarsCtxKey, map[string]any{})
ctx = context.WithValue(ctx, caddyhttp.OriginalRequestCtxKey, *r)
tr := Transport{EnvVars: map[string]string{
"HTTP_X_FORWARDED_PROTO": "https",
"HTTP_X_AUTH_USER": "{http.auth.user.id}",
}}
env, err := tr.buildEnv(r.WithContext(ctx))
require.NoError(t, err)
assert.Equal(t, "https", env["HTTP_X_FORWARDED_PROTO"])
assert.Equal(t, "alice", env["HTTP_X_AUTH_USER"])
}
// 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