This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop shadow-banned users from sending invites. (#8095)
- Loading branch information
Showing
7 changed files
with
226 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add support for shadow-banning users (ignoring any message send requests). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1974,3 +1974,103 @@ def test_bad_alias(self): | |
"""An alias which does not point to the room raises a SynapseError.""" | ||
self._set_canonical_alias({"alias": "@unknown:test"}, expected_code=400) | ||
self._set_canonical_alias({"alt_aliases": ["@unknown:test"]}, expected_code=400) | ||
|
||
|
||
class ShadowBannedTestCase(unittest.HomeserverTestCase): | ||
servlets = [ | ||
synapse.rest.admin.register_servlets_for_client_rest_resource, | ||
directory.register_servlets, | ||
login.register_servlets, | ||
room.register_servlets, | ||
] | ||
|
||
def prepare(self, reactor, clock, homeserver): | ||
self.banned_user_id = self.register_user("banned", "test") | ||
self.banned_access_token = self.login("banned", "test") | ||
|
||
self.store = self.hs.get_datastore() | ||
|
||
self.get_success( | ||
self.store.db_pool.simple_update( | ||
table="users", | ||
keyvalues={"name": self.banned_user_id}, | ||
updatevalues={"shadow_banned": True}, | ||
desc="shadow_ban", | ||
) | ||
) | ||
|
||
self.other_user_id = self.register_user("otheruser", "pass") | ||
self.other_access_token = self.login("otheruser", "pass") | ||
|
||
def test_invite(self): | ||
"""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, []) | ||
|
||
def test_invite_3pid(self): | ||
"""Ensure that a 3PID invite does not attempt to contact the identity server.""" | ||
identity_handler = self.hs.get_handlers().identity_handler | ||
identity_handler.lookup_3pid = Mock( | ||
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. | ||
request, channel = self.make_request( | ||
"POST", | ||
"/rooms/%s/invite" % (room_id,), | ||
{"id_server": "test", "medium": "email", "address": "[email protected]"}, | ||
access_token=self.banned_access_token, | ||
) | ||
self.render(request) | ||
self.assertEquals(200, channel.code, channel.result) | ||
|
||
# This should have raised an error earlier, but double check this wasn't called. | ||
identity_handler.lookup_3pid.assert_not_called() | ||
|
||
def test_create_room(self): | ||
"""Invitations during a room creation should be discarded, but the room still gets created.""" | ||
# The room creation is successful. | ||
request, channel = self.make_request( | ||
"POST", | ||
"/_matrix/client/r0/createRoom", | ||
{"visibility": "public", "invite": [self.other_user_id]}, | ||
access_token=self.banned_access_token, | ||
) | ||
self.render(request) | ||
self.assertEquals(200, channel.code, channel.result) | ||
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"]) |