Skip to content

Fix federated media downloads failing when a multipart response is split across chunks - #20282

Merged
devonh merged 6 commits into
element-hq:developfrom
ankit373:fix-multipart-chunked-parse
Nov 10, 2026
Merged

devonh merged 6 commits into
element-hq:developfrom
ankit373:fix-multipart-chunked-parse

Conversation

@ankit373

Copy link
Copy Markdown
Contributor

Fixes #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 bytearrays 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

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. 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.
  • Code style is correct (run the linters)

_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
ankit373 requested a review from a team as a code owner September 28, 2026 10:28
@ankit373
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
MadLittleMods requested review from a team and devonh and removed request for a team and MadLittleMods November 5, 2026 23:13

@devonh devonh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this seems good. Just a few things that could use cleaning up.

Comment thread synapse/http/client.py Resolved
Comment thread synapse/http/client.py Resolved
Comment thread tests/http/test_client.py Resolved
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>
…unked-parse

Signed-off-by: Ankit Jha <jhaankit373@gmail.com>

# Conflicts:
#	tests/http/test_client.py
@devonh
devonh merged commit 0744b28 into element-hq:develop Nov 10, 2026
75 of 78 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_MultipartParserProtocol does not properly handle chunked responses

3 participants