test_complexity.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright 2019 Matrix.org Foundation
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from http import HTTPStatus
  15. from unittest.mock import Mock
  16. from synapse.api.errors import Codes, SynapseError
  17. from synapse.rest import admin
  18. from synapse.rest.client import login, room
  19. from synapse.types import UserID
  20. from tests import unittest
  21. from tests.test_utils import make_awaitable
  22. class RoomComplexityTests(unittest.FederatingHomeserverTestCase):
  23. servlets = [
  24. admin.register_servlets,
  25. room.register_servlets,
  26. login.register_servlets,
  27. ]
  28. def default_config(self):
  29. config = super().default_config()
  30. config["limit_remote_rooms"] = {"enabled": True, "complexity": 0.05}
  31. return config
  32. def test_complexity_simple(self):
  33. u1 = self.register_user("u1", "pass")
  34. u1_token = self.login("u1", "pass")
  35. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  36. self.helper.send_state(
  37. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  38. )
  39. # Get the room complexity
  40. channel = self.make_signed_federation_request(
  41. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  42. )
  43. self.assertEqual(HTTPStatus.OK, channel.code)
  44. complexity = channel.json_body["v1"]
  45. self.assertTrue(complexity > 0, complexity)
  46. # Artificially raise the complexity
  47. store = self.hs.get_datastores().main
  48. store.get_current_state_event_counts = lambda x: make_awaitable(500 * 1.23)
  49. # Get the room complexity again -- make sure it's our artificial value
  50. channel = self.make_signed_federation_request(
  51. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  52. )
  53. self.assertEqual(HTTPStatus.OK, channel.code)
  54. complexity = channel.json_body["v1"]
  55. self.assertEqual(complexity, 1.23)
  56. def test_join_too_large(self):
  57. u1 = self.register_user("u1", "pass")
  58. handler = self.hs.get_room_member_handler()
  59. fed_transport = self.hs.get_federation_transport_client()
  60. # Mock out some things, because we don't want to test the whole join
  61. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  62. handler.federation_handler.do_invite_join = Mock(
  63. return_value=make_awaitable(("", 1))
  64. )
  65. d = handler._remote_join(
  66. None,
  67. ["other.example.com"],
  68. "roomid",
  69. UserID.from_string(u1),
  70. {"membership": "join"},
  71. )
  72. self.pump()
  73. # The request failed with a SynapseError saying the resource limit was
  74. # exceeded.
  75. f = self.get_failure(d, SynapseError)
  76. self.assertEqual(f.value.code, 400, f.value)
  77. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  78. def test_join_too_large_admin(self):
  79. # Check whether an admin can join if option "admins_can_join" is undefined,
  80. # this option defaults to false, so the join should fail.
  81. u1 = self.register_user("u1", "pass", admin=True)
  82. handler = self.hs.get_room_member_handler()
  83. fed_transport = self.hs.get_federation_transport_client()
  84. # Mock out some things, because we don't want to test the whole join
  85. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  86. handler.federation_handler.do_invite_join = Mock(
  87. return_value=make_awaitable(("", 1))
  88. )
  89. d = handler._remote_join(
  90. None,
  91. ["other.example.com"],
  92. "roomid",
  93. UserID.from_string(u1),
  94. {"membership": "join"},
  95. )
  96. self.pump()
  97. # The request failed with a SynapseError saying the resource limit was
  98. # exceeded.
  99. f = self.get_failure(d, SynapseError)
  100. self.assertEqual(f.value.code, 400, f.value)
  101. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  102. def test_join_too_large_once_joined(self):
  103. u1 = self.register_user("u1", "pass")
  104. u1_token = self.login("u1", "pass")
  105. # Ok, this might seem a bit weird -- I want to test that we actually
  106. # leave the room, but I don't want to simulate two servers. So, we make
  107. # a local room, which we say we're joining remotely, even if there's no
  108. # remote, because we mock that out. Then, we'll leave the (actually
  109. # local) room, which will be propagated over federation in a real
  110. # scenario.
  111. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  112. handler = self.hs.get_room_member_handler()
  113. fed_transport = self.hs.get_federation_transport_client()
  114. # Mock out some things, because we don't want to test the whole join
  115. fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
  116. handler.federation_handler.do_invite_join = Mock(
  117. return_value=make_awaitable(("", 1))
  118. )
  119. # Artificially raise the complexity
  120. self.hs.get_datastores().main.get_current_state_event_counts = (
  121. lambda x: make_awaitable(600)
  122. )
  123. d = handler._remote_join(
  124. None,
  125. ["other.example.com"],
  126. room_1,
  127. UserID.from_string(u1),
  128. {"membership": "join"},
  129. )
  130. self.pump()
  131. # The request failed with a SynapseError saying the resource limit was
  132. # exceeded.
  133. f = self.get_failure(d, SynapseError)
  134. self.assertEqual(f.value.code, 400)
  135. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  136. class RoomComplexityAdminTests(unittest.FederatingHomeserverTestCase):
  137. # Test the behavior of joining rooms which exceed the complexity if option
  138. # limit_remote_rooms.admins_can_join is True.
  139. servlets = [
  140. admin.register_servlets,
  141. room.register_servlets,
  142. login.register_servlets,
  143. ]
  144. def default_config(self):
  145. config = super().default_config()
  146. config["limit_remote_rooms"] = {
  147. "enabled": True,
  148. "complexity": 0.05,
  149. "admins_can_join": True,
  150. }
  151. return config
  152. def test_join_too_large_no_admin(self):
  153. # A user which is not an admin should not be able to join a remote room
  154. # which is too complex.
  155. u1 = self.register_user("u1", "pass")
  156. handler = self.hs.get_room_member_handler()
  157. fed_transport = self.hs.get_federation_transport_client()
  158. # Mock out some things, because we don't want to test the whole join
  159. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  160. handler.federation_handler.do_invite_join = Mock(
  161. return_value=make_awaitable(("", 1))
  162. )
  163. d = handler._remote_join(
  164. None,
  165. ["other.example.com"],
  166. "roomid",
  167. UserID.from_string(u1),
  168. {"membership": "join"},
  169. )
  170. self.pump()
  171. # The request failed with a SynapseError saying the resource limit was
  172. # exceeded.
  173. f = self.get_failure(d, SynapseError)
  174. self.assertEqual(f.value.code, 400, f.value)
  175. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  176. def test_join_too_large_admin(self):
  177. # An admin should be able to join rooms where a complexity check fails.
  178. u1 = self.register_user("u1", "pass", admin=True)
  179. handler = self.hs.get_room_member_handler()
  180. fed_transport = self.hs.get_federation_transport_client()
  181. # Mock out some things, because we don't want to test the whole join
  182. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  183. handler.federation_handler.do_invite_join = Mock(
  184. return_value=make_awaitable(("", 1))
  185. )
  186. d = handler._remote_join(
  187. None,
  188. ["other.example.com"],
  189. "roomid",
  190. UserID.from_string(u1),
  191. {"membership": "join"},
  192. )
  193. self.pump()
  194. # The request success since the user is an admin
  195. self.get_success(d)