Skip to content

fix(multipart): respect MaxBodyBytes limit - #1094

Open
leonklingele wants to merge 3 commits into
danielgtaylor:mainfrom
leonklingele:fix/multipart-max-bodybytes
Open

fix(multipart): respect MaxBodyBytes limit#1094
leonklingele wants to merge 3 commits into
danielgtaylor:mainfrom
leonklingele:fix/multipart-max-bodybytes

Conversation

@leonklingele

Copy link
Copy Markdown
Contributor

(huma.Operation).MaxBodyBytes was only enforced for normal request bodies via the
readBody path, but multipart requests take a separate path throug readForm /
(huma.Context).GetMultipartForm() and did not consult op.MaxBodyBytes.

This change makes the MaxBodyBytes value be respected in all paths.

`(huma.Operation).MaxBodyBytes` was only enforced for normal request bodies via the
`readBody` path, but multipart requests take a separate path throug `readForm` /
`(huma.Context).GetMultipartForm()` and did not consult `op.MaxBodyBytes`.

This change makes the `MaxBodyBytes` value be respected in all paths.
Copilot AI lite review requested due to automatic review settings August 8, 2026 13:21
@codecov

codecov Bot commented Aug 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.04%. Comparing base (198225e) to head (b5450f0).

Files with missing lines Patch % Lines
huma.go 66.66% 5 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1094      +/-   ##
==========================================
- Coverage   93.20%   93.04%   -0.16%     
==========================================
  Files          23       23              
  Lines        4988     5003      +15     
==========================================
+ Hits         4649     4655       +6     
- Misses        272      278       +6     
- Partials       67       70       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends request body size limiting so that huma.Operation.MaxBodyBytes is enforced for multipart requests (which previously bypassed the readBody path), and adds test coverage to prevent regressions.

Changes:

  • Pass op.MaxBodyBytes into the multipart parsing path (readForm) so multipart uploads are subject to the same limit.
  • Implement multipart parsing via a limited reader when MaxBodyBytes > 0 to detect oversize bodies.
  • Add a unit test ensuring oversized multipart requests return HTTP 413 and do not invoke the handler.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
huma.go Routes multipart parsing through a new readForm(ctx, maxBytes) implementation to enforce MaxBodyBytes.
huma_test.go Adds a test verifying multipart requests exceeding MaxBodyBytes return 413 and do not call the handler.
Suppressed comments (2)

huma.go:1029

  • When readForm returns a StatusError (e.g. 413 for MaxBodyBytes), this path appends it to res.Errors and later wraps it with WriteErr(..., "validation failed", ...), so clients get a 413 with an unrelated top-level detail message. Consider short-circuiting and writing StatusError responses directly here (similar to the handler error path).

This issue also appears on line 2075 of the same file.

				if err != nil {
					res.Errors = append(res.Errors, err)
				} else {

huma.go:2077

  • On multipart parse errors (ReadForm returning a non-nil form + err), the current code returns the form without cleaning up any temporary files created during parsing, and the caller ignores form on error. This can leak temp files on malformed/aborted uploads.
	if err != nil {
		return form, &ErrorDetail{Location: "body", Message: "cannot read multipart form: " + err.Error()}
	}

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread huma.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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