From 5bceecf985347fcf6dd23029ce440b502a0d4301 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 29 Oct 2026 22:15:39 +0100 Subject: [PATCH] fix(use_api_socket): give each service its own credentials file mode The mode of the injected config file was declared once for the whole project, so every service that received the credentials held a pointer to the same value, and changing it for one would have changed it for all of them. Nothing does that today, but the sharing is easy to trip on. Signed-off-by: Nicolas De Loof --- pkg/compose/apiSocket.go | 5 +++-- pkg/compose/apiSocket_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/compose/apiSocket.go b/pkg/compose/apiSocket.go index d77a12977..897849a56 100644 --- a/pkg/compose/apiSocket.go +++ b/pkg/compose/apiSocket.go @@ -99,8 +99,6 @@ func (s *composeService) useAPISocket(project *types.Project) (*types.Project, e Content: configBuf.String(), } - // like the file of the Docker CLI: readable by root only - mode := types.FileMode(0o600) for name, service := range project.Services { if !service.UseAPISocket { continue @@ -119,6 +117,9 @@ func (s *composeService) useAPISocket(project *types.Project) (*types.Project, e path := "/run/secrets/docker" service.Environment["DOCKER_CONFIG"] = &path + // like the file of the Docker CLI: readable by root only. One mode + // per service, so that no service shares it with another. + mode := types.FileMode(0o600) service.Configs = append(service.Configs, types.ServiceConfigObjConfig{ Source: apiSocketConfigName, Target: "/run/secrets/docker/config.json", diff --git a/pkg/compose/apiSocket_test.go b/pkg/compose/apiSocket_test.go index b94d737bc..886deb212 100644 --- a/pkg/compose/apiSocket_test.go +++ b/pkg/compose/apiSocket_test.go @@ -121,6 +121,23 @@ func TestUseAPISocketCredentialsAreForRootOnly(t *testing.T) { assert.Assert(t, project.Configs["#apisocket"].Content != "", "the config carries the credentials") } +// TestUseAPISocketServicesDoNotShareTheirMode pins that each service gets its +// own mode: changing it for one must not change it for the others. +func TestUseAPISocketServicesDoNotShareTheirMode(t *testing.T) { + svc := newAPISocketTestService(t, hostRegistryConfig()) + + project, err := svc.useAPISocket(apiSocketProject( + apiSocketService("one", types.MappingWithEquals{}), + apiSocketService("two", types.MappingWithEquals{}), + )) + + assert.NilError(t, err) + one, two := project.Services["one"].Configs[0], project.Services["two"].Configs[0] + assert.Assert(t, one.Mode != two.Mode, "the services share the same mode pointer") + *one.Mode = 0o644 + assert.Equal(t, *two.Mode, types.FileMode(0o600)) +} + // TestUseAPISocketHonorsDockerConfig pins that a service which sets // DOCKER_CONFIG itself gets the socket but no credentials, as with `docker run // --use-api-socket`: it is taken to know where its credentials are, and its