Files
ziti/controller/env/context.go
T
Andrew Martinez 703f74ae28 backport fixes GHSA-q8g9-jc4c-jp6q to v2.0 limit pre-auth request body buffering
- caps buffered HTTP request bodies at 1 MiB across the client, management, fabric management, and OIDC web APIs
- rejects oversized bodies with 413 REQUEST_ENTITY_TOO_LARGE before authentication
- surfaces request body read errors instead of ignoring them
- returns after writing the request context error response in the client API handler
- adds integration coverage for oversized bodies with and without Content-Length
2026-08-19 17:46:02 -04:00

34 lines
958 B
Go

package env
import (
"bytes"
"io"
"net/http"
"github.com/openziti/ziti/v2/common/eid"
"github.com/openziti/ziti/v2/controller/api"
"github.com/openziti/ziti/v2/controller/response"
)
// NewRequestContext creates a bare request context for responses rendered outside the normal
// request pipeline. The body read is capped at api.MaxRequestBodySize; a body that exceeds the
// cap is truncated rather than rejected, since this context only renders error responses.
func NewRequestContext(rw http.ResponseWriter, r *http.Request) *response.RequestContext {
rid := eid.New()
r.Body = http.MaxBytesReader(rw, r.Body, api.MaxRequestBodySize)
body, _ := io.ReadAll(r.Body)
r.Body = io.NopCloser(bytes.NewReader(body))
requestContext := &response.RequestContext{
Id: rid,
ResponseWriter: rw,
Request: r,
Body: body,
}
requestContext.Responder = response.NewResponder(requestContext)
return requestContext
}