Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: treat identity bind mounts as mapped when re-resolving escaped relative compose paths (#3924)
Kyle Mendell7b19b854ace7
diff --git a/backend/internal/project/service_test.go b/backend/internal/project/service_test.goindex 128b137f9..fcca9c631 100644--- a/backend/internal/project/service_test.go+++ b/backend/internal/project/service_test.go@@ -7226,7 +7226,7 @@ func TestProjectPathMapperUsesCurrentSettingsInternal(t *testing.T) {require.NoError(t, settingsService.SetStringSetting(t.Context(), "projectsDirectory", containerDir+":"+hostDir))mapper := service.projectPathMapperInternal(t.Context())require.NotNil(t, mapper)- mapped, err := mapper.ContainerToHost(filepath.Join(containerDir, "data"))+ mapped, _, err := mapper.ContainerToHost(filepath.Join(containerDir, "data"))require.NoError(t, err)require.Equal(t, filepath.Join(hostDir, "data"), mapped)}diff --git a/backend/pkg/projects/path_mapper.go b/backend/pkg/projects/path_mapper.goindex 54421d152..98e60f2df 100644--- a/backend/pkg/projects/path_mapper.go+++ b/backend/pkg/projects/path_mapper.go@@ -172,19 +172,18 @@ func joinHostRelativePathInternal(base, rel string) string {return joined}-// ContainerToHost translates a container path to host path-func (pm *PathMapper) ContainerToHost(containerPath string) (string, error) {- if !pm.IsNonMatchingMount() {- return containerPath, nil // No translation needed- }-+// ContainerToHost translates a container path to host path. mapped reports+// whether a mounted directory contains the path; an identity mount leaves the+// path unchanged yet still counts as mapped, while a path outside every mount+// is never mapped, even when no translation could occur.+func (pm *PathMapper) ContainerToHost(containerPath string) (string, bool, error) {// Auto-discovery mode: resolve against Arcane's real mount table so nested,// independently bind-mounted directories map to their own host path.if len(pm.mounts) > 0 {if host, ok := ResolveHostPath(pm.mounts, containerPath).Get(); ok {- return host, nil+ return host, true, nil}- return filepath.Clean(containerPath), nil // outside all mounts: leave unchanged+ return filepath.Clean(containerPath), false, nil // outside all mounts: leave unchanged}cleaned := filepath.Clean(containerPath)@@ -192,12 +191,12 @@ func (pm *PathMapper) ContainerToHost(containerPath string) (string, error) {// Calculate relative pathrelPath, err := filepath.Rel(pm.containerPrefix, cleaned)if err != nil {- return "", errors.WrapIf(err, "failed to calculate relative path")+ return "", false, errors.WrapIf(err, "failed to calculate relative path")}// Only translate paths within container prefixif strings.HasPrefix(relPath, "..") || relPath == ".." || filepath.IsAbs(relPath) {- return cleaned, nil+ return cleaned, false, nil}// Join with host prefix@@ -209,7 +208,7 @@ func (pm *PathMapper) ContainerToHost(containerPath string) (string, error) {hostPath = filepath.ToSlash(hostPath)}- return hostPath, nil+ return hostPath, true, nil}// TranslateVolumeSources translates bind mount sources in a Compose project.@@ -231,7 +230,7 @@ func (pm *PathMapper) TranslateVolumeSources(project *composetypes.Project, trancontinue}- hostPath, err := pm.ContainerToHost(volume.Source)+ hostPath, _, err := pm.ContainerToHost(volume.Source)if err != nil {return errors.WrapIff(err, "failed to translate volume source %q", volume.Source)}@@ -249,7 +248,7 @@ func (pm *PathMapper) TranslateVolumeSources(project *composetypes.Project, tran// Translate secretsfor name, secret := range project.Secrets {if secret.File != "" {- hostPath, err := pm.ContainerToHost(secret.File)+ hostPath, _, err := pm.ContainerToHost(secret.File)if err != nil {return errors.WrapIff(err, "failed to translate secret file %q", secret.File)}@@ -261,7 +260,7 @@ func (pm *PathMapper) TranslateVolumeSources(project *composetypes.Project, tran// Translate configsfor name, config := range project.Configs {if config.File != "" {- hostPath, err := pm.ContainerToHost(config.File)+ hostPath, _, err := pm.ContainerToHost(config.File)if err != nil {return errors.WrapIff(err, "failed to translate config file %q", config.File)}@@ -281,7 +280,7 @@ func (pm *PathMapper) TranslateVolumeSources(project *composetypes.Project, trancontinue}- hostPath, err := pm.ContainerToHost(device)+ hostPath, _, err := pm.ContainerToHost(device)if err != nil {return errors.WrapIff(err, "failed to translate volume device %q", device)}@@ -372,6 +371,9 @@ func RemapEscapedRelativeSources(}hostPath := joinHostRelativePathInternal(hostWorkingDir, rawSource)+ if hostPath == target.current {+ continue // identity mount: the Docker host already resolves it the same way+ }target.apply(hostPath)slog.WarnContext(ctx,"compose relative path resolves outside the mounted projects directory; remapped so the Docker host resolves it the same way `docker compose up` would",@@ -475,14 +477,14 @@ func isRemappableSourceInternal(rawSource string) bool {// itself is not inside a mounted directory, since there is then no host path to// anchor to.func hostWorkingDirInternal(ctx context.Context, pathMapper projecttypes.VolumeSourcePathMapper, containerWorkingDir string) (string, bool) {- hostWorkingDir, err := pathMapper.ContainerToHost(containerWorkingDir)+ hostWorkingDir, mapped, err := pathMapper.ContainerToHost(containerWorkingDir)if err != nil {slog.WarnContext(ctx, "failed to resolve host path for project directory; relative paths outside the projects mount may resolve incorrectly","working_dir", containerWorkingDir, "error", err)return "", false}- if hostWorkingDir == "" || filepath.Clean(hostWorkingDir) == filepath.Clean(containerWorkingDir) {+ if !mapped {slog.WarnContext(ctx, "project directory is not inside a mounted directory; relative paths outside the projects mount may resolve incorrectly","working_dir", containerWorkingDir)return "", falsediff --git a/backend/pkg/projects/path_mapper_test.go b/backend/pkg/projects/path_mapper_test.goindex 565aa7879..b602d87c7 100644--- a/backend/pkg/projects/path_mapper_test.go+++ b/backend/pkg/projects/path_mapper_test.go@@ -33,7 +33,7 @@ func TestNewPathMapperForConfiguredDirectory(t *testing.T) {source := filepath.Join(containerDir, "0/stack/compose.yaml")expected := filepath.Join(hostDir, "0/stack/compose.yaml")- translated, err := pathMapper.ContainerToHost(source)+ translated, _, err := pathMapper.ContainerToHost(source)require.NoError(t, err)require.Equal(t, filepath.ToSlash(expected), filepath.ToSlash(translated))})@@ -41,28 +41,29 @@ func TestNewPathMapperForConfiguredDirectory(t *testing.T) {func TestPathMapper_MatchingMount_NoTranslation(t *testing.T) {pm := NewPathMapper("/app/data/projects", "")- result, err := pm.ContainerToHost("/app/data/projects/test/data")+ result, _, err := pm.ContainerToHost("/app/data/projects/test/data")require.NoError(t, err)assert.Equal(t, "/app/data/projects/test/data", result)}func TestPathMapper_WindowsMount_Translation(t *testing.T) {pm := NewPathMapper("/app/data/projects", "D:/arcane/projects")- result, err := pm.ContainerToHost("/app/data/projects/test/data")+ result, _, err := pm.ContainerToHost("/app/data/projects/test/data")require.NoError(t, err)assert.Equal(t, "D:/arcane/projects/test/data", result)}func TestPathMapper_PathOutsidePrefix_NoTranslation(t *testing.T) {pm := NewPathMapper("/app/data/projects", "D:/arcane/projects")- result, err := pm.ContainerToHost("/etc/hosts")+ result, mapped, err := pm.ContainerToHost("/etc/hosts")require.NoError(t, err)+ assert.False(t, mapped)assert.Equal(t, "/etc/hosts", result)}func TestPathMapper_PathTraversalPrevention(t *testing.T) {pm := NewPathMapper("/app/data/projects", "/host/projects")- result, err := pm.ContainerToHost("/app/data/projects/../../etc/passwd")+ result, _, err := pm.ContainerToHost("/app/data/projects/../../etc/passwd")require.NoError(t, err)assert.Equal(t, "/app/etc/passwd", result)}@@ -77,12 +78,12 @@ func TestPathMapper_FromMounts_NestedIndependentMount(t *testing.T) {require.True(t, pm.IsNonMatchingMount())// Independently-mounted project resolves to its own host path (longest-prefix wins).- got, err := pm.ContainerToHost("/app/data/projects/homeassistant/service_postgresql/postgres_data")+ got, _, err := pm.ContainerToHost("/app/data/projects/homeassistant/service_postgresql/postgres_data")require.NoError(t, err)assert.Equal(t, "/home/user/homeassistant/service_postgresql/postgres_data", got)// A project without its own mount still re-bases under /app/data.- got, err = pm.ContainerToHost("/app/data/projects/other/data")+ got, _, err = pm.ContainerToHost("/app/data/projects/other/data")require.NoError(t, err)assert.Equal(t, "/home/user/.arcane/data/projects/other/data", got)}@@ -93,8 +94,9 @@ func TestPathMapper_FromMounts_NoMatchAndMatchingMounts(t *testing.T) {})// Path outside every mount is returned unchanged.- got, err := pm.ContainerToHost("/etc/hosts")+ got, mapped, err := pm.ContainerToHost("/etc/hosts")require.NoError(t, err)+ assert.False(t, mapped)assert.Equal(t, "/etc/hosts", got)// A table whose mounts all match (source == destination) needs no translation.diff --git a/types/project/compose_content.go b/types/project/compose_content.goindex df546a441..e529839cb 100644--- a/types/project/compose_content.go+++ b/types/project/compose_content.go@@ -8,10 +8,12 @@ import (type VolumeSourcePathMapper interface {TranslateVolumeSources(project *composetypes.Project, translateFileResources bool) error// ContainerToHost translates a single container-side path to its host-side- // equivalent, returning the path unchanged when it is outside every mounted- // directory. Needed to re-resolve relative Compose paths that escape the- // projects mount, where prefix translation has nothing to match.- ContainerToHost(containerPath string) (string, error)+ // equivalent. mapped is false when the path is outside every mounted+ // directory, in which case the path is returned unchanged; a path inside an+ // identity mount is also unchanged but reports mapped as true. Needed to+ // re-resolve relative Compose paths that escape the projects mount, where+ // prefix translation has nothing to match.+ ContainerToHost(containerPath string) (hostPath string, mapped bool, err error)}// ComposeContentOptions configures loading a Compose project from in-memory content.