volume: check the collection name in handlers that build file paths from it (#13087)

* volume: check the collection name in handlers that build file paths from it

AllocateVolume, VolumeMount, VolumeCopy and the EC rebuild, copy,
delete, mount and to-volume handlers joined the request collection into
volume file paths, so a collection such as "../x" reached files outside
the volume directory. They now apply checkVolumeCollection, as CopyFile
and ReceiveFile already do.

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

* seaweed-volume: check the collection name in handlers that build file paths from it

Mirrors the Go volume server: allocate_volume, volume_mount, volume_copy
and the EC rebuild, copy, delete, mount and to-volume handlers apply
check_volume_collection, as copy_file and receive_file already do.

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

* 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>

* seaweed-volume: check the source's collection before volume_copy builds paths from it

Mirrors the Go VolumeCopy: the collection the source reports in
ReadVolumeFileStatus names the destination files, so it gets
check_volume_collection too.

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

* volume: stub the source in the VolumeCopy source collection test

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

* seaweed-volume: keep check_volume_collection's doc comment as on master

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-28 09:50:53 -08:00
1 parent 16412c374a
commit 6b2ad112a8
5 files changed
+285

No files matched your search

+155
View File
@@ -1604,6 +1604,7 @@ impl VolumeServer for VolumeGrpcService {
self.check_grpc_admin_auth(&request)?;
self.state.check_maintenance()?;
let req = request.into_inner();
check_volume_collection(&req.collection).map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
let rp = crate::storage::super_block::ReplicaPlacement::from_string(&req.replication)
.map_err(|e| Status::invalid_argument(e.to_string()))?;
@@ -2007,6 +2008,8 @@ impl VolumeServer for VolumeGrpcService {
) -> Result<Response<volume_server_pb::VolumeMountResponse>, Status> {
self.check_grpc_admin_auth(&request)?;
let req = request.into_inner();
check_volume_collection(req.collection.as_deref().unwrap_or_default())
.map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
// A decode in flight may be mid-compaction on this volume's
@@ -2367,6 +2370,7 @@ impl VolumeServer for VolumeGrpcService {
self.check_grpc_admin_auth(&request)?;
self.state.check_maintenance()?;
let req = request.into_inner();
check_volume_collection(&req.collection).map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
if !self.state.allow_untrusted_remote_endpoints {
@@ -3457,6 +3461,7 @@ impl VolumeServer for VolumeGrpcService {
self.check_grpc_admin_auth(&request)?;
self.state.check_maintenance()?;
let req = request.into_inner();
check_volume_collection(&req.collection).map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
let collection = &req.collection;
@@ -3927,6 +3932,7 @@ impl VolumeServer for VolumeGrpcService {
self.check_grpc_admin_auth(&request)?;
self.state.check_maintenance()?;
let req = request.into_inner();
check_volume_collection(&req.collection).map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
if !self.state.allow_untrusted_remote_endpoints {
@@ -4007,6 +4013,7 @@ impl VolumeServer for VolumeGrpcService {
) -> Result<Response<volume_server_pb::VolumeEcShardsDeleteResponse>, Status> {
self.check_grpc_admin_auth(&request)?;
let req = request.into_inner();
check_volume_collection(&req.collection).map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
if req.full_teardown {
@@ -4150,6 +4157,7 @@ impl VolumeServer for VolumeGrpcService {
request: Request<volume_server_pb::VolumeEcShardsMountRequest>,
) -> Result<Response<volume_server_pb::VolumeEcShardsMountResponse>, Status> {
let req = request.into_inner();
check_volume_collection(&req.collection).map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
// Pre-validate the ENTIRE batch before any mutation: validating inside
@@ -4481,6 +4489,7 @@ impl VolumeServer for VolumeGrpcService {
self.check_grpc_admin_auth(&request)?;
self.state.check_maintenance()?;
let req = request.into_inner();
check_volume_collection(&req.collection).map_err(Status::invalid_argument)?;
let vid = VolumeId(req.volume_id);
// Staged mode: the caller decoded off-box and streamed the normal volume
@@ -6023,6 +6032,8 @@ mod copy_source {
.await
.map_err(|e| Status::internal(format!("read volume file status failed, {}", e)))?
.into_inner();
super::check_volume_collection(&vol_info.collection)
.map_err(|e| Status::internal(format!("source volume {}: {}", volume_id, e)))?;
Ok(Self(vol_info))
}
}
@@ -13375,6 +13386,150 @@ mod tests {
assert!(err.message().contains("is being copied"), "{}", err.message());
}
#[tokio::test]
async fn test_handlers_reject_a_collection_that_leaves_the_volume_dir() {
use volume_server_pb::*;
let (service, _tmp) = make_local_service_with_volume("collection_check", None);
for collection in ["..", "../outside", "a/b", "a\\b"] {
let c = || collection.to_string();
let errs = [
(
"allocate_volume",
service
.allocate_volume(Request::new(AllocateVolumeRequest {
volume_id: 7,
collection: c(),
..Default::default()
}))
.await
.err(),
),
(
"volume_mount",
service
.volume_mount(Request::new(VolumeMountRequest {
volume_id: 7,
collection: Some(c()),
..Default::default()
}))
.await
.err(),
),
(
"volume_copy",
service
.volume_copy(Request::new(VolumeCopyRequest {
volume_id: 7,
collection: c(),
..Default::default()
}))
.await
.err(),
),
(
"volume_ec_shards_rebuild",
service
.volume_ec_shards_rebuild(Request::new(VolumeEcShardsRebuildRequest {
volume_id: 7,
collection: c(),
..Default::default()
}))
.await
.err(),
),
(
"volume_ec_shards_copy",
service
.volume_ec_shards_copy(Request::new(VolumeEcShardsCopyRequest {
volume_id: 7,
collection: c(),
..Default::default()
}))
.await
.err(),
),
(
"volume_ec_shards_delete",
service
.volume_ec_shards_delete(Request::new(VolumeEcShardsDeleteRequest {
volume_id: 7,
collection: c(),
full_teardown: true,
..Default::default()
}))
.await
.err(),
),
(
"volume_ec_shards_mount",
service
.volume_ec_shards_mount(Request::new(VolumeEcShardsMountRequest {
volume_id: 7,
collection: c(),
shard_ids: vec![0],
..Default::default()
}))
.await
.err(),
),
(
"volume_ec_shards_to_volume",
service
.volume_ec_shards_to_volume(Request::new(VolumeEcShardsToVolumeRequest {
volume_id: 7,
collection: c(),
from_staged: true,
..Default::default()
}))
.await
.err(),
),
];
for (name, err) in errs {
let err = err.unwrap_or_else(|| panic!("{name}({collection:?}) succeeded"));
assert_eq!(
err.code(),
tonic::Code::InvalidArgument,
"{name}({collection:?})"
);
assert!(
err.message().contains("invalid collection"),
"{name}({collection:?}): {}",
err.message()
);
}
}
}
#[tokio::test]
async fn test_volume_copy_rejects_a_source_collection_that_leaves_the_volume_dir() {
let (source_service, _source_tmp) = make_local_service_with_volume("a\\b", None);
let (port, _shutdown) = serve_source(source_service).await;
let (dest_service, _dest_tmp) = make_local_service_with_volume("", None);
dest_service
.state
.store
.write()
.unwrap()
.delete_volume(VolumeId(1), false, false, false)
.unwrap();
let err = dest_service
.volume_copy(Request::new(volume_server_pb::VolumeCopyRequest {
volume_id: 1,
source_data_node: format!("127.0.0.1:1.{}", port),
..Default::default()
}))
.await
.err()
.expect("copy from a source reporting a path collection succeeded");
assert!(
err.message().contains("invalid collection"),
"{}",
err.message()
);
}
/// The staged adoption renames .copying files into place and mounts, so
/// it holds the copy claim for the whole span — Go's adoptStagedVolume.
#[tokio::test]
+6
View File
@@ -102,6 +102,9 @@ func (vs *VolumeServer) AllocateVolume(ctx context.Context, req *volume_server_p
if err := vs.checkGrpcAdminAuth(ctx); err != nil {
return resp, err
}
if err := checkVolumeCollection(req.Collection); err != nil {
return resp, err
}
if err := vs.CheckMaintenanceMode(); err != nil {
return resp, err
@@ -146,6 +149,9 @@ func (vs *VolumeServer) VolumeMount(ctx context.Context, req *volume_server_pb.V
if err := vs.checkGrpcAdminAuth(ctx); err != nil {
return resp, err
}
if err := checkVolumeCollection(req.GetCollection()); err != nil {
return resp, err
}
// A copy claims the volume, then waits on the source. A mount in that
// window loads files the copy is about to clear or overwrite, so the
@@ -0,0 +1,103 @@
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/needle"
)
type volumeCopyContextStream struct {
volume_server_pb.VolumeServer_VolumeCopyServer
}
func (volumeCopyContextStream) Context() context.Context { return context.Background() }
func TestVolumeHandlersRejectPathCollection(t *testing.T) {
ctx := context.Background()
for _, collection := range []string{"..", "../outside", "a/b", `a\b`} {
vs := &VolumeServer{}
calls := map[string]func() error{
"AllocateVolume": func() error {
_, err := vs.AllocateVolume(ctx, &volume_server_pb.AllocateVolumeRequest{VolumeId: 7, Collection: collection})
return err
},
"VolumeMount": func() error {
_, err := vs.VolumeMount(ctx, &volume_server_pb.VolumeMountRequest{VolumeId: 7, Collection: &collection})
return err
},
"VolumeCopy": func() error {
return vs.VolumeCopy(&volume_server_pb.VolumeCopyRequest{VolumeId: 7, Collection: collection}, volumeCopyContextStream{})
},
"VolumeEcShardsRebuild": func() error {
_, err := vs.VolumeEcShardsRebuild(ctx, &volume_server_pb.VolumeEcShardsRebuildRequest{VolumeId: 7, Collection: collection})
return err
},
"VolumeEcShardsCopy": func() error {
_, err := vs.VolumeEcShardsCopy(ctx, &volume_server_pb.VolumeEcShardsCopyRequest{VolumeId: 7, Collection: collection})
return err
},
"VolumeEcShardsDelete": func() error {
_, err := vs.VolumeEcShardsDelete(ctx, &volume_server_pb.VolumeEcShardsDeleteRequest{VolumeId: 7, Collection: collection, FullTeardown: true})
return err
},
"VolumeEcShardsMount": func() error {
_, err := vs.VolumeEcShardsMount(ctx, &volume_server_pb.VolumeEcShardsMountRequest{VolumeId: 7, Collection: collection, ShardIds: []uint32{0}})
return err
},
"VolumeEcShardsToVolume": func() error {
_, err := vs.VolumeEcShardsToVolume(ctx, &volume_server_pb.VolumeEcShardsToVolumeRequest{VolumeId: 7, Collection: collection, FromStaged: true})
return err
},
}
for name, call := range calls {
err := call()
if err == nil || !strings.Contains(err.Error(), "invalid collection") {
t.Errorf("%s(collection=%q) = %v, want an invalid collection error", name, collection, err)
}
}
}
}
type pathCollectionSource struct {
volume_server_pb.UnimplementedVolumeServerServer
}
func (pathCollectionSource) ReadVolumeFileStatus(ctx context.Context, req *volume_server_pb.ReadVolumeFileStatusRequest) (*volume_server_pb.ReadVolumeFileStatusResponse, error) {
return &volume_server_pb.ReadVolumeFileStatusResponse{VolumeId: req.VolumeId, Collection: "../outside"}, nil
}
func TestVolumeCopyRejectsSourcePathCollection(t *testing.T) {
const vid = needle.VolumeId(43)
port := serveGrpc(t, func(server *grpc.Server) {
volume_server_pb.RegisterVolumeServerServer(server, pathCollectionSource{})
})
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)
}
}
+6
View File
@@ -64,6 +64,9 @@ func (vs *VolumeServer) VolumeCopy(req *volume_server_pb.VolumeCopyRequest, stre
if err := vs.checkGrpcAdminAuth(stream.Context()); err != nil {
return err
}
if err := checkVolumeCollection(req.Collection); err != nil {
return err
}
if err := vs.CheckMaintenanceMode(); err != nil {
return err
}
@@ -130,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 != "" {
+15
View File
@@ -205,6 +205,9 @@ func (vs *VolumeServer) VolumeEcShardsRebuild(ctx context.Context, req *volume_s
if err := vs.checkGrpcAdminAuth(ctx); err != nil {
return nil, err
}
if err := checkVolumeCollection(req.Collection); err != nil {
return nil, err
}
if err := vs.CheckMaintenanceMode(); err != nil {
return nil, err
}
@@ -399,6 +402,9 @@ func (vs *VolumeServer) VolumeEcShardsCopy(ctx context.Context, req *volume_serv
if err := vs.checkGrpcAdminAuth(ctx); err != nil {
return nil, err
}
if err := checkVolumeCollection(req.Collection); err != nil {
return nil, err
}
if err := vs.CheckMaintenanceMode(); err != nil {
return nil, err
}
@@ -842,6 +848,9 @@ func (vs *VolumeServer) VolumeEcShardsDelete(ctx context.Context, req *volume_se
if err := vs.checkGrpcAdminAuth(ctx); err != nil {
return nil, err
}
if err := checkVolumeCollection(req.Collection); err != nil {
return nil, err
}
bName := erasure_coding.EcShardBaseFileName(req.Collection, int(req.VolumeId))
@@ -1249,6 +1258,9 @@ func isEcDataShardFile(fileName, baseName string) bool {
}
func (vs *VolumeServer) VolumeEcShardsMount(ctx context.Context, req *volume_server_pb.VolumeEcShardsMountRequest) (*volume_server_pb.VolumeEcShardsMountResponse, error) {
if err := checkVolumeCollection(req.Collection); err != nil {
return nil, err
}
glog.V(0).Infof("VolumeEcShardsMount: %v", req)
@@ -1467,6 +1479,9 @@ func (vs *VolumeServer) VolumeEcShardsToVolume(ctx context.Context, req *volume_
if err := vs.checkGrpcAdminAuth(ctx); err != nil {
return nil, err
}
if err := checkVolumeCollection(req.Collection); err != nil {
return nil, err
}
if err := vs.CheckMaintenanceMode(); err != nil {
return nil, err
}