synapse/tests/rest/client
Devon Hudson 8b0083cad9
Respond with useful error codes when Content-Length header/s are invalid (#19212)
Related to https://github.com/element-hq/synapse/issues/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
https://github.com/element-hq/synapse/issues/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.

This PR also responds with more meaningful error codes now in the case
of:
- multiple `Content-Length` headers
- invalid `Content-Length` header value
- request content size being larger than the `Content-Length` value

### 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))

---------

Co-authored-by: Eric Eastwood <erice@element.io>
2025-12-08 21:39:18 +00:00
..
sliding_sync Move towards a dedicated Duration class (#19223) 2025-11-26 10:56:59 +00:00
__init__.py Correctly mention previous copyright (#16820) 2024-01-23 11:26:48 +00:00
test_account_data.py Correctly mention previous copyright (#16820) 2024-01-23 11:26:48 +00:00
test_account.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_auth_metadata.py Support stable endpoint and scopes from the MSC3861 family (#18549) 2025-09-02 13:55:12 +02:00
test_auth.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_capabilities.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_consent.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_delayed_events.py MSC4140: Remove auth from delayed event management endpoints (#19152) 2025-11-13 18:56:17 +00:00
test_devices.py Allow using MSC4190 features without opt-in (#19031) 2025-10-13 13:07:11 +00:00
test_directory.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_ephemeral_message.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_events.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_filter.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_identity.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_keys.py Update KeyUploadServlet to handle case where client sends device_keys: null (#19023) 2025-10-07 16:23:55 +01:00
test_login_token_request.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_login.py Respond with useful error codes when Content-Length header/s are invalid (#19212) 2025-12-08 21:39:18 +00:00
test_matrixrtc.py Add MatrixRTC backend/services discovery endpoint (#18967) 2025-10-09 17:15:47 +01:00
test_media.py Respond with useful error codes when Content-Length header/s are invalid (#19212) 2025-12-08 21:39:18 +00:00
test_models.py Pydantic v2 (#19071) 2025-10-31 09:22:22 +00:00
test_mutual_rooms.py Put MSC2666 endpoint behind an experimental flag (#19219) 2025-11-25 18:03:33 +00:00
test_notifications.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_owned_state.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_password_policy.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_power_levels.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_presence.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_profile.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_push_rule_attrs.py Configure Synapse to run MSC4306: Thread Subscriptions Complement tests. (#18819) 2025-09-09 11:40:10 +01:00
test_read_marker.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_receipts.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_redactions.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_register.py Use type hinting generics in standard collections (#19046) 2025-10-22 16:48:19 -05:00
test_relations.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_rendezvous.py Use type hinting generics in standard collections (#19046) 2025-10-22 16:48:19 -05:00
test_reporting.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_retention.py Use type hinting generics in standard collections (#19046) 2025-10-22 16:48:19 -05:00
test_rooms.py Return 400 when canonical_alias content invalid (#19240) 2025-12-01 15:24:26 +00:00
test_sendtodevice.py Remove obsolete and experimental /sync/e2ee endpoint. (#18583) 2025-09-09 09:28:45 +01:00
test_shadow_banned.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_sync.py Use type hinting generics in standard collections (#19046) 2025-10-22 16:48:19 -05:00
test_tags.py Don't allow tagnames longer than 255 bytes (#18660) 2025-07-18 14:23:28 +01:00
test_third_party_rules.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
test_thread_subscriptions.py Pydantic v2 (#19071) 2025-10-31 09:22:22 +00:00
test_transactions.py Port Clock functions to use Duration class (#19229) 2025-12-01 13:55:06 +00:00
test_typing.py Introduce Clock.call_when_running(...) to include logcontext by default (#18944) 2025-09-22 10:27:59 -05:00
test_upgrade_room.py Write union types as X | Y where possible (#19111) 2025-11-06 14:02:33 -06:00
utils.py Respond with useful error codes when Content-Length header/s are invalid (#19212) 2025-12-08 21:39:18 +00:00