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.
This commit is contained in:
sam committed 2026-09-19 03:07:14 +10:00
1 parent 79e39ecf16
commit c4349deaac
2 files changed
+12

No files matched your search

+7
View File
@@ -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")
}
}
+5
View File
@@ -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