mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
* 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>
142 lines
4.7 KiB
Go
142 lines
4.7 KiB
Go
package sftpd
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pkg/sftp"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
)
|
|
|
|
func TestStdioUser(t *testing.T) {
|
|
u, err := StdioUser("", "alice", 1000, 1000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.HomeDir != "/" || u.Uid != 1000 || len(u.Permissions["/"]) != 1 || u.Permissions["/"][0] != PermAll {
|
|
t.Errorf("without a store got %+v, want the whole filer as uid 1000", u)
|
|
}
|
|
|
|
store := filepath.Join(t.TempDir(), "users.json")
|
|
if err := os.WriteFile(store, []byte(`[{"Username":"alice","HomeDir":"/home/alice","Uid":1001,"Permissions":{"/home/alice":["read"]}}]`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u, err = StdioUser(store, "alice", 1000, 1000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.HomeDir != "/home/alice" || u.Uid != 1001 {
|
|
t.Errorf("with a store got %+v, want the stored account", u)
|
|
}
|
|
if _, err := StdioUser(store, "mallory", 1000, 1000); err == nil {
|
|
t.Error("a user missing from the store got a session")
|
|
}
|
|
if _, err := StdioUser(filepath.Join(t.TempDir(), "missing.json"), "alice", 1000, 1000); err == nil {
|
|
t.Error("a missing store file got a session")
|
|
}
|
|
}
|
|
|
|
// An SSH user who cannot read the store is told why, not just refused.
|
|
func TestStdioUserUnreadableStore(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("mode 0 does not deny reads on Windows")
|
|
}
|
|
if os.Getuid() == 0 {
|
|
t.Skip("root reads any file")
|
|
}
|
|
store := filepath.Join(t.TempDir(), "users.json")
|
|
if err := os.WriteFile(store, []byte(`[]`), 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := StdioUser(store, "alice", 1000, 1000)
|
|
if err == nil || !strings.Contains(err.Error(), "not readable by alice") {
|
|
t.Fatalf("unreadable store: err = %v", err)
|
|
}
|
|
}
|
|
|
|
type pipeStream struct {
|
|
io.Reader
|
|
io.WriteCloser
|
|
}
|
|
|
|
// A session on a plain stream answers the SFTP handshake and ends cleanly
|
|
// when the client goes away, as sshd's Subsystem pipes do.
|
|
func TestServeStdio(t *testing.T) {
|
|
clientToServer, fromClient := io.Pipe()
|
|
toClient, serverToClient := io.Pipe()
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
u, _ := StdioUser("", "alice", 1000, 1000)
|
|
done <- ServeStdio(&SFTPServiceOptions{Filer: "127.0.0.1:1"}, u, pipeStream{clientToServer, serverToClient})
|
|
}()
|
|
|
|
client, err := sftp.NewClientPipe(toClient, fromClient)
|
|
if err != nil {
|
|
t.Fatalf("handshake: %v", err)
|
|
}
|
|
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 {
|
|
case err := <-done:
|
|
if err != nil {
|
|
t.Fatalf("ServeStdio: %v", err)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
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)
|
|
}
|
|
}
|