test_federation.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from mock import Mock
  16. from twisted.internet.defer import ensureDeferred, maybeDeferred, succeed
  17. from synapse.events import make_event_from_dict
  18. from synapse.logging.context import LoggingContext
  19. from synapse.types import Requester, UserID
  20. from synapse.util import Clock
  21. from synapse.util.retryutils import NotRetryingDestination
  22. from tests import unittest
  23. from tests.server import ThreadedMemoryReactorClock, setup_test_homeserver
  24. from tests.test_utils import make_awaitable
  25. class MessageAcceptTests(unittest.HomeserverTestCase):
  26. def setUp(self):
  27. self.http_client = Mock()
  28. self.reactor = ThreadedMemoryReactorClock()
  29. self.hs_clock = Clock(self.reactor)
  30. self.homeserver = setup_test_homeserver(
  31. self.addCleanup,
  32. http_client=self.http_client,
  33. clock=self.hs_clock,
  34. reactor=self.reactor,
  35. )
  36. user_id = UserID("us", "test")
  37. our_user = Requester(user_id, None, False, None, None)
  38. room_creator = self.homeserver.get_room_creation_handler()
  39. room_deferred = ensureDeferred(
  40. room_creator.create_room(
  41. our_user, room_creator._presets_dict["public_chat"], ratelimit=False
  42. )
  43. )
  44. self.reactor.advance(0.1)
  45. self.room_id = self.successResultOf(room_deferred)[0]["room_id"]
  46. self.store = self.homeserver.get_datastore()
  47. # Figure out what the most recent event is
  48. most_recent = self.successResultOf(
  49. maybeDeferred(
  50. self.homeserver.get_datastore().get_latest_event_ids_in_room,
  51. self.room_id,
  52. )
  53. )[0]
  54. join_event = make_event_from_dict(
  55. {
  56. "room_id": self.room_id,
  57. "sender": "@baduser:test.serv",
  58. "state_key": "@baduser:test.serv",
  59. "event_id": "$join:test.serv",
  60. "depth": 1000,
  61. "origin_server_ts": 1,
  62. "type": "m.room.member",
  63. "origin": "test.servx",
  64. "content": {"membership": "join"},
  65. "auth_events": [],
  66. "prev_state": [(most_recent, {})],
  67. "prev_events": [(most_recent, {})],
  68. }
  69. )
  70. self.handler = self.homeserver.get_handlers().federation_handler
  71. self.handler.do_auth = lambda origin, event, context, auth_events: succeed(
  72. context
  73. )
  74. self.client = self.homeserver.get_federation_client()
  75. self.client._check_sigs_and_hash_and_fetch = lambda dest, pdus, **k: succeed(
  76. pdus
  77. )
  78. # Send the join, it should return None (which is not an error)
  79. d = ensureDeferred(
  80. self.handler.on_receive_pdu(
  81. "test.serv", join_event, sent_to_us_directly=True
  82. )
  83. )
  84. self.reactor.advance(1)
  85. self.assertEqual(self.successResultOf(d), None)
  86. # Make sure we actually joined the room
  87. self.assertEqual(
  88. self.successResultOf(
  89. maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  90. )[0],
  91. "$join:test.serv",
  92. )
  93. def test_cant_hide_direct_ancestors(self):
  94. """
  95. If you send a message, you must be able to provide the direct
  96. prev_events that said event references.
  97. """
  98. async def post_json(destination, path, data, headers=None, timeout=0):
  99. # If it asks us for new missing events, give them NOTHING
  100. if path.startswith("/_matrix/federation/v1/get_missing_events/"):
  101. return {"events": []}
  102. self.http_client.post_json = post_json
  103. # Figure out what the most recent event is
  104. most_recent = self.successResultOf(
  105. maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  106. )[0]
  107. # Now lie about an event
  108. lying_event = make_event_from_dict(
  109. {
  110. "room_id": self.room_id,
  111. "sender": "@baduser:test.serv",
  112. "event_id": "one:test.serv",
  113. "depth": 1000,
  114. "origin_server_ts": 1,
  115. "type": "m.room.message",
  116. "origin": "test.serv",
  117. "content": {"body": "hewwo?"},
  118. "auth_events": [],
  119. "prev_events": [("two:test.serv", {}), (most_recent, {})],
  120. }
  121. )
  122. with LoggingContext(request="lying_event"):
  123. d = ensureDeferred(
  124. self.handler.on_receive_pdu(
  125. "test.serv", lying_event, sent_to_us_directly=True
  126. )
  127. )
  128. # Step the reactor, so the database fetches come back
  129. self.reactor.advance(1)
  130. # on_receive_pdu should throw an error
  131. failure = self.failureResultOf(d)
  132. self.assertEqual(
  133. failure.value.args[0],
  134. (
  135. "ERROR 403: Your server isn't divulging details about prev_events "
  136. "referenced in this event."
  137. ),
  138. )
  139. # Make sure the invalid event isn't there
  140. extrem = maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  141. self.assertEqual(self.successResultOf(extrem)[0], "$join:test.serv")
  142. def test_retry_device_list_resync(self):
  143. """Tests that device lists are marked as stale if they couldn't be synced, and
  144. that stale device lists are retried periodically.
  145. """
  146. remote_user_id = "@john:test_remote"
  147. remote_origin = "test_remote"
  148. # Track the number of attempts to resync the user's device list.
  149. self.resync_attempts = 0
  150. # When this function is called, increment the number of resync attempts (only if
  151. # we're querying devices for the right user ID), then raise a
  152. # NotRetryingDestination error to fail the resync gracefully.
  153. def query_user_devices(destination, user_id):
  154. if user_id == remote_user_id:
  155. self.resync_attempts += 1
  156. raise NotRetryingDestination(0, 0, destination)
  157. # Register the mock on the federation client.
  158. federation_client = self.homeserver.get_federation_client()
  159. federation_client.query_user_devices = Mock(side_effect=query_user_devices)
  160. # Register a mock on the store so that the incoming update doesn't fail because
  161. # we don't share a room with the user.
  162. store = self.homeserver.get_datastore()
  163. store.get_rooms_for_user = Mock(return_value=make_awaitable(["!someroom:test"]))
  164. # Manually inject a fake device list update. We need this update to include at
  165. # least one prev_id so that the user's device list will need to be retried.
  166. device_list_updater = self.homeserver.get_device_handler().device_list_updater
  167. self.get_success(
  168. device_list_updater.incoming_device_list_update(
  169. origin=remote_origin,
  170. edu_content={
  171. "deleted": False,
  172. "device_display_name": "Mobile",
  173. "device_id": "QBUAZIFURK",
  174. "prev_id": [5],
  175. "stream_id": 6,
  176. "user_id": remote_user_id,
  177. },
  178. )
  179. )
  180. # Check that there was one resync attempt.
  181. self.assertEqual(self.resync_attempts, 1)
  182. # Check that the resync attempt failed and caused the user's device list to be
  183. # marked as stale.
  184. need_resync = self.get_success(
  185. store.get_user_ids_requiring_device_list_resync()
  186. )
  187. self.assertIn(remote_user_id, need_resync)
  188. # Check that waiting for 30 seconds caused Synapse to retry resyncing the device
  189. # list.
  190. self.reactor.advance(30)
  191. self.assertEqual(self.resync_attempts, 2)
  192. def test_cross_signing_keys_retry(self):
  193. """Tests that resyncing a device list correctly processes cross-signing keys from
  194. the remote server.
  195. """
  196. remote_user_id = "@john:test_remote"
  197. remote_master_key = "85T7JXPFBAySB/jwby4S3lBPTqY3+Zg53nYuGmu1ggY"
  198. remote_self_signing_key = "QeIiFEjluPBtI7WQdG365QKZcFs9kqmHir6RBD0//nQ"
  199. # Register mock device list retrieval on the federation client.
  200. federation_client = self.homeserver.get_federation_client()
  201. federation_client.query_user_devices = Mock(
  202. return_value=succeed(
  203. {
  204. "user_id": remote_user_id,
  205. "stream_id": 1,
  206. "devices": [],
  207. "master_key": {
  208. "user_id": remote_user_id,
  209. "usage": ["master"],
  210. "keys": {"ed25519:" + remote_master_key: remote_master_key},
  211. },
  212. "self_signing_key": {
  213. "user_id": remote_user_id,
  214. "usage": ["self_signing"],
  215. "keys": {
  216. "ed25519:"
  217. + remote_self_signing_key: remote_self_signing_key
  218. },
  219. },
  220. }
  221. )
  222. )
  223. # Resync the device list.
  224. device_handler = self.homeserver.get_device_handler()
  225. self.get_success(
  226. device_handler.device_list_updater.user_device_resync(remote_user_id),
  227. )
  228. # Retrieve the cross-signing keys for this user.
  229. keys = self.get_success(
  230. self.store.get_e2e_cross_signing_keys_bulk(user_ids=[remote_user_id]),
  231. )
  232. self.assertTrue(remote_user_id in keys)
  233. # Check that the master key is the one returned by the mock.
  234. master_key = keys[remote_user_id]["master"]
  235. self.assertEqual(len(master_key["keys"]), 1)
  236. self.assertTrue("ed25519:" + remote_master_key in master_key["keys"].keys())
  237. self.assertTrue(remote_master_key in master_key["keys"].values())
  238. # Check that the self-signing key is the one returned by the mock.
  239. self_signing_key = keys[remote_user_id]["self_signing"]
  240. self.assertEqual(len(self_signing_key["keys"]), 1)
  241. self.assertTrue(
  242. "ed25519:" + remote_self_signing_key in self_signing_key["keys"].keys(),
  243. )
  244. self.assertTrue(remote_self_signing_key in self_signing_key["keys"].values())