sftp: refuse a setstat that two open uploads of its path could mean

Sending it to the stored entry instead could still apply a size the client
meant for its buffered upload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-02 14:57:56 -08:00
1 parent ba31775360
commit c52e2b9d51
3 files changed
+32 -15

No files matched your search

+12 -6
View File
@@ -2,6 +2,7 @@
package sftpd
import (
"fmt"
"io"
"os"
"slices"
@@ -87,15 +88,20 @@ func (fs *SftpServer) untrackUpload(w *SeaweedSftpFileWriter) {
}
}
// openUpload returns the upload open on absPath. A setstat names only the
// path, so with several uploads open there it cannot tell which one it is for.
func (fs *SftpServer) openUpload(absPath string) *SeaweedSftpFileWriter {
// openUpload returns the upload open on absPath, or nil. A setstat names only
// the path, so with several uploads open there it cannot tell which one it is
// for and is refused.
func (fs *SftpServer) openUpload(absPath string) (*SeaweedSftpFileWriter, error) {
fs.uploadsMu.Lock()
defer fs.uploadsMu.Unlock()
if open := fs.uploads[absPath]; len(open) == 1 {
return open[0]
switch open := fs.uploads[absPath]; len(open) {
case 0:
return nil, nil
case 1:
return open[0], nil
default:
return nil, fmt.Errorf("%s has %d uploads open: %w", absPath, len(open), sftp.ErrSSHFxFailure)
}
return nil
}
// setStat takes an fsetstat on the handle of a buffering upload, whose entry
+5 -1
View File
@@ -127,7 +127,11 @@ func (fs *SftpServer) dispatchCmd(r *sftp.Request) error {
case "Rmdir":
return fs.removeDir(absPath)
case "Setstat":
if w := fs.openUpload(absPath); w != nil {
w, err := fs.openUpload(absPath)
if err != nil {
return err
}
if w != nil {
if handled, err := w.setStat(r.AttrFlags(), r.Attributes()); handled {
return err
}
+15 -8
View File
@@ -376,7 +376,7 @@ func TestSetstatOnBufferingUpload(t *testing.T) {
if got.GetMtime() != mtime || got.GetMtimeNs() != 0 || os.FileMode(got.GetFileMode()).Perm() != 0600 {
t.Errorf("stored mtime %d.%09d mode %o, want %d.000000000 and 600", got.GetMtime(), got.GetMtimeNs(), got.GetFileMode(), mtime)
}
if fs.openUpload("/buckets/plain/new") != nil {
if w, _ := fs.openUpload("/buckets/plain/new"); w != nil {
t.Error("closed upload is still tracked")
}
@@ -398,10 +398,11 @@ func TestSetstatOnBufferingUpload(t *testing.T) {
}
// A setstat names only the path, so it can't pick between two uploads open
// on the same one and goes to the stored entry instead.
// on the same one, and changing the stored entry instead would be wrong too.
func TestSetstatWithTwoUploadsOnOnePath(t *testing.T) {
fs, _ := newLockTestServer(t, map[string]*filer_pb.Entry{
"/buckets/plain": {Name: "plain", IsDirectory: true},
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
"/buckets/plain": {Name: "plain", IsDirectory: true},
"/buckets/plain/new": {Name: "new", Attributes: &filer_pb.FuseAttributes{FileMode: 0644, FileSize: 10}},
})
open := func() io.WriteCloser {
req := sftp.NewRequest("Put", "/plain/new")
@@ -413,12 +414,18 @@ func TestSetstatWithTwoUploadsOnOnePath(t *testing.T) {
return w.(io.WriteCloser)
}
first, second := open(), open()
if fs.openUpload("/buckets/plain/new") != nil {
t.Fatal("setstat routed to one of two open uploads")
setstat := sftp.NewRequest("Setstat", "/plain/new")
setstat.Flags = 0x1 // size
setstat.Attrs = binary.BigEndian.AppendUint64(nil, 0)
if err := fs.dispatchCmd(setstat); err == nil {
t.Error("setstat with two uploads open succeeded")
}
if fake.lastUpdate != nil {
t.Errorf("setstat changed the stored entry: %v", fake.lastUpdate.Entry.Attributes)
}
_ = second.Close()
if got := fs.openUpload("/buckets/plain/new"); got != first {
t.Fatalf("open upload = %p, want the remaining one %p", got, first)
if got, err := fs.openUpload("/buckets/plain/new"); got != first || err != nil {
t.Fatalf("open upload = %p, %v, want the remaining one %p", got, err, first)
}
_ = first.Close()
}