test_third_party_rules.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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. import threading
  15. from typing import TYPE_CHECKING, Dict, Optional, Tuple
  16. from unittest.mock import Mock
  17. from synapse.api.constants import EventTypes, Membership
  18. from synapse.api.errors import SynapseError
  19. from synapse.events import EventBase
  20. from synapse.events.third_party_rules import load_legacy_third_party_event_rules
  21. from synapse.rest import admin
  22. from synapse.rest.client import login, room
  23. from synapse.types import JsonDict, Requester, StateMap
  24. from synapse.util.frozenutils import unfreeze
  25. from tests import unittest
  26. from tests.test_utils import make_awaitable
  27. if TYPE_CHECKING:
  28. from synapse.module_api import ModuleApi
  29. thread_local = threading.local()
  30. class LegacyThirdPartyRulesTestModule:
  31. def __init__(self, config: Dict, module_api: "ModuleApi"):
  32. # keep a record of the "current" rules module, so that the test can patch
  33. # it if desired.
  34. thread_local.rules_module = self
  35. self.module_api = module_api
  36. async def on_create_room(
  37. self, requester: Requester, config: dict, is_requester_admin: bool
  38. ):
  39. return True
  40. async def check_event_allowed(self, event: EventBase, state: StateMap[EventBase]):
  41. return True
  42. @staticmethod
  43. def parse_config(config):
  44. return config
  45. class LegacyDenyNewRooms(LegacyThirdPartyRulesTestModule):
  46. def __init__(self, config: Dict, module_api: "ModuleApi"):
  47. super().__init__(config, module_api)
  48. def on_create_room(
  49. self, requester: Requester, config: dict, is_requester_admin: bool
  50. ):
  51. return False
  52. class LegacyChangeEvents(LegacyThirdPartyRulesTestModule):
  53. def __init__(self, config: Dict, module_api: "ModuleApi"):
  54. super().__init__(config, module_api)
  55. async def check_event_allowed(self, event: EventBase, state: StateMap[EventBase]):
  56. d = event.get_dict()
  57. content = unfreeze(event.content)
  58. content["foo"] = "bar"
  59. d["content"] = content
  60. return d
  61. class ThirdPartyRulesTestCase(unittest.FederatingHomeserverTestCase):
  62. servlets = [
  63. admin.register_servlets,
  64. login.register_servlets,
  65. room.register_servlets,
  66. ]
  67. def make_homeserver(self, reactor, clock):
  68. hs = self.setup_test_homeserver()
  69. load_legacy_third_party_event_rules(hs)
  70. # We're not going to be properly signing events as our remote homeserver is fake,
  71. # therefore disable event signature checks.
  72. # Note that these checks are not relevant to this test case.
  73. # Have this homeserver auto-approve all event signature checking.
  74. async def approve_all_signature_checking(_, pdu):
  75. return pdu
  76. hs.get_federation_server()._check_sigs_and_hash = approve_all_signature_checking
  77. # Have this homeserver skip event auth checks. This is necessary due to
  78. # event auth checks ensuring that events were signed by the sender's homeserver.
  79. async def _check_event_auth(origin, event, context, *args, **kwargs):
  80. return context
  81. hs.get_federation_event_handler()._check_event_auth = _check_event_auth
  82. return hs
  83. def prepare(self, reactor, clock, homeserver):
  84. # Create some users and a room to play with during the tests
  85. self.user_id = self.register_user("kermit", "monkey")
  86. self.invitee = self.register_user("invitee", "hackme")
  87. self.tok = self.login("kermit", "monkey")
  88. # Some tests might prevent room creation on purpose.
  89. try:
  90. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  91. except Exception:
  92. pass
  93. def test_third_party_rules(self):
  94. """Tests that a forbidden event is forbidden from being sent, but an allowed one
  95. can be sent.
  96. """
  97. # patch the rules module with a Mock which will return False for some event
  98. # types
  99. async def check(ev, state):
  100. return ev.type != "foo.bar.forbidden", None
  101. callback = Mock(spec=[], side_effect=check)
  102. self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [
  103. callback
  104. ]
  105. channel = self.make_request(
  106. "PUT",
  107. "/_matrix/client/r0/rooms/%s/send/foo.bar.allowed/1" % self.room_id,
  108. {},
  109. access_token=self.tok,
  110. )
  111. self.assertEquals(channel.result["code"], b"200", channel.result)
  112. callback.assert_called_once()
  113. # there should be various state events in the state arg: do some basic checks
  114. state_arg = callback.call_args[0][1]
  115. for k in (("m.room.create", ""), ("m.room.member", self.user_id)):
  116. self.assertIn(k, state_arg)
  117. ev = state_arg[k]
  118. self.assertEqual(ev.type, k[0])
  119. self.assertEqual(ev.state_key, k[1])
  120. channel = self.make_request(
  121. "PUT",
  122. "/_matrix/client/r0/rooms/%s/send/foo.bar.forbidden/2" % self.room_id,
  123. {},
  124. access_token=self.tok,
  125. )
  126. self.assertEquals(channel.result["code"], b"403", channel.result)
  127. def test_third_party_rules_workaround_synapse_errors_pass_through(self):
  128. """
  129. Tests that the workaround introduced by https://github.com/matrix-org/synapse/pull/11042
  130. is functional: that SynapseErrors are passed through from check_event_allowed
  131. and bubble up to the web resource.
  132. NEW MODULES SHOULD NOT MAKE USE OF THIS WORKAROUND!
  133. This is a temporary workaround!
  134. """
  135. class NastyHackException(SynapseError):
  136. def error_dict(self):
  137. """
  138. This overrides SynapseError's `error_dict` to nastily inject
  139. JSON into the error response.
  140. """
  141. result = super().error_dict()
  142. result["nasty"] = "very"
  143. return result
  144. # add a callback that will raise our hacky exception
  145. async def check(ev, state) -> Tuple[bool, Optional[JsonDict]]:
  146. raise NastyHackException(429, "message")
  147. self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
  148. # Make a request
  149. channel = self.make_request(
  150. "PUT",
  151. "/_matrix/client/r0/rooms/%s/send/foo.bar.forbidden/2" % self.room_id,
  152. {},
  153. access_token=self.tok,
  154. )
  155. # Check the error code
  156. self.assertEquals(channel.result["code"], b"429", channel.result)
  157. # Check the JSON body has had the `nasty` key injected
  158. self.assertEqual(
  159. channel.json_body,
  160. {"errcode": "M_UNKNOWN", "error": "message", "nasty": "very"},
  161. )
  162. def test_cannot_modify_event(self):
  163. """cannot accidentally modify an event before it is persisted"""
  164. # first patch the event checker so that it will try to modify the event
  165. async def check(ev: EventBase, state):
  166. ev.content = {"x": "y"}
  167. return True, None
  168. self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
  169. # now send the event
  170. channel = self.make_request(
  171. "PUT",
  172. "/_matrix/client/r0/rooms/%s/send/modifyme/1" % self.room_id,
  173. {"x": "x"},
  174. access_token=self.tok,
  175. )
  176. # Because check_event_allowed raises an exception, it leads to a
  177. # 500 Internal Server Error
  178. self.assertEqual(channel.code, 500, channel.result)
  179. def test_modify_event(self):
  180. """The module can return a modified version of the event"""
  181. # first patch the event checker so that it will modify the event
  182. async def check(ev: EventBase, state):
  183. d = ev.get_dict()
  184. d["content"] = {"x": "y"}
  185. return True, d
  186. self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
  187. # now send the event
  188. channel = self.make_request(
  189. "PUT",
  190. "/_matrix/client/r0/rooms/%s/send/modifyme/1" % self.room_id,
  191. {"x": "x"},
  192. access_token=self.tok,
  193. )
  194. self.assertEqual(channel.result["code"], b"200", channel.result)
  195. event_id = channel.json_body["event_id"]
  196. # ... and check that it got modified
  197. channel = self.make_request(
  198. "GET",
  199. "/_matrix/client/r0/rooms/%s/event/%s" % (self.room_id, event_id),
  200. access_token=self.tok,
  201. )
  202. self.assertEqual(channel.result["code"], b"200", channel.result)
  203. ev = channel.json_body
  204. self.assertEqual(ev["content"]["x"], "y")
  205. def test_message_edit(self):
  206. """Ensure that the module doesn't cause issues with edited messages."""
  207. # first patch the event checker so that it will modify the event
  208. async def check(ev: EventBase, state):
  209. d = ev.get_dict()
  210. d["content"] = {
  211. "msgtype": "m.text",
  212. "body": d["content"]["body"].upper(),
  213. }
  214. return True, d
  215. self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [check]
  216. # Send an event, then edit it.
  217. channel = self.make_request(
  218. "PUT",
  219. "/_matrix/client/r0/rooms/%s/send/modifyme/1" % self.room_id,
  220. {
  221. "msgtype": "m.text",
  222. "body": "Original body",
  223. },
  224. access_token=self.tok,
  225. )
  226. self.assertEqual(channel.result["code"], b"200", channel.result)
  227. orig_event_id = channel.json_body["event_id"]
  228. channel = self.make_request(
  229. "PUT",
  230. "/_matrix/client/r0/rooms/%s/send/m.room.message/2" % self.room_id,
  231. {
  232. "m.new_content": {"msgtype": "m.text", "body": "Edited body"},
  233. "m.relates_to": {
  234. "rel_type": "m.replace",
  235. "event_id": orig_event_id,
  236. },
  237. "msgtype": "m.text",
  238. "body": "Edited body",
  239. },
  240. access_token=self.tok,
  241. )
  242. self.assertEqual(channel.result["code"], b"200", channel.result)
  243. edited_event_id = channel.json_body["event_id"]
  244. # ... and check that they both got modified
  245. channel = self.make_request(
  246. "GET",
  247. "/_matrix/client/r0/rooms/%s/event/%s" % (self.room_id, orig_event_id),
  248. access_token=self.tok,
  249. )
  250. self.assertEqual(channel.result["code"], b"200", channel.result)
  251. ev = channel.json_body
  252. self.assertEqual(ev["content"]["body"], "ORIGINAL BODY")
  253. channel = self.make_request(
  254. "GET",
  255. "/_matrix/client/r0/rooms/%s/event/%s" % (self.room_id, edited_event_id),
  256. access_token=self.tok,
  257. )
  258. self.assertEqual(channel.result["code"], b"200", channel.result)
  259. ev = channel.json_body
  260. self.assertEqual(ev["content"]["body"], "EDITED BODY")
  261. def test_send_event(self):
  262. """Tests that a module can send an event into a room via the module api"""
  263. content = {
  264. "msgtype": "m.text",
  265. "body": "Hello!",
  266. }
  267. event_dict = {
  268. "room_id": self.room_id,
  269. "type": "m.room.message",
  270. "content": content,
  271. "sender": self.user_id,
  272. }
  273. event: EventBase = self.get_success(
  274. self.hs.get_module_api().create_and_send_event_into_room(event_dict)
  275. )
  276. self.assertEquals(event.sender, self.user_id)
  277. self.assertEquals(event.room_id, self.room_id)
  278. self.assertEquals(event.type, "m.room.message")
  279. self.assertEquals(event.content, content)
  280. @unittest.override_config(
  281. {
  282. "third_party_event_rules": {
  283. "module": __name__ + ".LegacyChangeEvents",
  284. "config": {},
  285. }
  286. }
  287. )
  288. def test_legacy_check_event_allowed(self):
  289. """Tests that the wrapper for legacy check_event_allowed callbacks works
  290. correctly.
  291. """
  292. channel = self.make_request(
  293. "PUT",
  294. "/_matrix/client/r0/rooms/%s/send/m.room.message/1" % self.room_id,
  295. {
  296. "msgtype": "m.text",
  297. "body": "Original body",
  298. },
  299. access_token=self.tok,
  300. )
  301. self.assertEqual(channel.result["code"], b"200", channel.result)
  302. event_id = channel.json_body["event_id"]
  303. channel = self.make_request(
  304. "GET",
  305. "/_matrix/client/r0/rooms/%s/event/%s" % (self.room_id, event_id),
  306. access_token=self.tok,
  307. )
  308. self.assertEqual(channel.result["code"], b"200", channel.result)
  309. self.assertIn("foo", channel.json_body["content"].keys())
  310. self.assertEqual(channel.json_body["content"]["foo"], "bar")
  311. @unittest.override_config(
  312. {
  313. "third_party_event_rules": {
  314. "module": __name__ + ".LegacyDenyNewRooms",
  315. "config": {},
  316. }
  317. }
  318. )
  319. def test_legacy_on_create_room(self):
  320. """Tests that the wrapper for legacy on_create_room callbacks works
  321. correctly.
  322. """
  323. self.helper.create_room_as(self.user_id, tok=self.tok, expect_code=403)
  324. def test_sent_event_end_up_in_room_state(self):
  325. """Tests that a state event sent by a module while processing another state event
  326. doesn't get dropped from the state of the room. This is to guard against a bug
  327. where Synapse has been observed doing so, see https://github.com/matrix-org/synapse/issues/10830
  328. """
  329. event_type = "org.matrix.test_state"
  330. # This content will be updated later on, and since we actually use a reference on
  331. # the dict it does the right thing. It's a bit hacky but a handy way of making
  332. # sure the state actually gets updated.
  333. event_content = {"i": -1}
  334. api = self.hs.get_module_api()
  335. # Define a callback that sends a custom event on power levels update.
  336. async def test_fn(event: EventBase, state_events):
  337. if event.is_state and event.type == EventTypes.PowerLevels:
  338. await api.create_and_send_event_into_room(
  339. {
  340. "room_id": event.room_id,
  341. "sender": event.sender,
  342. "type": event_type,
  343. "content": event_content,
  344. "state_key": "",
  345. }
  346. )
  347. return True, None
  348. self.hs.get_third_party_event_rules()._check_event_allowed_callbacks = [test_fn]
  349. # Sometimes the bug might not happen the first time the event type is added
  350. # to the state but might happen when an event updates the state of the room for
  351. # that type, so we test updating the state several times.
  352. for i in range(5):
  353. # Update the content of the custom state event to be sent by the callback.
  354. event_content["i"] = i
  355. # Update the room's power levels with a different value each time so Synapse
  356. # doesn't consider an update redundant.
  357. self._update_power_levels(event_default=i)
  358. # Check that the new event made it to the room's state.
  359. channel = self.make_request(
  360. method="GET",
  361. path="/rooms/" + self.room_id + "/state/" + event_type,
  362. access_token=self.tok,
  363. )
  364. self.assertEqual(channel.code, 200)
  365. self.assertEqual(channel.json_body["i"], i)
  366. def test_on_new_event(self):
  367. """Test that the on_new_event callback is called on new events"""
  368. on_new_event = Mock(make_awaitable(None))
  369. self.hs.get_third_party_event_rules()._on_new_event_callbacks.append(
  370. on_new_event
  371. )
  372. # Send a message event to the room and check that the callback is called.
  373. self.helper.send(room_id=self.room_id, tok=self.tok)
  374. self.assertEqual(on_new_event.call_count, 1)
  375. # Check that the callback is also called on membership updates.
  376. self.helper.invite(
  377. room=self.room_id,
  378. src=self.user_id,
  379. targ=self.invitee,
  380. tok=self.tok,
  381. )
  382. self.assertEqual(on_new_event.call_count, 2)
  383. args, _ = on_new_event.call_args
  384. self.assertEqual(args[0].membership, Membership.INVITE)
  385. self.assertEqual(args[0].state_key, self.invitee)
  386. # Check that the invitee's membership is correct in the state that's passed down
  387. # to the callback.
  388. self.assertEqual(
  389. args[1][(EventTypes.Member, self.invitee)].membership,
  390. Membership.INVITE,
  391. )
  392. # Send an event over federation and check that the callback is also called.
  393. self._send_event_over_federation()
  394. self.assertEqual(on_new_event.call_count, 3)
  395. def _send_event_over_federation(self) -> None:
  396. """Send a dummy event over federation and check that the request succeeds."""
  397. body = {
  398. "origin": self.hs.config.server.server_name,
  399. "origin_server_ts": self.clock.time_msec(),
  400. "pdus": [
  401. {
  402. "sender": self.user_id,
  403. "type": EventTypes.Message,
  404. "state_key": "",
  405. "content": {"body": "hello world", "msgtype": "m.text"},
  406. "room_id": self.room_id,
  407. "depth": 0,
  408. "origin_server_ts": self.clock.time_msec(),
  409. "prev_events": [],
  410. "auth_events": [],
  411. "signatures": {},
  412. "unsigned": {},
  413. }
  414. ],
  415. }
  416. channel = self.make_request(
  417. method="PUT",
  418. path="/_matrix/federation/v1/send/1",
  419. content=body,
  420. federation_auth_origin=self.hs.config.server.server_name.encode("utf8"),
  421. )
  422. self.assertEqual(channel.code, 200, channel.result)
  423. def _update_power_levels(self, event_default: int = 0):
  424. """Updates the room's power levels.
  425. Args:
  426. event_default: Value to use for 'events_default'.
  427. """
  428. self.helper.send_state(
  429. room_id=self.room_id,
  430. event_type=EventTypes.PowerLevels,
  431. body={
  432. "ban": 50,
  433. "events": {
  434. "m.room.avatar": 50,
  435. "m.room.canonical_alias": 50,
  436. "m.room.encryption": 100,
  437. "m.room.history_visibility": 100,
  438. "m.room.name": 50,
  439. "m.room.power_levels": 100,
  440. "m.room.server_acl": 100,
  441. "m.room.tombstone": 100,
  442. },
  443. "events_default": event_default,
  444. "invite": 0,
  445. "kick": 50,
  446. "redact": 50,
  447. "state_default": 50,
  448. "users": {self.user_id: 100},
  449. "users_default": 0,
  450. },
  451. tok=self.tok,
  452. )