mirror of
https://github.com/element-hq/synapse.git
synced 2026-12-04 09:16:02 +00:00
419cc6cf3165bc84f85d728583b2d21f81536ad1
/context for out-of-band memberships (#20500)
[More internal context here](https://claude.ai/artifact/2BcZgjUcvHJjV1jdsuPHLv#6a7be7a8-1857), this is part of a series of PRs to try and fix many buggy membership transitions. Replaces #19959 and #20440. This PR fixes 500 errors when reading a room your homeserver is not in, after you were invited to it or knocked on it. This is what happens when your *leave* membership is stored *out of band*, which is the case in two main scenarios: - a remote invite, your local homeserver doesn't have the full state of the room; when you reject that invite, your *leave membership* is stored out of band - you knock on a room, same: your local HS doesn't have the room state, stores the knock out of band; when you then rescind (cancel) the knock, that *leave membership* is also stored out of band Now that currently breaks a few APIs which try to look at the state of the room: [`GET /rooms/{roomId}/state`](https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomidstate), `/state/{eventType}/{stateKey}`, `/members` and `/rooms/{roomId}/initialSync`. Previously, Synapse would try to serve the state and fail to do so if your leave event is stored out of band. Because the only reason your leave event would be stored out of band is when you *never were in the room in the first place*, this changes the response of those endpoints to a 403 (because you were never in the room) when your leave event is stored OOB. The check is in `Auth.check_user_in_room`, so it applies to every endpoint which accepts departed users. `/messages` and `/threads` returned a 200 with an empty chunk for such a room, and now return the 403 the spec has for a user who isn't a member. Now, the reason we're not *just* checking `is_outlier()` (aka "we don't have the room state for this event") but also explicitly `is_out_of_band_membership()` for this part is that a history purge (admin API or retention) also leaves outliers behind: it keeps the state events, but drops the room state at them. A user who left before the purge really was in the room, so replying with a 403 would be wrong according to the spec (historical members should be able to see the state of the room as of their leave). I prefer deferring the conversation around what we should reply here to another PR later The second commit is similar, but for `/context` on your own membership event (the invite, the knock, or the leave). You are allowed to see that event, but the server has nothing around it, so the request ended in a 500. It now returns a 404, and the event itself is still served by `/event/{eventId}`. I did a spec clarification PR to explain that we should return a 404 for events where the server doesn't have context: https://github.com/matrix-org/matrix-spec/pull/2474 Complement test for `/context`: https://github.com/matrix-org/complement/pull/965 This one only checks `is_outlier()`, so `/context` on a leave from before a purge also goes from a 500 to a 404. <details><summary>Claude's original PR description with more random details 🤖</summary> The idea is the same in both commits: your own membership events are served, but the server has no state or timeline for the room, so it says so instead of failing. The easiest example: - Your local homeserver gets [an invite from a remote server](https://spec.matrix.org/latest/server-server-api/#inviting-to-a-room) for a room it is not in, and stores it as an "out of band" event: an outlier, with no state and no position in the room's timeline. - You reject it with [`/leave`](https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3roomsroomidleave). The leave is stored out of band too. **First commit: `/state`, `/members` and `/initialSync` after the leave.** [`GET /rooms/{roomId}/state`](https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomidstate), `/state/{eventType}/{stateKey}`, `/members` and `/rooms/{roomId}/initialSync` let a user who left a room read its state as of their leave. Synapse tries to load the state at that out-of-band leave, there is none, and the request ends in a `KeyError` and a 500. While the invite was still pending, the same requests got a 403. The out-of-band leave now gets that same 403: as far as this server knows the user was never in the room. The spec doesn't really cover a leave without a join here; its 403 for these endpoints is "You aren't a member of the room and weren't previously a member of the room". On matrix.org this is the only cause of 500s on these endpoints, around a thousand a day. Over three days (Nov 10 to 12) that was about 2000 requests from around 100 users, nearly all from Element X and matrix-rust-sdk on `/members` and `/state/m.room.encryption/`, retried again and again for the same rooms. The check is in `Auth.check_user_in_room`, so it applies to every endpoint which accepts departed users. That means `/messages`, `/relations` and `/threads`, which returned a 200 with an empty chunk for such a room, now return the 403 as well. **Second commit: `/context` on the membership event.** You are allowed to see your own membership events, so [`GET /rooms/{roomId}/context/{eventId}`](https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomidcontexteventid) on the invite, the knock or the leave passes the visibility check. It then fails when looking up what is around the event, and returns a 500. It now returns a 404 `M_NOT_FOUND`, as it already does for an event the server doesn't know. The event itself stays available from [`GET /rooms/{roomId}/event/{eventId}`](https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3roomsroomideventeventid), which returns a 200. #19959 and #20440 returned a 200 with the event alone and no `start`/`end` tokens. I dropped that: it needs spec text to say what missing tokens mean, and no client I looked at needs the response. Element X does call `/context` on such events (30 to 100 times a day on matrix.org, all ending in this 500), but only as the fallback of matrix-rust-sdk's notification client when sliding sync doesn't return the event, and it already copes with an error there. In those logs the users had all joined the room since, so this is most likely the invite, which stays an outlier after the join. 🤖 Generated with [Claude Code](https://claude.com/claude-code) </details> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
.. image:: https://github.com/element-hq/synapse/raw/develop/docs/element_logo_white_bg.svg :height: 60px **Element Synapse - Matrix homeserver implementation** |support| |development| |documentation| |license| |pypi| |python| Synapse is an open source `Matrix <https://matrix.org>`__ homeserver implementation, written and maintained by `Element <https://element.io>`_. `Matrix <https://github.com/matrix-org>`__ is the open standard for secure and interoperable real-time communications. You can directly run and manage the source code in this repository, available under an AGPL license (or alternatively under a commercial license from Element). There is no support provided by Element unless you have a subscription from Element. 🚀 Getting started ================== This component is developed and maintained by `Element <https://element.io>`_. It gets shipped as part of the **Element Server Suite (ESS)** which provides the official means of deployment. ESS is a Matrix distribution from Element with focus on quality and ease of use. It ships a full Matrix stack tailored to the respective use case. There are three editions of ESS: - `ESS Community <https://github.com/element-hq/ess-helm>`_ - the free Matrix distribution from Element tailored to small-/mid-scale, non-commercial community use cases - `ESS Pro <https://element.io/server-suite>`_ - the commercial Matrix distribution from Element for professional use - `ESS TI-M <https://element.io/server-suite/ti-messenger>`_ - a special version of ESS Pro focused on the requirements of TI-Messenger Pro and ePA as specified by the German National Digital Health Agency Gematik 🛠️ Standalone installation and configuration ============================================ The Synapse documentation describes `options for installing Synapse standalone <https://element-hq.github.io/synapse/latest/setup/installation.html>`_. See below for more useful documentation links. - `Synapse configuration options <https://element-hq.github.io/synapse/latest/usage/configuration/config_documentation.html>`_ - `Synapse configuration for federation <https://element-hq.github.io/synapse/latest/federate.html>`_ - `Using a reverse proxy with Synapse <https://element-hq.github.io/synapse/latest/reverse_proxy.html>`_ - `Upgrading Synapse <https://element-hq.github.io/synapse/develop/upgrade.html>`_ 🎯 Troubleshooting and support ============================== 🚀 Professional support ----------------------- Enterprise quality support for Synapse including SLAs is available as part of an `Element Server Suite (ESS) <https://element.io/pricing>`_ subscription. If you are an existing ESS subscriber then you can raise a `support request <https://customer.element.io/support>`_ and access the `Element product documentation <https://docs.element.io>`_. 🤝 Community support -------------------- The `Admin FAQ <https://element-hq.github.io/synapse/latest/usage/administration/admin_faq.html>`_ includes tips on dealing with some common problems. For more details, see `Synapse's wider documentation <https://element-hq.github.io/synapse/latest/>`_. For additional support installing or managing Synapse, please ask in the community support room |room|_ (from a matrix.org account if necessary). We do not use GitHub issues for support requests, only for bug reports and feature requests. .. |room| replace:: ``#synapse:matrix.org`` .. _room: https://matrix.to/#/#synapse:matrix.org .. |docs| replace:: ``docs`` .. _docs: docs 🛠️ Development ============== We welcome contributions to Synapse from the community! The best place to get started is our `guide for contributors <https://element-hq.github.io/synapse/latest/development/contributing_guide.html>`_. This is part of our broader `documentation <https://element-hq.github.io/synapse/latest>`_, which includes information for Synapse developers as well as Synapse administrators. Developers might be particularly interested in: * `Synapse's database schema <https://element-hq.github.io/synapse/latest/development/database_schema.html>`_, * `notes on Synapse's implementation details <https://element-hq.github.io/synapse/latest/development/internal_documentation/index.html>`_, and * `how we use git <https://element-hq.github.io/synapse/latest/development/git.html>`_. Alongside all that, join our developer community on Matrix: `#synapse-dev:matrix.org <https://matrix.to/#/#synapse-dev:matrix.org>`_, featuring real humans! Copyright and Licensing ======================= | Copyright 2014–2017 OpenMarket Ltd | Copyright 2017 Vector Creations Ltd | Copyright 2017–2025 New Vector Ltd | Copyright 2025 Element Creations Ltd This software is dual-licensed by Element Creations Ltd (Element). It can be used either: (1) for free under the terms of the GNU Affero General Public License (as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version); OR (2) under the terms of a paid-for Element Commercial License agreement between you and Element (the terms of which may vary depending on what you and Element have agreed to). Unless required by applicable law or agreed to in writing, software distributed under the Licenses is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. Please contact `licensing@element.io <mailto:licensing@element.io>`_ to purchase an Element commercial license for this software. .. |support| image:: https://img.shields.io/badge/matrix-community%20support-success :alt: (get community support in #synapse:matrix.org) :target: https://matrix.to/#/#synapse:matrix.org .. |development| image:: https://img.shields.io/matrix/synapse-dev:matrix.org?label=development&logo=matrix :alt: (discuss development on #synapse-dev:matrix.org) :target: https://matrix.to/#/#synapse-dev:matrix.org .. |documentation| image:: https://img.shields.io/badge/documentation-%E2%9C%93-success :alt: (Rendered documentation on GitHub Pages) :target: https://element-hq.github.io/synapse/latest/ .. |license| image:: https://img.shields.io/github/license/element-hq/synapse :alt: (check license in LICENSE file) :target: LICENSE .. |pypi| image:: https://img.shields.io/pypi/v/matrix-synapse :alt: (latest version released on PyPi) :target: https://pypi.org/project/matrix-synapse .. |python| image:: https://img.shields.io/pypi/pyversions/matrix-synapse :alt: (supported python versions) :target: https://pypi.org/project/matrix-synapse
Languages
Python
94.9%
Rust
3.9%
HTML
0.3%
Shell
0.3%
Go
0.2%
Other
0.1%