Repository navigation
Cap JSON request bodies - #305
Conversation
decode stops at 32 MiB. ReadTimeout limits header and body arrival without cutting a handler that is already running.
BaaRai
left a comment
There was a problem hiding this comment.
Thanks for capping request bodies by default, that is the right direction.
1. The cap only covers callers of decode. decode (server/server_mgmt.go:87-90) now wraps the body, but around thirty non-test call sites still use json.NewDecoder(r.Body) directly, and most of them have no MaxBytesReader in front. Examples:
- server/server.go:1172 (
setActive) - server/goals_api.go:50 (
addGoal) - server/constraints_api.go:65 (
addConstraint) - server/notify_api.go:190 (
notifyCreateChannel) - server/customtool.go:166 (
pgTestCustomTool) - server/task_credentials.go:67 (
addTaskCredential) - server/server_mgmt.go:380 (
pgSaveAgentConfig)
Fix: route these handlers through decode, or apply a default body limit in middleware for non-multipart requests.
2. Use the real ResponseWriter. decode calls http.MaxBytesReader(nil, ...) (server/server_mgmt.go:88), so the server is not told the request was too large, and the call sites answer with writeErr(w, 400, err.Error()) (server/server_mgmt.go:261-263, 301-303): 400 instead of 413. Please pass w into decode and map *http.MaxBytesError to 413. server/chatupload.go:88-92 and server/assets.go:21-29 already do this and can serve as the model.
Handlers that decode JSON directly were still unbounded, and a body past the cap was reported as a bad request.
BaaRai
left a comment
There was a problem hiding this comment.
Thanks, this covers the review: the middleware caps every non-multipart body with the real ResponseWriter, the 400 is answered as 413 once the cap was hit, decode keeps the direct-call path, and multipart uploads are untouched. The new tests pin the cap, the 413 rewrite and the multipart exemption.
…orks A handler that installs its own MaxBytesReader and then calls decode lost the 413: decode converted the error to a plain one and the middleware's limitedBody had been replaced. Return the original *http.MaxBytesError and update the cap test to errors.As.
|
Merged as f8bc3b3. Thanks for the rework: the middleware, the 413 mapping and the multipart exemption are right. I pushed one commit that returns the original *http.MaxBytesError from decode, so a handler that installs its own smaller limit still answers 413 instead of losing the error type. |
Summary
decodestops at 32 MiB. A handler that already set a smallerMaxBytesReaderkeeps that smaller cap.ReadTimeoutbounds how long headers and the body may take to arrive. It does not limit a running handler, so an event stream stays open.Test plan
TestDecodeAcceptsSmallBodyandTestDecodeCapsBody.