caddytls: sync ech publication history to the ech configs (#8376)

* sync ech publication history to the ech configs

* sync ech publication history for subsequent publishers

* Add ECH multi-publisher regression test

Co-authored-by: WeidiDeng <28780594+WeidiDeng@users.noreply.github.com>

* create publication history maps before it's copied

* update comments

* fix lint

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: WeidiDeng <28780594+WeidiDeng@users.noreply.github.com>
This commit is contained in:
WeidiDeng 2026-11-08 19:18:43 +08:00 • committed by GitHub
parent 2563d48326
commit daa74db1d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 119 additions and 10 deletions

View file

@ -329,6 +329,18 @@ func (ech *ECH) updateKeyList() {
}
}
// addPublicationHistoryToEchConfigList is needed to initialize the publication history
// because without it, caddy won't remember successfully published ech records in memory.
// Ranging over the config slice copies each struct, so initializing a map on
// a copy would not update the original config. Initialize it before copying instead.
func addPublicationHistoryToEchConfigList(echCfgList echConfigList) {
for i := range echCfgList {
if echCfgList[i].meta.Publications == nil {
echCfgList[i].meta.Publications = make(publicationHistory)
}
}
}
// publishECHConfigs publishes any configs that are configured for publication and which haven't been published already.
func (t *TLS) publishECHConfigs(ctx context.Context, logger *zap.Logger) error {
if err := ctx.Err(); err != nil {
@ -378,7 +390,7 @@ func (t *TLS) publishECHConfigs(ctx context.Context, logger *zap.Logger) error {
// publish with it, and figure out which inner names to publish
// to/for, then publish
for _, publication := range publicationList {
t.EncryptedClientHello.configsMu.RLock()
t.EncryptedClientHello.configsMu.Lock()
// this publication is either configured for specific ECH configs,
// or we just use an implied default of all ECH configs
var echCfgList echConfigList
@ -386,6 +398,7 @@ func (t *TLS) publishECHConfigs(ctx context.Context, logger *zap.Logger) error {
if publication.Configs == nil {
// by default, publish all configs
for _, configs := range t.EncryptedClientHello.configs {
addPublicationHistoryToEchConfigList(configs)
echCfgList = append(echCfgList, configs...)
for _, c := range configs {
configIDs = append(configIDs, c.ConfigID)
@ -394,6 +407,7 @@ func (t *TLS) publishECHConfigs(ctx context.Context, logger *zap.Logger) error {
} else {
for _, cfgOuterName := range publication.Configs {
if cfgList, ok := t.EncryptedClientHello.configs[cfgOuterName]; ok {
addPublicationHistoryToEchConfigList(cfgList)
echCfgList = append(echCfgList, cfgList...)
for _, c := range cfgList {
configIDs = append(configIDs, c.ConfigID)
@ -401,7 +415,7 @@ func (t *TLS) publishECHConfigs(ctx context.Context, logger *zap.Logger) error {
}
}
}
t.EncryptedClientHello.configsMu.RUnlock()
t.EncryptedClientHello.configsMu.Unlock()
// marshal the ECH config list as binary for publication
echCfgListBin, err := echCfgList.MarshalBinary()
@ -519,9 +533,6 @@ func (t *TLS) publishECHConfigs(ctx context.Context, logger *zap.Logger) error {
zap.Uint8s("config_ids", configIDs))
for _, cfg := range echCfgList {
if cfg.meta.Publications == nil {
cfg.meta.Publications = make(publicationHistory)
}
if _, ok := cfg.meta.Publications[publisherKey]; !ok {
cfg.meta.Publications[publisherKey] = make(map[string]time.Time)
}

View file

@ -16,6 +16,7 @@ package caddytls
import (
"context"
"encoding/json"
"io/fs"
"sync"
"testing"
@ -138,10 +139,14 @@ func (s *blockingStorage) Stat(ctx context.Context, key string) (certmagic.KeyIn
}
type blockingPublisher struct {
key string
publishFunc func(ctx context.Context, innerNames []string, echConfigList []byte) error
}
func (p *blockingPublisher) PublisherKey() string {
if p.key != "" {
return p.key
}
return "blocking_test_publisher"
}
@ -152,6 +157,103 @@ func (p *blockingPublisher) PublishECHConfigList(ctx context.Context, innerNames
return nil
}
func TestTLSECHPublishTracksMultiplePublishers(t *testing.T) {
storedMetadata := make(map[string][]byte)
storage := &blockingStorage{
storeFunc: func(_ context.Context, key string, value []byte) error {
storedMetadata[key] = append([]byte(nil), value...)
return nil
},
}
ctx, cleanup := newTestContextWithStorage(t, storage)
defer cleanup()
publishCalls := make(map[string]int)
publisher1 := &blockingPublisher{
key: "publisher1",
publishFunc: func(context.Context, []string, []byte) error {
publishCalls["publisher1"]++
return nil
},
}
publisher2 := &blockingPublisher{
key: "publisher2",
publishFunc: func(context.Context, []string, []byte) error {
publishCalls["publisher2"]++
return nil
},
}
publicKey, _, err := hpke.KEM_X25519_HKDF_SHA256.Scheme().GenerateKeyPair()
if err != nil {
t.Fatalf("GenerateKeyPair failed: %v", err)
}
ech := &ECH{
configsMu: new(sync.RWMutex),
Publication: []*ECHPublication{
{
Domains: []string{"example.com"},
publishers: []ECHPublisher{publisher1, publisher2},
},
},
configs: map[string][]echConfig{
"example.com": {
{
PublicKey: publicKey,
Version: draftTLSESNI25,
ConfigID: 1,
RawPublicName: "example.com",
KEMID: hpke.KEM_X25519_HKDF_SHA256,
CipherSuites: []hpkeSymmetricCipherSuite{
{
KDFID: hpke.KDF_HKDF_SHA256,
AEADID: hpke.AEAD_AES128GCM,
},
},
},
},
},
}
tlsApp := &TLS{
ctx: ctx,
logger: zap.NewNop(),
EncryptedClientHello: ech,
serverNames: map[string]serverNameRegistration{
"example.com": {},
},
}
for i := 0; i < 2; i++ {
if err := tlsApp.publishECHConfigs(ctx.Context, tlsApp.logger); err != nil {
t.Fatalf("publishECHConfigs call %d failed: %v", i+1, err)
}
}
for _, publisherKey := range []string{"publisher1", "publisher2"} {
if publishCalls[publisherKey] != 1 {
t.Errorf("%s was called %d times; want once", publisherKey, publishCalls[publisherKey])
}
}
assertPublicationHistory := func(t *testing.T, history publicationHistory) {
t.Helper()
for _, publisherKey := range []string{"publisher1", "publisher2"} {
if publishedAt := history[publisherKey]["example.com"]; publishedAt.IsZero() {
t.Errorf("missing publication history for %s", publisherKey)
}
}
}
assertPublicationHistory(t, ech.configs["example.com"][0].meta.Publications)
var storedMeta echConfigMeta
if err := json.Unmarshal(storedMetadata[echMetaKey(1)], &storedMeta); err != nil {
t.Fatalf("unmarshaling stored ECH metadata: %v", err)
}
assertPublicationHistory(t, storedMeta.Publications)
}
func TestTLSStorageCleanStopSynchronization(t *testing.T) {
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
@ -359,9 +461,6 @@ func TestTLSECHPublishBlockingPublisherStopSynchronization(t *testing.T) {
AEADID: hpke.AEAD_AES128GCM,
},
},
meta: echConfigMeta{
Publications: make(publicationHistory),
},
},
},
},
@ -428,8 +527,7 @@ func TestTLSECHRotateBlockingStorageStopSynchronization(t *testing.T) {
ConfigID: 1,
RawPublicName: "example.com",
meta: echConfigMeta{
Created: time.Now().Add(-24 * time.Hour * 31), // > 30 days old so rotationNeeded returns true
Publications: make(publicationHistory),
Created: time.Now().Add(-24 * time.Hour * 31), // > 30 days old so rotationNeeded returns true
},
},
},