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

Commit 47e28b4

Browse files
authored
Ignore EDUs for rooms we're not in (#10317)
1 parent bcb0962 commit 47e28b4

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

changelog.d/10317.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix purging rooms that other homeservers are still sending events for. Contributed by @ilmari.

synapse/handlers/receipts.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def __init__(self, hs: "HomeServer"):
3030

3131
self.server_name = hs.config.server_name
3232
self.store = hs.get_datastore()
33+
self.event_auth_handler = hs.get_event_auth_handler()
34+
3335
self.hs = hs
3436

3537
# We only need to poke the federation sender explicitly if its on the
@@ -59,6 +61,19 @@ async def _received_remote_receipt(self, origin: str, content: JsonDict) -> None
5961
"""Called when we receive an EDU of type m.receipt from a remote HS."""
6062
receipts = []
6163
for room_id, room_values in content.items():
64+
# If we're not in the room just ditch the event entirely. This is
65+
# probably an old server that has come back and thinks we're still in
66+
# the room (or we've been rejoined to the room by a state reset).
67+
is_in_room = await self.event_auth_handler.check_host_in_room(
68+
room_id, self.server_name
69+
)
70+
if not is_in_room:
71+
logger.info(
72+
"Ignoring receipt from %s as we're not in the room",
73+
origin,
74+
)
75+
continue
76+
6277
for receipt_type, users in room_values.items():
6378
for user_id, user_values in users.items():
6479
if get_domain_from_id(user_id) != origin:

synapse/handlers/typing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def __init__(self, hs: "HomeServer"):
208208

209209
self.auth = hs.get_auth()
210210
self.notifier = hs.get_notifier()
211+
self.event_auth_handler = hs.get_event_auth_handler()
211212

212213
self.hs = hs
213214

@@ -326,6 +327,19 @@ async def _recv_edu(self, origin: str, content: JsonDict) -> None:
326327
room_id = content["room_id"]
327328
user_id = content["user_id"]
328329

330+
# If we're not in the room just ditch the event entirely. This is
331+
# probably an old server that has come back and thinks we're still in
332+
# the room (or we've been rejoined to the room by a state reset).
333+
is_in_room = await self.event_auth_handler.check_host_in_room(
334+
room_id, self.server_name
335+
)
336+
if not is_in_room:
337+
logger.info(
338+
"Ignoring typing update from %s as we're not in the room",
339+
origin,
340+
)
341+
return
342+
329343
member = RoomMember(user_id=user_id, room_id=room_id)
330344

331345
# Check that the string is a valid user id

tests/handlers/test_typing.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
# Test room id
3939
ROOM_ID = "a-room"
4040

41+
# Room we're not in
42+
OTHER_ROOM_ID = "another-room"
43+
4144

4245
def _expect_edu_transaction(edu_type, content, origin="test"):
4346
return {
@@ -115,6 +118,11 @@ async def check_user_in_room(room_id, user_id):
115118

116119
hs.get_auth().check_user_in_room = check_user_in_room
117120

121+
async def check_host_in_room(room_id, server_name):
122+
return room_id == ROOM_ID
123+
124+
hs.get_event_auth_handler().check_host_in_room = check_host_in_room
125+
118126
def get_joined_hosts_for_room(room_id):
119127
return {member.domain for member in self.room_members}
120128

@@ -244,6 +252,35 @@ def test_started_typing_remote_recv(self):
244252
],
245253
)
246254

255+
def test_started_typing_remote_recv_not_in_room(self):
256+
self.room_members = [U_APPLE, U_ONION]
257+
258+
self.assertEquals(self.event_source.get_current_key(), 0)
259+
260+
channel = self.make_request(
261+
"PUT",
262+
"/_matrix/federation/v1/send/1000000",
263+
_make_edu_transaction_json(
264+
"m.typing",
265+
content={
266+
"room_id": OTHER_ROOM_ID,
267+
"user_id": U_ONION.to_string(),
268+
"typing": True,
269+
},
270+
),
271+
federation_auth_origin=b"farm",
272+
)
273+
self.assertEqual(channel.code, 200)
274+
275+
self.on_new_event.assert_not_called()
276+
277+
self.assertEquals(self.event_source.get_current_key(), 0)
278+
events = self.get_success(
279+
self.event_source.get_new_events(room_ids=[OTHER_ROOM_ID], from_key=0)
280+
)
281+
self.assertEquals(events[0], [])
282+
self.assertEquals(events[1], 0)
283+
247284
@override_config({"send_federation": True})
248285
def test_stopped_typing(self):
249286
self.room_members = [U_APPLE, U_BANANA, U_ONION]

0 commit comments

Comments
 (0)