filer: match the read-only rule on the entry path a write lands on (#12969)

* filer: check the read-only rule on the path a POST or PUT writes

A write to an existing directory without a trailing slash stores the
entry under it (the multipart file name, or the base name on PUT), but
the read-only rule was only matched on the request path, so a rule on
the directory's slash-terminated prefix did not apply.

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

* filer: check the read-only rule on the copy destination entry

Copying into an existing directory stores the entry under it, so match
the read-only rule on that final path rather than only the request path.

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

* filer: check the read-only rule on the path saveMetaData persists

saveMetaData resolves the target path with its own directory lookup, so
a directory created after the early check could still route the entry
under a read-only prefix.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-24 20:14:20 -08:00
1 parent 79ddcb49e6
commit 1ddc01d93f
3 files changed
+127

No files matched your search

@@ -106,6 +106,10 @@ func (fs *FilerServer) copy(ctx context.Context, w http.ResponseWriter, r *http.
newName = util.Nvl(newName, oldName)
finalDstPath = util.FullPath(newDir).Child(newName)
}
if fs.filer.FilerConf.MatchStorageRule(string(finalDstPath)).ReadOnly {
writeJsonError(w, r, http.StatusInsufficientStorage, fmt.Errorf("%w: %s", ErrReadOnly, finalDstPath))
return
}
if srcPath == finalDstPath {
glog.V(2).InfofCtx(ctx, "FilerServer.copy rejected self-copy for %s", finalDstPath)
@@ -50,6 +50,8 @@ func (fs *FilerServer) autoChunk(ctx context.Context, w http.ResponseWriter, r *
if err != nil {
errStr := err.Error()
switch {
case errors.Is(err, ErrReadOnly):
writeJsonError(w, r, http.StatusInsufficientStorage, err)
case errStr == constants.ErrMsgOperationNotPermitted:
writeJsonError(w, r, http.StatusForbidden, err)
case strings.HasPrefix(errStr, "read input:") || errStr == io.ErrUnexpectedEOF.Error():
@@ -191,6 +193,9 @@ func (fs *FilerServer) requestChunkSize(query url.Values) int32 {
func (fs *FilerServer) checkPermissions(ctx context.Context, r *http.Request, fileName string) error {
fullPath := fs.fixFilePath(ctx, r, fileName)
if fs.filer.FilerConf.MatchStorageRule(fullPath).ReadOnly {
return fmt.Errorf("%w: %s", ErrReadOnly, fullPath)
}
enforced, err := fs.wormEnforcedForEntry(ctx, fullPath)
if err != nil {
return err
@@ -256,6 +261,10 @@ func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileNa
// fix the path
path := fs.fixFilePath(ctx, r, fileName)
if fs.filer.FilerConf.MatchStorageRule(path).ReadOnly {
replyerr = fmt.Errorf("%w: %s", ErrReadOnly, path)
return
}
var entry *filer.Entry
var newChunks []*filer_pb.FileChunk
@@ -1,14 +1,19 @@
package weed_server
import (
"bytes"
"context"
"errors"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
@@ -215,6 +220,115 @@ func TestPostHandlerMatchesStorageRuleOnDecodedPath(t *testing.T) {
}
}
// A write to an existing directory without a trailing slash lands on a child
// of it, so the read-only rule has to be matched on that child path.
func TestPostHandlerChecksReadOnlyOnFinalPath(t *testing.T) {
const readOnlyPrefix = "/buckets/demo/"
multipartBody := func(t *testing.T) (*bytes.Buffer, string) {
body := &bytes.Buffer{}
mw := multipart.NewWriter(body)
part, err := mw.CreateFormFile("file", "marker.txt")
if err != nil {
t.Fatalf("CreateFormFile: %v", err)
}
part.Write([]byte("hello"))
mw.Close()
return body, mw.FormDataContentType()
}
tests := []struct {
name string
request func(t *testing.T) *http.Request
child string
}{
{
name: "multipart POST",
request: func(t *testing.T) *http.Request {
body, contentType := multipartBody(t)
req := httptest.NewRequest(http.MethodPost, "/buckets/demo", body)
req.Header.Set("Content-Type", contentType)
return req
},
child: readOnlyPrefix + "marker.txt",
},
{
name: "PUT",
request: func(t *testing.T) *http.Request {
return httptest.NewRequest(http.MethodPut, "/buckets/demo", strings.NewReader("hello"))
},
child: readOnlyPrefix + "demo",
},
{
name: "copy",
request: func(t *testing.T) *http.Request {
return httptest.NewRequest(http.MethodPost, "/buckets/demo?cp.from=/src.txt", http.NoBody)
},
child: readOnlyPrefix + "src.txt",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := newRenameTestStore()
source := newFileEntry("/src.txt", 11)
source.Content = []byte("hello")
store.entries["/src.txt"] = source
store.entries["/buckets/demo"] = newDirectoryEntry("/buckets/demo", 10)
server := &FilerServer{
filer: newRenameTestFiler(t, store),
option: &FilerOption{SaveToFilerLimit: 1024},
entryLockTable: util.NewLockTable[util.FullPath](),
}
if err := server.filer.FilerConf.AddLocationConf(&filer_pb.FilerConf_PathConf{
LocationPrefix: readOnlyPrefix,
ReadOnly: true,
}); err != nil {
t.Fatalf("AddLocationConf: %v", err)
}
rec := httptest.NewRecorder()
server.PostHandler(rec, tt.request(t), 0)
if rec.Code != http.StatusInsufficientStorage {
t.Errorf("status = %d, want %d; body=%q", rec.Code, http.StatusInsufficientStorage, rec.Body.String())
}
if _, err := store.FindEntry(context.Background(), util.FullPath(tt.child)); err == nil {
t.Errorf("%s landed in the read-only path", tt.child)
}
})
}
}
// saveMetaData resolves the path it persists on its own, so it has to refuse a
// read-only path even when the earlier check saw a different one.
func TestSaveMetaDataChecksReadOnlyOnPersistedPath(t *testing.T) {
const readOnlyPrefix = "/buckets/demo/"
store := newRenameTestStore()
store.entries["/buckets/demo"] = newDirectoryEntry("/buckets/demo", 10)
server := &FilerServer{
filer: newRenameTestFiler(t, store),
option: &FilerOption{},
entryLockTable: util.NewLockTable[util.FullPath](),
}
if err := server.filer.FilerConf.AddLocationConf(&filer_pb.FilerConf_PathConf{
LocationPrefix: readOnlyPrefix,
ReadOnly: true,
}); err != nil {
t.Fatalf("AddLocationConf: %v", err)
}
chunks := []*filer_pb.FileChunk{{FileId: "3,01637037d6", Size: 5}}
req := httptest.NewRequest(http.MethodPost, "/buckets/demo", http.NoBody)
_, err, uncommitted := server.saveMetaData(context.Background(), req, "marker.txt", "", &operation.StorageOption{}, 0, nil, chunks, 5, nil)
if !errors.Is(err, ErrReadOnly) {
t.Fatalf("saveMetaData err = %v, want ErrReadOnly", err)
}
if len(uncommitted) != 1 {
t.Errorf("uncommitted chunks = %d, want the uploaded chunk back for cleanup", len(uncommitted))
}
if _, err := store.FindEntry(context.Background(), readOnlyPrefix+"marker.txt"); err == nil {
t.Error("entry landed in the read-only path")
}
}
// A completed TUS upload uploads its chunks under the target path's rule, so the
// entry it lands has to expire with them.
func TestCompleteTusUploadAppliesRuleTtl(t *testing.T) {