Skip to content

Modern Docker Management, Designed for Everyone

fix: improve lifecycle permission diagnostics (#3404)

Greg849ae46a40f5
diff --git a/backend/internal/services/lifecycle_path_diagnostics_other.go b/backend/internal/services/lifecycle_path_diagnostics_other.go
new file mode 100644
index 000000000..7b3befdd1
--- /dev/null
+++ b/backend/internal/services/lifecycle_path_diagnostics_other.go
@@ -0,0 +1,15 @@
+//go:build !unix
+
+package services
+
+import "fmt"
+
+func describeLifecyclePathAccessInternal(projectPath, resolvedScriptPath string) string {
+ return fmt.Sprintf(
+ "Project path=%q, resolved script path=%q. "+
+ "Ensure the Arcane process can traverse every parent directory and inspect the script. "+
+ "A script being executable is not sufficient when ownership, ACLs, or mount permissions block access.",
+ projectPath,
+ resolvedScriptPath,
+ )
+}
diff --git a/backend/internal/services/lifecycle_path_diagnostics_unix.go b/backend/internal/services/lifecycle_path_diagnostics_unix.go
new file mode 100644
index 000000000..326a57227
--- /dev/null
+++ b/backend/internal/services/lifecycle_path_diagnostics_unix.go
@@ -0,0 +1,96 @@
+//go:build unix
+
+package services
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+ "syscall"
+)
+
+func describeLifecyclePathAccessInternal(projectPath, resolvedScriptPath string) string {
+ var lines []string
+
+ lines = append(lines,
+ fmt.Sprintf(
+ "Arcane process identity: uid=%d gid=%d.",
+ os.Geteuid(),
+ os.Getegid(),
+ ),
+ "Path inspection:",
+ )
+
+ for _, candidate := range lifecycleDiagnosticPathsInternal(projectPath, resolvedScriptPath) {
+ info, err := os.Lstat(candidate)
+ if err != nil {
+ lines = append(lines, fmt.Sprintf(" %q: %v", candidate, err))
+ break
+ }
+
+ owner := "uid=unknown gid=unknown"
+ if stat, ok := info.Sys().(*syscall.Stat_t); ok {
+ owner = fmt.Sprintf("uid=%d gid=%d", stat.Uid, stat.Gid)
+ }
+
+ lines = append(lines, fmt.Sprintf(
+ " %q: mode=%s permissions=%04o %s type=%s",
+ candidate,
+ info.Mode(),
+ info.Mode().Perm(),
+ owner,
+ lifecycleFileTypeInternal(info),
+ ))
+ }
+
+ lines = append(lines,
+ "Ensure the Arcane process can traverse every parent directory and inspect the script.",
+ "A script mode of 0755 is not sufficient when a parent directory lacks execute permission or ownership, ACLs, or mount permissions block access.",
+ )
+
+ return strings.Join(lines, " ")
+}
+
+func lifecycleDiagnosticPathsInternal(projectPath, resolvedScriptPath string) []string {
+ absProject, err := filepath.Abs(projectPath)
+ if err != nil {
+ return []string{projectPath, resolvedScriptPath}
+ }
+
+ absScript, err := filepath.Abs(resolvedScriptPath)
+ if err != nil {
+ return []string{absProject, resolvedScriptPath}
+ }
+
+ relative, err := filepath.Rel(absProject, absScript)
+ if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
+ return []string{absProject, absScript}
+ }
+
+ paths := []string{absProject}
+ current := absProject
+
+ for component := range strings.SplitSeq(relative, string(filepath.Separator)) {
+ if component == "" || component == "." {
+ continue
+ }
+ current = filepath.Join(current, component)
+ paths = append(paths, current)
+ }
+
+ return paths
+}
+
+func lifecycleFileTypeInternal(info os.FileInfo) string {
+ switch {
+ case info.Mode()&os.ModeSymlink != 0:
+ return "symlink"
+ case info.IsDir():
+ return "directory"
+ case info.Mode().IsRegular():
+ return "regular"
+ default:
+ return "other"
+ }
+}
diff --git a/backend/internal/services/lifecycle_path_diagnostics_unix_test.go b/backend/internal/services/lifecycle_path_diagnostics_unix_test.go
new file mode 100644
index 000000000..804924a91
--- /dev/null
+++ b/backend/internal/services/lifecycle_path_diagnostics_unix_test.go
@@ -0,0 +1,98 @@
+//go:build unix
+
+package services
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestDescribeLifecyclePathAccess_ReportsPathMetadata(t *testing.T) {
+ dir := t.TempDir()
+ scriptsDir := filepath.Join(dir, "scripts")
+ scriptPath := filepath.Join(scriptsDir, "pre-deploy.sh")
+
+ require.NoError(t, os.MkdirAll(scriptsDir, 0o750))
+ require.NoError(t, os.WriteFile(scriptPath, []byte("#!/bin/sh\n"), 0o755))
+
+ got := describeLifecyclePathAccessInternal(dir, scriptPath)
+
+ assert.Contains(t, got, "Arcane process identity: uid=")
+ assert.Contains(t, got, "Path inspection:")
+ assert.Contains(t, got, scriptsDir)
+ assert.Contains(t, got, scriptPath)
+ assert.Contains(t, got, "permissions=0750")
+ assert.Contains(t, got, "permissions=0755")
+ assert.Contains(t, got, "type=directory")
+ assert.Contains(t, got, "type=regular")
+ assert.Contains(t, got, "A script mode of 0755 is not sufficient")
+}
+
+func TestDescribeLifecyclePathAccess_StopsAtFirstInaccessibleComponent(t *testing.T) {
+ if os.Geteuid() == 0 {
+ t.Skip("root bypasses ordinary filesystem permission checks")
+ }
+
+ dir := t.TempDir()
+ blockedDir := filepath.Join(dir, "blocked")
+ scriptPath := filepath.Join(blockedDir, "pre-deploy.sh")
+
+ require.NoError(t, os.MkdirAll(blockedDir, 0o700))
+ require.NoError(t, os.WriteFile(scriptPath, []byte("#!/bin/sh\n"), 0o755))
+ require.NoError(t, os.Chmod(blockedDir, 0o000))
+ t.Cleanup(func() {
+ _ = os.Chmod(blockedDir, 0o700)
+ })
+
+ got := describeLifecyclePathAccessInternal(dir, scriptPath)
+
+ assert.Contains(t, got, fmt.Sprintf("%q: mode=", blockedDir))
+ assert.Contains(t, got, fmt.Sprintf("%q: lstat ", scriptPath))
+ assert.Contains(t, got, "permission denied")
+ assert.NotContains(t, got, fmt.Sprintf("%q: mode=", scriptPath))
+}
+
+func TestLifecycleDiagnosticPaths_RemainsInsideProject(t *testing.T) {
+ dir := t.TempDir()
+ scriptPath := filepath.Join(dir, "scripts", "pre-deploy.sh")
+
+ got := lifecycleDiagnosticPathsInternal(dir, scriptPath)
+
+ require.Len(t, got, 3)
+ assert.Equal(t, dir, got[0])
+ assert.Equal(t, filepath.Join(dir, "scripts"), got[1])
+ assert.Equal(t, scriptPath, got[2])
+}
+
+func TestValidateScriptPath_PermissionDeniedIncludesDiagnostics(t *testing.T) {
+ if os.Geteuid() == 0 {
+ t.Skip("root bypasses ordinary filesystem permission checks")
+ }
+
+ dir := t.TempDir()
+ blockedDir := filepath.Join(dir, "blocked")
+ scriptPath := filepath.Join(blockedDir, "pre-deploy.sh")
+
+ require.NoError(t, os.MkdirAll(blockedDir, 0o700))
+ require.NoError(t, os.WriteFile(scriptPath, []byte("#!/bin/sh\n"), 0o755))
+ require.NoError(t, os.Chmod(blockedDir, 0o000))
+ t.Cleanup(func() {
+ _ = os.Chmod(blockedDir, 0o700)
+ })
+
+ err := validateScriptPathInternal(dir, "blocked/pre-deploy.sh")
+
+ require.Error(t, err)
+ assert.ErrorIs(t, err, os.ErrPermission)
+ assert.Contains(t, err.Error(), "Arcane pre-deploy validation")
+ assert.Contains(t, err.Error(), "Arcane process identity: uid=")
+ assert.Contains(t, err.Error(), blockedDir)
+ assert.Contains(t, err.Error(), scriptPath)
+ assert.Contains(t, err.Error(), "permission denied")
+ assert.Contains(t, err.Error(), "A script mode of 0755 is not sufficient")
+}
diff --git a/backend/internal/services/lifecycle_service.go b/backend/internal/services/lifecycle_service.go
index b7313623e..d79b57d95 100644
--- a/backend/internal/services/lifecycle_service.go
+++ b/backend/internal/services/lifecycle_service.go
@@ -501,7 +501,7 @@ func validateScriptPathInternal(projectPath, scriptPath string) error {
info, err := os.Lstat(absScript)
if err != nil {
- return errors.WrapIff(err, "stat script %q", scriptPath)
+ return describeScriptStatErrorInternal(err, projectPath, scriptPath, absScript)
}
if info.Mode()&os.ModeSymlink != 0 {
return errors.Errorf("script path %q is a symlink; symlinks are not allowed", scriptPath)
@@ -512,6 +512,19 @@ func validateScriptPathInternal(projectPath, scriptPath string) error {
return nil
}
+func describeScriptStatErrorInternal(err error, projectPath, scriptPath, resolvedScriptPath string) error {
+ if os.IsPermission(err) {
+ return errors.WrapIff(
+ err,
+ "Arcane pre-deploy validation could not inspect script %q. %s",
+ scriptPath,
+ describeLifecyclePathAccessInternal(projectPath, resolvedScriptPath),
+ )
+ }
+
+ return errors.WrapIff(err, "stat script %q", scriptPath)
+}
+
// parseLifecycleEnvTextInternal reads admin-configured env config as the
// same KEY=VALUE text format used by .env files: one entry per line, blank
// and "#"-prefixed lines ignored, keys must match POSIX identifier syntax.
diff --git a/backend/internal/services/lifecycle_service_test.go b/backend/internal/services/lifecycle_service_test.go
index 16f124b4e..704006c0f 100644
--- a/backend/internal/services/lifecycle_service_test.go
+++ b/backend/internal/services/lifecycle_service_test.go
@@ -173,6 +173,35 @@ func TestValidateScriptPath_RejectsMissingFile(t *testing.T) {
require.Error(t, err)
}
+func TestDescribeScriptStatError_PermissionDeniedIsActionable(t *testing.T) {
+ err := describeScriptStatErrorInternal(
+ os.ErrPermission,
+ "/app/data/projects/example",
+ "scripts/pre-deploy.sh",
+ "/app/data/projects/example/scripts/pre-deploy.sh",
+ )
+
+ require.Error(t, err)
+ assert.ErrorIs(t, err, os.ErrPermission)
+ assert.Contains(t, err.Error(), "Arcane pre-deploy validation")
+ assert.Contains(t, err.Error(), "permission denied")
+ assert.Contains(t, err.Error(), "scripts/pre-deploy.sh")
+}
+
+func TestDescribeScriptStatError_PreservesGenericStatFailure(t *testing.T) {
+ err := describeScriptStatErrorInternal(
+ os.ErrNotExist,
+ "/app/data/projects/example",
+ "missing.sh",
+ "/app/data/projects/example/missing.sh",
+ )
+
+ require.Error(t, err)
+ assert.ErrorIs(t, err, os.ErrNotExist)
+ assert.Contains(t, err.Error(), `stat script "missing.sh"`)
+ assert.NotContains(t, err.Error(), "Arcane pre-deploy validation could not inspect")
+}
+
func TestValidateScriptPath_RejectsDirectory(t *testing.T) {
dir := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(dir, "scripts"), 0o755))