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>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-11-28 04:40:50 -08:00
1 parent efcee4a186
commit 007c4efe97
4 files changed
+85

No files matched your search

+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,61 @@
package weed_server
import (
"context"
"strings"
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
)
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)
}
}
}
}
+3
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
}
+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
}