Skip to content

Cap JSON request bodies - #305

Merged
BaaRai merged 3 commits into
BaaRai:mainfrom
Protonmatter:protonmatter/json-body-cap
Nov 17, 2026
Merged

BaaRai merged 3 commits into
BaaRai:mainfrom
Protonmatter:protonmatter/json-body-cap

Conversation

@Protonmatter

Copy link
Copy Markdown
Contributor

Summary

Test plan

  • TestDecodeAcceptsSmallBody and TestDecodeCapsBody.

decode stops at 32 MiB. ReadTimeout limits header and body arrival without cutting a handler that is already running.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@BaaRai
BaaRai merged commit f8bc3b3 into BaaRai:main Nov 17, 2026
5 checks passed
@BaaRai

BaaRai commented Nov 17, 2026

Copy link
Copy Markdown
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants