add size limit to metadata of room & participant (#566)

* add size limit to metadata of room & participant

* description in config-sample

* solve comments
This commit is contained in:
cnderrauber
2022-03-25 12:48:11 +08:00
committed by GitHub
parent 61655c6ff0
commit 3352ee6b79
7 changed files with 101 additions and 15 deletions
+2
View File
@@ -113,6 +113,8 @@ keys:
# # allow tracks to be unmuted remotely, defaults to false
# # tracks can always be muted from the Room Service APIs
# enable_remote_unmute: true
# # limit size of room and participant's metadata, 0 for no limit
# max_metadata_size: 0
# Webhooks
# when configured, LiveKit notifies your URL handler with room events
+1
View File
@@ -132,6 +132,7 @@ type RoomConfig struct {
MaxParticipants uint32 `yaml:"max_participants"`
EmptyTimeout uint32 `yaml:"empty_timeout"`
EnableRemoteUnmute bool `yaml:"enable_remote_unmute"`
MaxMetadataSize uint32 `yaml:"max_metadata_size"`
}
type CodecSpec struct {
+10 -9
View File
@@ -3,13 +3,14 @@ package service
import "errors"
var (
ErrEgressNotFound = errors.New("egress does not exist")
ErrEgressNotConnected = errors.New("egress not connected (redis required)")
ErrRoomNotFound = errors.New("requested room does not exist")
ErrRoomLockFailed = errors.New("could not lock room")
ErrRoomUnlockFailed = errors.New("could not unlock room, lock token does not match")
ErrParticipantNotFound = errors.New("participant does not exist")
ErrTrackNotFound = errors.New("track is not found")
ErrWebHookMissingAPIKey = errors.New("api_key is required to use webhooks")
ErrOperationFailed = errors.New("operation cannot be completed")
ErrEgressNotFound = errors.New("egress does not exist")
ErrEgressNotConnected = errors.New("egress not connected (redis required)")
ErrRoomNotFound = errors.New("requested room does not exist")
ErrRoomLockFailed = errors.New("could not lock room")
ErrRoomUnlockFailed = errors.New("could not unlock room, lock token does not match")
ErrParticipantNotFound = errors.New("participant does not exist")
ErrTrackNotFound = errors.New("track is not found")
ErrWebHookMissingAPIKey = errors.New("api_key is required to use webhooks")
ErrOperationFailed = errors.New("operation cannot be completed")
ErrMetadataExceedsLimits = errors.New("metadata size exceeds limits")
)
+13 -1
View File
@@ -2,6 +2,7 @@ package service
import (
"context"
"strconv"
"time"
"github.com/pkg/errors"
@@ -11,6 +12,7 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
)
@@ -24,13 +26,15 @@ type RoomService struct {
router routing.MessageRouter
roomAllocator RoomAllocator
roomStore ServiceStore
conf config.RoomConfig
}
func NewRoomService(ra RoomAllocator, rs ServiceStore, router routing.MessageRouter) (svc *RoomService, err error) {
func NewRoomService(ra RoomAllocator, rs ServiceStore, router routing.MessageRouter, conf config.RoomConfig) (svc *RoomService, err error) {
svc = &RoomService{
router: router,
roomAllocator: ra,
roomStore: rs,
conf: conf,
}
return
}
@@ -216,6 +220,10 @@ func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteR
}
func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.UpdateParticipantRequest) (*livekit.ParticipantInfo, error) {
if s.conf.MaxMetadataSize > 0 && len(req.Metadata) > int(s.conf.MaxMetadataSize) {
return nil, twirp.InvalidArgumentError(ErrMetadataExceedsLimits.Error(), strconv.Itoa(int(s.conf.MaxMetadataSize)))
}
err := s.writeParticipantMessage(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity), &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_UpdateParticipant{
UpdateParticipant: req,
@@ -278,6 +286,10 @@ func (s *RoomService) SendData(ctx context.Context, req *livekit.SendDataRequest
}
func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.UpdateRoomMetadataRequest) (*livekit.Room, error) {
if s.conf.MaxMetadataSize > 0 && len(req.Metadata) > int(s.conf.MaxMetadataSize) {
return nil, twirp.InvalidArgumentError(ErrMetadataExceedsLimits.Error(), strconv.Itoa(int(s.conf.MaxMetadataSize)))
}
if err := EnsureAdminPermission(ctx, livekit.RoomName(req.Room)); err != nil {
return nil, twirpAuthError(err)
}
+64 -4
View File
@@ -7,7 +7,9 @@ import (
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/stretchr/testify/require"
"github.com/twitchtv/twirp"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing/routingfakes"
"github.com/livekit/livekit-server/pkg/service"
"github.com/livekit/livekit-server/pkg/service/servicefakes"
@@ -15,7 +17,7 @@ import (
func TestDeleteRoom(t *testing.T) {
t.Run("normal deletion", func(t *testing.T) {
svc := newTestRoomService()
svc := newTestRoomService(config.RoomConfig{})
grant := &auth.ClaimGrants{
Video: &auth.VideoGrant{
RoomCreate: true,
@@ -30,7 +32,7 @@ func TestDeleteRoom(t *testing.T) {
})
t.Run("missing permissions", func(t *testing.T) {
svc := newTestRoomService()
svc := newTestRoomService(config.RoomConfig{})
grant := &auth.ClaimGrants{
Video: &auth.VideoGrant{},
}
@@ -42,11 +44,69 @@ func TestDeleteRoom(t *testing.T) {
})
}
func newTestRoomService() *TestRoomService {
func TestMetaDataLimits(t *testing.T) {
t.Run("metadata exceed limits", func(t *testing.T) {
svc := newTestRoomService(config.RoomConfig{MaxMetadataSize: 5})
grant := &auth.ClaimGrants{
Video: &auth.VideoGrant{},
}
ctx := service.WithGrants(context.Background(), grant)
_, err := svc.UpdateParticipant(ctx, &livekit.UpdateParticipantRequest{
Room: "testroom",
Identity: "123",
Metadata: "abcdefg",
})
terr, ok := err.(twirp.Error)
require.True(t, ok)
require.Equal(t, twirp.InvalidArgument, terr.Code())
_, err = svc.UpdateRoomMetadata(ctx, &livekit.UpdateRoomMetadataRequest{
Room: "testroom",
Metadata: "abcdefg",
})
terr, ok = err.(twirp.Error)
require.True(t, ok)
require.Equal(t, twirp.InvalidArgument, terr.Code())
})
notExceedsLimitsSvc := map[string]*TestRoomService{
"metadata noe exceeds limits": newTestRoomService(config.RoomConfig{MaxMetadataSize: 5}),
"metadata no limits": newTestRoomService(config.RoomConfig{}), // no limits
}
for n, s := range notExceedsLimitsSvc {
svc := s
t.Run(n, func(t *testing.T) {
grant := &auth.ClaimGrants{
Video: &auth.VideoGrant{},
}
ctx := service.WithGrants(context.Background(), grant)
_, err := svc.UpdateParticipant(ctx, &livekit.UpdateParticipantRequest{
Room: "testroom",
Identity: "123",
Metadata: "abc",
})
terr, ok := err.(twirp.Error)
require.True(t, ok)
require.NotEqual(t, twirp.InvalidArgument, terr.Code())
_, err = svc.UpdateRoomMetadata(ctx, &livekit.UpdateRoomMetadataRequest{
Room: "testroom",
Metadata: "abc",
})
terr, ok = err.(twirp.Error)
require.True(t, ok)
require.NotEqual(t, twirp.InvalidArgument, terr.Code())
})
}
}
func newTestRoomService(conf config.RoomConfig) *TestRoomService {
router := &routingfakes.FakeRouter{}
allocator := &servicefakes.FakeRoomAllocator{}
store := &servicefakes.FakeServiceStore{}
svc, err := service.NewRoomService(allocator, store, router)
svc, err := service.NewRoomService(allocator, store, router, conf)
if err != nil {
panic(err)
}
+5
View File
@@ -36,6 +36,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
createWebhookNotifier,
createClientConfiguration,
routing.CreateRouter,
getRoomConf,
wire.Bind(new(routing.MessageRouter), new(routing.Router)),
wire.Bind(new(livekit.RoomService), new(*RoomService)),
telemetry.NewAnalyticsService,
@@ -153,3 +154,7 @@ func createStore(rc *redis.Client) ObjectStore {
func createClientConfiguration() clientconfiguration.ClientConfigurationManager {
return clientconfiguration.NewStaticClientConfigurationManager(clientconfiguration.StaticConfigurations)
}
func getRoomConf(config *config.Config) config.RoomConfig {
return config.Room
}
+6 -1
View File
@@ -37,7 +37,8 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
if err != nil {
return nil, err
}
roomService, err := NewRoomService(roomAllocator, objectStore, router)
roomConfig := getRoomConf(conf)
roomService, err := NewRoomService(roomAllocator, objectStore, router, roomConfig)
if err != nil {
return nil, err
}
@@ -173,3 +174,7 @@ func createStore(rc *redis.Client) ObjectStore {
func createClientConfiguration() clientconfiguration.ClientConfigurationManager {
return clientconfiguration.NewStaticClientConfigurationManager(clientconfiguration.StaticConfigurations)
}
func getRoomConf(config2 *config.Config) config.RoomConfig {
return config2.Room
}