1
0

test_federation.py 11 KB

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