Files
seaweedfs/weed/sftpd/sftp_open_flags_test.go
T
9290e6d69e sftp: honor the CREATE, EXCL and APPEND open flags (#13354)
* sftp: honor the CREATE, EXCL and APPEND open flags

An upload open ignored three of its flags:

- Without CREATE, opening a missing file created it on close. It now fails
  with NO_SUCH_FILE, as OpenSSH's sftp-server and SFTPGo do.
- CREATE|EXCL on an existing file succeeded and overwrote it. The open now
  creates the empty file with an exclusive filer create, so it fails when
  the name is taken, also while another exclusive upload of it buffers.
- APPEND wrote at the client's offset, usually 0, over the start of the
  file. A write below the size the file had at open is now refused, as
  SFTPGo does. Writes keep their offsets rather than going to the current
  end, because pkg/sftp may run a client's pipelined writes in any order.

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

* sftp: leave a file alone when an open of it changes nothing

An APPEND open whose writes were all refused still uploaded the copied
bytes on close, which rewrote the file and handed it to the session's
user. Any open that copied the existing file now uploads only once the
handle writes or sets attributes, not only read-write opens.

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>
2026-12-03 11:12:02 -08:00

158 lines
4.8 KiB
Go

package sftpd
import (
"context"
"errors"
"os"
"path"
"slices"
"testing"
"github.com/pkg/sftp"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
// uploadRequest opens with WRITE|CREAT, as clients do for an upload.
func uploadRequest(p string) *sftp.Request {
return &sftp.Request{Method: "Put", Filepath: p, Flags: 0x02 | 0x08}
}
func (f *lockTestFiler) CreateEntry(_ context.Context, req *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) {
f.mu.Lock()
defer f.mu.Unlock()
p := path.Join(req.Directory, req.Entry.Name)
if _, ok := f.entries[p]; ok && req.OExcl {
return &filer_pb.CreateEntryResponse{Error: "exists", ErrorCode: filer_pb.FilerError_ENTRY_ALREADY_EXISTS}, nil
}
f.entries[p] = req.Entry
return &filer_pb.CreateEntryResponse{}, nil
}
func (rw *rwTest) entry(p string) *filer_pb.Entry {
rw.fake.mu.Lock()
defer rw.fake.mu.Unlock()
return rw.fake.entries[p]
}
func TestOpenMissingFileWithoutCreate(t *testing.T) {
for _, flags := range []int{os.O_WRONLY, os.O_RDWR} {
rw := newRWTest(t, PermAll)
f, err := rw.client.OpenFile("/b/missing.txt", flags)
if err == nil {
_, _ = f.Write([]byte("x"))
_ = f.Close()
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("open missing with flags %#x: err = %v, want not exist", flags, err)
}
if got := rw.uploads(); len(got) != 0 {
t.Errorf("open missing with flags %#x uploaded %q, want nothing", flags, got)
}
if e := rw.entry("/buckets/b/missing.txt"); e != nil {
t.Errorf("open missing with flags %#x created %v", flags, e)
}
}
}
func TestExclusiveCreateRefusesExistingFile(t *testing.T) {
for _, flags := range []int{os.O_WRONLY, os.O_RDWR} {
rw := newRWTest(t, PermAll)
f, err := rw.client.OpenFile("/b/rw.txt", flags|os.O_CREATE|os.O_EXCL)
if err == nil {
_, _ = f.WriteAt([]byte("XY"), 0)
_ = f.Close()
t.Errorf("exclusive create of an existing file with flags %#x succeeded", flags)
}
if got := rw.uploads(); len(got) != 0 {
t.Errorf("exclusive create of an existing file uploaded %q, want nothing", got)
}
if c := string(rw.entry("/buckets/b/rw.txt").GetContent()); c != "abcdef" {
t.Errorf("content after refused exclusive create = %q, want %q", c, "abcdef")
}
}
}
func TestExclusiveCreateTakesNameAtOpen(t *testing.T) {
rw := newRWTest(t, PermAll)
f, err := rw.client.OpenFile("/b/new.txt", os.O_WRONLY|os.O_CREATE|os.O_EXCL)
if err != nil {
t.Fatalf("exclusive create of a new file: %v", err)
}
if rw.entry("/buckets/b/new.txt") == nil {
t.Errorf("exclusive create left no entry before close")
}
if g, err := rw.client.OpenFile("/b/new.txt", os.O_WRONLY|os.O_CREATE|os.O_EXCL); err == nil {
_ = g.Close()
t.Errorf("second exclusive create of the same name succeeded")
}
if _, err := f.Write([]byte("lock")); err != nil {
t.Fatalf("write: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close: %v", err)
}
if got := rw.uploads(); !slices.Equal(got, []string{"lock"}) {
t.Errorf("uploaded = %q, want %q", got, []string{"lock"})
}
}
// Like SFTPGo, APPEND refuses writes below the size at open.
func TestAppendKeepsExistingContent(t *testing.T) {
for _, flags := range []int{os.O_WRONLY, os.O_RDWR} {
rw := newRWTest(t, PermAll)
f, err := rw.client.OpenFile("/b/rw.txt", flags|os.O_APPEND)
if err != nil {
t.Fatalf("open append with flags %#x: %v", flags, err)
}
if _, err := f.WriteAt([]byte("12"), 0); err == nil {
t.Errorf("append with flags %#x: write at 0 succeeded", flags)
}
if _, err := f.WriteAt([]byte("34"), 6); err != nil {
t.Errorf("append with flags %#x: write at the end: %v", flags, err)
}
if err := f.Close(); err != nil {
t.Fatalf("close: %v", err)
}
if c := string(rw.entry("/buckets/b/rw.txt").GetContent()); c != "abcdef34" {
t.Errorf("content after append with flags %#x = %q, want %q", flags, c, "abcdef34")
}
}
}
func TestAppendAfterTruncate(t *testing.T) {
rw := newRWTest(t, PermAll)
f, err := rw.client.OpenFile("/b/rw.txt", os.O_WRONLY|os.O_APPEND)
if err != nil {
t.Fatalf("open append: %v", err)
}
if err := f.Truncate(2); err != nil {
t.Fatalf("truncate: %v", err)
}
if _, err := f.WriteAt([]byte("XY"), 2); err != nil {
t.Fatalf("write at the new end: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close: %v", err)
}
if got := rw.uploads(); !slices.Equal(got, []string{"abXY"}) {
t.Errorf("uploaded = %q, want %q", got, []string{"abXY"})
}
}
func TestRefusedAppendLeavesFileAlone(t *testing.T) {
rw := newRWTest(t, PermAll)
f, err := rw.client.OpenFile("/b/rw.txt", os.O_WRONLY|os.O_APPEND)
if err != nil {
t.Fatalf("open append: %v", err)
}
if _, err := f.WriteAt([]byte("12"), 0); err == nil {
t.Errorf("append: write at 0 succeeded")
}
if err := f.Close(); err != nil {
t.Fatalf("close: %v", err)
}
if got := rw.uploads(); len(got) != 0 {
t.Errorf("uploaded %q after the only write was refused, want nothing", got)
}
}