Silently discard epilogue data after the closing boundary#259
Merged
Conversation
Django's MultiPartParser and Werkzeug's sansio multipart parser both silently accept (and ignore) the optional CRLF + epilogue that RFC 2046 section 5.1.1 allows after the closing boundary. python-multipart was logging "Skipping data after last boundary" instead, and the single- chunk guard added in #193 also missed the case where the trailing CRLF was split across two writes - a trailing `\r` at the end of one chunk would fall through to the warning branch because `i + 1 < length` was false, even though the `\n` arrived in the next chunk. Drop the warning and the split-chunk guard entirely. The epilogue is short-circuited the same way as before (no O(N) scan), just without the spurious log line. Closes #246.
Owner
Author
|
I ran a quick parser-only benchmark comparing this PR ( Payload shape: valid = (
b"--boundary\r\n"
b"Content-Disposition: form-data; name=\"f\"\r\n"
b"\r\nval\r\n"
b"--boundary--"
)
crlf_epilogue = valid + b"\r\n" * (size // 2)
non_crlf_epilogue = valid + b"X" * sizeResults:
So this makes CRLF epilogue handling take the same short-circuit path as other epilogue data. |
Kludex
commented
Apr 10, 2026
Kludex
commented
Apr 10, 2026
tests/test_multipart.py
Outdated
Comment on lines
+1383
to
+1385
| Covers both the single-chunk case and the case where the trailing | ||
| CRLF is split across `write()` calls (e.g. when `\r` and `\n` land in | ||
| separate TCP chunks behind a proxy). |
Owner
Author
There was a problem hiding this comment.
Can we use the 120 line limit?
f71d297 to
ece73d3
Compare
ece73d3 to
13cef88
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.
Summary
"Skipping data after last boundary"warning. RFC 2046 section 5.1.1 allows an optional CRLF + epilogue after the closing boundary, and both Django'sMultiPartParserand Werkzeug's sansio multipart parser accept it silently - python-multipart was the outlier."Skipping data after last boundary"warning when CRLF is split across chunks #246: the previous guard atmultipart.py:1417only paired\rwith\nwhen both landed in the samewrite()call, so a trailing\rat the end of one chunk followed by\nin the next (common behind network proxies) produced a spurious warning.i = length; break, preserving the O(1) behavior added in Hard break if found data after last boundary onMultipartParser#189 - no epilogue scanning, no DoS surface.Test plan
test_multipart_parser_epilogue_emits_no_warningscovers single-chunk CRLF, split CRLF across two writes, boundary-then-CRLF split, and non-empty epilogue - all asserting zero warnings.test_multipart_parser_data_after_last_boundary, 50M trailing bytes) still short-circuit.pytest --cov- 134 passed, 100% coverage.ruff check,ruff format --check,mypy --strictall clean.Closes #246.