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