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 <noreply@anthropic.com>

* sftp: check the OP_UNSUPPORTED status a client receives for a symlink

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Denis Maier <denis@dAir.lab>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-12-03 10:36:56 -08:00
1 parent 7c41ed0635
commit 65d082e788
3 files changed
+67 -1

No files matched your search

+10 -1
View File
@@ -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
+10
View File
@@ -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
+47
View File
@@ -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)
}
}