Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit173b7a2

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 Serveroption for changing the current working directory.Attempt to upstream:pkg/sftp#528Supercedes andcloses#4420Fixes#3620* Update fork
1 parent9402013 commit173b7a2

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

‎agent/agent.go

Lines changed: 12 additions & 1 deletion
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+
varopts []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+
iferr!=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
iferr!=nil {
453464
a.logger.Debug(session.Context(),"initialize sftp server",slog.Error(err))
454465
return

‎agent/agent_test.go

Lines changed: 10 additions & 0 deletions
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+
ifruntime.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
defersshClient.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

Lines changed: 6 additions & 0 deletions
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
replacegithub.com/gliderlabs/ssh =>github.com/coder/sshv0.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+
replacegithub.com/pkg/sftp =>github.com/mafredri/sftpv1.13.6-0.20221014125459-6a7168cf46fd
59+
5460
require (
5561
cdr.dev/slogv1.4.2-0.20220525200111-18dce5c2cd5f
5662
cloud.google.com/go/computev1.10.0

‎go.sum

Lines changed: 2 additions & 4 deletions
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-colorfulv1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
12261226
github.com/lufia/plan9statsv0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
12271227
github.com/lyft/protoc-gen-starv0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
1228+
github.com/mafredri/sftpv1.13.6-0.20221014125459-6a7168cf46fd h1:X7ZK1YGbCoPkllDq/lG5PLV4k3LVddypzoH5hVgzmiw=
1229+
github.com/mafredri/sftpv1.13.6-0.20221014125459-6a7168cf46fd/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
12281230
github.com/mafredri/udpv0.1.2-0.20220805105907-b2872e92e98d h1:E+lU8/1jcUd3guqaRvjAGCcwoPe7jcYrNv9K0OzEwdQ=
12291231
github.com/mafredri/udpv0.1.2-0.20220805105907-b2872e92e98d/go.mod h1:GUd681aT3Tj7pdNkUtqBz5pp/GLMGIaMI9Obq6+ob48=
12301232
github.com/magiconair/propertiesv1.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/errorsv0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
15061508
github.com/pkg/errorsv0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
15071509
github.com/pkg/profilev1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
1508-
github.com/pkg/sftpv1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
1509-
github.com/pkg/sftpv1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
1510-
github.com/pkg/sftpv1.13.5 h1:a3RLUqkyjYRtBTZJZ1VRrKbN3zhuPLlUc3sphVz81go=
1511-
github.com/pkg/sftpv1.13.5/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
15121510
github.com/pmezard/go-difflibv0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15131511
github.com/pmezard/go-difflibv1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15141512
github.com/pmezard/go-difflibv1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp