mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
sftp: set the attributes queued on an upload in its post-upload update
A new file can be under its bucket's default retention as soon as the PUT stores it, and a setstat after that is refused, so "put -p" into such a bucket stored the file and then failed the close. Apply the queued attributes in the update that already sets the owner after the upload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ad4015eecd
commit
e1aca2255c
4 files changed
+26
-22
No files matched your search
@@ -156,13 +156,5 @@ func (w *SeaweedSftpFileWriter) Close() error {
|
||||
}
|
||||
|
||||
// Stream the file to the absolute path (after HomeDir translation)
|
||||
if err := w.fs.putFile(w.absPath, w.tmpFile, w.fs.user, mode); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, st := range w.setStats {
|
||||
if err := w.fs.setFileStat(w.absPath, st.flags, st.attrs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return w.fs.putFile(w.absPath, w.tmpFile, w.fs.user, mode, w.setStats)
|
||||
}
|
||||
@@ -477,7 +477,11 @@ func (fs *SftpServer) setFileStat(absPath string, flags sftp.FileAttrFlags, attr
|
||||
return err
|
||||
}
|
||||
dir, _ := util.FullPath(absPath).DirAndName()
|
||||
// apply attrs
|
||||
applyFileStat(entry, flags, attrs)
|
||||
return fs.updateEntry(dir, entry)
|
||||
}
|
||||
|
||||
func applyFileStat(entry *filer_pb.Entry, flags sftp.FileAttrFlags, attrs *sftp.FileStat) {
|
||||
if flags.Permissions {
|
||||
entry.Attributes.FileMode = uint32(attrs.FileMode())
|
||||
}
|
||||
@@ -496,7 +500,6 @@ func (fs *SftpServer) setFileStat(absPath string, flags sftp.FileAttrFlags, attr
|
||||
entry.Attributes.Md5 = nil
|
||||
}
|
||||
}
|
||||
return fs.updateEntry(dir, entry)
|
||||
}
|
||||
|
||||
// ==================== Directory Operations ====================
|
||||
@@ -656,7 +659,9 @@ func (fs *SftpServer) requireEntry(absPath string, isDir bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User, mode os.FileMode) error {
|
||||
// putFile uploads the file, then sets the owner to user and applies stats,
|
||||
// the attributes a client set while the upload buffered.
|
||||
func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User, mode os.FileMode, stats []fileStat) error {
|
||||
dir, filename := util.FullPath(filepath).DirAndName()
|
||||
// Escape the path so a "?" in the filename cannot inject filer query commands like cp.from/mv.from.
|
||||
query := url.Values{"mode": {strconv.FormatUint(uint64(mode.Perm()), 8)}}
|
||||
@@ -712,8 +717,7 @@ func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User
|
||||
if result.Error != "" {
|
||||
return fmt.Errorf("filer error: %s", result.Error)
|
||||
}
|
||||
// Update file ownership using the same pattern as other functions
|
||||
if user != nil {
|
||||
if user != nil || len(stats) > 0 {
|
||||
err := fs.callWithClient(false, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
|
||||
// Look up the file to get its current entry
|
||||
lookupResp, err := client.LookupDirectoryEntry(ctx, &filer_pb.LookupDirectoryEntryRequest{
|
||||
@@ -728,10 +732,14 @@ func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User
|
||||
return fmt.Errorf("file not found after upload: %s/%s", dir, filename)
|
||||
}
|
||||
|
||||
// Update the entry with new uid/gid
|
||||
entry := lookupResp.Entry
|
||||
entry.Attributes.Uid = user.Uid
|
||||
entry.Attributes.Gid = user.Gid
|
||||
if user != nil {
|
||||
entry.Attributes.Uid = user.Uid
|
||||
entry.Attributes.Gid = user.Gid
|
||||
}
|
||||
for _, st := range stats {
|
||||
applyFileStat(entry, st.flags, st.attrs)
|
||||
}
|
||||
|
||||
// Update the entry in the filer
|
||||
_, err = client.UpdateEntry(ctx, &filer_pb.UpdateEntryRequest{
|
||||
@@ -743,7 +751,7 @@ func (fs *SftpServer) putFile(filepath string, reader io.Reader, user *user.User
|
||||
|
||||
if err != nil {
|
||||
// Log the error but don't fail the whole operation
|
||||
glog.Errorf("Failed to update file ownership for %s: %v", filepath, err)
|
||||
glog.Errorf("Failed to update file attributes for %s: %v", filepath, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/pkg/sftp"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/security"
|
||||
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
|
||||
"google.golang.org/grpc"
|
||||
@@ -45,7 +46,7 @@ func TestPutFileEscapesQueryInjection(t *testing.T) {
|
||||
fs := &SftpServer{filerAddr: pb.ServerAddress(strings.TrimPrefix(ts.URL, "http://"))}
|
||||
|
||||
malicious := "/home/alice/steal?cp.from=/home/bob/secret.txt"
|
||||
if err := fs.putFile(malicious, strings.NewReader("dummy"), nil, 0644); err != nil {
|
||||
if err := fs.putFile(malicious, strings.NewReader("dummy"), nil, 0644, nil); err != nil {
|
||||
t.Fatalf("putFile: %v", err)
|
||||
}
|
||||
|
||||
@@ -324,7 +325,8 @@ func TestUploadFileMode(t *testing.T) {
|
||||
}
|
||||
|
||||
// An fsetstat on an upload's handle, as "put -p" sends before closing, finds
|
||||
// no entry yet: it has to wait for the upload and apply then.
|
||||
// no entry yet: it has to wait for the upload and apply then, even when the
|
||||
// upload took the bucket's default retention.
|
||||
func TestSetstatOnBufferingUpload(t *testing.T) {
|
||||
fs, fake := newLockTestServer(t, map[string]*filer_pb.Entry{
|
||||
"/buckets/plain": {Name: "plain", IsDirectory: true},
|
||||
@@ -334,7 +336,9 @@ func TestSetstatOnBufferingUpload(t *testing.T) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
gotMode, gotBody = r.URL.Query().Get("mode"), string(body)
|
||||
fake.mu.Lock()
|
||||
fake.entries[r.URL.Path] = &filer_pb.Entry{Name: path.Base(r.URL.Path), Attributes: &filer_pb.FuseAttributes{FileMode: 0644, FileSize: uint64(len(body)), MtimeNs: 123}}
|
||||
entry := lockedEntry(path.Base(r.URL.Path), map[string]string{s3_constants.ExtObjectLockModeKey: s3_constants.RetentionModeCompliance, s3_constants.ExtRetentionUntilDateKey: retainUntil(time.Hour)})
|
||||
entry.Attributes = &filer_pb.FuseAttributes{FileMode: 0644, FileSize: uint64(len(body)), MtimeNs: 123}
|
||||
fake.entries[r.URL.Path] = entry
|
||||
fake.mu.Unlock()
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
}))
|
||||
|
||||
@@ -423,7 +423,7 @@ func TestPutFileAppliesBucketDefaultRetention(t *testing.T) {
|
||||
fs.bucketsDir = bucketsDir
|
||||
for _, tt := range tests {
|
||||
gotHeader = nil
|
||||
if err := fs.putFile(tt.path, strings.NewReader("data"), nil, 0644); err != nil {
|
||||
if err := fs.putFile(tt.path, strings.NewReader("data"), nil, 0644, nil); err != nil {
|
||||
t.Fatalf("putFile %s: %v", tt.path, err)
|
||||
}
|
||||
mode := gotHeader[s3_constants.ExtObjectLockModeKey]
|
||||
|
||||
Reference in new issue
Block a user