Skip to content

Modern Docker Management, Designed for Everyone

fix: expand env variables in compose include paths (#2194)

Giulio Savini510bb02e26d8
diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.go
index e96111a78..18b9427e9 100644
--- a/backend/internal/services/project_service.go
+++ b/backend/internal/services/project_service.go
@@ -675,7 +675,14 @@ func (s *ProjectService) GetProjectDetails(ctx context.Context, projectID string
func (s *ProjectService) enrichWithIncludeFiles(ctx context.Context, projectPath string, resp *project.Details) {
composeFile, detectErr := projects.DetectComposeFile(projectPath)
if detectErr == nil {
- includes, parseErr := projects.ParseIncludes(composeFile)
+ // Load environment variables so that include paths with ${VAR} references are expanded
+ projectsDirSetting := s.settingsService.GetStringSetting(ctx, "projectsDirectory", "/app/data/projects")
+ projectsDirectory, _ := projects.GetProjectsDirectory(ctx, strings.TrimSpace(projectsDirSetting))
+ autoInjectEnv := s.settingsService.GetBoolSetting(ctx, "autoInjectEnv", false)
+ envLoader := projects.NewEnvLoader(projectsDirectory, filepath.Dir(composeFile), autoInjectEnv)
+ envMap, _, _ := envLoader.LoadEnvironment(ctx)
+
+ includes, parseErr := projects.ParseIncludes(composeFile, envMap)
if parseErr == nil {
var includeFiles []project.IncludeFile
for _, inc := range includes {
diff --git a/backend/pkg/projects/includes.go b/backend/pkg/projects/includes.go
index 3bafc4c19..fa8a43ad4 100644
--- a/backend/pkg/projects/includes.go
+++ b/backend/pkg/projects/includes.go
@@ -11,6 +11,16 @@ import (
"github.com/goccy/go-yaml"
)
+// expandEnvVarsInternal expands ${VAR} and $VAR references in a string using the provided env map.
+func expandEnvVarsInternal(s string, envMap EnvMap) string {
+ return os.Expand(s, func(key string) string {
+ if val, ok := envMap[key]; ok {
+ return val
+ }
+ return ""
+ })
+}
+
// Security Model for Include Files:
// - READ: Docker Compose allows include files from anywhere (parent dirs, absolute paths, etc.)
// We allow reading from any path to maintain compatibility with standard Docker Compose behavior
@@ -23,8 +33,9 @@ type IncludeFile struct {
Content string `json:"content"`
}
-// ParseIncludes reads a compose file and extracts all include directives
-func ParseIncludes(composeFilePath string) ([]IncludeFile, error) {
+// ParseIncludes reads a compose file and extracts all include directives.
+// envMap is used to expand variables (e.g., ${VAR}) in include paths.
+func ParseIncludes(composeFilePath string, envMap EnvMap) ([]IncludeFile, error) {
content, err := os.ReadFile(composeFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read compose file: %w", err)
@@ -47,12 +58,12 @@ func ParseIncludes(composeFilePath string) ([]IncludeFile, error) {
switch v := includes.(type) {
case []any:
for _, item := range v {
- if include, err := parseIncludeItem(item, composeDir); err == nil {
+ if include, err := parseIncludeItemInternal(item, composeDir, envMap); err == nil {
includeFiles = append(includeFiles, include)
}
}
case string:
- if include, err := parseIncludeItem(v, composeDir); err == nil {
+ if include, err := parseIncludeItemInternal(v, composeDir, envMap); err == nil {
includeFiles = append(includeFiles, include)
}
}
@@ -60,7 +71,7 @@ func ParseIncludes(composeFilePath string) ([]IncludeFile, error) {
return includeFiles, nil
}
-func parseIncludeItem(item any, baseDir string) (IncludeFile, error) {
+func parseIncludeItemInternal(item any, baseDir string, envMap EnvMap) (IncludeFile, error) {
var includePath string
switch v := item.(type) {
@@ -78,6 +89,11 @@ func parseIncludeItem(item any, baseDir string) (IncludeFile, error) {
return IncludeFile{}, fmt.Errorf("empty include path")
}
+ // Expand environment variables in the include path (e.g., ${PROJECT_STACK_DIR})
+ if len(envMap) > 0 {
+ includePath = expandEnvVarsInternal(includePath, envMap)
+ }
+
fullPath := includePath
if !filepath.IsAbs(includePath) {
fullPath = filepath.Join(baseDir, includePath)