diff --git a/weed/server/volume_grpc_collection_name_test.go b/weed/server/volume_grpc_collection_name_test.go index 0cc840ef3f..c47675de2e 100644 --- a/weed/server/volume_grpc_collection_name_test.go +++ b/weed/server/volume_grpc_collection_name_test.go @@ -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) + } +} diff --git a/weed/server/volume_grpc_copy.go b/weed/server/volume_grpc_copy.go index e8db79fadb..da9e240899 100644 --- a/weed/server/volume_grpc_copy.go +++ b/weed/server/volume_grpc_copy.go @@ -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 != "" {