test_federation_sender.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. # Copyright 2019 New Vector Ltd
  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 Optional
  15. from unittest.mock import Mock
  16. from signedjson import key, sign
  17. from signedjson.types import BaseKey, SigningKey
  18. from twisted.internet import defer
  19. from synapse.api.constants import EduTypes, RoomEncryptionAlgorithms
  20. from synapse.rest import admin
  21. from synapse.rest.client import login
  22. from synapse.types import JsonDict, ReadReceipt
  23. from tests.test_utils import make_awaitable
  24. from tests.unittest import HomeserverTestCase, override_config
  25. class FederationSenderReceiptsTestCases(HomeserverTestCase):
  26. def make_homeserver(self, reactor, clock):
  27. hs = self.setup_test_homeserver(
  28. federation_transport_client=Mock(spec=["send_transaction"]),
  29. )
  30. hs.get_storage_controllers().state.get_current_hosts_in_room = Mock(
  31. return_value=make_awaitable({"test", "host2"})
  32. )
  33. return hs
  34. @override_config({"send_federation": True})
  35. def test_send_receipts(self):
  36. mock_send_transaction = (
  37. self.hs.get_federation_transport_client().send_transaction
  38. )
  39. mock_send_transaction.return_value = make_awaitable({})
  40. sender = self.hs.get_federation_sender()
  41. receipt = ReadReceipt(
  42. "room_id", "m.read", "user_id", ["event_id"], {"ts": 1234}
  43. )
  44. self.successResultOf(defer.ensureDeferred(sender.send_read_receipt(receipt)))
  45. self.pump()
  46. # expect a call to send_transaction
  47. mock_send_transaction.assert_called_once()
  48. json_cb = mock_send_transaction.call_args[0][1]
  49. data = json_cb()
  50. self.assertEqual(
  51. data["edus"],
  52. [
  53. {
  54. "edu_type": EduTypes.RECEIPT,
  55. "content": {
  56. "room_id": {
  57. "m.read": {
  58. "user_id": {
  59. "event_ids": ["event_id"],
  60. "data": {"ts": 1234},
  61. }
  62. }
  63. }
  64. },
  65. }
  66. ],
  67. )
  68. @override_config({"send_federation": True})
  69. def test_send_receipts_with_backoff(self):
  70. """Send two receipts in quick succession; the second should be flushed, but
  71. only after 20ms"""
  72. mock_send_transaction = (
  73. self.hs.get_federation_transport_client().send_transaction
  74. )
  75. mock_send_transaction.return_value = make_awaitable({})
  76. sender = self.hs.get_federation_sender()
  77. receipt = ReadReceipt(
  78. "room_id", "m.read", "user_id", ["event_id"], {"ts": 1234}
  79. )
  80. self.successResultOf(defer.ensureDeferred(sender.send_read_receipt(receipt)))
  81. self.pump()
  82. # expect a call to send_transaction
  83. mock_send_transaction.assert_called_once()
  84. json_cb = mock_send_transaction.call_args[0][1]
  85. data = json_cb()
  86. self.assertEqual(
  87. data["edus"],
  88. [
  89. {
  90. "edu_type": EduTypes.RECEIPT,
  91. "content": {
  92. "room_id": {
  93. "m.read": {
  94. "user_id": {
  95. "event_ids": ["event_id"],
  96. "data": {"ts": 1234},
  97. }
  98. }
  99. }
  100. },
  101. }
  102. ],
  103. )
  104. mock_send_transaction.reset_mock()
  105. # send the second RR
  106. receipt = ReadReceipt(
  107. "room_id", "m.read", "user_id", ["other_id"], {"ts": 1234}
  108. )
  109. self.successResultOf(defer.ensureDeferred(sender.send_read_receipt(receipt)))
  110. self.pump()
  111. mock_send_transaction.assert_not_called()
  112. self.reactor.advance(19)
  113. mock_send_transaction.assert_not_called()
  114. self.reactor.advance(10)
  115. mock_send_transaction.assert_called_once()
  116. json_cb = mock_send_transaction.call_args[0][1]
  117. data = json_cb()
  118. self.assertEqual(
  119. data["edus"],
  120. [
  121. {
  122. "edu_type": EduTypes.RECEIPT,
  123. "content": {
  124. "room_id": {
  125. "m.read": {
  126. "user_id": {
  127. "event_ids": ["other_id"],
  128. "data": {"ts": 1234},
  129. }
  130. }
  131. }
  132. },
  133. }
  134. ],
  135. )
  136. class FederationSenderDevicesTestCases(HomeserverTestCase):
  137. servlets = [
  138. admin.register_servlets,
  139. login.register_servlets,
  140. ]
  141. def make_homeserver(self, reactor, clock):
  142. return self.setup_test_homeserver(
  143. federation_transport_client=Mock(
  144. spec=["send_transaction", "query_user_devices"]
  145. ),
  146. )
  147. def default_config(self):
  148. c = super().default_config()
  149. c["send_federation"] = True
  150. return c
  151. def prepare(self, reactor, clock, hs):
  152. # stub out `get_rooms_for_user` and `get_users_in_room` so that the
  153. # server thinks the user shares a room with `@user2:host2`
  154. def get_rooms_for_user(user_id):
  155. return defer.succeed({"!room:host1"})
  156. hs.get_datastores().main.get_rooms_for_user = get_rooms_for_user
  157. def get_users_in_room(room_id):
  158. return defer.succeed({"@user2:host2"})
  159. hs.get_datastores().main.get_users_in_room = get_users_in_room
  160. # whenever send_transaction is called, record the edu data
  161. self.edus = []
  162. self.hs.get_federation_transport_client().send_transaction.side_effect = (
  163. self.record_transaction
  164. )
  165. def record_transaction(self, txn, json_cb):
  166. data = json_cb()
  167. self.edus.extend(data["edus"])
  168. return defer.succeed({})
  169. def test_send_device_updates(self):
  170. """Basic case: each device update should result in an EDU"""
  171. # create a device
  172. u1 = self.register_user("user", "pass")
  173. self.login(u1, "pass", device_id="D1")
  174. # expect one edu
  175. self.assertEqual(len(self.edus), 1)
  176. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D1", None)
  177. # We queue up device list updates to be sent over federation, so we
  178. # advance to clear the queue.
  179. self.reactor.advance(1)
  180. # a second call should produce no new device EDUs
  181. self.hs.get_federation_sender().send_device_messages("host2")
  182. self.assertEqual(self.edus, [])
  183. # a second device
  184. self.login("user", "pass", device_id="D2")
  185. self.assertEqual(len(self.edus), 1)
  186. self.check_device_update_edu(self.edus.pop(0), u1, "D2", stream_id)
  187. def test_dont_send_device_updates_for_remote_users(self):
  188. """Check that we don't send device updates for remote users"""
  189. # Send the server a device list EDU for the other user, this will cause
  190. # it to try and resync the device lists.
  191. self.hs.get_federation_transport_client().query_user_devices.return_value = (
  192. make_awaitable(
  193. {
  194. "stream_id": "1",
  195. "user_id": "@user2:host2",
  196. "devices": [{"device_id": "D1"}],
  197. }
  198. )
  199. )
  200. self.get_success(
  201. self.hs.get_device_handler().device_list_updater.incoming_device_list_update(
  202. "host2",
  203. {
  204. "user_id": "@user2:host2",
  205. "device_id": "D1",
  206. "stream_id": "1",
  207. "prev_ids": [],
  208. },
  209. )
  210. )
  211. self.reactor.advance(1)
  212. # We shouldn't see an EDU for that update
  213. self.assertEqual(self.edus, [])
  214. # Check that we did successfully process the inbound EDU (otherwise this
  215. # test would pass if we failed to process the EDU)
  216. devices = self.get_success(
  217. self.hs.get_datastores().main.get_cached_devices_for_user("@user2:host2")
  218. )
  219. self.assertIn("D1", devices)
  220. def test_upload_signatures(self):
  221. """Uploading signatures on some devices should produce updates for that user"""
  222. e2e_handler = self.hs.get_e2e_keys_handler()
  223. # register two devices
  224. u1 = self.register_user("user", "pass")
  225. self.login(u1, "pass", device_id="D1")
  226. self.login(u1, "pass", device_id="D2")
  227. # expect two edus
  228. self.assertEqual(len(self.edus), 2)
  229. stream_id = None
  230. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D1", stream_id)
  231. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D2", stream_id)
  232. # upload signing keys for each device
  233. device1_signing_key = self.generate_and_upload_device_signing_key(u1, "D1")
  234. device2_signing_key = self.generate_and_upload_device_signing_key(u1, "D2")
  235. # We queue up device list updates to be sent over federation, so we
  236. # advance to clear the queue.
  237. self.reactor.advance(1)
  238. # expect two more edus
  239. self.assertEqual(len(self.edus), 2)
  240. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D1", stream_id)
  241. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D2", stream_id)
  242. # upload master key and self-signing key
  243. master_signing_key = generate_self_id_key()
  244. master_key = {
  245. "user_id": u1,
  246. "usage": ["master"],
  247. "keys": {key_id(master_signing_key): encode_pubkey(master_signing_key)},
  248. }
  249. # private key: HvQBbU+hc2Zr+JP1sE0XwBe1pfZZEYtJNPJLZJtS+F8
  250. selfsigning_signing_key = generate_self_id_key()
  251. selfsigning_key = {
  252. "user_id": u1,
  253. "usage": ["self_signing"],
  254. "keys": {
  255. key_id(selfsigning_signing_key): encode_pubkey(selfsigning_signing_key)
  256. },
  257. }
  258. sign.sign_json(selfsigning_key, u1, master_signing_key)
  259. cross_signing_keys = {
  260. "master_key": master_key,
  261. "self_signing_key": selfsigning_key,
  262. }
  263. self.get_success(
  264. e2e_handler.upload_signing_keys_for_user(u1, cross_signing_keys)
  265. )
  266. # We queue up device list updates to be sent over federation, so we
  267. # advance to clear the queue.
  268. self.reactor.advance(1)
  269. # expect signing key update edu
  270. self.assertEqual(len(self.edus), 2)
  271. self.assertEqual(self.edus.pop(0)["edu_type"], EduTypes.SIGNING_KEY_UPDATE)
  272. self.assertEqual(
  273. self.edus.pop(0)["edu_type"], EduTypes.UNSTABLE_SIGNING_KEY_UPDATE
  274. )
  275. # sign the devices
  276. d1_json = build_device_dict(u1, "D1", device1_signing_key)
  277. sign.sign_json(d1_json, u1, selfsigning_signing_key)
  278. d2_json = build_device_dict(u1, "D2", device2_signing_key)
  279. sign.sign_json(d2_json, u1, selfsigning_signing_key)
  280. ret = self.get_success(
  281. e2e_handler.upload_signatures_for_device_keys(
  282. u1,
  283. {u1: {"D1": d1_json, "D2": d2_json}},
  284. )
  285. )
  286. self.assertEqual(ret["failures"], {})
  287. # We queue up device list updates to be sent over federation, so we
  288. # advance to clear the queue.
  289. self.reactor.advance(1)
  290. # expect two edus, in one or two transactions. We don't know what order the
  291. # devices will be updated.
  292. self.assertEqual(len(self.edus), 2)
  293. stream_id = None # FIXME: there is a discontinuity in the stream IDs: see #7142
  294. for edu in self.edus:
  295. self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
  296. c = edu["content"]
  297. if stream_id is not None:
  298. self.assertEqual(c["prev_id"], [stream_id])
  299. self.assertGreaterEqual(c["stream_id"], stream_id)
  300. stream_id = c["stream_id"]
  301. devices = {edu["content"]["device_id"] for edu in self.edus}
  302. self.assertEqual({"D1", "D2"}, devices)
  303. def test_delete_devices(self):
  304. """If devices are deleted, that should result in EDUs too"""
  305. # create devices
  306. u1 = self.register_user("user", "pass")
  307. self.login("user", "pass", device_id="D1")
  308. self.login("user", "pass", device_id="D2")
  309. self.login("user", "pass", device_id="D3")
  310. # We queue up device list updates to be sent over federation, so we
  311. # advance to clear the queue.
  312. self.reactor.advance(1)
  313. # expect three edus
  314. self.assertEqual(len(self.edus), 3)
  315. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D1", None)
  316. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D2", stream_id)
  317. stream_id = self.check_device_update_edu(self.edus.pop(0), u1, "D3", stream_id)
  318. # delete them again
  319. self.get_success(
  320. self.hs.get_device_handler().delete_devices(u1, ["D1", "D2", "D3"])
  321. )
  322. # We queue up device list updates to be sent over federation, so we
  323. # advance to clear the queue.
  324. self.reactor.advance(1)
  325. # expect three edus, in an unknown order
  326. self.assertEqual(len(self.edus), 3)
  327. for edu in self.edus:
  328. self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
  329. c = edu["content"]
  330. self.assertGreaterEqual(
  331. c.items(),
  332. {"user_id": u1, "prev_id": [stream_id], "deleted": True}.items(),
  333. )
  334. self.assertGreaterEqual(c["stream_id"], stream_id)
  335. stream_id = c["stream_id"]
  336. devices = {edu["content"]["device_id"] for edu in self.edus}
  337. self.assertEqual({"D1", "D2", "D3"}, devices)
  338. def test_unreachable_server(self):
  339. """If the destination server is unreachable, all the updates should get sent on
  340. recovery
  341. """
  342. mock_send_txn = self.hs.get_federation_transport_client().send_transaction
  343. mock_send_txn.side_effect = lambda t, cb: defer.fail(AssertionError("fail"))
  344. # create devices
  345. u1 = self.register_user("user", "pass")
  346. self.login("user", "pass", device_id="D1")
  347. self.login("user", "pass", device_id="D2")
  348. self.login("user", "pass", device_id="D3")
  349. # delete them again
  350. self.get_success(
  351. self.hs.get_device_handler().delete_devices(u1, ["D1", "D2", "D3"])
  352. )
  353. # We queue up device list updates to be sent over federation, so we
  354. # advance to clear the queue.
  355. self.reactor.advance(1)
  356. self.assertGreaterEqual(mock_send_txn.call_count, 4)
  357. # recover the server
  358. mock_send_txn.side_effect = self.record_transaction
  359. self.hs.get_federation_sender().send_device_messages("host2")
  360. # We queue up device list updates to be sent over federation, so we
  361. # advance to clear the queue.
  362. self.reactor.advance(1)
  363. # for each device, there should be a single update
  364. self.assertEqual(len(self.edus), 3)
  365. stream_id = None
  366. for edu in self.edus:
  367. self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
  368. c = edu["content"]
  369. self.assertEqual(c["prev_id"], [stream_id] if stream_id is not None else [])
  370. if stream_id is not None:
  371. self.assertGreaterEqual(c["stream_id"], stream_id)
  372. stream_id = c["stream_id"]
  373. devices = {edu["content"]["device_id"] for edu in self.edus}
  374. self.assertEqual({"D1", "D2", "D3"}, devices)
  375. def test_prune_outbound_device_pokes1(self):
  376. """If a destination is unreachable, and the updates are pruned, we should get
  377. a single update.
  378. This case tests the behaviour when the server has never been reachable.
  379. """
  380. mock_send_txn = self.hs.get_federation_transport_client().send_transaction
  381. mock_send_txn.side_effect = lambda t, cb: defer.fail(AssertionError("fail"))
  382. # create devices
  383. u1 = self.register_user("user", "pass")
  384. self.login("user", "pass", device_id="D1")
  385. self.login("user", "pass", device_id="D2")
  386. self.login("user", "pass", device_id="D3")
  387. # delete them again
  388. self.get_success(
  389. self.hs.get_device_handler().delete_devices(u1, ["D1", "D2", "D3"])
  390. )
  391. # We queue up device list updates to be sent over federation, so we
  392. # advance to clear the queue.
  393. self.reactor.advance(1)
  394. self.assertGreaterEqual(mock_send_txn.call_count, 4)
  395. # run the prune job
  396. self.reactor.advance(10)
  397. self.get_success(
  398. self.hs.get_datastores().main._prune_old_outbound_device_pokes(prune_age=1)
  399. )
  400. # recover the server
  401. mock_send_txn.side_effect = self.record_transaction
  402. self.hs.get_federation_sender().send_device_messages("host2")
  403. # We queue up device list updates to be sent over federation, so we
  404. # advance to clear the queue.
  405. self.reactor.advance(1)
  406. # there should be a single update for this user.
  407. self.assertEqual(len(self.edus), 1)
  408. edu = self.edus.pop(0)
  409. self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
  410. c = edu["content"]
  411. # synapse uses an empty prev_id list to indicate "needs a full resync".
  412. self.assertEqual(c["prev_id"], [])
  413. def test_prune_outbound_device_pokes2(self):
  414. """If a destination is unreachable, and the updates are pruned, we should get
  415. a single update.
  416. This case tests the behaviour when the server was reachable, but then goes
  417. offline.
  418. """
  419. # create first device
  420. u1 = self.register_user("user", "pass")
  421. self.login("user", "pass", device_id="D1")
  422. # expect the update EDU
  423. self.assertEqual(len(self.edus), 1)
  424. self.check_device_update_edu(self.edus.pop(0), u1, "D1", None)
  425. # now the server goes offline
  426. mock_send_txn = self.hs.get_federation_transport_client().send_transaction
  427. mock_send_txn.side_effect = lambda t, cb: defer.fail(AssertionError("fail"))
  428. self.login("user", "pass", device_id="D2")
  429. self.login("user", "pass", device_id="D3")
  430. # We queue up device list updates to be sent over federation, so we
  431. # advance to clear the queue.
  432. self.reactor.advance(1)
  433. # delete them again
  434. self.get_success(
  435. self.hs.get_device_handler().delete_devices(u1, ["D1", "D2", "D3"])
  436. )
  437. self.assertGreaterEqual(mock_send_txn.call_count, 3)
  438. # run the prune job
  439. self.reactor.advance(10)
  440. self.get_success(
  441. self.hs.get_datastores().main._prune_old_outbound_device_pokes(prune_age=1)
  442. )
  443. # recover the server
  444. mock_send_txn.side_effect = self.record_transaction
  445. self.hs.get_federation_sender().send_device_messages("host2")
  446. # We queue up device list updates to be sent over federation, so we
  447. # advance to clear the queue.
  448. self.reactor.advance(1)
  449. # ... and we should get a single update for this user.
  450. self.assertEqual(len(self.edus), 1)
  451. edu = self.edus.pop(0)
  452. self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
  453. c = edu["content"]
  454. # synapse uses an empty prev_id list to indicate "needs a full resync".
  455. self.assertEqual(c["prev_id"], [])
  456. def check_device_update_edu(
  457. self,
  458. edu: JsonDict,
  459. user_id: str,
  460. device_id: str,
  461. prev_stream_id: Optional[int],
  462. ) -> int:
  463. """Check that the given EDU is an update for the given device
  464. Returns the stream_id.
  465. """
  466. self.assertEqual(edu["edu_type"], EduTypes.DEVICE_LIST_UPDATE)
  467. content = edu["content"]
  468. expected = {
  469. "user_id": user_id,
  470. "device_id": device_id,
  471. "prev_id": [prev_stream_id] if prev_stream_id is not None else [],
  472. }
  473. self.assertLessEqual(expected.items(), content.items())
  474. if prev_stream_id is not None:
  475. self.assertGreaterEqual(content["stream_id"], prev_stream_id)
  476. return content["stream_id"]
  477. def check_signing_key_update_txn(
  478. self,
  479. txn: JsonDict,
  480. ) -> None:
  481. """Check that the txn has an EDU with a signing key update."""
  482. edus = txn["edus"]
  483. self.assertEqual(len(edus), 2)
  484. def generate_and_upload_device_signing_key(
  485. self, user_id: str, device_id: str
  486. ) -> SigningKey:
  487. """Generate a signing keypair for the given device, and upload it"""
  488. sk = key.generate_signing_key(device_id)
  489. device_dict = build_device_dict(user_id, device_id, sk)
  490. self.get_success(
  491. self.hs.get_e2e_keys_handler().upload_keys_for_user(
  492. user_id,
  493. device_id,
  494. {"device_keys": device_dict},
  495. )
  496. )
  497. return sk
  498. def generate_self_id_key() -> SigningKey:
  499. """generate a signing key whose version is its public key
  500. ... as used by the cross-signing-keys.
  501. """
  502. k = key.generate_signing_key("x")
  503. k.version = encode_pubkey(k)
  504. return k
  505. def key_id(k: BaseKey) -> str:
  506. return "%s:%s" % (k.alg, k.version)
  507. def encode_pubkey(sk: SigningKey) -> str:
  508. """Encode the public key corresponding to the given signing key as base64"""
  509. return key.encode_verify_key_base64(key.get_verify_key(sk))
  510. def build_device_dict(user_id: str, device_id: str, sk: SigningKey):
  511. """Build a dict representing the given device"""
  512. return {
  513. "user_id": user_id,
  514. "device_id": device_id,
  515. "algorithms": [
  516. "m.olm.curve25519-aes-sha2",
  517. RoomEncryptionAlgorithms.MEGOLM_V1_AES_SHA2,
  518. ],
  519. "keys": {
  520. "curve25519:" + device_id: "curve25519+key",
  521. key_id(sk): encode_pubkey(sk),
  522. },
  523. }