From 65d082e788775b308fd374d5f6bbc445cadb57ff Mon Sep 17 00:00:00 2001 From: "D.Maier" <73213965+denMaier@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:36:56 +0100 Subject: [PATCH] sftp: answer unsupported requests with OP_UNSUPPORTED (#13342) * sftp: answer unsupported requests with OP_UNSUPPORTED SYMLINK and the hardlink@openssh.com extension were answered with SSH_FX_FAILURE ("unsupported: Symlink"). Clients such as OpenSSH sftp and rclone use SSH_FX_OP_UNSUPPORTED to fall back; FAILURE reads as an attempt that broke. Return sftp.ErrSSHFxOpUnsupported instead, before path resolution, since a symlink's Filepath is the raw link target. The version reply also used pkg/sftp's default extension list, which advertises hardlink@openssh.com and statvfs@openssh.com. Weed implements neither, so advertise only posix-rename@openssh.com. Co-Authored-By: Claude Opus 5.5 * sftp: check the OP_UNSUPPORTED status a client receives for a symlink Co-Authored-By: Claude Opus 5.5 * sftp: answer READLINK with OP_UNSUPPORTED Without a Readlink handler pkg/sftp sends READLINK to Filelist with Method "Readlink", and listDir listed the path: a folder answered with its first child's name as the link target, and a file or symlink with NO_SUCH_FILE. Answer it, and any other method Filelist does not serve, with OP_UNSUPPORTED like SYMLINK and hardlink. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Denis Maier Co-authored-by: Claude Opus 5.5 Co-authored-by: Chris Lu --- weed/sftpd/sftp_filer.go | 11 +++++++- weed/sftpd/sftp_service.go | 10 ++++++++ weed/sftpd/sftp_stdio_test.go | 47 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/weed/sftpd/sftp_filer.go b/weed/sftpd/sftp_filer.go index 62e31ec5f2..afdaa6c8e3 100644 --- a/weed/sftpd/sftp_filer.go +++ b/weed/sftpd/sftp_filer.go @@ -108,6 +108,12 @@ func (fs *SftpServer) withTimeoutContext(fn func(ctx context.Context) error) err // ==================== Command Dispatcher ==================== func (fs *SftpServer) dispatchCmd(r *sftp.Request) error { + if r.Method == "Symlink" || r.Method == "Link" { + // Answer before resolving paths: for Symlink, Filepath is the raw link + // target. OP_UNSUPPORTED lets clients fall back. + glog.V(1).Infof("Dispatch: unsupported %s %s -> %s", r.Method, r.Filepath, r.Target) + return fmt.Errorf("%s: %w", r.Method, sftp.ErrSSHFxOpUnsupported) + } absPath, err := fs.toAbsolutePath(r.Filepath) if err != nil { return err @@ -134,7 +140,7 @@ func (fs *SftpServer) dispatchCmd(r *sftp.Request) error { } return fs.setFileStat(absPath, r.AttrFlags(), r.Attributes()) default: - return fmt.Errorf("unsupported: %s", r.Method) + return fmt.Errorf("%s: %w", r.Method, sftp.ErrSSHFxOpUnsupported) } } @@ -515,6 +521,9 @@ func applyFileStat(entry *filer_pb.Entry, flags sftp.FileAttrFlags, attrs *sftp. // ==================== Directory Operations ==================== func (fs *SftpServer) listDir(r *sftp.Request) (sftp.ListerAt, error) { + if r.Method != "List" && r.Method != "Stat" && r.Method != "Lstat" { + return nil, fmt.Errorf("%s: %w", r.Method, sftp.ErrSSHFxOpUnsupported) + } absPath, err := fs.toAbsolutePath(r.Filepath) if err != nil { return nil, err diff --git a/weed/sftpd/sftp_service.go b/weed/sftpd/sftp_service.go index fcdc0a538e..19237f06e9 100644 --- a/weed/sftpd/sftp_service.go +++ b/weed/sftpd/sftp_service.go @@ -19,6 +19,16 @@ import ( "google.golang.org/grpc" ) +// pkg/sftp advertises hardlink and statvfs by default, which weed does not +// implement. The list is package-level state in pkg/sftp; weed's SFTP server is +// its only user in this binary, so set it once before any session starts. +// posix-rename is served as a plain rename. +func init() { + if err := sftp.SetSFTPExtensions("posix-rename@openssh.com"); err != nil { + glog.Errorf("sftp: set advertised extensions: %v", err) + } +} + // SFTPService holds configuration for the SFTP service. type SFTPService struct { options SFTPServiceOptions diff --git a/weed/sftpd/sftp_stdio_test.go b/weed/sftpd/sftp_stdio_test.go index 2901a94acd..89fa1f4925 100644 --- a/weed/sftpd/sftp_stdio_test.go +++ b/weed/sftpd/sftp_stdio_test.go @@ -1,6 +1,7 @@ package sftpd import ( + "errors" "io" "os" "path/filepath" @@ -10,6 +11,7 @@ import ( "time" "github.com/pkg/sftp" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" ) func TestStdioUser(t *testing.T) { @@ -81,6 +83,19 @@ func TestServeStdio(t *testing.T) { if wd, err := client.Getwd(); err != nil || wd != "/" { t.Errorf("Getwd = %q, %v; want /", wd, err) } + // Only extensions weed implements are advertised. + for name, want := range map[string]bool{"posix-rename@openssh.com": true, "hardlink@openssh.com": false, "statvfs@openssh.com": false} { + if _, ok := client.HasExtension(name); ok != want { + t.Errorf("%s advertised = %v, want %v", name, ok, want) + } + } + var status *sftp.StatusError + if err := client.Symlink("/a", "/b"); !errors.As(err, &status) || status.FxCode() != sftp.ErrSSHFxOpUnsupported { + t.Errorf("Symlink = %v, want OP_UNSUPPORTED", err) + } + if _, err := client.ReadLink("/a"); !errors.As(err, &status) || status.FxCode() != sftp.ErrSSHFxOpUnsupported { + t.Errorf("ReadLink = %v, want OP_UNSUPPORTED", err) + } client.Close() select { @@ -92,3 +107,35 @@ func TestServeStdio(t *testing.T) { t.Fatal("ServeStdio did not return after the client closed") } } + +// weed has no symlinks or hardlinks. OP_UNSUPPORTED lets clients fall back, +// also for a relative link target that leaves the home directory. +func TestLinksAnswerOpUnsupported(t *testing.T) { + fs := newCopyTestServer(t, "", map[string]*filer_pb.Entry{}) + fs.user.HomeDir = "/home/alice" + fs.user.Permissions = map[string][]string{"/": {"*"}} + for _, c := range []struct{ method, path, target string }{ + {"Symlink", "/a", "/b"}, + {"Symlink", "../shared", "link"}, + {"Link", "/a", "/b"}, + } { + req := sftp.NewRequest(c.method, c.path) + req.Target = c.target + if err := fs.dispatchCmd(req); !errors.Is(err, sftp.ErrSSHFxOpUnsupported) { + t.Errorf("%s %s -> %s: got %v, want OP_UNSUPPORTED", c.method, c.path, c.target, err) + } + } +} + +// Without a Readlink handler pkg/sftp sends READLINK to Filelist, which would +// list a folder and answer its first child's name as the link target. +func TestReadlinkAnswersOpUnsupported(t *testing.T) { + fs := newCopyTestServer(t, "", map[string]*filer_pb.Entry{ + "/home/dir": {Name: "dir", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{}}, + "/home/dir/child": {Name: "child", Attributes: &filer_pb.FuseAttributes{}}, + }) + fs.user.Permissions = map[string][]string{"/": {"*"}} + if _, err := fs.listDir(sftp.NewRequest("Readlink", "/home/dir")); !errors.Is(err, sftp.ErrSSHFxOpUnsupported) { + t.Errorf("Readlink /home/dir: got %v, want OP_UNSUPPORTED", err) + } +}