Files
seaweedfs/weed/s3api/s3_objectlock/object_lock_check_test.go
T
Chris LuandClaude Opus 5.5 b0c8bc93dd sftp: honor Object Lock and WORM on delete, rename, overwrite and setstat (#12954)
* sftp: refuse to delete entries under Object Lock or WORM

rm and rmdir sent DeleteEntry with no protection check, so an SFTP user
could delete versions under legal hold or retention, and filer.conf WORM
files, that the S3 API and the filer HTTP API refuse to delete. Check the
entry first and pin the delete to the Object Lock state that was checked.

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

* sftp: refuse to rename entries under Object Lock or WORM

A rename moved a protected version out of its .versions folder, or
replaced a protected target, which the delete check alone does not stop.

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

* sftp: refuse to overwrite or setstat entries under Object Lock or WORM

An upload over an existing protected entry replaced its content, and a
setstat could truncate it.

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

* sftp: let a rename onto itself succeed for a protected entry

The filer treats it as a no-op, so there is nothing to protect.

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

* sftp: read WORM rules from a filer.conf stored in chunks

ReadFilerConf without a master client reads only inline content, so a
chunked filer.conf looked empty and WORM went unenforced.

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

* sftp: pin setstat to the Object Lock state it checked

The update writes back the entry it read, so a hold set in between
would be dropped. The filer now refuses the update instead.

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

* 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>

* sftp: refuse to rename a directory that holds protected entries

A directory rename moves every descendant, so checking the directory
alone let it carry locked versions away from their keys, or WORM files
out from under their rule. Refuse when a WORM rule covers the subtree,
as the filer does for scoped tokens, or when an object in it is under
Object Lock.

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

* s3_objectlock: scan a literal .versions directory recursively

A directory named exactly .versions was listed as flat version files, so a
locked object nested below it was missed by the subtree and bucket scans.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 12:37:57 -08:00

110 lines
3.6 KiB
Go

package s3_objectlock
import (
"context"
"io"
"strconv"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type fakeListStream struct {
entries []*filer_pb.Entry
index int
}
func (s *fakeListStream) Recv() (*filer_pb.ListEntriesResponse, error) {
if s.index >= len(s.entries) {
return nil, io.EOF
}
e := s.entries[s.index]
s.index++
return &filer_pb.ListEntriesResponse{Entry: e}, nil
}
func (s *fakeListStream) Header() (metadata.MD, error) { return metadata.MD{}, nil }
func (s *fakeListStream) Trailer() metadata.MD { return metadata.MD{} }
func (s *fakeListStream) CloseSend() error { return nil }
func (s *fakeListStream) Context() context.Context { return context.Background() }
func (s *fakeListStream) SendMsg(any) error { return nil }
func (s *fakeListStream) RecvMsg(any) error { return nil }
// fakeFilerClient serves a fixed directory -> entries map.
type fakeFilerClient struct {
filer_pb.SeaweedFilerClient
dirs map[string][]*filer_pb.Entry
}
func (c *fakeFilerClient) ListEntries(ctx context.Context, in *filer_pb.ListEntriesRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[filer_pb.ListEntriesResponse], error) {
entries := c.dirs[in.Directory]
if in.StartFromFileName != "" {
// Already returned everything on the first page; the second call gets nothing.
return &fakeListStream{}, nil
}
return &fakeListStream{entries: entries}, nil
}
func lockedDirMarker(name string) *filer_pb.Entry {
until := strconv.FormatInt(time.Now().Add(24*time.Hour).Unix(), 10)
return &filer_pb.Entry{
Name: name,
IsDirectory: true,
Extended: map[string][]byte{
s3_constants.ExtObjectLockModeKey: []byte(s3_constants.RetentionModeCompliance),
s3_constants.ExtRetentionUntilDateKey: []byte(until),
},
}
}
func TestHasObjectsWithActiveLocksDirectoryMarker(t *testing.T) {
client := &fakeFilerClient{dirs: map[string][]*filer_pb.Entry{
"/buckets/lockb": {lockedDirMarker("records")},
// The marker's own subtree is empty; only the marker entry carries the lock.
"/buckets/lockb/records": {},
}}
has, err := HasObjectsWithActiveLocks(context.Background(), client, "/buckets/lockb")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !has {
t.Fatalf("expected the locked directory marker to be reported as an active lock")
}
}
func TestHasObjectsWithActiveLocksUnlockedDirectory(t *testing.T) {
client := &fakeFilerClient{dirs: map[string][]*filer_pb.Entry{
"/buckets/plainb": {{Name: "prefix", IsDirectory: true}},
"/buckets/plainb/prefix": {{Name: "obj.txt"}},
}}
has, err := HasObjectsWithActiveLocks(context.Background(), client, "/buckets/plainb")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if has {
t.Fatalf("expected no active lock for an ordinary prefix and object")
}
}
func TestHasObjectsWithActiveLocksBelowLiteralVersionsDirectory(t *testing.T) {
client := &fakeFilerClient{dirs: map[string][]*filer_pb.Entry{
"/buckets/b": {{Name: s3_constants.VersionsFolder, IsDirectory: true}},
"/buckets/b/.versions": {{Name: "nested", IsDirectory: true}},
"/buckets/b/.versions/nested": {{Name: "held", Extended: map[string][]byte{s3_constants.ExtLegalHoldKey: []byte(s3_constants.LegalHoldOn)}}},
}}
has, err := HasObjectsWithActiveLocks(context.Background(), client, "/buckets/b")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !has {
t.Fatalf("expected the held object below a literal .versions directory to be reported")
}
}