Skip to content

Conversation

@devonh
Copy link
Member

@devonh devonh commented Nov 21, 2025

Related to #17035, when Synapse receives a request that is larger than the maximum size allowed, it aborts the connection without ever sending back a HTTP response.
I dug into our usage of twisted and how best to try and report such an error and this is what I came up with.

It would be ideal to be able to report the status from within handleContentChunk but that is called too early on in the twisted http handling code, before things have been setup enough to be able to properly write a response.
I tested this change out locally (both with C-S and S-S apis) and they do receive a 413 response now in addition to the connection being closed.

Hopefully this will aid in being able to quickly detect when #17035 is occurring as the current situation makes it very hard to narrow things down to that specific issue without making a lot of assumptions.

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)

@devonh devonh requested a review from a team as a code owner November 21, 2025 17:14
)
self.responseHeaders.setRawHeaders(b"content-length", [b"0"])

logger.warning(
Copy link
Member Author

Choose a reason for hiding this comment

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

Should this be an info level log?

@MadLittleMods MadLittleMods added A-Federation A-to-device-messages EDU messages sent exactly once to a specific set of devices. Related to E2EE labels Nov 24, 2025
tests/server.py Outdated
Comment on lines 431 to 436
# `max_request_body_size` copied from `synapse/app/_base.py -> max_request_body_size()`
req = request(
channel,
site,
our_server_name="test_server",
max_request_body_size=200 * MAX_PDU_SIZE,
Copy link
Contributor

Choose a reason for hiding this comment

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

We should just have a constant that is shared

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a new constant and moved the reasoning comment along with it.

Comment on lines 193 to 194
# FIXME: This can be removed once we Twisted releases a fix and we update to a
# version that is patched
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is the PR that added this feature? I see the commit: 4b7154c

I'm looking for the context as is there a Twisted issue to track?

Copy link
Contributor

Choose a reason for hiding this comment

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

Context is in https://github.com/matrix-org/internal-config/issues/1572

Security Advisory: GHSA-rfq8-j7rh-8hf2

Which links to twisted/twisted#4688 (comment) but not exactly the best thing to link to. Perhaps, we can just link to the security advisory ⏩

Copy link
Member Author

Choose a reason for hiding this comment

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

Link has been added.

command.decode("ascii", errors="replace"),
self.get_redacted_uri(),
)
self.write(b"")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we write a better standard error response with errcode and error (human readable description of the problem)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a better error response. Should be good now.

Comment on lines -1731 to -1733
# old versions of twisted don't do form-parsing without a valid
# content-length header.
("Content-Length", str(len(content))),
Copy link
Member Author

Choose a reason for hiding this comment

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

make_request already adds a Content-Length header for this same reason. These all result in duplicate headers.

@devonh devonh requested a review from MadLittleMods November 27, 2025 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Federation A-to-device-messages EDU messages sent exactly once to a specific set of devices. Related to E2EE A-Validation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants