From 373eb29e594e572c3e1e435d360addc441defbb0 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 5 Dec 2026 11:10:42 -0800 Subject: [PATCH] s3tables: table policy, maintenance and tag operations refuse a view (#13439) * s3tables: table policy and maintenance reads refuse views and unfinished records readTableMetadataAttr only checked that the table-metadata attribute was present, so the table policy and maintenance operations accepted a view or a record a create or rename had not finished writing. Route it through tableMetadata, the check GetTable and the other table operations use. Co-Authored-By: Claude Opus 5.5 * s3tables: tagging a table ARN refuses a view or unfinished record TagResource, ListTagsForResource and UntagResource read the table-metadata attribute directly for a table ARN, so a view or a record still being created or renamed under that name took and answered tags as a table. Read a table target through readTableMetadataAttr. Co-Authored-By: Claude Opus 5.5 * s3tables: table record writes refuse a view requireTableCreatedAt compared only the creation time, so a view holding the name at write time was not rejected on its own. Refuse it, so table policy, tag and maintenance writes never land on a view. Co-Authored-By: Claude Opus 5.5 * s3tables: test table operations against a view and an unfinished record Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- weed/s3api/s3tables/filer_ops.go | 15 +-- weed/s3api/s3tables/handler_policy.go | 16 ++- .../s3tables/handler_view_not_table_test.go | 97 +++++++++++++++++++ 3 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 weed/s3api/s3tables/handler_view_not_table_test.go diff --git a/weed/s3api/s3tables/filer_ops.go b/weed/s3api/s3tables/filer_ops.go index 150bf9171d..eea629e45e 100644 --- a/weed/s3api/s3tables/filer_ops.go +++ b/weed/s3api/s3tables/filer_ops.go @@ -479,14 +479,15 @@ func (h *S3TablesHandler) loadNamespaceMetadata(ctx context.Context, filerClient return &metadata, nil } -// readTableMetadataAttr returns the table-metadata xattr at tablePath. A -// directory without it is not a table, so its absence reads as a missing entry. +// readTableMetadataAttr returns the table-metadata xattr at tablePath. A view, +// an unfinished record, or a directory without it is not a table, so each reads +// as a missing entry. func (h *S3TablesHandler) readTableMetadataAttr(ctx context.Context, client filer_pb.SeaweedFilerClient, tablePath string) ([]byte, error) { - data, err := h.getExtendedAttribute(ctx, client, tablePath, ExtendedKeyMetadata) - if errors.Is(err, ErrAttributeNotFound) { - return nil, fmt.Errorf("%w: %s", filer_pb.ErrNotFound, tablePath) + entry, err := h.lookupEntry(ctx, client, tablePath) + if err != nil { + return nil, err } - return data, err + return tableMetadata(entry) } // lookupEntry returns the filer entry at the given path. @@ -777,7 +778,7 @@ func (h *S3TablesHandler) mutateCatalogRecord( // settings into a conflict. func requireTableCreatedAt(extended map[string][]byte, path string, createdAt time.Time) error { var current tableMetadataInternal - if json.Unmarshal(extended[ExtendedKeyMetadata], ¤t) != nil || !current.CreatedAt.Equal(createdAt) { + if EntryType(extended) == EntryTypeView || json.Unmarshal(extended[ExtendedKeyMetadata], ¤t) != nil || !current.CreatedAt.Equal(createdAt) { return fmt.Errorf("%w: %s holds another table", ErrConcurrentUpdate, path) } return nil diff --git a/weed/s3api/s3tables/handler_policy.go b/weed/s3api/s3tables/handler_policy.go index 8575298dd1..03d699969a 100644 --- a/weed/s3api/s3tables/handler_policy.go +++ b/weed/s3api/s3tables/handler_policy.go @@ -799,7 +799,7 @@ func (h *S3TablesHandler) handleTagResource(w http.ResponseWriter, r *http.Reque requestTagKeys := mapKeys(req.Tags) err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { // Read metadata for ownership check - data, err := h.getExtendedAttribute(r.Context(), client, resourcePath, ExtendedKeyMetadata) + data, err := h.readResourceMetadata(r.Context(), client, resourcePath, rType) if err != nil { if errors.Is(err, filer_pb.ErrNotFound) || errors.Is(err, ErrAttributeNotFound) { return h.absentTagTargetError(r, client, "TagResource", "not authorized to tag resource", req.ResourceARN, rType) @@ -965,7 +965,7 @@ func (h *S3TablesHandler) handleListTagsForResource(w http.ResponseWriter, r *ht var tableNamespace, tableName string err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { // Read metadata for ownership check - data, err := h.getExtendedAttribute(r.Context(), client, resourcePath, ExtendedKeyMetadata) + data, err := h.readResourceMetadata(r.Context(), client, resourcePath, rType) if err != nil { if errors.Is(err, filer_pb.ErrNotFound) || errors.Is(err, ErrAttributeNotFound) { return h.absentTagTargetError(r, client, "ListTagsForResource", "not authorized to list tags for resource", req.ResourceARN, rType) @@ -1094,7 +1094,7 @@ func (h *S3TablesHandler) handleUntagResource(w http.ResponseWriter, r *http.Req var createdAt time.Time err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error { // Read metadata for ownership check - data, err := h.getExtendedAttribute(r.Context(), client, resourcePath, ExtendedKeyMetadata) + data, err := h.readResourceMetadata(r.Context(), client, resourcePath, rType) if err != nil { if errors.Is(err, filer_pb.ErrNotFound) || errors.Is(err, ErrAttributeNotFound) { return h.absentTagTargetError(r, client, "UntagResource", "not authorized to untag resource", req.ResourceARN, rType) @@ -1239,6 +1239,16 @@ func (h *S3TablesHandler) absentTagTargetError(r *http.Request, client filer_pb. return filer_pb.ErrNotFound } +// readResourceMetadata returns the metadata xattr of a tagging target. A table +// ARN reads it the way the table operations do, so a view or an unfinished +// record at that name is absent. +func (h *S3TablesHandler) readResourceMetadata(ctx context.Context, client filer_pb.SeaweedFilerClient, resourcePath string, rType ResourceType) ([]byte, error) { + if rType == ResourceTypeTable { + return h.readTableMetadataAttr(ctx, client, resourcePath) + } + return h.getExtendedAttribute(ctx, client, resourcePath, ExtendedKeyMetadata) +} + // resolveResourcePath determines the resource path and extended attribute key from a resource ARN func (h *S3TablesHandler) resolveResourcePath(resourceARN string) (path string, key string, rType ResourceType, err error) { // Try parsing as table ARN first diff --git a/weed/s3api/s3tables/handler_view_not_table_test.go b/weed/s3api/s3tables/handler_view_not_table_test.go new file mode 100644 index 0000000000..2546d298f5 --- /dev/null +++ b/weed/s3api/s3tables/handler_view_not_table_test.go @@ -0,0 +1,97 @@ +package s3tables + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// A view, or a table its create has not finished writing, is not a table: the +// table policy, maintenance and tag operations answer NoSuchTable for its name +// and write nothing to it. +func TestTableOperationsRefuseViewAndUnfinishedRecord(t *testing.T) { + fs, m := startRenameManager(t) + seedView(t, fs, "v") + nsPath := GetNamespacePath(renameTestBucket, "ns") + fs.Put(nsPath, "c", map[string][]byte{ + ExtendedKeyMetadata: []byte(`{"name":"c","namespace":"ns","ownerAccountId":"` + DefaultAccountID + `"}`), + ExtendedKeyDeleting: []byte(createClaim), + }) + bucketARN := mustBucketARN(t) + + for _, name := range []string{"v", "c"} { + before := extendedOf(t, fs, nsPath, name) + tableARN, err := BuildTableARN(DefaultRegion, DefaultAccountID, renameTestBucket, "ns", name) + require.NoError(t, err) + ops := []struct { + operation string + input interface{} + }{ + {"PutTablePolicy", &PutTablePolicyRequest{ + TableBucketARN: bucketARN, + Namespace: []string{"ns"}, + Name: name, + ResourcePolicy: `{"Statement":[{"Effect":"Allow","Principal":"*","Action":"s3tables:*","Resource":"*"}]}`, + }}, + {"GetTablePolicy", &GetTablePolicyRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"DeleteTablePolicy", &DeleteTablePolicyRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"PutTableMaintenanceConfiguration", &PutTableMaintenanceConfigurationRequest{ + TableBucketARN: bucketARN, + Namespace: []string{"ns"}, + Name: name, + Type: MaintenanceTypeIcebergCompaction, + Value: &MaintenanceConfigurationValue{Status: MaintenanceStatusEnabled}, + }}, + {"GetTableMaintenanceConfiguration", &GetTableMaintenanceConfigurationRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"GetTableMaintenanceJobStatus", &GetTableMaintenanceJobStatusRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"TagResource", &TagResourceRequest{ResourceARN: tableARN, Tags: map[string]string{"k": "v"}}}, + {"ListTagsForResource", &ListTagsForResourceRequest{ResourceARN: tableARN}}, + {"UntagResource", &UntagResourceRequest{ResourceARN: tableARN, TagKeys: []string{"k"}}}, + } + for _, op := range ops { + t.Run(name+"/"+op.operation, func(t *testing.T) { + status, resp := runHandler(t, m, fs.Client, op.operation, op.input) + assert.Equal(t, http.StatusNotFound, status, "%+v", resp) + assert.Equal(t, ErrCodeNoSuchTable, resp.Type, "%+v", resp) + }) + } + assert.Equal(t, before, extendedOf(t, fs, nsPath, name), "%s must not be written to", name) + } +} + +// An identity granted only s3tables:PutTablePolicy cannot plant a policy on +// another account's view through the table API and so open the view to itself. +func TestPutTablePolicyCannotOpenAView(t *testing.T) { + fs, m := startRenameManager(t) + m.SetTrusted(false) + m.SetDefaultAllow(false) + seedView(t, fs, "v") + bucketARN := mustBucketARN(t) + + call := func(operation string, input interface{}) int { + body, err := json.Marshal(input) + require.NoError(t, err) + identity := &testIdentity{Name: "other", Account: &testIdentityAccount{Id: "other"}, + Actions: []string{"s3tables:PutTablePolicy"}} + req, err := newManagerRequest(s3_constants.SetIdentityInContext(assignedCtx(), identity), operation, body, "other") + require.NoError(t, err) + rec := httptest.NewRecorder() + m.handler.HandleRequest(rec, req, NewManagerClient(fs.Client)) + return rec.Code + } + + assert.Equal(t, http.StatusNotFound, call("PutTablePolicy", &PutTablePolicyRequest{ + TableBucketARN: bucketARN, + Namespace: []string{"ns"}, + Name: "v", + ResourcePolicy: `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"s3tables:*","Resource":"*"}]}`, + })) + assert.NotEqual(t, http.StatusOK, call("GetView", &GetViewRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: "v"})) + assert.NotEqual(t, http.StatusOK, call("DeleteView", &DeleteViewRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: "v"})) + assert.NotNil(t, fs.Get(GetNamespacePath(renameTestBucket, "ns"), "v"), "the view must still be stored") +}