Repository navigation
Fix federated media downloads failing when a multipart response is split across chunks - #20282
Merged
Merged
Conversation
_MultipartParserProtocol assigned each header and JSON callback value instead of accumulating it, so a value split across two dataReceived calls kept only its tail. Buffer header names, header values and the JSON part until the parser signals their end. Signed-off-by: Ankit Jha <ankit.jha@tradomate.one>
ankit373
requested review from
MadLittleMods
and removed request for
a team
September 28, 2026 10:28
Signed-off-by: Ankit Jha <ankit.jha@tradomate.one>
python-multipart 0.0.9, the oldest version we support, never calls on_header_begin, but the parser cleared its header buffers there. On that version the buffers were never reset, so from the second header onwards the field name became a concatenation such as "content-typecontent-type" and matched nothing. The content type, content disposition and redirect location of the file part were lost, and for a redirect the closing boundary was written out as file data. Reset the buffers when a header ends instead, which every supported version does. Add regression tests that run the real parsing path with a parser that skips on_header_begin, so this is caught on any installed version and not only by the olddeps job. Signed-off-by: Ankit Jha <ankit.jha@tradomate.one>
MadLittleMods
requested review from
a team and
devonh
and removed request for
a team and
MadLittleMods
November 5, 2026 23:13
devonh
requested changes
Nov 9, 2026
devonh
left a comment
Member
There was a problem hiding this comment.
Overall this seems good. Just a few things that could use cleaning up.
The first header of the metadata part must be content-type, as the old code required, and its value must be application/json. Raise a ValueError with the bad value so the warning says what went wrong. Import MultipartParser at the top of the test module. Signed-off-by: Ankit Jha <jhaankit373@gmail.com>
3 tasks
…unked-parse Signed-off-by: Ankit Jha <jhaankit373@gmail.com> # Conflicts: # tests/http/test_client.py
devonh
approved these changes
Nov 10, 2026
ll-SKY-ll
pushed a commit
to ll-SKY-ll/synapse
that referenced
this pull request
Nov 17, 2026
…lit across chunks (element-hq#20282) Fixes element-hq#20035 `_MultipartParserProtocol` assigned each `on_header_field` / `on_header_value` / `on_part_data` value instead of accumulating it. python-multipart calls these once per `dataReceived` chunk, so any header name, header value or JSON part split across two chunks kept only its last piece. A long `Location` in a federated media redirect was the visible case: the truncated URL had no host and `get_file` failed with `TypeError`. The protocol now buffers header names, header values and the JSON part in `bytearray`s and acts on them in `on_header_end` / `on_part_end`, with a part counter replacing the `in_json` / `json_done` / `in_*` flags. The file part still streams straight to the output stream, and the existing `max_length` check still bounds everything that gets buffered. The new tests parse a file response and a redirect response split at every byte offset, and byte by byte, and check the result matches the unsplit parse. Before the fix, 100 and 115 split offsets respectively produced a wrong result. ### Pull Request Checklist <!-- Please read https://element-hq.github.io/synapse/latest/development/contributing_guide.html before submitting your pull request --> * [x] Pull request is based on the develop branch * [x] Pull request includes a [changelog file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog). The entry should: - Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from `EventStore` to `EventWorkerStore`.". - Use markdown where necessary, mostly for `code blocks`. - End with either a period (.) or an exclamation mark (!). - Start with a capital letter. - Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry. * [x] [Code style](https://element-hq.github.io/synapse/latest/code_style.html) is correct (run the [linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters)) --------- Signed-off-by: Ankit Jha <ankit.jha@tradomate.one> Signed-off-by: Ankit Jha <jhaankit373@gmail.com>
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.
Fixes #20035
_MultipartParserProtocolassigned eachon_header_field/on_header_value/on_part_datavalue instead of accumulating it. python-multipart calls these once perdataReceivedchunk, so any header name, header value or JSON part split across two chunks kept only its last piece. A longLocationin a federated media redirect was the visible case: the truncated URL had no host andget_filefailed withTypeError.The protocol now buffers header names, header values and the JSON part in
bytearrays and acts on them inon_header_end/on_part_end, with a part counter replacing thein_json/json_done/in_*flags. The file part still streams straight to the output stream, and the existingmax_lengthcheck still bounds everything that gets buffered.The new tests parse a file response and a redirect response split at every byte offset, and byte by byte, and check the result matches the unsplit parse. Before the fix, 100 and 115 split offsets respectively produced a wrong result.
Pull Request Checklist
EventStoretoEventWorkerStore.".code blocks.