volume: check the source's collection before VolumeCopy builds paths from it

VolumeCopy names the destination files after the collection the source
reports in ReadVolumeFileStatus, so that value needs the same check as
the request's collection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-28 05:01:00 -08:00
1 parent d6c53c5b70
commit af92ae23a7
2 files changed
+57

No files matched your search

@@ -2,10 +2,19 @@ package weed_server
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"github.com/seaweedfs/seaweedfs/weed/storage"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
)
type volumeCopyContextStream struct {
@@ -59,3 +68,48 @@ func TestVolumeHandlersRejectPathCollection(t *testing.T) {
}
}
}
type pathCollectionSource struct {
volumeCopyStatusServer
}
func (s *pathCollectionSource) ReadVolumeFileStatus(ctx context.Context, req *volume_server_pb.ReadVolumeFileStatusRequest) (*volume_server_pb.ReadVolumeFileStatusResponse, error) {
resp, err := s.delegate.ReadVolumeFileStatus(ctx, req)
if resp != nil {
resp.Collection = "../outside"
}
return resp, err
}
func TestVolumeCopyRejectsSourcePathCollection(t *testing.T) {
const vid = needle.VolumeId(43)
sourceStore := newVolumeCopyTestStore(t, t.TempDir())
if err := sourceStore.AddVolume(vid, "", storage.NeedleMapInMemory, "000", "", 0,
needle.GetCurrentVersion(), 0, types.HardDriveType, 0); err != nil {
t.Fatalf("add source volume: %v", err)
}
source := &pathCollectionSource{volumeCopyStatusServer{delegate: &VolumeServer{store: sourceStore}}}
port := serveGrpc(t, func(server *grpc.Server) {
volume_server_pb.RegisterVolumeServerServer(server, source)
})
targetDir := filepath.Join(t.TempDir(), "volumes")
if err := os.Mkdir(targetDir, 0o755); err != nil {
t.Fatal(err)
}
target := &VolumeServer{
store: newVolumeCopyTestStore(t, targetDir),
grpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
AllowUntrustedRemoteEndpoints: true,
}
err := target.VolumeCopy(&volume_server_pb.VolumeCopyRequest{
VolumeId: uint32(vid),
SourceDataNode: fmt.Sprintf("127.0.0.1:%d.%d", port-10000, port),
}, &fakeVolumeCopyStream{})
if err == nil || !strings.Contains(err.Error(), "invalid collection") {
t.Fatalf("VolumeCopy error = %v, want an invalid collection error", err)
}
if leaked, _ := filepath.Glob(filepath.Join(filepath.Dir(targetDir), "outside_43*")); len(leaked) > 0 {
t.Fatalf("VolumeCopy wrote outside the volume directory: %v", leaked)
}
}
+3
View File
@@ -133,6 +133,9 @@ func (vs *VolumeServer) VolumeCopy(req *volume_server_pb.VolumeCopyRequest, stre
if nil != err {
return fmt.Errorf("read volume file status failed, %w", err)
}
if err = checkVolumeCollection(volFileInfoResp.Collection); err != nil {
return fmt.Errorf("source volume %d: %w", req.VolumeId, err)
}
diskType := volFileInfoResp.DiskType
if req.DiskType != "" {