Don't read uploaded files during request validation - #494
Merged
Conversation
ahx
marked this pull request as ready for review
August 12, 2026 10:24
ahx
force-pushed
the
dont-load-binary-file-uploads
branch
4 times, most recently
from
August 12, 2026 13:12
cdd08ac to
e4dac66
Compare
Multipart parts that are sent as a file were read into memory in full before the request body was validated, so a single large upload to any documented multipart/form-data route could exhaust the memory of the server process. Request validation runs before the application, so this did not require authentication. Such a part is now passed through as Rack parsed it, and validated as an empty binary String, because the content of a file is accepted as is. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
ahx
force-pushed
the
dont-load-binary-file-uploads
branch
from
August 12, 2026 13:33
b4d7c52 to
b31e094
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every
multipart/form-datapart that arrived as a file was read into memory in full before the request body was validated. Request validation runs before any application or authentication code, so one large upload to any documented multipart route could grow the process heap proportionally to the upload and get it OOM-killed. Reading the file bought nothing: content of aformat: binaryfield is accepted as is.Such a part is now passed through as Rack parsed it (
{ filename:, type:, name:, tempfile:, head: }) — the same shape Sinatra and Hanami hand to an app — and it is validated as an empty binary String, sotype: string,format: binaryandrequiredkeep working.Breaking:
parsed_body['file']is the part instead of the file content. Useparsed_body['file'][:tempfile]to read or stream it.Known limitations, all in the CHANGELOG:
minLength,maxLength,pattern) on a field sent as a file are no longer validated.after_request_body_property_validationhook sees the empty placeholder for such a field.contentTypein theencodingmap, are still read in full. Bounding those needs a size limit and is not part of this PR.Measured through the middleware: a 300 MB upload grew RSS by 375 MB before, and by 63 MB after. A 900 MB upload also grows it by 63 MB, i.e. memory no longer scales with the upload — the rest is Rack's own buffering.
🤖 Generated with Claude Code