fix: limit join request and WHIP request body to http.DefaultMaxHeaderBytes (#4450)

* fix: CS-1665

* cleanup

* cleanup and testes

* updates
This commit is contained in:
Anunay Maheshwari
2026-04-16 01:12:33 +05:30
committed by GitHub
parent 3cfb71e7ca
commit 1d804737f9
5 changed files with 111 additions and 16 deletions
+16 -11
View File
@@ -15,14 +15,11 @@
package service
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"math/rand"
"net/http"
@@ -128,9 +125,7 @@ func decodeAttributes(str string) (map[string]string, error) {
return attrs, nil
}
var gzipReaderPool = sync.Pool{
New: func() any { return &gzip.Reader{} },
}
var errJoinRequestTooLarge = errors.New("join request too large")
func (s *RTCService) validateInternal(
lgr logger.Logger,
@@ -138,6 +133,10 @@ func (s *RTCService) validateInternal(
needsJoinRequest bool,
strict bool,
) (livekit.RoomName, routing.ParticipantInit, int, error) {
if claims := GetGrants(r.Context()); claims == nil || claims.Video == nil {
return "", routing.ParticipantInit{}, http.StatusUnauthorized, rtc.ErrPermissionDenied
}
var params ValidateConnectRequestParams
useSinglePeerConnection := false
joinRequest := &livekit.JoinRequest{}
@@ -174,17 +173,23 @@ func (s *RTCService) validateInternal(
switch wrappedJoinRequest.Compression {
case livekit.WrappedJoinRequest_NONE:
if len(wrappedJoinRequest.JoinRequest) > http.DefaultMaxHeaderBytes {
return "", routing.ParticipantInit{}, http.StatusBadRequest, errJoinRequestTooLarge
}
if err := proto.Unmarshal(wrappedJoinRequest.JoinRequest, joinRequest); err != nil {
return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot unmarshal join request")
}
case livekit.WrappedJoinRequest_GZIP:
reader := gzipReaderPool.Get().(*gzip.Reader)
defer gzipReaderPool.Put(reader)
reader.Reset(bytes.NewReader(wrappedJoinRequest.JoinRequest))
protoBytes, err := io.ReadAll(reader)
protoBytes, err := DecompressGzip(wrappedJoinRequest.JoinRequest)
if err != nil {
return "", routing.ParticipantInit{}, http.StatusBadRequest, errors.New("cannot read decompressed join request")
switch {
case errors.Is(err, ErrGzipTooLarge):
err = errJoinRequestTooLarge
case errors.Is(err, ErrGzipReadFailed):
err = errors.New("cannot read decompressed join request")
}
return "", routing.ParticipantInit{}, http.StatusBadRequest, err
}
if err := proto.Unmarshal(protoBytes, joinRequest); err != nil {