feat(config): add maxRequestBytes for the unified body cap

The unified chat and embeddings routes reject bodies above 4 MiB. Long
agent sessions exceed this limit and receive a 413.

Add Config.maxRequestBytes. Zero or a negative value keeps the 4 MiB
default. A positive value raises the cap, clamped to 64 MiB.

An adversarial body decodes to about 12x live heap. An operator who raises
the cap must also set maxInFlightBodyRequests and the pod memory limit.
This commit is contained in:
2026-10-16 00:34:14 +01:00
parent 4ecfa10532
commit a328a8687e
4 changed files with 96 additions and 1 deletions
+5 -1
View File
@@ -74,6 +74,10 @@ const maxRequestBytes = 10 << 20
// package ships.
const maxUnifiedRequestBytes = 4 << 20
// maxExplicitUnifiedRequestBytes is the ceiling for Config.MaxRequestBytes,
// so an explicit value cannot disable the bound.
const maxExplicitUnifiedRequestBytes = 64 << 20
// gatewayAliasKey is the request map's internal-convention key carrying
// the client-requested model id through to an adapter (ruling a, ALIAS
// ECHO), distinct from "model" itself, which runUnified rewrites to the
@@ -1069,7 +1073,7 @@ func (g *Gateway) readAndDecodeUnifiedBody(sw *statusTrackingWriter, r *http.Req
return nil, nil, false
}
body, oversize, err := readCapped(r.Body, maxUnifiedRequestBytes)
body, oversize, err := readCapped(r.Body, g.unifiedRequestBytes)
if err != nil {
envelope(sw, http.StatusBadRequest, "invalid_request_error", "cannot read request body")
return nil, nil, false