Ignore EDUs for rooms we're not in (#10317)
This commit is contained in:
parent
bcb0962a72
commit
47e28b4031
|
@ -0,0 +1 @@
|
|||
Fix purging rooms that other homeservers are still sending events for. Contributed by @ilmari.
|
|
@ -30,6 +30,8 @@ class ReceiptsHandler(BaseHandler):
|
|||
|
||||
self.server_name = hs.config.server_name
|
||||
self.store = hs.get_datastore()
|
||||
self.event_auth_handler = hs.get_event_auth_handler()
|
||||
|
||||
self.hs = hs
|
||||
|
||||
# We only need to poke the federation sender explicitly if its on the
|
||||
|
@ -59,6 +61,19 @@ class ReceiptsHandler(BaseHandler):
|
|||
"""Called when we receive an EDU of type m.receipt from a remote HS."""
|
||||
receipts = []
|
||||
for room_id, room_values in content.items():
|
||||
# If we're not in the room just ditch the event entirely. This is
|
||||
# probably an old server that has come back and thinks we're still in
|
||||
# the room (or we've been rejoined to the room by a state reset).
|
||||
is_in_room = await self.event_auth_handler.check_host_in_room(
|
||||
room_id, self.server_name
|
||||
)
|
||||
if not is_in_room:
|
||||
logger.info(
|
||||
"Ignoring receipt from %s as we're not in the room",
|
||||
origin,
|
||||
)
|
||||
continue
|
||||
|
||||
for receipt_type, users in room_values.items():
|
||||
for user_id, user_values in users.items():
|
||||
if get_domain_from_id(user_id) != origin:
|
||||
|
|
|
@ -208,6 +208,7 @@ class TypingWriterHandler(FollowerTypingHandler):
|
|||
|
||||
self.auth = hs.get_auth()
|
||||
self.notifier = hs.get_notifier()
|
||||
self.event_auth_handler = hs.get_event_auth_handler()
|
||||
|
||||
self.hs = hs
|
||||
|
||||
|
@ -326,6 +327,19 @@ class TypingWriterHandler(FollowerTypingHandler):
|
|||
room_id = content["room_id"]
|
||||
user_id = content["user_id"]
|
||||
|
||||
# If we're not in the room just ditch the event entirely. This is
|
||||
# probably an old server that has come back and thinks we're still in
|
||||
# the room (or we've been rejoined to the room by a state reset).
|
||||
is_in_room = await self.event_auth_handler.check_host_in_room(
|
||||
room_id, self.server_name
|
||||
)
|
||||
if not is_in_room:
|
||||
logger.info(
|
||||
"Ignoring typing update from %s as we're not in the room",
|
||||
origin,
|
||||
)
|
||||
return
|
||||
|
||||
member = RoomMember(user_id=user_id, room_id=room_id)
|
||||
|
||||
# Check that the string is a valid user id
|
||||
|
|
|
@ -38,6 +38,9 @@ U_ONION = UserID.from_string("@onion:farm")
|
|||
# Test room id
|
||||
ROOM_ID = "a-room"
|
||||
|
||||
# Room we're not in
|
||||
OTHER_ROOM_ID = "another-room"
|
||||
|
||||
|
||||
def _expect_edu_transaction(edu_type, content, origin="test"):
|
||||
return {
|
||||
|
@ -115,6 +118,11 @@ class TypingNotificationsTestCase(unittest.HomeserverTestCase):
|
|||
|
||||
hs.get_auth().check_user_in_room = check_user_in_room
|
||||
|
||||
async def check_host_in_room(room_id, server_name):
|
||||
return room_id == ROOM_ID
|
||||
|
||||
hs.get_event_auth_handler().check_host_in_room = check_host_in_room
|
||||
|
||||
def get_joined_hosts_for_room(room_id):
|
||||
return {member.domain for member in self.room_members}
|
||||
|
||||
|
@ -244,6 +252,35 @@ class TypingNotificationsTestCase(unittest.HomeserverTestCase):
|
|||
],
|
||||
)
|
||||
|
||||
def test_started_typing_remote_recv_not_in_room(self):
|
||||
self.room_members = [U_APPLE, U_ONION]
|
||||
|
||||
self.assertEquals(self.event_source.get_current_key(), 0)
|
||||
|
||||
channel = self.make_request(
|
||||
"PUT",
|
||||
"/_matrix/federation/v1/send/1000000",
|
||||
_make_edu_transaction_json(
|
||||
"m.typing",
|
||||
content={
|
||||
"room_id": OTHER_ROOM_ID,
|
||||
"user_id": U_ONION.to_string(),
|
||||
"typing": True,
|
||||
},
|
||||
),
|
||||
federation_auth_origin=b"farm",
|
||||
)
|
||||
self.assertEqual(channel.code, 200)
|
||||
|
||||
self.on_new_event.assert_not_called()
|
||||
|
||||
self.assertEquals(self.event_source.get_current_key(), 0)
|
||||
events = self.get_success(
|
||||
self.event_source.get_new_events(room_ids=[OTHER_ROOM_ID], from_key=0)
|
||||
)
|
||||
self.assertEquals(events[0], [])
|
||||
self.assertEquals(events[1], 0)
|
||||
|
||||
@override_config({"send_federation": True})
|
||||
def test_stopped_typing(self):
|
||||
self.room_members = [U_APPLE, U_BANANA, U_ONION]
|
||||
|
|
Loading…
Reference in New Issue