2020-08-26 15:49:01 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
2023-11-21 23:29:58 +03:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it 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.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-08-26 15:49:01 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2021-04-09 20:44:38 +03:00
|
|
|
from unittest.mock import Mock, patch
|
2020-08-26 15:49:01 +03:00
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2020-08-26 15:49:01 +03:00
|
|
|
import synapse.rest.admin
|
2022-05-27 14:14:36 +03:00
|
|
|
from synapse.api.constants import EduTypes, EventTypes
|
2021-08-17 14:57:58 +03:00
|
|
|
from synapse.rest.client import (
|
|
|
|
directory,
|
|
|
|
login,
|
|
|
|
profile,
|
|
|
|
room,
|
|
|
|
room_upgrade_rest_servlet,
|
|
|
|
)
|
2022-02-28 20:47:37 +03:00
|
|
|
from synapse.server import HomeServer
|
2022-08-22 16:17:59 +03:00
|
|
|
from synapse.types import UserID, create_requester
|
2022-02-28 20:47:37 +03:00
|
|
|
from synapse.util import Clock
|
2020-08-26 15:49:01 +03:00
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class _ShadowBannedBase(unittest.HomeserverTestCase):
|
2022-02-28 20:47:37 +03:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
# Create two users, one of which is shadow-banned.
|
|
|
|
self.banned_user_id = self.register_user("banned", "test")
|
|
|
|
self.banned_access_token = self.login("banned", "test")
|
|
|
|
|
2022-02-23 14:04:02 +03:00
|
|
|
self.store = self.hs.get_datastores().main
|
2020-08-26 15:49:01 +03:00
|
|
|
|
|
|
|
self.get_success(
|
2021-01-25 22:49:39 +03:00
|
|
|
self.store.set_shadow_banned(UserID.from_string(self.banned_user_id), True)
|
2020-08-26 15:49:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
self.other_user_id = self.register_user("otheruser", "pass")
|
|
|
|
self.other_access_token = self.login("otheruser", "pass")
|
|
|
|
|
|
|
|
|
|
|
|
# To avoid the tests timing out don't add a delay to "annoy the requester".
|
|
|
|
@patch("random.randint", new=lambda a, b: 0)
|
|
|
|
class RoomTestCase(_ShadowBannedBase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
directory.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
room_upgrade_rest_servlet.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_invite(self) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
"""Invites from shadow-banned users don't actually get sent."""
|
|
|
|
|
|
|
|
# The create works fine.
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
self.banned_user_id, tok=self.banned_access_token
|
|
|
|
)
|
|
|
|
|
|
|
|
# Inviting the user completes successfully.
|
|
|
|
self.helper.invite(
|
|
|
|
room=room_id,
|
|
|
|
src=self.banned_user_id,
|
|
|
|
tok=self.banned_access_token,
|
|
|
|
targ=self.other_user_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
# But the user wasn't actually invited.
|
|
|
|
invited_rooms = self.get_success(
|
|
|
|
self.store.get_invited_rooms_for_local_user(self.other_user_id)
|
|
|
|
)
|
|
|
|
self.assertEqual(invited_rooms, [])
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_invite_3pid(self) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
"""Ensure that a 3PID invite does not attempt to contact the identity server."""
|
2020-10-09 14:24:34 +03:00
|
|
|
identity_handler = self.hs.get_identity_handler()
|
2023-08-29 17:38:56 +03:00
|
|
|
identity_handler.lookup_3pid = Mock( # type: ignore[method-assign]
|
2020-08-26 15:49:01 +03:00
|
|
|
side_effect=AssertionError("This should not get called")
|
|
|
|
)
|
|
|
|
|
|
|
|
# The create works fine.
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
self.banned_user_id, tok=self.banned_access_token
|
|
|
|
)
|
|
|
|
|
|
|
|
# Inviting the user completes successfully.
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 15:49:01 +03:00
|
|
|
"POST",
|
|
|
|
"/rooms/%s/invite" % (room_id,),
|
2022-08-31 15:10:25 +03:00
|
|
|
{
|
|
|
|
"id_server": "test",
|
|
|
|
"medium": "email",
|
|
|
|
"address": "test@test.test",
|
|
|
|
"id_access_token": "anytoken",
|
|
|
|
},
|
2020-08-26 15:49:01 +03:00
|
|
|
access_token=self.banned_access_token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2020-08-26 15:49:01 +03:00
|
|
|
|
|
|
|
# This should have raised an error earlier, but double check this wasn't called.
|
|
|
|
identity_handler.lookup_3pid.assert_not_called()
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_create_room(self) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
"""Invitations during a room creation should be discarded, but the room still gets created."""
|
|
|
|
# The room creation is successful.
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 15:49:01 +03:00
|
|
|
"POST",
|
|
|
|
"/_matrix/client/r0/createRoom",
|
|
|
|
{"visibility": "public", "invite": [self.other_user_id]},
|
|
|
|
access_token=self.banned_access_token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2020-08-26 15:49:01 +03:00
|
|
|
room_id = channel.json_body["room_id"]
|
|
|
|
|
|
|
|
# But the user wasn't actually invited.
|
|
|
|
invited_rooms = self.get_success(
|
|
|
|
self.store.get_invited_rooms_for_local_user(self.other_user_id)
|
|
|
|
)
|
|
|
|
self.assertEqual(invited_rooms, [])
|
|
|
|
|
|
|
|
# Since a real room was created, the other user should be able to join it.
|
|
|
|
self.helper.join(room_id, self.other_user_id, tok=self.other_access_token)
|
|
|
|
|
|
|
|
# Both users should be in the room.
|
|
|
|
users = self.get_success(self.store.get_users_in_room(room_id))
|
|
|
|
self.assertCountEqual(users, ["@banned:test", "@otheruser:test"])
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_message(self) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
"""Messages from shadow-banned users don't actually get sent."""
|
|
|
|
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
self.other_user_id, tok=self.other_access_token
|
|
|
|
)
|
|
|
|
|
|
|
|
# The user should be in the room.
|
|
|
|
self.helper.join(room_id, self.banned_user_id, tok=self.banned_access_token)
|
|
|
|
|
|
|
|
# Sending a message should complete successfully.
|
|
|
|
result = self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={"msgtype": "m.text", "body": "with right label"},
|
|
|
|
tok=self.banned_access_token,
|
|
|
|
)
|
|
|
|
self.assertIn("event_id", result)
|
|
|
|
event_id = result["event_id"]
|
|
|
|
|
|
|
|
latest_events = self.get_success(
|
|
|
|
self.store.get_latest_event_ids_in_room(room_id)
|
|
|
|
)
|
|
|
|
self.assertNotIn(event_id, latest_events)
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_upgrade(self) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
"""A room upgrade should fail, but look like it succeeded."""
|
|
|
|
|
|
|
|
# The create works fine.
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
self.banned_user_id, tok=self.banned_access_token
|
|
|
|
)
|
|
|
|
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 15:49:01 +03:00
|
|
|
"POST",
|
|
|
|
"/_matrix/client/r0/rooms/%s/upgrade" % (room_id,),
|
|
|
|
{"new_version": "6"},
|
|
|
|
access_token=self.banned_access_token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2020-08-26 15:49:01 +03:00
|
|
|
# A new room_id should be returned.
|
|
|
|
self.assertIn("replacement_room", channel.json_body)
|
|
|
|
|
|
|
|
new_room_id = channel.json_body["replacement_room"]
|
|
|
|
|
|
|
|
# It doesn't really matter what API we use here, we just want to assert
|
|
|
|
# that the room doesn't exist.
|
|
|
|
summary = self.get_success(self.store.get_room_summary(new_room_id))
|
|
|
|
# The summary should be empty since the room doesn't exist.
|
|
|
|
self.assertEqual(summary, {})
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_typing(self) -> None:
|
2020-08-26 19:05:36 +03:00
|
|
|
"""Typing notifications should not be propagated into the room."""
|
|
|
|
# The create works fine.
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
self.banned_user_id, tok=self.banned_access_token
|
|
|
|
)
|
|
|
|
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 19:05:36 +03:00
|
|
|
"PUT",
|
|
|
|
"/rooms/%s/typing/%s" % (room_id, self.banned_user_id),
|
|
|
|
{"typing": True, "timeout": 30000},
|
|
|
|
access_token=self.banned_access_token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(200, channel.code)
|
2020-08-26 19:05:36 +03:00
|
|
|
|
|
|
|
# There should be no typing events.
|
2021-09-21 20:34:26 +03:00
|
|
|
event_source = self.hs.get_event_sources().sources.typing
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(event_source.get_current_key(), 0)
|
2020-08-26 19:05:36 +03:00
|
|
|
|
|
|
|
# The other user can join and send typing events.
|
|
|
|
self.helper.join(room_id, self.other_user_id, tok=self.other_access_token)
|
|
|
|
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 19:05:36 +03:00
|
|
|
"PUT",
|
|
|
|
"/rooms/%s/typing/%s" % (room_id, self.other_user_id),
|
|
|
|
{"typing": True, "timeout": 30000},
|
|
|
|
access_token=self.other_access_token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(200, channel.code)
|
2020-08-26 19:05:36 +03:00
|
|
|
|
|
|
|
# These appear in the room.
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(event_source.get_current_key(), 1)
|
2020-08-26 19:05:36 +03:00
|
|
|
events = self.get_success(
|
2021-09-21 20:34:26 +03:00
|
|
|
event_source.get_new_events(
|
|
|
|
user=UserID.from_string(self.other_user_id),
|
|
|
|
from_key=0,
|
2023-02-14 22:03:35 +03:00
|
|
|
limit=10,
|
2021-09-21 20:34:26 +03:00
|
|
|
room_ids=[room_id],
|
|
|
|
is_guest=False,
|
|
|
|
)
|
2020-08-26 19:05:36 +03:00
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(
|
2020-08-26 19:05:36 +03:00
|
|
|
events[0],
|
|
|
|
[
|
|
|
|
{
|
2022-05-27 14:14:36 +03:00
|
|
|
"type": EduTypes.TYPING,
|
2020-08-26 19:05:36 +03:00
|
|
|
"room_id": room_id,
|
|
|
|
"content": {"user_ids": [self.other_user_id]},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-08-26 15:49:01 +03:00
|
|
|
|
|
|
|
# To avoid the tests timing out don't add a delay to "annoy the requester".
|
|
|
|
@patch("random.randint", new=lambda a, b: 0)
|
|
|
|
class ProfileTestCase(_ShadowBannedBase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
login.register_servlets,
|
|
|
|
profile.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_displayname(self) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
"""Profile changes should succeed, but don't end up in a room."""
|
|
|
|
original_display_name = "banned"
|
|
|
|
new_display_name = "new name"
|
|
|
|
|
|
|
|
# Join a room.
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
self.banned_user_id, tok=self.banned_access_token
|
|
|
|
)
|
|
|
|
|
|
|
|
# The update should succeed.
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 15:49:01 +03:00
|
|
|
"PUT",
|
|
|
|
"/_matrix/client/r0/profile/%s/displayname" % (self.banned_user_id,),
|
|
|
|
{"displayname": new_display_name},
|
|
|
|
access_token=self.banned_access_token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2020-08-26 15:49:01 +03:00
|
|
|
self.assertEqual(channel.json_body, {})
|
|
|
|
|
|
|
|
# The user's display name should be updated.
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 15:49:01 +03:00
|
|
|
"GET", "/profile/%s/displayname" % (self.banned_user_id,)
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
self.assertEqual(channel.json_body["displayname"], new_display_name)
|
|
|
|
|
|
|
|
# But the display name in the room should not be.
|
|
|
|
message_handler = self.hs.get_message_handler()
|
|
|
|
event = self.get_success(
|
|
|
|
message_handler.get_room_data(
|
2022-08-22 16:17:59 +03:00
|
|
|
create_requester(self.banned_user_id),
|
2021-02-17 01:32:34 +03:00
|
|
|
room_id,
|
|
|
|
"m.room.member",
|
|
|
|
self.banned_user_id,
|
2020-08-26 15:49:01 +03:00
|
|
|
)
|
|
|
|
)
|
2023-02-14 22:03:35 +03:00
|
|
|
assert event is not None
|
2020-08-26 15:49:01 +03:00
|
|
|
self.assertEqual(
|
|
|
|
event.content, {"membership": "join", "displayname": original_display_name}
|
|
|
|
)
|
|
|
|
|
2022-02-28 20:47:37 +03:00
|
|
|
def test_room_displayname(self) -> None:
|
2020-08-26 15:49:01 +03:00
|
|
|
"""Changes to state events for a room should be processed, but not end up in the room."""
|
|
|
|
original_display_name = "banned"
|
|
|
|
new_display_name = "new name"
|
|
|
|
|
|
|
|
# Join a room.
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
self.banned_user_id, tok=self.banned_access_token
|
|
|
|
)
|
|
|
|
|
|
|
|
# The update should succeed.
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2020-08-26 15:49:01 +03:00
|
|
|
"PUT",
|
|
|
|
"/_matrix/client/r0/rooms/%s/state/m.room.member/%s"
|
|
|
|
% (room_id, self.banned_user_id),
|
|
|
|
{"membership": "join", "displayname": new_display_name},
|
|
|
|
access_token=self.banned_access_token,
|
|
|
|
)
|
2022-02-28 15:12:29 +03:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2020-08-26 15:49:01 +03:00
|
|
|
self.assertIn("event_id", channel.json_body)
|
|
|
|
|
|
|
|
# The display name in the room should not be changed.
|
|
|
|
message_handler = self.hs.get_message_handler()
|
|
|
|
event = self.get_success(
|
|
|
|
message_handler.get_room_data(
|
2022-08-22 16:17:59 +03:00
|
|
|
create_requester(self.banned_user_id),
|
2021-02-17 01:32:34 +03:00
|
|
|
room_id,
|
|
|
|
"m.room.member",
|
|
|
|
self.banned_user_id,
|
2020-08-26 15:49:01 +03:00
|
|
|
)
|
|
|
|
)
|
2023-02-14 22:03:35 +03:00
|
|
|
assert event is not None
|
2020-08-26 15:49:01 +03:00
|
|
|
self.assertEqual(
|
|
|
|
event.content, {"membership": "join", "displayname": original_display_name}
|
|
|
|
)
|