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

Commitadfa45e

Browse files
committed
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
1 parent94db085 commitadfa45e

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

‎agent/agent.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,18 @@ func (a *agent) init(ctx context.Context) {
447447
"sftp":func(session ssh.Session) {
448448
session.DisablePTYEmulation()
449449

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

‎agent/agent_test.go

Lines changed: 6 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,17 @@ 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")
215218
conn,_:=setupAgent(t, codersdk.WorkspaceAgentMetadata{},0)
216219
sshClient,err:=conn.SSHClient()
217220
require.NoError(t,err)
218221
defersshClient.Close()
219222
client,err:=sftp.NewClient(sshClient)
220223
require.NoError(t,err)
224+
wd,err:=client.Getwd()
225+
require.NoError(t,err,"get working directory")
226+
require.Equal(t,u.HomeDir,wd,"working directory should be home user home")
221227
tempFile:=filepath.Join(t.TempDir(),"sftp")
222228
file,err:=client.Create(tempFile)
223229
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.20220926024748-50f0
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.20221014103342-f1dc1fef3d9e
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.20221014103342-f1dc1fef3d9e h1:+xFsQ1Ugjladoxp2Pju5m9FSCp799Opvr/ncVhDk7EI=
1229+
github.com/mafredri/sftpv1.13.6-0.20221014103342-f1dc1fef3d9e/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