Skip to content

Commit 97839b9

Browse files
blink-so[bot]f0ssel
andcommitted
improve: enhance PATH restoration for sudo environment preservation
Improved the PATH restoration logic to be more comprehensive: - Added more system paths including sbin directories - Added common user tool paths (Rust, Go, npm global) - Better preservation of tool paths from current PATH (/opt, /snap/bin) - Added duplicate removal while preserving path order - Enhanced debug logging for PATH restoration process This should help resolve issues where user-installed tools like 'claude' are not found when running jail with sudo. Co-authored-by: f0ssel <[email protected]>
1 parent 8f25301 commit 97839b9

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

environment/sudo.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,44 +65,71 @@ func RestoreOriginalUserEnvironment(logger *slog.Logger) map[string]string {
6565

6666
// restoreUserPath attempts to construct a reasonable PATH for the original user
6767
func restoreUserPath(originalUser *user.User, logger *slog.Logger) string {
68-
// Start with common system paths
68+
// Start with comprehensive system paths (in order of preference)
6969
systemPaths := []string{
7070
"/usr/local/bin",
7171
"/usr/bin",
7272
"/bin",
73+
"/usr/local/sbin",
74+
"/usr/sbin",
75+
"/sbin",
7376
}
7477

7578
// Add user-specific paths
7679
userPaths := []string{
7780
filepath.Join(originalUser.HomeDir, ".local", "bin"),
7881
filepath.Join(originalUser.HomeDir, "bin"),
82+
filepath.Join(originalUser.HomeDir, ".cargo", "bin"), // Rust tools
83+
filepath.Join(originalUser.HomeDir, "go", "bin"), // Go tools
84+
filepath.Join(originalUser.HomeDir, ".npm-global", "bin"), // npm global tools
7985
}
8086

8187
// Check if user paths exist and add them
82-
var validPaths []string
88+
var validUserPaths []string
8389
for _, path := range userPaths {
8490
if _, err := os.Stat(path); err == nil {
85-
validPaths = append(validPaths, path)
91+
validUserPaths = append(validUserPaths, path)
92+
logger.Debug("Found user path", "path", path)
8693
}
8794
}
8895

89-
// Combine user paths + system paths
90-
allPaths := append(validPaths, systemPaths...)
91-
92-
// Also try to preserve some paths from current PATH that might be user-specific
96+
// Try to preserve paths from current PATH that might be user-specific or important
97+
var preservedPaths []string
9398
currentPath := os.Getenv("PATH")
9499
if currentPath != "" {
95100
for _, path := range strings.Split(currentPath, ":") {
96101
// Include paths that contain the user's home directory
97102
if strings.Contains(path, originalUser.HomeDir) {
98103
if _, err := os.Stat(path); err == nil {
99-
allPaths = append([]string{path}, allPaths...)
104+
preservedPaths = append(preservedPaths, path)
105+
logger.Debug("Preserved user-specific path from current PATH", "path", path)
106+
}
107+
}
108+
// Also preserve common tool paths that might not be in system paths
109+
if strings.Contains(path, "/opt/") || strings.Contains(path, "/snap/bin") {
110+
if _, err := os.Stat(path); err == nil {
111+
preservedPaths = append(preservedPaths, path)
112+
logger.Debug("Preserved tool path from current PATH", "path", path)
100113
}
101114
}
102115
}
103116
}
104117

105-
restoredPath := strings.Join(allPaths, ":")
118+
// Combine all paths: preserved user paths + valid user paths + system paths
119+
allPaths := append(preservedPaths, validUserPaths...)
120+
allPaths = append(allPaths, systemPaths...)
121+
122+
// Remove duplicates while preserving order
123+
seen := make(map[string]bool)
124+
var uniquePaths []string
125+
for _, path := range allPaths {
126+
if !seen[path] {
127+
seen[path] = true
128+
uniquePaths = append(uniquePaths, path)
129+
}
130+
}
131+
132+
restoredPath := strings.Join(uniquePaths, ":")
106133
logger.Debug("Restored PATH for user", "user", originalUser.Username, "path", restoredPath)
107134
return restoredPath
108135
}

0 commit comments

Comments
 (0)