mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
ce233d6e62
commit
fd3f7d6db3
3 files changed
+44
-5
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user