mirror of
https://github.com/livekit/livekit.git
synced 2026-12-05 06:18:09 +00:00
198 lines
7.5 KiB
Go
198 lines
7.5 KiB
Go
// Copyright 2024 LiveKit, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/livekit/livekit-server/pkg/config"
|
|
"github.com/livekit/protocol/auth"
|
|
"github.com/livekit/protocol/livekit"
|
|
"github.com/livekit/protocol/logger"
|
|
)
|
|
|
|
// stubRoomAllocator is a RoomAllocator whose ValidateCreateRoom always succeeds. The embedded
|
|
// interface is nil, so any other method panics, which keeps the tests honest about what the
|
|
// join path actually calls.
|
|
type stubRoomAllocator struct{ RoomAllocator }
|
|
|
|
func (stubRoomAllocator) ValidateCreateRoom(context.Context, livekit.RoomName) error { return nil }
|
|
|
|
func grantRequest(grant *auth.ClaimGrants) *http.Request {
|
|
r := httptest.NewRequest(http.MethodPost, "/", nil)
|
|
return r.WithContext(WithGrants(r.Context(), grant, "apikey"))
|
|
}
|
|
|
|
// ConnectLimitCase is a join token that is over one limit. Every join path must reject it.
|
|
// It is exported so that TestStartSessionConnectLimits in package service_test runs the same cases.
|
|
type ConnectLimitCase struct {
|
|
Name string
|
|
Limit config.LimitConfig
|
|
Grant func() *auth.ClaimGrants // fresh grant per call, since a join path may change it
|
|
Err error
|
|
}
|
|
|
|
func ConnectLimitCases() []ConnectLimitCase {
|
|
return []ConnectLimitCase{
|
|
{
|
|
Name: "identity exceeds",
|
|
Limit: config.LimitConfig{MaxParticipantIdentityLength: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) { g.Identity = "toolongidentity" }),
|
|
Err: ErrParticipantIdentityExceedsLimits,
|
|
},
|
|
{
|
|
Name: "room name exceeds",
|
|
Limit: config.LimitConfig{MaxRoomNameLength: 3},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) { g.Video.Room = "toolongroom" }),
|
|
Err: ErrRoomNameExceedsLimits,
|
|
},
|
|
{
|
|
Name: "name exceeds",
|
|
Limit: config.LimitConfig{MaxParticipantNameLength: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) { g.Name = "toolongname" }),
|
|
Err: ErrNameExceedsLimits,
|
|
},
|
|
{
|
|
Name: "metadata exceeds",
|
|
Limit: config.LimitConfig{MaxMetadataSize: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) { g.Metadata = "abcdefg" }),
|
|
Err: ErrMetadataExceedsLimits,
|
|
},
|
|
{
|
|
Name: "attributes exceeds",
|
|
Limit: config.LimitConfig{MaxAttributesSize: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) { g.Attributes = map[string]string{"k": "abcdefg"} }),
|
|
Err: ErrAttributeExceedsLimits,
|
|
},
|
|
{
|
|
Name: "room config metadata exceeds",
|
|
Limit: config.LimitConfig{MaxMetadataSize: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) { g.RoomConfig = &auth.RoomConfiguration{Metadata: "abcdefg"} }),
|
|
Err: ErrMetadataExceedsLimits,
|
|
},
|
|
{
|
|
Name: "room config agent metadata exceeds",
|
|
Limit: config.LimitConfig{MaxMetadataSize: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) {
|
|
g.RoomConfig = &auth.RoomConfiguration{Agents: []*livekit.RoomAgentDispatch{{Metadata: "abcdefg"}}}
|
|
}),
|
|
Err: ErrMetadataExceedsLimits,
|
|
},
|
|
{
|
|
Name: "room config agent attributes exceeds",
|
|
Limit: config.LimitConfig{MaxAttributesSize: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) {
|
|
g.RoomConfig = &auth.RoomConfiguration{Agents: []*livekit.RoomAgentDispatch{{Attributes: map[string]string{"k": "abcdefg"}}}}
|
|
}),
|
|
Err: ErrAttributeExceedsLimits,
|
|
},
|
|
{
|
|
Name: "room config agent lk attributes exceeds",
|
|
Limit: config.LimitConfig{MaxLKAttributesSize: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) {
|
|
g.RoomConfig = &auth.RoomConfiguration{Agents: []*livekit.RoomAgentDispatch{{Attributes: map[string]string{"lk.k": "abcdefg"}}}}
|
|
}),
|
|
Err: ErrAttributeExceedsLimits,
|
|
},
|
|
{
|
|
Name: "room config agent invalid thread id",
|
|
Grant: grantWith(func(g *auth.ClaimGrants) {
|
|
g.RoomConfig = &auth.RoomConfiguration{Agents: []*livekit.RoomAgentDispatch{{ThreadId: "not-a-thread-id"}}}
|
|
}),
|
|
Err: ErrInvalidAgentThreadID,
|
|
},
|
|
{
|
|
Name: "lk attributes exceeds",
|
|
Limit: config.LimitConfig{MaxAttributesSize: 100, MaxLKAttributesSize: 5},
|
|
Grant: grantWith(func(g *auth.ClaimGrants) { g.Attributes = map[string]string{"lk.k": "abcdefg"} }),
|
|
Err: ErrAttributeExceedsLimits,
|
|
},
|
|
}
|
|
}
|
|
|
|
func baseGrant() *auth.ClaimGrants {
|
|
return &auth.ClaimGrants{
|
|
Identity: "id",
|
|
Name: "name",
|
|
Video: &auth.VideoGrant{RoomJoin: true, Room: "room"},
|
|
}
|
|
}
|
|
|
|
func grantWith(mutate func(*auth.ClaimGrants)) func() *auth.ClaimGrants {
|
|
return func() *auth.ClaimGrants {
|
|
g := baseGrant()
|
|
mutate(g)
|
|
return g
|
|
}
|
|
}
|
|
|
|
// TestConnectLimits checks that both HTTP join paths (WHIP ingest and the signal connection via
|
|
// ValidateConnectRequest) reject every ConnectLimitCase.
|
|
func TestConnectLimits(t *testing.T) {
|
|
for _, tc := range ConnectLimitCases() {
|
|
t.Run("WHIP/"+tc.Name, func(t *testing.T) {
|
|
svc := &WHIPService{config: &config.Config{Limit: tc.Limit}, roomAllocator: stubRoomAllocator{}}
|
|
_, code, err := svc.validateCreate(httptest.NewRecorder(), grantRequest(tc.Grant()))
|
|
require.ErrorIs(t, err, tc.Err)
|
|
require.Equal(t, http.StatusBadRequest, code)
|
|
})
|
|
|
|
t.Run("signal/"+tc.Name, func(t *testing.T) {
|
|
// nil router skips the node-limit check, reaching the grant checks
|
|
_, code, err := ValidateConnectRequest(logger.GetLogger(), grantRequest(tc.Grant()), tc.Limit, ValidateConnectRequestParams{}, nil, stubRoomAllocator{})
|
|
require.ErrorIs(t, err, tc.Err)
|
|
require.Equal(t, http.StatusBadRequest, code)
|
|
})
|
|
}
|
|
|
|
t.Run("signal/identity exceeds after publish suffix", func(t *testing.T) {
|
|
g := baseGrant()
|
|
g.Video.SetCanPublish(true)
|
|
// "id" fits a limit of 5, but the final identity "id#abcd" does not
|
|
params := ValidateConnectRequestParams{publish: "abcd"}
|
|
_, code, err := ValidateConnectRequest(logger.GetLogger(), grantRequest(g), config.LimitConfig{MaxParticipantIdentityLength: 5}, params, nil, stubRoomAllocator{})
|
|
require.ErrorIs(t, err, ErrParticipantIdentityExceedsLimits)
|
|
require.Equal(t, http.StatusBadRequest, code)
|
|
})
|
|
|
|
t.Run("within limits", func(t *testing.T) {
|
|
limit := config.LimitConfig{MaxParticipantIdentityLength: 5, MaxRoomNameLength: 10, MaxParticipantNameLength: 5, MaxMetadataSize: 5, MaxAttributesSize: 5}
|
|
_, code, err := ValidateConnectRequest(logger.GetLogger(), grantRequest(baseGrant()), limit, ValidateConnectRequestParams{}, nil, stubRoomAllocator{})
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, code)
|
|
})
|
|
|
|
t.Run("room config with a nil agent", func(t *testing.T) {
|
|
g := baseGrant()
|
|
g.RoomConfig = &auth.RoomConfiguration{Agents: []*livekit.RoomAgentDispatch{nil}}
|
|
limit := config.LimitConfig{MaxMetadataSize: 5, MaxAttributesSize: 5}
|
|
require.NoError(t, CheckConnectLimits(logger.GetLogger(), limit, g.Identity, livekit.RoomName(g.Video.Room), g))
|
|
})
|
|
|
|
t.Run("lk attributes do not count against attributes limit", func(t *testing.T) {
|
|
g := baseGrant()
|
|
g.Attributes = map[string]string{"k": "abcd", "lk.k": "abcdefg"}
|
|
limit := config.LimitConfig{MaxAttributesSize: 5, MaxLKAttributesSize: 11}
|
|
_, code, err := ValidateConnectRequest(logger.GetLogger(), grantRequest(g), limit, ValidateConnectRequestParams{}, nil, stubRoomAllocator{})
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, code)
|
|
})
|
|
}
|