Skip to content

mime/multipart: add field Reader.MaxMIMEHeaderSize #73087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
starius opened this issue Mar 28, 2025 · 2 comments · May be fixed by #70933
Open

mime/multipart: add field Reader.MaxMIMEHeaderSize #73087

starius opened this issue Mar 28, 2025 · 2 comments · May be fixed by #70933
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal

Comments

@starius
Copy link
Contributor

starius commented Mar 28, 2025

Proposal Details

Please provide a way to limit the maximum size of the header when using http.ParseMultipartForm.

Currently package mime/multipart has a hardcoded constant maxMIMEHeaderSize equal to 10 megabytes. This means that an app having multipart HTTP endpoints should allocate at least 10 megabytes of RAM per request, which is quite high. Most headers are much smaller and allowing 10 megabytes for the header is not useful, just exposes an app to memory exhaustion by many parallel slow requests with large MIME headers.

I propose to add a public field MaxMIMEHeaderSize to multipart.Reader.
Methods NextPart() and NextRawPart() should use the value of that field if it is not 0, otherwise they should use the constant.

To use this new field, one would call request.MultipartReader() method in http package, set the field to the desired value (e.g. 1024) and then parse the form using reader.NextPart() method without the risk that multipart headers eat 10 megabytes of RAM.

Fixes #26339

Alternatives considered

There is an alternative proposal #68889 to add maximum size and number of parts in ParseMultipartForm. The proposals don't contradict each other. Request.ParseMultipartFormLimited can use field MaxMIMEHeaderSize of the reader.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/637915 mentions this issue: mime/multipart: add field Reader.MaxMIMEHeaderSize

@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants