test_federation.py 10 KB

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