2019-05-29 17:47:16 +02:00
|
|
|
# Copyright 2019 Matrix.org Foundation
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock
|
2019-07-29 18:47:27 +02:00
|
|
|
|
|
|
|
from synapse.api.errors import Codes, SynapseError
|
2019-05-29 17:47:16 +02:00
|
|
|
from synapse.rest import admin
|
2021-08-17 13:57:58 +02:00
|
|
|
from synapse.rest.client import login, room
|
2019-07-29 18:47:27 +02:00
|
|
|
from synapse.types import UserID
|
2019-05-29 17:47:16 +02:00
|
|
|
|
|
|
|
from tests import unittest
|
2020-07-30 14:01:33 +02:00
|
|
|
from tests.test_utils import make_awaitable
|
2019-05-29 17:47:16 +02:00
|
|
|
|
|
|
|
|
2019-11-27 22:54:07 +01:00
|
|
|
class RoomComplexityTests(unittest.FederatingHomeserverTestCase):
|
2019-05-29 17:47:16 +02:00
|
|
|
|
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2020-03-24 19:33:49 +01:00
|
|
|
def default_config(self):
|
|
|
|
config = super().default_config()
|
2019-07-29 18:47:27 +02:00
|
|
|
config["limit_remote_rooms"] = {"enabled": True, "complexity": 0.05}
|
2019-05-29 17:47:16 +02:00
|
|
|
return config
|
|
|
|
|
|
|
|
def test_complexity_simple(self):
|
|
|
|
|
|
|
|
u1 = self.register_user("u1", "pass")
|
|
|
|
u1_token = self.login("u1", "pass")
|
|
|
|
|
|
|
|
room_1 = self.helper.create_room_as(u1, tok=u1_token)
|
|
|
|
self.helper.send_state(
|
|
|
|
room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the room complexity
|
2022-02-11 13:06:02 +01:00
|
|
|
channel = self.make_signed_federation_request(
|
2019-05-29 17:47:16 +02:00
|
|
|
"GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
|
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(200, channel.code)
|
2019-05-29 17:47:16 +02:00
|
|
|
complexity = channel.json_body["v1"]
|
|
|
|
self.assertTrue(complexity > 0, complexity)
|
|
|
|
|
|
|
|
# Artificially raise the complexity
|
2022-02-23 12:04:02 +01:00
|
|
|
store = self.hs.get_datastores().main
|
2020-08-27 23:24:46 +02:00
|
|
|
store.get_current_state_event_counts = lambda x: make_awaitable(500 * 1.23)
|
2019-05-29 17:47:16 +02:00
|
|
|
|
|
|
|
# Get the room complexity again -- make sure it's our artificial value
|
2022-02-11 13:06:02 +01:00
|
|
|
channel = self.make_signed_federation_request(
|
2019-05-29 17:47:16 +02:00
|
|
|
"GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
|
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(200, channel.code)
|
2019-05-29 17:47:16 +02:00
|
|
|
complexity = channel.json_body["v1"]
|
|
|
|
self.assertEqual(complexity, 1.23)
|
2019-07-29 18:47:27 +02:00
|
|
|
|
|
|
|
def test_join_too_large(self):
|
|
|
|
|
|
|
|
u1 = self.register_user("u1", "pass")
|
|
|
|
|
|
|
|
handler = self.hs.get_room_member_handler()
|
|
|
|
fed_transport = self.hs.get_federation_transport_client()
|
|
|
|
|
|
|
|
# Mock out some things, because we don't want to test the whole join
|
2020-09-08 13:26:55 +02:00
|
|
|
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
|
2020-05-22 15:21:54 +02:00
|
|
|
handler.federation_handler.do_invite_join = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(("", 1))
|
2020-05-22 15:21:54 +02:00
|
|
|
)
|
2019-07-29 18:47:27 +02:00
|
|
|
|
|
|
|
d = handler._remote_join(
|
|
|
|
None,
|
2019-11-27 22:54:07 +01:00
|
|
|
["other.example.com"],
|
2019-07-29 18:47:27 +02:00
|
|
|
"roomid",
|
|
|
|
UserID.from_string(u1),
|
|
|
|
{"membership": "join"},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# The request failed with a SynapseError saying the resource limit was
|
|
|
|
# exceeded.
|
|
|
|
f = self.get_failure(d, SynapseError)
|
|
|
|
self.assertEqual(f.value.code, 400, f.value)
|
|
|
|
self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
|
|
|
|
|
2020-07-28 14:41:44 +02:00
|
|
|
def test_join_too_large_admin(self):
|
|
|
|
# Check whether an admin can join if option "admins_can_join" is undefined,
|
|
|
|
# this option defaults to false, so the join should fail.
|
|
|
|
|
|
|
|
u1 = self.register_user("u1", "pass", admin=True)
|
|
|
|
|
|
|
|
handler = self.hs.get_room_member_handler()
|
|
|
|
fed_transport = self.hs.get_federation_transport_client()
|
|
|
|
|
|
|
|
# Mock out some things, because we don't want to test the whole join
|
2020-09-08 13:26:55 +02:00
|
|
|
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
|
2020-07-28 14:41:44 +02:00
|
|
|
handler.federation_handler.do_invite_join = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(("", 1))
|
2020-07-28 14:41:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
d = handler._remote_join(
|
|
|
|
None,
|
|
|
|
["other.example.com"],
|
|
|
|
"roomid",
|
|
|
|
UserID.from_string(u1),
|
|
|
|
{"membership": "join"},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# The request failed with a SynapseError saying the resource limit was
|
|
|
|
# exceeded.
|
|
|
|
f = self.get_failure(d, SynapseError)
|
|
|
|
self.assertEqual(f.value.code, 400, f.value)
|
|
|
|
self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
|
|
|
|
|
2019-07-29 18:47:27 +02:00
|
|
|
def test_join_too_large_once_joined(self):
|
|
|
|
|
|
|
|
u1 = self.register_user("u1", "pass")
|
|
|
|
u1_token = self.login("u1", "pass")
|
|
|
|
|
|
|
|
# Ok, this might seem a bit weird -- I want to test that we actually
|
|
|
|
# leave the room, but I don't want to simulate two servers. So, we make
|
|
|
|
# a local room, which we say we're joining remotely, even if there's no
|
|
|
|
# remote, because we mock that out. Then, we'll leave the (actually
|
|
|
|
# local) room, which will be propagated over federation in a real
|
|
|
|
# scenario.
|
|
|
|
room_1 = self.helper.create_room_as(u1, tok=u1_token)
|
|
|
|
|
|
|
|
handler = self.hs.get_room_member_handler()
|
|
|
|
fed_transport = self.hs.get_federation_transport_client()
|
|
|
|
|
|
|
|
# Mock out some things, because we don't want to test the whole join
|
2020-09-08 13:26:55 +02:00
|
|
|
fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
|
2020-05-22 15:21:54 +02:00
|
|
|
handler.federation_handler.do_invite_join = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(("", 1))
|
2020-05-22 15:21:54 +02:00
|
|
|
)
|
2019-07-29 18:47:27 +02:00
|
|
|
|
|
|
|
# Artificially raise the complexity
|
2022-02-23 12:04:02 +01:00
|
|
|
self.hs.get_datastores().main.get_current_state_event_counts = (
|
2021-02-16 23:32:34 +01:00
|
|
|
lambda x: make_awaitable(600)
|
2019-07-29 18:47:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
d = handler._remote_join(
|
|
|
|
None,
|
2019-11-27 22:54:07 +01:00
|
|
|
["other.example.com"],
|
2019-07-29 18:47:27 +02:00
|
|
|
room_1,
|
|
|
|
UserID.from_string(u1),
|
|
|
|
{"membership": "join"},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# The request failed with a SynapseError saying the resource limit was
|
|
|
|
# exceeded.
|
|
|
|
f = self.get_failure(d, SynapseError)
|
|
|
|
self.assertEqual(f.value.code, 400)
|
|
|
|
self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
|
2020-07-28 14:41:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RoomComplexityAdminTests(unittest.FederatingHomeserverTestCase):
|
|
|
|
# Test the behavior of joining rooms which exceed the complexity if option
|
|
|
|
# limit_remote_rooms.admins_can_join is True.
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def default_config(self):
|
|
|
|
config = super().default_config()
|
|
|
|
config["limit_remote_rooms"] = {
|
|
|
|
"enabled": True,
|
|
|
|
"complexity": 0.05,
|
|
|
|
"admins_can_join": True,
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
|
|
|
|
def test_join_too_large_no_admin(self):
|
|
|
|
# A user which is not an admin should not be able to join a remote room
|
|
|
|
# which is too complex.
|
|
|
|
|
|
|
|
u1 = self.register_user("u1", "pass")
|
|
|
|
|
|
|
|
handler = self.hs.get_room_member_handler()
|
|
|
|
fed_transport = self.hs.get_federation_transport_client()
|
|
|
|
|
|
|
|
# Mock out some things, because we don't want to test the whole join
|
2020-09-08 13:26:55 +02:00
|
|
|
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
|
2020-07-28 14:41:44 +02:00
|
|
|
handler.federation_handler.do_invite_join = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(("", 1))
|
2020-07-28 14:41:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
d = handler._remote_join(
|
|
|
|
None,
|
|
|
|
["other.example.com"],
|
|
|
|
"roomid",
|
|
|
|
UserID.from_string(u1),
|
|
|
|
{"membership": "join"},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# The request failed with a SynapseError saying the resource limit was
|
|
|
|
# exceeded.
|
|
|
|
f = self.get_failure(d, SynapseError)
|
|
|
|
self.assertEqual(f.value.code, 400, f.value)
|
|
|
|
self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
|
|
|
|
|
|
|
|
def test_join_too_large_admin(self):
|
|
|
|
# An admin should be able to join rooms where a complexity check fails.
|
|
|
|
|
|
|
|
u1 = self.register_user("u1", "pass", admin=True)
|
|
|
|
|
|
|
|
handler = self.hs.get_room_member_handler()
|
|
|
|
fed_transport = self.hs.get_federation_transport_client()
|
|
|
|
|
|
|
|
# Mock out some things, because we don't want to test the whole join
|
2020-09-08 13:26:55 +02:00
|
|
|
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
|
2020-07-28 14:41:44 +02:00
|
|
|
handler.federation_handler.do_invite_join = Mock(
|
2020-09-08 13:26:55 +02:00
|
|
|
return_value=make_awaitable(("", 1))
|
2020-07-28 14:41:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
d = handler._remote_join(
|
|
|
|
None,
|
|
|
|
["other.example.com"],
|
|
|
|
"roomid",
|
|
|
|
UserID.from_string(u1),
|
|
|
|
{"membership": "join"},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# The request success since the user is an admin
|
|
|
|
self.get_success(d)
|