1
0

test_federation.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. # Copyright 2020 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. from typing import Collection, List, Optional, Union
  15. from unittest.mock import Mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.api.errors import FederationError
  18. from synapse.api.room_versions import RoomVersion, RoomVersions
  19. from synapse.events import EventBase, make_event_from_dict
  20. from synapse.events.snapshot import EventContext
  21. from synapse.federation.federation_base import event_from_pdu_json
  22. from synapse.handlers.device import DeviceListUpdater
  23. from synapse.http.types import QueryParams
  24. from synapse.logging.context import LoggingContext
  25. from synapse.server import HomeServer
  26. from synapse.types import JsonDict, UserID, create_requester
  27. from synapse.util import Clock
  28. from synapse.util.retryutils import NotRetryingDestination
  29. from tests import unittest
  30. from tests.test_utils import make_awaitable
  31. class MessageAcceptTests(unittest.HomeserverTestCase):
  32. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  33. self.http_client = Mock()
  34. return self.setup_test_homeserver(federation_http_client=self.http_client)
  35. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  36. user_id = UserID("us", "test")
  37. our_user = create_requester(user_id)
  38. room_creator = self.hs.get_room_creation_handler()
  39. self.room_id = self.get_success(
  40. room_creator.create_room(
  41. our_user, room_creator._presets_dict["public_chat"], ratelimit=False
  42. )
  43. )[0]
  44. self.store = self.hs.get_datastores().main
  45. # Figure out what the most recent event is
  46. most_recent = self.get_success(
  47. self.hs.get_datastores().main.get_latest_event_ids_in_room(self.room_id)
  48. )[0]
  49. join_event = make_event_from_dict(
  50. {
  51. "room_id": self.room_id,
  52. "sender": "@baduser:test.serv",
  53. "state_key": "@baduser:test.serv",
  54. "event_id": "$join:test.serv",
  55. "depth": 1000,
  56. "origin_server_ts": 1,
  57. "type": "m.room.member",
  58. "origin": "test.servx",
  59. "content": {"membership": "join"},
  60. "auth_events": [],
  61. "prev_state": [(most_recent, {})],
  62. "prev_events": [(most_recent, {})],
  63. }
  64. )
  65. self.handler = self.hs.get_federation_handler()
  66. federation_event_handler = self.hs.get_federation_event_handler()
  67. async def _check_event_auth(
  68. origin: Optional[str], event: EventBase, context: EventContext
  69. ) -> None:
  70. pass
  71. federation_event_handler._check_event_auth = _check_event_auth # type: ignore[assignment]
  72. self.client = self.hs.get_federation_client()
  73. async def _check_sigs_and_hash_for_pulled_events_and_fetch(
  74. dest: str, pdus: Collection[EventBase], room_version: RoomVersion
  75. ) -> List[EventBase]:
  76. return list(pdus)
  77. self.client._check_sigs_and_hash_for_pulled_events_and_fetch = _check_sigs_and_hash_for_pulled_events_and_fetch # type: ignore[assignment]
  78. # Send the join, it should return None (which is not an error)
  79. self.assertEqual(
  80. self.get_success(
  81. federation_event_handler.on_receive_pdu("test.serv", join_event)
  82. ),
  83. None,
  84. )
  85. # Make sure we actually joined the room
  86. self.assertEqual(
  87. self.get_success(self.store.get_latest_event_ids_in_room(self.room_id))[0],
  88. "$join:test.serv",
  89. )
  90. def test_cant_hide_direct_ancestors(self) -> None:
  91. """
  92. If you send a message, you must be able to provide the direct
  93. prev_events that said event references.
  94. """
  95. async def post_json(
  96. destination: str,
  97. path: str,
  98. data: Optional[JsonDict] = None,
  99. long_retries: bool = False,
  100. timeout: Optional[int] = None,
  101. ignore_backoff: bool = False,
  102. args: Optional[QueryParams] = None,
  103. ) -> Union[JsonDict, list]:
  104. # If it asks us for new missing events, give them NOTHING
  105. if path.startswith("/_matrix/federation/v1/get_missing_events/"):
  106. return {"events": []}
  107. return {}
  108. self.http_client.post_json = post_json
  109. # Figure out what the most recent event is
  110. most_recent = self.get_success(
  111. self.store.get_latest_event_ids_in_room(self.room_id)
  112. )[0]
  113. # Now lie about an event
  114. lying_event = make_event_from_dict(
  115. {
  116. "room_id": self.room_id,
  117. "sender": "@baduser:test.serv",
  118. "event_id": "one:test.serv",
  119. "depth": 1000,
  120. "origin_server_ts": 1,
  121. "type": "m.room.message",
  122. "origin": "test.serv",
  123. "content": {"body": "hewwo?"},
  124. "auth_events": [],
  125. "prev_events": [("two:test.serv", {}), (most_recent, {})],
  126. }
  127. )
  128. federation_event_handler = self.hs.get_federation_event_handler()
  129. with LoggingContext("test-context"):
  130. failure = self.get_failure(
  131. federation_event_handler.on_receive_pdu("test.serv", lying_event),
  132. FederationError,
  133. )
  134. # on_receive_pdu should throw an error
  135. self.assertEqual(
  136. failure.value.args[0],
  137. (
  138. "ERROR 403: Your server isn't divulging details about prev_events "
  139. "referenced in this event."
  140. ),
  141. )
  142. # Make sure the invalid event isn't there
  143. extrem = self.get_success(self.store.get_latest_event_ids_in_room(self.room_id))
  144. self.assertEqual(extrem[0], "$join:test.serv")
  145. def test_retry_device_list_resync(self) -> None:
  146. """Tests that device lists are marked as stale if they couldn't be synced, and
  147. that stale device lists are retried periodically.
  148. """
  149. remote_user_id = "@john:test_remote"
  150. remote_origin = "test_remote"
  151. # Track the number of attempts to resync the user's device list.
  152. self.resync_attempts = 0
  153. # When this function is called, increment the number of resync attempts (only if
  154. # we're querying devices for the right user ID), then raise a
  155. # NotRetryingDestination error to fail the resync gracefully.
  156. def query_user_devices(
  157. destination: str, user_id: str, timeout: int = 30000
  158. ) -> JsonDict:
  159. if user_id == remote_user_id:
  160. self.resync_attempts += 1
  161. raise NotRetryingDestination(0, 0, destination)
  162. # Register the mock on the federation client.
  163. federation_client = self.hs.get_federation_client()
  164. federation_client.query_user_devices = Mock(side_effect=query_user_devices) # type: ignore[assignment]
  165. # Register a mock on the store so that the incoming update doesn't fail because
  166. # we don't share a room with the user.
  167. store = self.hs.get_datastores().main
  168. store.get_rooms_for_user = Mock(return_value=make_awaitable(["!someroom:test"]))
  169. # Manually inject a fake device list update. We need this update to include at
  170. # least one prev_id so that the user's device list will need to be retried.
  171. device_list_updater = self.hs.get_device_handler().device_list_updater
  172. assert isinstance(device_list_updater, DeviceListUpdater)
  173. self.get_success(
  174. device_list_updater.incoming_device_list_update(
  175. origin=remote_origin,
  176. edu_content={
  177. "deleted": False,
  178. "device_display_name": "Mobile",
  179. "device_id": "QBUAZIFURK",
  180. "prev_id": [5],
  181. "stream_id": 6,
  182. "user_id": remote_user_id,
  183. },
  184. )
  185. )
  186. # Check that there was one resync attempt.
  187. self.assertEqual(self.resync_attempts, 1)
  188. # Check that the resync attempt failed and caused the user's device list to be
  189. # marked as stale.
  190. need_resync = self.get_success(
  191. store.get_user_ids_requiring_device_list_resync()
  192. )
  193. self.assertIn(remote_user_id, need_resync)
  194. # Check that waiting for 30 seconds caused Synapse to retry resyncing the device
  195. # list.
  196. self.reactor.advance(30)
  197. self.assertEqual(self.resync_attempts, 2)
  198. def test_cross_signing_keys_retry(self) -> None:
  199. """Tests that resyncing a device list correctly processes cross-signing keys from
  200. the remote server.
  201. """
  202. remote_user_id = "@john:test_remote"
  203. remote_master_key = "85T7JXPFBAySB/jwby4S3lBPTqY3+Zg53nYuGmu1ggY"
  204. remote_self_signing_key = "QeIiFEjluPBtI7WQdG365QKZcFs9kqmHir6RBD0//nQ"
  205. # Register mock device list retrieval on the federation client.
  206. federation_client = self.hs.get_federation_client()
  207. federation_client.query_user_devices = Mock( # type: ignore[assignment]
  208. return_value=make_awaitable(
  209. {
  210. "user_id": remote_user_id,
  211. "stream_id": 1,
  212. "devices": [],
  213. "master_key": {
  214. "user_id": remote_user_id,
  215. "usage": ["master"],
  216. "keys": {"ed25519:" + remote_master_key: remote_master_key},
  217. },
  218. "self_signing_key": {
  219. "user_id": remote_user_id,
  220. "usage": ["self_signing"],
  221. "keys": {
  222. "ed25519:"
  223. + remote_self_signing_key: remote_self_signing_key
  224. },
  225. },
  226. }
  227. )
  228. )
  229. # Resync the device list.
  230. device_handler = self.hs.get_device_handler()
  231. self.get_success(
  232. device_handler.device_list_updater.multi_user_device_resync(
  233. [remote_user_id]
  234. ),
  235. )
  236. # Retrieve the cross-signing keys for this user.
  237. keys = self.get_success(
  238. self.store.get_e2e_cross_signing_keys_bulk(user_ids=[remote_user_id]),
  239. )
  240. self.assertIn(remote_user_id, keys)
  241. key = keys[remote_user_id]
  242. assert key is not None
  243. # Check that the master key is the one returned by the mock.
  244. master_key = key["master"]
  245. self.assertEqual(len(master_key["keys"]), 1)
  246. self.assertTrue("ed25519:" + remote_master_key in master_key["keys"].keys())
  247. self.assertTrue(remote_master_key in master_key["keys"].values())
  248. # Check that the self-signing key is the one returned by the mock.
  249. self_signing_key = key["self_signing"]
  250. self.assertEqual(len(self_signing_key["keys"]), 1)
  251. self.assertTrue(
  252. "ed25519:" + remote_self_signing_key in self_signing_key["keys"].keys(),
  253. )
  254. self.assertTrue(remote_self_signing_key in self_signing_key["keys"].values())
  255. class StripUnsignedFromEventsTestCase(unittest.TestCase):
  256. def test_strip_unauthorized_unsigned_values(self) -> None:
  257. event1 = {
  258. "sender": "@baduser:test.serv",
  259. "state_key": "@baduser:test.serv",
  260. "event_id": "$event1:test.serv",
  261. "depth": 1000,
  262. "origin_server_ts": 1,
  263. "type": "m.room.member",
  264. "origin": "test.servx",
  265. "content": {"membership": "join"},
  266. "auth_events": [],
  267. "unsigned": {"malicious garbage": "hackz", "more warez": "more hackz"},
  268. }
  269. filtered_event = event_from_pdu_json(event1, RoomVersions.V1)
  270. # Make sure unauthorized fields are stripped from unsigned
  271. self.assertNotIn("more warez", filtered_event.unsigned)
  272. def test_strip_event_maintains_allowed_fields(self) -> None:
  273. event2 = {
  274. "sender": "@baduser:test.serv",
  275. "state_key": "@baduser:test.serv",
  276. "event_id": "$event2:test.serv",
  277. "depth": 1000,
  278. "origin_server_ts": 1,
  279. "type": "m.room.member",
  280. "origin": "test.servx",
  281. "auth_events": [],
  282. "content": {"membership": "join"},
  283. "unsigned": {
  284. "malicious garbage": "hackz",
  285. "more warez": "more hackz",
  286. "age": 14,
  287. "invite_room_state": [],
  288. },
  289. }
  290. filtered_event2 = event_from_pdu_json(event2, RoomVersions.V1)
  291. self.assertIn("age", filtered_event2.unsigned)
  292. self.assertEqual(14, filtered_event2.unsigned["age"])
  293. self.assertNotIn("more warez", filtered_event2.unsigned)
  294. # Invite_room_state is allowed in events of type m.room.member
  295. self.assertIn("invite_room_state", filtered_event2.unsigned)
  296. self.assertEqual([], filtered_event2.unsigned["invite_room_state"])
  297. def test_strip_event_removes_fields_based_on_event_type(self) -> None:
  298. event3 = {
  299. "sender": "@baduser:test.serv",
  300. "state_key": "@baduser:test.serv",
  301. "event_id": "$event3:test.serv",
  302. "depth": 1000,
  303. "origin_server_ts": 1,
  304. "type": "m.room.power_levels",
  305. "origin": "test.servx",
  306. "content": {},
  307. "auth_events": [],
  308. "unsigned": {
  309. "malicious garbage": "hackz",
  310. "more warez": "more hackz",
  311. "age": 14,
  312. "invite_room_state": [],
  313. },
  314. }
  315. filtered_event3 = event_from_pdu_json(event3, RoomVersions.V1)
  316. self.assertIn("age", filtered_event3.unsigned)
  317. # Invite_room_state field is only permitted in event type m.room.member
  318. self.assertNotIn("invite_room_state", filtered_event3.unsigned)
  319. self.assertNotIn("more warez", filtered_event3.unsigned)