From 3a46d767ff8e1d2e3bf681bbea8de13441794da0 Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Mon, 12 Oct 2026 16:38:47 +1000 Subject: [PATCH] httpcaddyfile: isolate TLS policies across listeners (#8192) (cherry picked from commit 25c6901e05214573b87c48e07e3913e319032ec6) --- caddyconfig/httpcaddyfile/httptype.go | 20 +++++- caddyconfig/httpcaddyfile/httptype_test.go | 80 ++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index ac6f78e3b..0ef715d5c 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -804,7 +804,12 @@ func (st *ServerType) serversFromPairings( if cpVals, ok := sblock.pile["tls.connection_policy"]; ok { // tls connection policies for _, cpVal := range cpVals { - cp := cpVal.Value.(*caddytls.ConnectionPolicy) + // Policies are parsed once per original server block. Each listener + // rewrites and consolidates them independently. + cp, err := cloneConnectionPolicy(cpVal.Value.(*caddytls.ConnectionPolicy)) + if err != nil { + return nil, fmt.Errorf("cloning TLS connection policy: %v", err) + } // make sure the policy covers all hostnames from the block for _, h := range hosts { @@ -1129,6 +1134,19 @@ func (st *ServerType) serversFromPairings( return servers, nil } +func cloneConnectionPolicy(cp *caddytls.ConnectionPolicy) (*caddytls.ConnectionPolicy, error) { + encoded, err := json.Marshal(cp) + if err != nil { + return nil, err + } + + cloned := new(caddytls.ConnectionPolicy) + if err := json.Unmarshal(encoded, cloned); err != nil { + return nil, err + } + return cloned, nil +} + // sniNames returns the server names a connection policy's sni matcher matches. // The bool is false when the policy has no sni matcher, or when it does not // decode - the latter is unexpected enough to warn about rather than silently diff --git a/caddyconfig/httpcaddyfile/httptype_test.go b/caddyconfig/httpcaddyfile/httptype_test.go index b9a94fca9..bb7c98e62 100644 --- a/caddyconfig/httpcaddyfile/httptype_test.go +++ b/caddyconfig/httpcaddyfile/httptype_test.go @@ -2,6 +2,7 @@ package httpcaddyfile import ( "encoding/json" + "slices" "strings" "testing" @@ -298,3 +299,82 @@ func TestDefaultSNIWithoutHTTPS(t *testing.T) { t.Errorf("Expected default_sni 'my-sni.com' in TLS connection policies, but it was missing. Generated JSON: %s", string(result)) } } + +func TestTLSConnectionPoliciesAreIsolatedAcrossListeners(t *testing.T) { + caddyfileStr := `(mtls) { + tls { + client_auth { + mode require_and_verify + trust_pool file /tmp/root.crt + } + } + } + + abc.domain.com, abc.domain.com:2000 { + import mtls + respond "first" + } + + domain.domain.com { + import mtls + respond "second" + } + + ghi.domain.com, ghi.domain.com:2000 { + import mtls + respond "third" + }` + + adapter := caddyfile.Adapter{ServerType: ServerType{}} + result, _, err := adapter.Adapt([]byte(caddyfileStr), nil) + if err != nil { + t.Fatalf("Failed to adapt Caddyfile: %v", err) + } + + var config struct { + Apps struct { + HTTP struct { + Servers map[string]*caddyhttp.Server `json:"servers"` + } `json:"http"` + } `json:"apps"` + } + if err := json.Unmarshal(result, &config); err != nil { + t.Fatalf("Failed to unmarshal JSON config: %v", err) + } + + for listener, expectedNames := range map[string][]string{ + ":2000": {"abc.domain.com", "ghi.domain.com"}, + ":443": {"abc.domain.com", "domain.domain.com", "ghi.domain.com"}, + } { + var server *caddyhttp.Server + for _, candidate := range config.Apps.HTTP.Servers { + if slices.Contains(candidate.Listen, listener) { + server = candidate + break + } + } + if server == nil { + t.Fatalf("Expected a server listening on %s", listener) + } + + var names []string + for _, policy := range server.TLSConnPolicies { + if policy.ClientAuthentication == nil { + continue + } + if policy.ClientAuthentication.Mode != "require_and_verify" { + t.Fatalf("Expected %s client authentication on %s, got %s", "require_and_verify", listener, policy.ClientAuthentication.Mode) + } + var policyNames []string + if err := json.Unmarshal(policy.MatchersRaw["sni"], &policyNames); err != nil { + t.Fatalf("Failed to unmarshal SNI matcher for %s: %v", listener, err) + } + names = append(names, policyNames...) + } + + slices.Sort(names) + if !slices.Equal(names, expectedNames) { + t.Errorf("Expected client authentication for %v on %s, got %v", expectedNames, listener, names) + } + } +}