Quentin GliechandClaude Fable 5.2 cf0b0a9239 Fix 500s on membership changes in rooms the server is not in (#20499)
[More internal context
here](https://claude.ai/artifact/2BcZgjUcvHJjV1jdsuPHLv#dbe58feb-2677),
this is part of a series of PRs to try and fix many buggy membership
transitions.

This PR fixes a 500 error when trying to update the membership in rooms
where the server has no forward extremities.

To understand what this concretely means, the easiest example to
understand is when you try to reject a remote invite *twice*. This
means:

- Your local homeserver got [an invite from a remote
server](https://spec.matrix.org/latest/server-server-api/#inviting-to-a-room)
for a room it doesn't know anything about.
- In this case, it is storing the invitation as an "out of band" event,
and doesn't have any forward extremities for this room
- The spec then gives us special
[`/make_join`](https://spec.matrix.org/latest/server-server-api/#joining-rooms)
and
[`/make_leave`](https://spec.matrix.org/latest/server-server-api/#leaving-rooms-rejecting-invites)
dances that will let our homeserver craft proper join or leave events
without yet being in the room
- That's what the [`/leave` C-S
API](https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3roomsroomidleave)
uses, allowing a client to leave an invited room. Problem is when you
call this API twice:
- On the first `/leave`, our current local membership is "invite", so it
will do the `/make_leave` dance (or generate the leave itself if the
remote can't be reached), and then update our current local membership
to "leave" and store that leave event also out of band
- On the second `/leave`, our current local membership is "leave", so it
will try to build that event regularly, but is missing forward
extremities to do so

The reason clients call `/leave` twice is that they miss the leave event
in some circumstances, which will be addressed in other PRs.

But the "double leave" issue was always a problem. This *technically*
addresses https://github.com/element-hq/synapse/issues/18473 and
https://github.com/element-hq/synapse/issues/19485 by not making those
`/leave` return a 500, but the real problem is the leave event not going
down to clients.

This does two things, in two separate commits:

- first a safeguard: in a room we are not in and have no forward
extremities for, any membership change that isn't one of the special
cases gets a 403 because you're "not in the room" instead of a 500
- then replicate what we do normally which is to deduplicate the event
for identical `/leave` when the leave is stored OOB

#20503 is stacked on top of this PR because it makes a new case
reachable (a local user banned in a room we are not in), where `/leave`
would hit the same 500 without the safeguard here.

<details>
<summary>Original PR description from Claude Code 🤖</summary>

A user rejects a remote invite while the inviting server can't be
reached, or rescinds a knock. Synapse generates a local out-of-band
leave, stores it as an outlier and records it in
`local_current_membership`. If the user then calls `/leave` again
(clients do when they did not see the first leave; #20438 is one such
case), `update_membership_locked` finds no prev events for the room, the
LEAVE branch has no arm for a current membership of `leave`, and
`create_new_client_event` fails its "no prev_events" assertion with a
500. `/invite`, `/ban` and `/kick` in such a room hit the same
assertion. This is a few hundred 500s a day on matrix.org, mostly people
rejecting spam invites from servers that no longer answer. Reported as
#18473, #19485 and #18003.

When the server is not in the room and the room has no forward
extremities, an identical leave (same sender, same content) now returns
the existing event, as `deduplicate_state_event` does in a room the
server is in. Any other membership change there gets a 403
(`M_FORBIDDEN`, or `M_NOT_JOINED` with MSC3848) before an event is
built: a leave with different content, a leave by a banned user, and
`/invite`, `/ban` or `/kick`. That is the error the auth rules give a
sender who is not in the room. A room the server has left but still
holds forward extremities for keeps taking the existing path, which
already answers through the deduplication and the auth rules. Rooms
whose extremities `delete_old_current_state_events` removed are covered
too.

The leave is still not sent to the remote server, and it is still
invisible to clients; #20438 is for the latter.

Tests in `tests/handlers/test_room_member.py`: a repeat leave after a
failed invite rejection (the same event comes back, different content
gets a 403), and an invite, ban and kick in that room. All of them fail
with the assertion on develop.

#20503 is stacked on this. #20444 adds a `BAN` arm to the same branch
and will need a rebase.

https://claude.ai/code/session_01Usuxv8JofVGciXVtxbdTtr

</details>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 15:57:17 +01:00
2026-10-26 16:26:19 -05:00
2026-11-10 13:28:59 -06:00
2026-09-18 12:00:45 +01:00
2026-11-04 14:18:37 -06:00
2026-10-27 09:34:38 -05:00
2024-11-20 15:01:56 +00:00
2023-12-13 16:15:22 +00:00
2026-10-26 16:26:19 -05:00
2026-11-10 13:28:59 -06:00
2023-12-13 16:15:22 +00:00

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
S
Description
No description provided
Readme
643 MiB
0 Stars 1 Watchers 0 Forks
Languages
Python 94.9%
Rust 3.8%
HTML 0.3%
Shell 0.3%
Go 0.2%
Other 0.2%