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
+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)
}