Files
seaweedfs/weed/server/filer_grpc_scope_stream_test.go
T
430ca2b585 filer: path- and method-scoped gRPC tokens (scope enforcement, fail closed) (#12904)
* filer: path- and method-scoped tokens on the filesystem gRPC service

* filer: close scoped admission and compatibility gaps

* filer: test subscription key ambiguity and retained write success

* filer: assert verified peer chains survive subscription fallback

* filer: keep a hard link's count and chunks together under a scoped token

A scoped write may keep a hard link the target already has, but the link
count it sends was stored as is, and its chunks could stay on the name
with the link dropped. Either let a scoped token delete its own name and
take the chunks of a name outside its grants with it: the forged count
reaches zero, or the detached name owns the shared chunks.

The handler recheck now refuses a scoped write that changes a stored
link's count or keeps its chunks without the link. Dropping the link
together with the data, and appending to a linked file, are unchanged.

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

* filer: leave subscriptions with a non-scope bearer to the existing verifier

The stream interceptor refused a SubscribeMetadata or SubscribeLocalMetadata
call whose bearer does not verify under the filer key unless the bearer was
a subscription token, and it checked that token with the peer stripped. With
no subscription key, or on an mTLS peer, the existing verifier admits such a
call, so filer.sync sending another cluster's admin token stopped.

When the bearer is no filer scope, the subscription verifier now decides on
the original context, as before scoped tokens. A scoped token still reaches
the refusal for scoped subscriptions, and malformed metadata is still refused.

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

* filer: refuse scoped writes that would make ancestors outside the grant

A scoped create, append or rename checked only its target. The filer then
creates the target's missing ancestors up to the first one it finds, and
turns that one into a directory if it is a file, dropping its ttl. A token
granted /data/proj/ws could so promote the file /data/proj, or create /q and
/q/r for a grant /q/r/ws.

The scope now walks the same ancestors before admitting the write and
refuses one outside the write grants that is missing or not a directory. A
create with skip_check_parent_directory makes no ancestors and is not
walked.

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

* filer: require write grants for scoped lock mutations

* filer: document scoped signatures and read-only token upgrades

* filer: enforce WORM retention for scoped mutations

* filer: refuse scoped writes through shared hard links

* filer: drop series notes from scope comments

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

* filer: refuse scoped subtree mutations across WORM rules

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 08:45:34 -08:00

185 lines
5.9 KiB
Go

package weed_server
import (
"context"
"errors"
"io"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
)
func TestScopeListEntriesBoundsDirectoriesAndFiltersFilerConf(t *testing.T) {
h := newScopeHarness(t)
h.fake.put(scopeFilerConfDir+"/"+scopeFilerConfName, entryWith(scopeFilerConfName))
h.fake.put(scopeFilerConfDir+"/visible.json", entryWith("visible.json"))
c := h.client(scopedToken("alice", time.Hour, []string{scopeFilerConfDir}, nil))
stream, err := c.ListEntries(scopeCtx(t), &filer_pb.ListEntriesRequest{Directory: scopeFilerConfDir})
if err != nil {
t.Fatalf("list in scope: %v", err)
}
names := map[string]bool{}
for {
response, recvErr := stream.Recv()
if errors.Is(recvErr, io.EOF) {
break
}
if recvErr != nil {
t.Fatalf("list response: %v", recvErr)
}
names[response.GetEntry().GetName()] = true
}
if names[scopeFilerConfName] {
t.Fatal("scoped list exposed filer.conf")
}
if !names["visible.json"] {
t.Fatalf("scoped list dropped visible entry: %v", names)
}
before := len(h.fake.received("ListEntries"))
out, err := c.ListEntries(scopeCtx(t), &filer_pb.ListEntriesRequest{Directory: "/buckets/t10"})
if err == nil {
_, err = out.Recv()
}
expectScopeCode(t, "list outside scope", err, codes.PermissionDenied)
if got := len(h.fake.received("ListEntries")); got != before {
t.Fatalf("out-of-scope list reached handler: %d calls before=%d", got, before)
}
}
func TestScopeStreamRenameBoundsBothPaths(t *testing.T) {
h := newScopeHarness(t)
h.fake.putDir(aliceWS)
c := h.client(scopedToken("alice", time.Hour, []string{aliceWS}, nil))
allowed, err := c.StreamRenameEntry(scopeCtx(t), &filer_pb.StreamRenameEntryRequest{
OldDirectory: aliceWS,
OldName: "old",
NewDirectory: aliceWS + "/sub",
NewName: "new",
})
if err != nil {
t.Fatalf("in-scope stream rename: %v", err)
}
if _, err := allowed.Recv(); !errors.Is(err, io.EOF) {
t.Fatalf("in-scope stream rename ended with %v, want EOF", err)
}
before := len(h.fake.received("StreamRenameEntry"))
refused, err := c.StreamRenameEntry(scopeCtx(t), &filer_pb.StreamRenameEntryRequest{
OldDirectory: aliceWS,
OldName: "old",
NewDirectory: bobWS,
NewName: "new",
})
if err == nil {
_, err = refused.Recv()
}
expectScopeCode(t, "cross-prefix stream rename", err, codes.PermissionDenied)
if got := len(h.fake.received("StreamRenameEntry")); got != before {
t.Fatalf("refused stream rename reached handler: %d calls before=%d", got, before)
}
}
func TestScopeStreamMutateRefusesMessageAndContinues(t *testing.T) {
h := newScopeHarness(t)
c := h.client(scopedToken("alice", time.Hour, []string{aliceWS}, nil))
stream, err := c.StreamMutateEntry(scopeCtx(t))
if err != nil {
t.Fatalf("open mutate stream: %v", err)
}
sendDelete := func(id uint64, directory string) {
t.Helper()
if err := stream.Send(&filer_pb.StreamMutateEntryRequest{
RequestId: id,
Request: &filer_pb.StreamMutateEntryRequest_DeleteRequest{DeleteRequest: &filer_pb.DeleteEntryRequest{Directory: directory, Name: "file"}},
}); err != nil {
t.Fatalf("send %d: %v", id, err)
}
}
receive := func(id uint64) *filer_pb.StreamMutateEntryResponse {
t.Helper()
response, err := stream.Recv()
if err != nil {
t.Fatalf("receive %d: %v", id, err)
}
if response.GetRequestId() != id {
t.Fatalf("response id=%d, want %d", response.GetRequestId(), id)
}
return response
}
sendDelete(1, aliceWS)
if response := receive(1); response.GetError() != "" {
t.Fatalf("in-scope delete returned %q", response.GetError())
}
sendDelete(2, bobWS)
response := receive(2)
if response.GetErrno() != scopeErrnoEACCES || response.GetError() == "" || !response.GetIsLast() {
t.Fatalf("out-of-scope delete response=%v, want EACCES refusal", response)
}
sendDelete(3, aliceWS)
if response := receive(3); response.GetError() != "" {
t.Fatalf("stream did not continue after refusal: %q", response.GetError())
}
if err := stream.CloseSend(); err != nil {
t.Fatalf("close mutate stream: %v", err)
}
if _, err := stream.Recv(); !errors.Is(err, io.EOF) {
t.Fatalf("mutate stream after close = %v, want EOF", err)
}
if got := len(h.fake.received("StreamMutateEntry")); got != 2 {
t.Fatalf("handler received %d messages, want the two allowed messages", got)
}
}
func TestScopeStreamExpiryStopsFurtherMessages(t *testing.T) {
underlying := &scopeExpiryServerStream{ctx: context.Background()}
wrapped := &scopedStream{
ServerStream: underlying,
sc: &scope{exp: time.Now().Add(time.Minute)},
}
if err := wrapped.RecvMsg(&filer_pb.StreamMutateEntryRequest{}); err != nil {
t.Fatalf("receive before expiry: %v", err)
}
if underlying.recvCalls != 1 {
t.Fatalf("underlying receive calls=%d, want 1", underlying.recvCalls)
}
wrapped.sc.exp = time.Now().Add(-time.Second)
expectScopeCode(t, "receive after token expiry", wrapped.RecvMsg(&filer_pb.StreamMutateEntryRequest{}), codes.Unauthenticated)
expectScopeCode(t, "send after token expiry", wrapped.SendMsg(&filer_pb.StreamMutateEntryResponse{}), codes.Unauthenticated)
if underlying.recvCalls != 1 {
t.Fatalf("expired stream attempted another receive: calls=%d, want 1", underlying.recvCalls)
}
if underlying.sendCalls != 0 {
t.Fatalf("expired response reached underlying stream: %d sends", underlying.sendCalls)
}
}
type scopeExpiryServerStream struct {
ctx context.Context
recvCalls int
sendCalls int
}
func (s *scopeExpiryServerStream) SetHeader(metadata.MD) error { return nil }
func (s *scopeExpiryServerStream) SendHeader(metadata.MD) error { return nil }
func (s *scopeExpiryServerStream) SetTrailer(metadata.MD) {}
func (s *scopeExpiryServerStream) Context() context.Context { return s.ctx }
func (s *scopeExpiryServerStream) SendMsg(any) error {
s.sendCalls++
return nil
}
func (s *scopeExpiryServerStream) RecvMsg(any) error {
s.recvCalls++
return nil
}