From c8591a1e46e4d0e39b6ca20633c44cd42f874f71 Mon Sep 17 00:00:00 2001 From: awss Date: Fri, 9 Oct 2026 07:48:00 +0100 Subject: [PATCH] httpcaddyfile: keep a site block's TLS policy for ECH public names (#8156) * httpcaddyfile: keep a site block's TLS policy for ECH public names The global ech option added its own automation policy for every public name, even when a site block already had a policy for that name. If the site block set its own TLS options, adapting failed with "hostname appears in more than one automation policy". If a hostless catch-all with the same settings was also present, the site's policy was merged away and the name got only the global settings. Skip public names that already have a policy, as tls_automate_names does, and only add the ECH policy when it still has subjects, since a policy without subjects acts as a catch-all. * httpcaddyfile: check ECH public names the way the TLS app matches them The check for an existing site policy only matched a subject spelled exactly like the ECH public name. A site policy for *.example.com, or for example.com when the public name is written Example.COM, already covers that name at runtime, but the global ECH policy was still added and sorted ahead of it, so the site's issuer settings were overridden. Lowercase and trim the public name as ECH provisioning does and compare it with certmagic.MatchWildcard, which is how the TLS app picks a policy for a name. --- caddyconfig/httpcaddyfile/tlsapp.go | 36 ++++++-- .../ech_site_block_mixed_case.caddyfiletest | 56 ++++++++++++ .../ech_site_block_tls_internal.caddyfiletest | 76 +++++++++++++++++ ...ech_site_block_tls_resolvers.caddyfiletest | 85 +++++++++++++++++++ .../ech_site_block_wildcard.caddyfiletest | 76 +++++++++++++++++ ...ch_site_block_with_catch_all.caddyfiletest | 76 +++++++++++++++++ 6 files changed, 399 insertions(+), 6 deletions(-) create mode 100644 caddytest/integration/caddyfile_adapt/ech_site_block_mixed_case.caddyfiletest create mode 100644 caddytest/integration/caddyfile_adapt/ech_site_block_tls_internal.caddyfiletest create mode 100644 caddytest/integration/caddyfile_adapt/ech_site_block_tls_resolvers.caddyfiletest create mode 100644 caddytest/integration/caddyfile_adapt/ech_site_block_wildcard.caddyfiletest create mode 100644 caddytest/integration/caddyfile_adapt/ech_site_block_with_catch_all.caddyfiletest diff --git a/caddyconfig/httpcaddyfile/tlsapp.go b/caddyconfig/httpcaddyfile/tlsapp.go index 833586e8d..47cfa7b1b 100644 --- a/caddyconfig/httpcaddyfile/tlsapp.go +++ b/caddyconfig/httpcaddyfile/tlsapp.go @@ -344,20 +344,27 @@ func (st ServerType) buildTLSApp( tlsApp.EncryptedClientHello = ech // outer server names will need certificates, so make sure they're included - // in an automation policy for them that applies any global options + // in an automation policy for them that applies any global options. A name + // that a site block's policy already covers keeps that policy, because a + // second policy for it would be rejected as ambiguous or picked ahead of + // the site's. ap, err := newBaseAutomationPolicy(options, warnings, true) if err != nil { return nil, warnings, err } for _, cfg := range ech.Configs { - if cfg.PublicName != "" { - ap.SubjectsRaw = append(ap.SubjectsRaw, cfg.PublicName) + if cfg.PublicName == "" || automationPolicyCoversName(tlsApp.Automation, cfg.PublicName) { + continue } + ap.SubjectsRaw = append(ap.SubjectsRaw, cfg.PublicName) } - if tlsApp.Automation == nil { - tlsApp.Automation = new(caddytls.AutomationConfig) + // without subjects, the policy would apply to every name as a catch-all + if len(ap.SubjectsRaw) > 0 { + if tlsApp.Automation == nil { + tlsApp.Automation = new(caddytls.AutomationConfig) + } + tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap) } - tlsApp.Automation.Policies = append(tlsApp.Automation.Policies, ap) } // if the storage clean interval is a boolean, then it's "off" to disable cleaning @@ -955,6 +962,23 @@ func automationPolicyExistsForSubject(automation *caddytls.AutomationConfig, sub }) } +// automationPolicyCoversName reports whether some automation policy with +// subjects would be selected for the ECH public name at runtime. The TLS app +// trims and lowercases public names, then picks a policy for each one with +// certmagic.MatchWildcard, so a wildcard or differently cased subject covers +// it too. A catch-all policy names no subjects and so never matches. +func automationPolicyCoversName(automation *caddytls.AutomationConfig, publicName string) bool { + if automation == nil { + return false + } + name := strings.ToLower(strings.TrimSpace(publicName)) + return slices.ContainsFunc(automation.Policies, func(ap *caddytls.AutomationPolicy) bool { + return slices.ContainsFunc(ap.SubjectsRaw, func(subj string) bool { + return certmagic.MatchWildcard(name, subj) + }) + }) +} + // hasGlobalACMEDefaults reports whether any global option is set that an // automation policy without issuers of its own would later be filled in with. // A policy is worth creating for a subject when this is true, even if the diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_mixed_case.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_mixed_case.caddyfiletest new file mode 100644 index 000000000..fa0e8e98a --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_mixed_case.caddyfiletest @@ -0,0 +1,56 @@ +{ + ech Example.COM +} + +example.com { + tls internal +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "example.com" + ], + "issuers": [ + { + "module": "internal" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "Example.COM" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_tls_internal.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_internal.caddyfiletest new file mode 100644 index 000000000..c703eede3 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_internal.caddyfiletest @@ -0,0 +1,76 @@ +{ + email nobody@example.com + ech example.com ech.example.net +} + +example.com { + tls internal +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "example.com" + ], + "issuers": [ + { + "module": "internal" + } + ] + }, + { + "subjects": [ + "ech.example.net" + ], + "issuers": [ + { + "email": "nobody@example.com", + "module": "acme" + }, + { + "ca": "https://acme.zerossl.com/v2/DV90", + "email": "nobody@example.com", + "module": "acme" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "example.com" + }, + { + "public_name": "ech.example.net" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_tls_resolvers.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_resolvers.caddyfiletest new file mode 100644 index 000000000..1dd6035df --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_tls_resolvers.caddyfiletest @@ -0,0 +1,85 @@ +{ + acme_dns mock foo + ech example.com +} + +example.com { + tls { + resolvers 1.1.1.1 + } +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "example.com" + ], + "issuers": [ + { + "challenges": { + "dns": { + "provider": { + "argument": "foo", + "name": "mock" + }, + "resolvers": [ + "1.1.1.1" + ] + } + }, + "module": "acme" + } + ] + }, + { + "issuers": [ + { + "challenges": { + "dns": { + "provider": { + "argument": "foo", + "name": "mock" + } + } + }, + "module": "acme" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "example.com" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_wildcard.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_wildcard.caddyfiletest new file mode 100644 index 000000000..cf11644ea --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_wildcard.caddyfiletest @@ -0,0 +1,76 @@ +{ + email nobody@example.com + ech ech.example.com ech.example.net +} + +*.example.com { + tls internal +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "*.example.com" + ] + } + ], + "terminal": true + } + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "subjects": [ + "*.example.com" + ], + "issuers": [ + { + "module": "internal" + } + ] + }, + { + "subjects": [ + "ech.example.net" + ], + "issuers": [ + { + "email": "nobody@example.com", + "module": "acme" + }, + { + "ca": "https://acme.zerossl.com/v2/DV90", + "email": "nobody@example.com", + "module": "acme" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "ech.example.com" + }, + { + "public_name": "ech.example.net" + } + ] + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/ech_site_block_with_catch_all.caddyfiletest b/caddytest/integration/caddyfile_adapt/ech_site_block_with_catch_all.caddyfiletest new file mode 100644 index 000000000..e4e1e1f66 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/ech_site_block_with_catch_all.caddyfiletest @@ -0,0 +1,76 @@ +{ + ech example.com +} + +example.com { + tls internal +} + +:8443 { + tls internal + respond "catch-all" +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "example.com" + ] + } + ], + "terminal": true + } + ] + }, + "srv1": { + "listen": [ + ":8443" + ], + "routes": [ + { + "handle": [ + { + "body": "catch-all", + "handler": "static_response" + } + ] + } + ], + "tls_connection_policies": [ + {} + ] + } + } + }, + "tls": { + "automation": { + "policies": [ + { + "issuers": [ + { + "module": "internal" + } + ] + } + ] + }, + "encrypted_client_hello": { + "configs": [ + { + "public_name": "example.com" + } + ] + } + } + } +}