From fd3f7d6db3cdd94dbf7f16804586447a934bcb77 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 24 Nov 2026 11:49:31 -0800 Subject: [PATCH] sftp: recheck an upload's target when it commits, and refuse directories The upload buffers until Close, so a hold set while it was open still let it overwrite the entry. A PUT to a directory path also lands on a child of that directory, whose protection was never checked. Co-Authored-By: Claude Opus 5.5 --- weed/sftpd/sftp_file_writer.go | 3 +++ weed/sftpd/sftp_filer.go | 22 +++++++++++++++++----- weed/sftpd/sftp_object_lock_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/weed/sftpd/sftp_file_writer.go b/weed/sftpd/sftp_file_writer.go index 4687598eed..285f150d8e 100644 --- a/weed/sftpd/sftp_file_writer.go +++ b/weed/sftpd/sftp_file_writer.go @@ -88,6 +88,9 @@ func (w *SeaweedSftpFileWriter) Close() error { if err := w.fs.requireParentDir(w.absPath); err != nil { return err } + if err := w.fs.checkWritable(w.absPath); err != nil { + return err + } // Stream the file to the absolute path (after HomeDir translation) return w.fs.putFile(w.absPath, w.tmpFile, w.fs.user) diff --git a/weed/sftpd/sftp_filer.go b/weed/sftpd/sftp_filer.go index 29497cca92..75c72d0989 100644 --- a/weed/sftpd/sftp_filer.go +++ b/weed/sftpd/sftp_filer.go @@ -162,11 +162,7 @@ func (fs *SftpServer) newFileWriter(r *sftp.Request) (io.WriterAt, error) { if err := fs.requireParentDir(absPath); err != nil { return nil, err } - if entry, err := fs.getEntry(absPath); err == nil { - if err := fs.checkEntryProtected(absPath, entry); err != nil { - return nil, err - } - } else if !errors.Is(err, os.ErrNotExist) { + if err := fs.checkWritable(absPath); err != nil { return nil, err } // Create a temporary file to buffer writes @@ -195,6 +191,22 @@ func (fs *SftpServer) newFileWriter(r *sftp.Request) (io.WriterAt, error) { }, nil } +// checkWritable refuses an upload onto a directory, which the filer would +// redirect to a child, or onto a protected entry. +func (fs *SftpServer) checkWritable(absPath string) error { + entry, err := fs.getEntry(absPath) + if errors.Is(err, os.ErrNotExist) { + return nil + } + if err != nil { + return err + } + if entry.IsDirectory { + return fmt.Errorf("%s is a directory", absPath) + } + return fs.checkEntryProtected(absPath, entry) +} + func (fs *SftpServer) copyExistingContent(absPath string, dst io.Writer) error { entry, err := fs.getEntry(absPath) if err == os.ErrNotExist { diff --git a/weed/sftpd/sftp_object_lock_test.go b/weed/sftpd/sftp_object_lock_test.go index b8335f63cb..458061b7e3 100644 --- a/weed/sftpd/sftp_object_lock_test.go +++ b/weed/sftpd/sftp_object_lock_test.go @@ -300,3 +300,27 @@ func TestRemoveReadsChunkedFilerConf(t *testing.T) { t.Fatalf("rm under a WORM rule from a chunked filer.conf: got %v, want os.ErrPermission", err) } } + +func TestUploadRechecksTargetAtClose(t *testing.T) { + fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{ + "/buckets/b": {Name: "b", IsDirectory: true}, + "/buckets/b/dir": {Name: "dir", IsDirectory: true}, + "/buckets/b/f": {Name: "f"}, + }) + if w, err := fs.newFileWriter(sftp.NewRequest("Put", "/b/dir")); err == nil { + w.(io.Closer).Close() + t.Fatalf("upload onto a directory was accepted") + } + + w, err := fs.newFileWriter(sftp.NewRequest("Put", "/b/f")) + if err != nil { + t.Fatalf("open: %v", err) + } + fake.mu.Lock() + fake.entries["/buckets/b/f"] = lockedEntry("f", map[string]string{s3_constants.ExtLegalHoldKey: s3_constants.LegalHoldOn}) + fake.entries["/buckets/b/f"].Attributes = &filer_pb.FuseAttributes{FileMode: 0644, Uid: 1000} + fake.mu.Unlock() + if err := w.(io.Closer).Close(); !errors.Is(err, os.ErrPermission) { + t.Fatalf("close after a hold was set: got %v, want os.ErrPermission", err) + } +}