httpcaddyfile: isolate TLS policies across listeners (#8192)

(cherry picked from commit 25c6901e05)
This commit is contained in:
Zen Dodd 2026-10-12 16:38:47 +10:00 • committed by Francis Lavoie
parent bf3fa59201
commit 3a46d767ff
No known key found for this signature in database
2 changed files with 99 additions and 1 deletions

View file

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

View file

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