Skip to content

lima: avoid mutating user config when injecting default DNS host - #1630

Open
ousamabenyounes wants to merge 1 commit into
abiosoft:mainfrom
ousamabenyounes:fix/dnshosts-config-mutation
Open

lima: avoid mutating user config when injecting default DNS host#1630
ousamabenyounes wants to merge 1 commit into
abiosoft:mainfrom
ousamabenyounes:fix/dnshosts-config-mutation

Conversation

@ousamabenyounes

Copy link
Copy Markdown

Problem

newConf in environment/vm/lima/yaml.go builds the lima config from the colima
config. It assigned the user's DNS-hosts map by reference and then injected
colima's internal host.docker.internal default into it:

l.HostResolver.Hosts = conf.Network.DNSHosts // aliases the caller's map
...
if _, ok := l.HostResolver.Hosts["host.docker.internal"]; !ok {
    l.HostResolver.Hosts["host.docker.internal"] = "host.lima.internal"
}

Because Go maps are reference types, mutating l.HostResolver.Hosts also mutates
the caller's conf.Network.DNSHosts. That same config object is later saved
(l.conf = confSaveToFile, and the CLI's configmanager.Save shares the
same underlying map), so any user who has at least one dnsHosts entry ends up
with an unwanted host.docker.internal: host.lima.internal line silently written
into their colima.yaml.

When conf.Network.DNSHosts was nil the code happened to allocate a fresh map,
so the leak only affected users who set their own DNS hosts.

Fix

Copy the user-provided hosts into a new map before injecting the internal default,
so the caller's config is never mutated. Behavior is otherwise unchanged: the
host.docker.internal default is still added when absent, and a user override is
still respected.

Test verification (RED → GREEN)

Added Test_newConf_doesNotMutateDNSHosts, which calls newConf with a
user-supplied dnsHosts map and asserts the original map is left untouched.

RED — on the unmodified base (bug present):

=== RUN   Test_newConf_doesNotMutateDNSHosts
    yaml_test.go:121: newConf mutated caller DNSHosts, injected host.docker.internal: map[host.docker.internal:host.lima.internal myhost.internal:1.2.3.4]
    yaml_test.go:124: newConf mutated caller DNSHosts size, got 2 entries: map[host.docker.internal:host.lima.internal myhost.internal:1.2.3.4]
--- FAIL: Test_newConf_doesNotMutateDNSHosts (0.00s)
FAIL	github.com/abiosoft/colima/environment/vm/lima

GREEN — with the fix:

=== RUN   Test_newConf_doesNotMutateDNSHosts
--- PASS: Test_newConf_doesNotMutateDNSHosts (0.00s)
ok  	github.com/abiosoft/colima/environment/vm/lima

go build ./..., go test ./..., and golangci-lint run (v2.11.3) are all green.

newConf aliased conf.Network.DNSHosts and then inserted the internal
host.docker.internal default into it. Since maps are reference types, this
mutated the caller's config in place, which is later persisted, leaking
host.docker.internal into the user's saved colima.yaml. Copy the hosts into
a new map before injecting the default.

Signed-off-by: Ben Younes <2910651+ousamabenyounes@users.noreply.github.com>
@ousamabenyounes
ousamabenyounes force-pushed the fix/dnshosts-config-mutation branch from 1ee8472 to db572a2 Compare September 7, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant