From c4349deaaccb95ba25fb274fea24176ee503d550 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 19 Sep 2026 03:07:14 +1000 Subject: [PATCH] fix(config): clone the Services map so an overlay cannot mutate the published snapshot Config.Clone deep-copies every reference-type field except Services, which became a map[string]ServiceConnection. A value copy shares the map header, and yaml.Unmarshal writes keys into whatever map it is handed -- so Loader.ApplyOverlay, which clones precisely so it cannot corrupt the base document, rewrote the published snapshot in place: ApplyOverlay(doc, {"services": {"state": {"target": "10.0.0.5:9090"}}}) left doc.Config.Services["state"].Target changed That defeats the reason Clone exists: a failed or partial overlay must not alter the configuration still being served. Red: TestConfigCloneDeepCopiesReferenceFields failed with "Services map is shared" after the case was added; green with the one-line clone. --- internal/config/clone_test.go | 7 +++++++ internal/config/config.go | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/internal/config/clone_test.go b/internal/config/clone_test.go index dd3f613db..8240b4995 100644 --- a/internal/config/clone_test.go +++ b/internal/config/clone_test.go @@ -32,6 +32,9 @@ func TestConfigCloneDeepCopiesReferenceFields(t *testing.T) { Hosted: map[string]ImageHostedProvider{"openai": {Enabled: true}}, Local: map[string]ImageLocalProvider{"sdxl": {Enabled: true}}, }, + Services: Services{ + ServiceNameState: {Target: "127.0.0.1:9090", TargetToken: "secret"}, + }, } got := orig.Clone() @@ -52,6 +55,7 @@ func TestConfigCloneDeepCopiesReferenceFields(t *testing.T) { *got.Tools.WebFetch.Enabled = false got.Image.Hosted["openai"] = ImageHostedProvider{Enabled: false} got.Image.Local["sdxl"] = ImageLocalProvider{Enabled: false} + got.Services[ServiceNameState] = ServiceConnection{Target: "changed"} if orig.Models["builder"] != "m" { t.Error("Models map is shared") @@ -95,4 +99,7 @@ func TestConfigCloneDeepCopiesReferenceFields(t *testing.T) { if !orig.Image.Local["sdxl"].Enabled { t.Error("Image.Local map is shared") } + if orig.Services[ServiceNameState].TargetToken != "secret" { + t.Error("Services map is shared") + } } diff --git a/internal/config/config.go b/internal/config/config.go index 7f169ecab..2627b3665 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -665,6 +665,11 @@ func (c Config) Clone() Config { c.Memory.ProviderConfig = maps.Clone(c.Memory.ProviderConfig) c.Image.Hosted = maps.Clone(c.Image.Hosted) c.Image.Local = maps.Clone(c.Image.Local) + // Services is a map of structs, so the header is shared by the value copy + // above and yaml.Unmarshal writes keys into whatever map it is handed. Without + // this line an overlay -- or a dashboard PATCH -- rewrites the PUBLISHED + // snapshot in place, which is the mutation Clone exists to prevent. + c.Services = maps.Clone(c.Services) if c.Tools.WebFetch.Enabled != nil { v := *c.Tools.WebFetch.Enabled c.Tools.WebFetch.Enabled = &v