fix: Start SFTP sessions in user home (working directory) (#4549)

* fix: Start SFTP sessions in user home (working directory)

This commit switches to our fork of `pkg/sftp` which includes a Server
option for changing the current working directory.

Attempt to upstream: https://github.com/pkg/sftp/pull/528

Supercedes and closes #4420

Fixes #3620

* Update fork
This commit is contained in:
Mathias Fredriksson
2022-10-21 09:54:06 -05:00
committed by GitHub
parent 940201313f
commit 173b7a2c83
4 changed files with 30 additions and 5 deletions
+12 -1
View File
@@ -448,7 +448,18 @@ func (a *agent) init(ctx context.Context) {
"sftp": func(session ssh.Session) {
session.DisablePTYEmulation()
server, err := sftp.NewServer(session)
var opts []sftp.ServerOption
// Change current working directory to the users home
// directory so that SFTP connections land there.
// https://github.com/coder/coder/issues/3620
u, err := user.Current()
if err != nil {
a.logger.Warn(ctx, "get sftp working directory failed, unable to get current user", slog.Error(err))
} else {
opts = append(opts, sftp.WithServerWorkingDirectory(u.HomeDir))
}
server, err := sftp.NewServer(session, opts...)
if err != nil {
a.logger.Debug(session.Context(), "initialize sftp server", slog.Error(err))
return