Files
podinfo/pkg/api/http/body_limit_test.go
Stefan Prodan 5037beda6c Cap HTTP request and websocket message sizes
Unauthenticated clients could exhaust process memory (and, for /store,
disk) by posting arbitrarily large bodies, and could OOM or pin
goroutines via the /ws/echo websocket.

- Wrap request bodies in http.MaxBytesReader (10 MiB) on the echo,
  store, cache and token handlers via a shared readLimitedBody helper
- Bound /ws/echo: per-message read limit, idle read deadline with
  ping/pong keepalive, and write deadlines
- Add regression tests for both limits
2026-06-08 23:58:43 +03:00

43 lines
1.4 KiB
Go

package http
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
// TestRequestBodySizeLimit verifies body-reading handlers reject payloads larger
// than maxRequestBodySize instead of buffering them into memory. The echo
// handler is used because with no backends configured it simply reflects the
// body and needs no external dependencies.
func TestRequestBodySizeLimit(t *testing.T) {
srv := NewMockServer()
srv.router.HandleFunc("/echo", srv.echoHandler)
// A body within the limit is accepted (202).
within := httptest.NewRequest("POST", "/echo", bytes.NewReader(make([]byte, 1024)))
rr := httptest.NewRecorder()
srv.router.ServeHTTP(rr, within)
if rr.Code != http.StatusAccepted {
t.Errorf("within-limit body: got status %d want %d", rr.Code, http.StatusAccepted)
}
// A body over the limit is rejected with a 413 code in the response body.
over := httptest.NewRequest("POST", "/echo", bytes.NewReader(make([]byte, maxRequestBodySize+1)))
rr = httptest.NewRecorder()
srv.router.ServeHTTP(rr, over)
var resp struct {
Code int `json:"code"`
Message string `json:"message"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("oversize body: response is not the expected error JSON: %v (body=%q)", err, rr.Body.String())
}
if resp.Code != http.StatusRequestEntityTooLarge {
t.Errorf("oversize body: got code %d want %d", resp.Code, http.StatusRequestEntityTooLarge)
}
}