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
+29
View File
@@ -15,10 +15,13 @@
package service
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"regexp"
@@ -39,6 +42,32 @@ import (
"github.com/livekit/protocol/logger"
)
var (
ErrGzipReadFailed = errors.New("cannot read decompressed data")
ErrGzipTooLarge = errors.New("decompressed data too large")
)
var gzipReaderPool = sync.Pool{
New: func() any { return &gzip.Reader{} },
}
func DecompressGzip(compressed []byte) ([]byte, error) {
reader := gzipReaderPool.Get().(*gzip.Reader)
defer gzipReaderPool.Put(reader)
if err := reader.Reset(bytes.NewReader(compressed)); err != nil {
return nil, fmt.Errorf("%w: %w", ErrGzipReadFailed, err)
}
out, err := io.ReadAll(io.LimitReader(reader, http.DefaultMaxHeaderBytes+1))
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrGzipReadFailed, err)
}
if len(out) > http.DefaultMaxHeaderBytes {
return nil, ErrGzipTooLarge
}
return out, nil
}
func handleError(w http.ResponseWriter, r *http.Request, status int, err error, keysAndValues ...any) {
keysAndValues = append(keysAndValues, "status", status)
if r != nil && r.URL != nil {