Skip to content

Commit 173b7a2

Browse files
authored
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: pkg/sftp#528 Supercedes and closes #4420 Fixes #3620 * Update fork
1 parent 9402013 commit 173b7a2

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

agent/agent.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,18 @@ func (a *agent) init(ctx context.Context) {
448448
"sftp": func(session ssh.Session) {
449449
session.DisablePTYEmulation()
450450

451-
server, err := sftp.NewServer(session)
451+
var opts []sftp.ServerOption
452+
// Change current working directory to the users home
453+
// directory so that SFTP connections land there.
454+
// https://github.com/coder/coder/issues/3620
455+
u, err := user.Current()
456+
if err != nil {
457+
a.logger.Warn(ctx, "get sftp working directory failed, unable to get current user", slog.Error(err))
458+
} else {
459+
opts = append(opts, sftp.WithServerWorkingDirectory(u.HomeDir))
460+
}
461+
462+
server, err := sftp.NewServer(session, opts...)
452463
if err != nil {
453464
a.logger.Debug(session.Context(), "initialize sftp server", slog.Error(err))
454465
return

agent/agent_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/netip"
1111
"os"
1212
"os/exec"
13+
"os/user"
1314
"path/filepath"
1415
"runtime"
1516
"strconv"
@@ -212,12 +213,21 @@ func TestAgent(t *testing.T) {
212213

213214
t.Run("SFTP", func(t *testing.T) {
214215
t.Parallel()
216+
u, err := user.Current()
217+
require.NoError(t, err, "get current user")
218+
home := u.HomeDir
219+
if runtime.GOOS == "windows" {
220+
home = "/" + strings.ReplaceAll(home, "\\", "/")
221+
}
215222
conn, _ := setupAgent(t, codersdk.WorkspaceAgentMetadata{}, 0)
216223
sshClient, err := conn.SSHClient()
217224
require.NoError(t, err)
218225
defer sshClient.Close()
219226
client, err := sftp.NewClient(sshClient)
220227
require.NoError(t, err)
228+
wd, err := client.Getwd()
229+
require.NoError(t, err, "get working directory")
230+
require.Equal(t, home, wd, "working directory should be home user home")
221231
tempFile := filepath.Join(t.TempDir(), "sftp")
222232
file, err := client.Create(tempFile)
223233
require.NoError(t, err)

go.mod

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20221015033036-5861
5151
// makes importing it directly a bit messy.
5252
replace github.com/gliderlabs/ssh => github.com/coder/ssh v0.0.0-20220811105153-fcea99919338
5353

54+
// The sftp server implementation used by us does not support changing
55+
// the working directory, this fork adds support for it.
56+
//
57+
// Attempt to upstream: https://github.com/pkg/sftp/pull/528
58+
replace github.com/pkg/sftp => github.com/mafredri/sftp v1.13.6-0.20221014125459-6a7168cf46fd
59+
5460
require (
5561
cdr.dev/slog v1.4.2-0.20220525200111-18dce5c2cd5f
5662
cloud.google.com/go/compute v1.10.0

go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
12251225
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
12261226
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
12271227
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
1228+
github.com/mafredri/sftp v1.13.6-0.20221014125459-6a7168cf46fd h1:X7ZK1YGbCoPkllDq/lG5PLV4k3LVddypzoH5hVgzmiw=
1229+
github.com/mafredri/sftp v1.13.6-0.20221014125459-6a7168cf46fd/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
12281230
github.com/mafredri/udp v0.1.2-0.20220805105907-b2872e92e98d h1:E+lU8/1jcUd3guqaRvjAGCcwoPe7jcYrNv9K0OzEwdQ=
12291231
github.com/mafredri/udp v0.1.2-0.20220805105907-b2872e92e98d/go.mod h1:GUd681aT3Tj7pdNkUtqBz5pp/GLMGIaMI9Obq6+ob48=
12301232
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@@ -1505,10 +1507,6 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
15051507
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
15061508
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
15071509
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
1508-
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
1509-
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
1510-
github.com/pkg/sftp v1.13.5 h1:a3RLUqkyjYRtBTZJZ1VRrKbN3zhuPLlUc3sphVz81go=
1511-
github.com/pkg/sftp v1.13.5/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
15121510
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15131511
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15141512
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

0 commit comments

Comments
 (0)