Validate multipart boundary length against the RFC 2046 limit - #210
Merged
Merged
Conversation
RFC 2046 section 5.1.1 limits a `multipart/form-data` boundary to at most 70 characters. `requiredBoundary()` currently returns the boundary from the `Content-Type` header without checking its length, so a boundary of any size is accepted and handed straight to the parser. Besides being out of spec, an over-long boundary makes the parser's boundary matching do more work than it should for no legitimate benefit — a well-formed request never needs a boundary longer than the limit. - Reject a boundary whose UTF-8 length exceeds 70 in `requiredBoundary()`, throwing a new `RuntimeError.multipartBoundaryTooLong`. Matching is done on UTF-8 bytes, consistent with how the parser consumes the boundary. - Map `multipartBoundaryTooLong` to a `400 Bad Request` response, alongside the other malformed-`Content-Type` errors. - Add a unit test covering the boundaries at the limit (70, accepted) and just over it (71, rejected). Requests with a spec-compliant boundary decode exactly as before. A boundary longer than 70 bytes is now rejected up front with a clear error (surfaced as `400 Bad Request` on the server path) instead of being passed to the parser.
AzeemJiva
approved these changes
Sep 10, 2026
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.
Motivation
RFC 2046 section 5.1.1 limits a
multipart/form-databoundary to at most70 characters.
requiredBoundary()currently returns the boundary fromthe
Content-Typeheader without checking its length, so a boundary ofany size is accepted and handed straight to the parser. Besides being out
of spec, an over-long boundary makes the parser's boundary matching do
more work than it should for no legitimate benefit — a well-formed request
never needs a boundary longer than the limit.
Modifications
requiredBoundary(),throwing a new
RuntimeError.multipartBoundaryTooLong. Matching is doneon UTF-8 bytes, consistent with how the parser consumes the boundary.
multipartBoundaryTooLongto a400 Bad Requestresponse, alongsidethe other malformed-
Content-Typeerrors.just over it (71, rejected).
Result
Requests with a spec-compliant boundary decode exactly as before. A
boundary longer than 70 bytes is now rejected up front with a clear error
(surfaced as
400 Bad Requeston the server path) instead of being passedto the parser.