Skip to content

Add test for /context on an out-of-band membership event - #965

Draft
sandhose wants to merge 1 commit into
mainfrom
quenting/fix-oob-read-paths
Draft

sandhose wants to merge 1 commit into
mainfrom
quenting/fix-oob-read-paths

Conversation

@sandhose

@sandhose sandhose commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

This adds tests for this spec clarification PR: matrix-org/matrix-spec#2474

It checks that calling /context on an "out of band" membership like a remote invite returns a 404 as expected

Requires this fix on the Synapse side; the test fails in Synapse without it: element-hq/synapse#20500

@sandhose
sandhose force-pushed the quenting/fix-oob-read-paths branch from 3a001e2 to 7805d4b Compare November 13, 2026 12:27
sandhose added a commit to element-hq/synapse that referenced this pull request Nov 17, 2026
…ships (#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:
matrix-org/matrix-spec#2474

Complement test for `/context`:
matrix-org/complement#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant