mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix(agent/agentssh)!: use configured directory for SFTP connections (#21194)
BREAKING CHANGE: SFTP/SCP now respects the agent's configured directory. If your workspace agent has a custom `dir` configured in Terraform, SFTP and SCP connections will now land there instead of `$HOME`. Previously, only SSH and rsync respected this setting, which caused confusing behavior where `scp file.txt coder:.` and `rsync file.txt coder:.` would put files in different places. If you have scripts that relied on SFTP/SCP always using `$HOME` regardless of agent configuration, you may need to use explicit paths instead.
This commit is contained in:
@@ -829,13 +829,19 @@ func (s *Server) sftpHandler(logger slog.Logger, session ssh.Session) error {
|
||||
session.DisablePTYEmulation()
|
||||
|
||||
var opts []sftp.ServerOption
|
||||
// Change current working directory to the users home
|
||||
// directory so that SFTP connections land there.
|
||||
homedir, err := userHomeDir()
|
||||
if err != nil {
|
||||
logger.Warn(ctx, "get sftp working directory failed, unable to get home dir", slog.Error(err))
|
||||
} else {
|
||||
opts = append(opts, sftp.WithServerWorkingDirectory(homedir))
|
||||
// Change current working directory to the configured
|
||||
// directory (or home directory if not set) so that SFTP
|
||||
// connections land there.
|
||||
dir := s.config.WorkingDirectory()
|
||||
if dir == "" {
|
||||
var err error
|
||||
dir, err = userHomeDir()
|
||||
if err != nil {
|
||||
logger.Warn(ctx, "get sftp working directory failed, unable to get home dir", slog.Error(err))
|
||||
}
|
||||
}
|
||||
if dir != "" {
|
||||
opts = append(opts, sftp.WithServerWorkingDirectory(dir))
|
||||
}
|
||||
|
||||
server, err := sftp.NewServer(session, opts...)
|
||||
|
||||
Reference in New Issue
Block a user