Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 827f198

Browse files
authored
Fix error when sending message into deleted room. (#15235)
When a room is deleted in Synapse we remove the event forward extremities in the room, so if (say a bot) tries to send a message into the room we error out due to not being able to calculate prev events for the new event *before* we check if the sender is in the room. Fixes #8094
1 parent a5fb382 commit 827f198

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

changelog.d/15235.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix long-standing error when sending message into deleted room.

synapse/handlers/message.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,10 +987,11 @@ async def create_and_send_nonmember_event(
987987
# a situation where event persistence can't keep up, causing
988988
# extremities to pile up, which in turn leads to state resolution
989989
# taking longer.
990-
async with self.limiter.queue(event_dict["room_id"]):
990+
room_id = event_dict["room_id"]
991+
async with self.limiter.queue(room_id):
991992
if txn_id:
992993
event = await self.get_event_from_transaction(
993-
requester, txn_id, event_dict["room_id"]
994+
requester, txn_id, room_id
994995
)
995996
if event:
996997
# we know it was persisted, so must have a stream ordering
@@ -1000,6 +1001,18 @@ async def create_and_send_nonmember_event(
10001001
event.internal_metadata.stream_ordering,
10011002
)
10021003

1004+
# If we don't have any prev event IDs specified then we need to
1005+
# check that the host is in the room (as otherwise populating the
1006+
# prev events will fail), at which point we may as well check the
1007+
# local user is in the room.
1008+
if not prev_event_ids:
1009+
user_id = requester.user.to_string()
1010+
is_user_in_room = await self.store.check_local_user_in_room(
1011+
user_id, room_id
1012+
)
1013+
if not is_user_in_room:
1014+
raise AuthError(403, f"User {user_id} not in room {room_id}")
1015+
10031016
# Try several times, it could fail with PartialStateConflictError
10041017
# in handle_new_client_event, cf comment in except block.
10051018
max_retries = 5

tests/rest/admin/test_room.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,21 @@ def test_shutdown_room_block_peek(self) -> None:
402402
# Assert we can no longer peek into the room
403403
self._assert_peek(self.room_id, expect_code=403)
404404

405+
def test_room_delete_send(self) -> None:
406+
"""Test that sending into a deleted room returns a 403"""
407+
channel = self.make_request(
408+
"DELETE",
409+
self.url,
410+
content={},
411+
access_token=self.admin_user_tok,
412+
)
413+
414+
self.assertEqual(200, channel.code, msg=channel.json_body)
415+
416+
self.helper.send(
417+
self.room_id, "test message", expect_code=403, tok=self.other_user_tok
418+
)
419+
405420
def _is_blocked(self, room_id: str, expect: bool = True) -> None:
406421
"""Assert that the room is blocked or not"""
407422
d = self.store.is_room_blocked(room_id)

0 commit comments

Comments
 (0)