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:
@@ -15,7 +15,10 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -75,3 +78,52 @@ func TestIsValidDomain(t *testing.T) {
|
||||
require.Equal(t, service.IsValidDomain(key), result)
|
||||
}
|
||||
}
|
||||
|
||||
func compress(t *testing.T, payload []byte) []byte {
|
||||
t.Helper()
|
||||
var buf bytes.Buffer
|
||||
gw, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
|
||||
require.NoError(t, err)
|
||||
_, err = gw.Write(payload)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, gw.Close())
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestDecompressGzip(t *testing.T) {
|
||||
t.Run("small payload", func(t *testing.T) {
|
||||
out, err := service.DecompressGzip(compress(t, []byte("hello world")))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte("hello world"), out)
|
||||
})
|
||||
|
||||
t.Run("payload exactly at cap", func(t *testing.T) {
|
||||
raw := make([]byte, http.DefaultMaxHeaderBytes)
|
||||
out, err := service.DecompressGzip(compress(t, raw))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out, http.DefaultMaxHeaderBytes)
|
||||
})
|
||||
|
||||
t.Run("payload one byte over capd", func(t *testing.T) {
|
||||
raw := make([]byte, http.DefaultMaxHeaderBytes+1)
|
||||
_, err := service.DecompressGzip(compress(t, raw))
|
||||
require.ErrorIs(t, err, service.ErrGzipTooLarge)
|
||||
})
|
||||
|
||||
t.Run("gzip decompression bomb", func(t *testing.T) {
|
||||
// 100 MB of zeros
|
||||
raw := make([]byte, 100<<20)
|
||||
compressed := compress(t, raw)
|
||||
require.Less(t, len(compressed), 1<<20,
|
||||
"sanity: bomb input should compress dramatically")
|
||||
|
||||
_, err := service.DecompressGzip(compressed)
|
||||
require.ErrorIs(t, err, service.ErrGzipTooLarge)
|
||||
})
|
||||
|
||||
t.Run("malformed gzip compression", func(t *testing.T) {
|
||||
_, err := service.DecompressGzip([]byte("not gzip data"))
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "cannot read decompressed")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user