fix: preserve newlines in multipart form data decoding#8144
Open
Krishnachaitanyakc wants to merge 3 commits intomitmproxy:mainfrom
Open
fix: preserve newlines in multipart form data decoding#8144Krishnachaitanyakc wants to merge 3 commits intomitmproxy:mainfrom
Krishnachaitanyakc wants to merge 3 commits intomitmproxy:mainfrom
Conversation
The previous `decode_multipart` implementation used `splitlines()` to parse each part and then joined the body lines with `b""`, which silently stripped all newline characters (`\n`, `\r\n`) from field values. This corrupted any multipart payload whose values contained embedded newlines — including plain text with line breaks and binary file uploads (e.g. JPEG images whose raw bytes happen to include 0x0A / 0x0D). The fix locates the header/body separator (`\r\n\r\n` or `\n\n`) with `bytes.find` and keeps the body as a single contiguous `bytes` object. Only the trailing CRLF (or LF) that RFC 2046 defines as part of the boundary delimiter is stripped; all other newlines inside the body are preserved. Tests are updated to use RFC-compliant CRLF delimiters and new cases are added for embedded newlines, bare-LF newlines, binary content, and encode/decode round-tripping. Fixes mitmproxy#4466
The encoder appended an extra empty-bytes element after each value, producing `value\r\n\r\n` before the next boundary instead of the RFC 2046 compliant `value\r\n`. This caused decode_multipart (which correctly strips only one trailing CRLF) to return values with a spurious `\r\n` suffix, breaking the test_set_multipart_form round-trip test in test_http.py. Remove the extra `hdrs.append(b"")` from the encoder and update the encode/round-trip tests to match the corrected output format.
Author
|
@mhils can you please review this? |
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
Fixes #4466 — multipart form data values containing newlines (
\nor\r\n) were silently stripped during decoding, corrupting text fields with line breaks and binary file uploads (e.g. JPEG images).decode_multipartusedbytes.splitlines()to parse each part and then joined body lines withb"", which discarded all newline characters from the body content.splitlines()+ join approach withbytes.find()to locate the header/body separator (\r\n\r\nor\n\n), keeping the body as a single contiguousbytesobject. Only the trailing CRLF/LF that RFC 2046 defines as part of the boundary delimiter is stripped.test_decodeto use RFC-compliant CRLF delimiters and added new test cases for:\r\nand\nbytesTest plan
test_decodepasses with CRLF-delimited payloadstest_decode_with_lfpasses for bare-LF payloadstest_decode_content_preserves_newlinescatches the original bug (valuea\r\nbnot flattened toab)test_decode_binary_contentensures binary data with embedded\r\n/\nbytes is preservedtest_decode_roundtripconfirms encode then decode recovers field namestest_encodestill passes unchanged