fix(agent): unify working directory resolution (#26102)

agentssh's CommandEnv, sftpHandler, and agentproc each resolved the
session working directory on their own and had drifted: sftpHandler used
the configured directory without checking it exists and bypassed the
injected EnvInfoer, while the others stat-checked and fell back to home.
Home and shell lookups could also skip the EnvInfoer seam through the
exported usershell.HomeDir and Get.

Resolve through a single usershell.ResolveWorkingDirectory and confine
host home and shell lookups to usershell, so SSH sessions, the process
API, and tests can no longer diverge and the injected environment is
always honored. This also fixes SFTP landing in a configured directory
that no longer exists.

Refs coder/coder#26099
This commit is contained in:
Mathias Fredriksson
2026-06-08 14:24:32 +03:00
committed by GitHub
parent 1e5dd83a95
commit 3955df796e
11 changed files with 231 additions and 116 deletions
+4 -3
View File
@@ -2,9 +2,10 @@ package usershell
import "os/exec"
// Get returns the command prompt binary name.
// Deprecated: use SystemEnvInfo.UserShell instead.
func Get(username string) (string, error) {
// get resolves the Windows shell, preferring pwsh.exe, then
// powershell.exe, then cmd.exe. It backs SystemEnvInfo.Shell. Callers
// resolve the shell through an EnvInfoer.
func get(username string) (string, error) {
_, err := exec.LookPath("pwsh.exe")
if err == nil {
return "pwsh.exe", nil