You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1562 lines
60 KiB

  1. # Copyright 2016 OpenMarket Ltd
  2. # Copyright 2019 New Vector Ltd
  3. # Copyright 2019,2020 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import logging
  17. from typing import (
  18. TYPE_CHECKING,
  19. Any,
  20. Dict,
  21. Iterable,
  22. List,
  23. Mapping,
  24. Optional,
  25. Set,
  26. Tuple,
  27. )
  28. from synapse.api import errors
  29. from synapse.api.constants import EduTypes, EventTypes
  30. from synapse.api.errors import (
  31. Codes,
  32. FederationDeniedError,
  33. HttpResponseException,
  34. InvalidAPICallError,
  35. RequestSendFailed,
  36. SynapseError,
  37. )
  38. from synapse.logging.opentracing import log_kv, set_tag, trace
  39. from synapse.metrics.background_process_metrics import (
  40. run_as_background_process,
  41. wrap_as_background_process,
  42. )
  43. from synapse.types import (
  44. JsonDict,
  45. JsonMapping,
  46. ScheduledTask,
  47. StrCollection,
  48. StreamKeyType,
  49. StreamToken,
  50. TaskStatus,
  51. UserID,
  52. get_domain_from_id,
  53. get_verify_key_from_cross_signing_key,
  54. )
  55. from synapse.util import stringutils
  56. from synapse.util.async_helpers import Linearizer
  57. from synapse.util.caches.expiringcache import ExpiringCache
  58. from synapse.util.cancellation import cancellable
  59. from synapse.util.metrics import measure_func
  60. from synapse.util.retryutils import (
  61. NotRetryingDestination,
  62. filter_destinations_by_retry_limiter,
  63. )
  64. if TYPE_CHECKING:
  65. from synapse.server import HomeServer
  66. logger = logging.getLogger(__name__)
  67. DELETE_DEVICE_MSGS_TASK_NAME = "delete_device_messages"
  68. MAX_DEVICE_DISPLAY_NAME_LEN = 100
  69. DELETE_STALE_DEVICES_INTERVAL_MS = 24 * 60 * 60 * 1000
  70. class DeviceWorkerHandler:
  71. device_list_updater: "DeviceListWorkerUpdater"
  72. def __init__(self, hs: "HomeServer"):
  73. self.clock = hs.get_clock()
  74. self.hs = hs
  75. self.store = hs.get_datastores().main
  76. self.notifier = hs.get_notifier()
  77. self.state = hs.get_state_handler()
  78. self._appservice_handler = hs.get_application_service_handler()
  79. self._state_storage = hs.get_storage_controllers().state
  80. self._auth_handler = hs.get_auth_handler()
  81. self._event_sources = hs.get_event_sources()
  82. self.server_name = hs.hostname
  83. self._msc3852_enabled = hs.config.experimental.msc3852_enabled
  84. self._query_appservices_for_keys = (
  85. hs.config.experimental.msc3984_appservice_key_query
  86. )
  87. self._task_scheduler = hs.get_task_scheduler()
  88. self.device_list_updater = DeviceListWorkerUpdater(hs)
  89. self._task_scheduler.register_action(
  90. self._delete_device_messages, DELETE_DEVICE_MSGS_TASK_NAME
  91. )
  92. @trace
  93. async def get_devices_by_user(self, user_id: str) -> List[JsonDict]:
  94. """
  95. Retrieve the given user's devices
  96. Args:
  97. user_id: The user ID to query for devices.
  98. Returns:
  99. info on each device
  100. """
  101. set_tag("user_id", user_id)
  102. device_map = await self.store.get_devices_by_user(user_id)
  103. ips = await self.store.get_last_client_ip_by_device(user_id, device_id=None)
  104. devices = list(device_map.values())
  105. for device in devices:
  106. _update_device_from_client_ips(device, ips)
  107. log_kv(device_map)
  108. return devices
  109. async def get_dehydrated_device(
  110. self, user_id: str
  111. ) -> Optional[Tuple[str, JsonDict]]:
  112. """Retrieve the information for a dehydrated device.
  113. Args:
  114. user_id: the user whose dehydrated device we are looking for
  115. Returns:
  116. a tuple whose first item is the device ID, and the second item is
  117. the dehydrated device information
  118. """
  119. return await self.store.get_dehydrated_device(user_id)
  120. @trace
  121. async def get_device(self, user_id: str, device_id: str) -> JsonDict:
  122. """Retrieve the given device
  123. Args:
  124. user_id: The user to get the device from
  125. device_id: The device to fetch.
  126. Returns:
  127. info on the device
  128. Raises:
  129. errors.NotFoundError: if the device was not found
  130. """
  131. device = await self.store.get_device(user_id, device_id)
  132. if device is None:
  133. raise errors.NotFoundError()
  134. ips = await self.store.get_last_client_ip_by_device(user_id, device_id)
  135. _update_device_from_client_ips(device, ips)
  136. set_tag("device", str(device))
  137. set_tag("ips", str(ips))
  138. return device
  139. @cancellable
  140. async def get_device_changes_in_shared_rooms(
  141. self, user_id: str, room_ids: StrCollection, from_token: StreamToken
  142. ) -> Set[str]:
  143. """Get the set of users whose devices have changed who share a room with
  144. the given user.
  145. """
  146. changed_users = await self.store.get_device_list_changes_in_rooms(
  147. room_ids, from_token.device_list_key
  148. )
  149. if changed_users is not None:
  150. # We also check if the given user has changed their device. If
  151. # they're in no rooms then the above query won't include them.
  152. changed = await self.store.get_users_whose_devices_changed(
  153. from_token.device_list_key, [user_id]
  154. )
  155. changed_users.update(changed)
  156. return changed_users
  157. # If the DB returned None then the `from_token` is too old, so we fall
  158. # back on looking for device updates for all users.
  159. users_who_share_room = await self.store.get_users_who_share_room_with_user(
  160. user_id
  161. )
  162. tracked_users = set(users_who_share_room)
  163. # Always tell the user about their own devices
  164. tracked_users.add(user_id)
  165. changed = await self.store.get_users_whose_devices_changed(
  166. from_token.device_list_key, tracked_users
  167. )
  168. return changed
  169. @trace
  170. @measure_func("device.get_user_ids_changed")
  171. @cancellable
  172. async def get_user_ids_changed(
  173. self, user_id: str, from_token: StreamToken
  174. ) -> JsonDict:
  175. """Get list of users that have had the devices updated, or have newly
  176. joined a room, that `user_id` may be interested in.
  177. """
  178. set_tag("user_id", user_id)
  179. set_tag("from_token", str(from_token))
  180. now_room_key = self.store.get_room_max_token()
  181. room_ids = await self.store.get_rooms_for_user(user_id)
  182. changed = await self.get_device_changes_in_shared_rooms(
  183. user_id, room_ids, from_token
  184. )
  185. # Then work out if any users have since joined
  186. rooms_changed = self.store.get_rooms_that_changed(room_ids, from_token.room_key)
  187. member_events = await self.store.get_membership_changes_for_user(
  188. user_id, from_token.room_key, now_room_key
  189. )
  190. rooms_changed.update(event.room_id for event in member_events)
  191. stream_ordering = from_token.room_key.stream
  192. possibly_changed = set(changed)
  193. possibly_left = set()
  194. for room_id in rooms_changed:
  195. # Check if the forward extremities have changed. If not then we know
  196. # the current state won't have changed, and so we can skip this room.
  197. try:
  198. if not await self.store.have_room_forward_extremities_changed_since(
  199. room_id, stream_ordering
  200. ):
  201. continue
  202. except errors.StoreError:
  203. pass
  204. current_state_ids = await self._state_storage.get_current_state_ids(
  205. room_id, await_full_state=False
  206. )
  207. # The user may have left the room
  208. # TODO: Check if they actually did or if we were just invited.
  209. if room_id not in room_ids:
  210. for etype, state_key in current_state_ids.keys():
  211. if etype != EventTypes.Member:
  212. continue
  213. possibly_left.add(state_key)
  214. continue
  215. # Fetch the current state at the time.
  216. try:
  217. event_ids = await self.store.get_forward_extremities_for_room_at_stream_ordering(
  218. room_id, stream_ordering=stream_ordering
  219. )
  220. except errors.StoreError:
  221. # we have purged the stream_ordering index since the stream
  222. # ordering: treat it the same as a new room
  223. event_ids = []
  224. # special-case for an empty prev state: include all members
  225. # in the changed list
  226. if not event_ids:
  227. log_kv(
  228. {"event": "encountered empty previous state", "room_id": room_id}
  229. )
  230. for etype, state_key in current_state_ids.keys():
  231. if etype != EventTypes.Member:
  232. continue
  233. possibly_changed.add(state_key)
  234. continue
  235. current_member_id = current_state_ids.get((EventTypes.Member, user_id))
  236. if not current_member_id:
  237. continue
  238. # mapping from event_id -> state_dict
  239. prev_state_ids = await self._state_storage.get_state_ids_for_events(
  240. event_ids,
  241. await_full_state=False,
  242. )
  243. # Check if we've joined the room? If so we just blindly add all the users to
  244. # the "possibly changed" users.
  245. for state_dict in prev_state_ids.values():
  246. member_event = state_dict.get((EventTypes.Member, user_id), None)
  247. if not member_event or member_event != current_member_id:
  248. for etype, state_key in current_state_ids.keys():
  249. if etype != EventTypes.Member:
  250. continue
  251. possibly_changed.add(state_key)
  252. break
  253. # If there has been any change in membership, include them in the
  254. # possibly changed list. We'll check if they are joined below,
  255. # and we're not toooo worried about spuriously adding users.
  256. for key, event_id in current_state_ids.items():
  257. etype, state_key = key
  258. if etype != EventTypes.Member:
  259. continue
  260. # check if this member has changed since any of the extremities
  261. # at the stream_ordering, and add them to the list if so.
  262. for state_dict in prev_state_ids.values():
  263. prev_event_id = state_dict.get(key, None)
  264. if not prev_event_id or prev_event_id != event_id:
  265. if state_key != user_id:
  266. possibly_changed.add(state_key)
  267. break
  268. if possibly_changed or possibly_left:
  269. possibly_joined = possibly_changed
  270. possibly_left = possibly_changed | possibly_left
  271. # Double check if we still share rooms with the given user.
  272. users_rooms = await self.store.get_rooms_for_users(possibly_left)
  273. for changed_user_id, entries in users_rooms.items():
  274. if any(rid in room_ids for rid in entries):
  275. possibly_left.discard(changed_user_id)
  276. else:
  277. possibly_joined.discard(changed_user_id)
  278. else:
  279. possibly_joined = set()
  280. possibly_left = set()
  281. result = {"changed": list(possibly_joined), "left": list(possibly_left)}
  282. log_kv(result)
  283. return result
  284. async def on_federation_query_user_devices(self, user_id: str) -> JsonDict:
  285. stream_id, devices = await self.store.get_e2e_device_keys_for_federation_query(
  286. user_id
  287. )
  288. master_key = await self.store.get_e2e_cross_signing_key(user_id, "master")
  289. self_signing_key = await self.store.get_e2e_cross_signing_key(
  290. user_id, "self_signing"
  291. )
  292. # Check if the application services have any results.
  293. if self._query_appservices_for_keys:
  294. # Query the appservice for all devices for this user.
  295. query: Dict[str, Optional[List[str]]] = {user_id: None}
  296. # Query the appservices for any keys.
  297. appservice_results = await self._appservice_handler.query_keys(query)
  298. # Merge results, overriding anything from the database.
  299. appservice_devices = appservice_results.get("device_keys", {}).get(
  300. user_id, {}
  301. )
  302. # Filter the database results to only those devices that the appservice has
  303. # *not* responded with.
  304. devices = [d for d in devices if d["device_id"] not in appservice_devices]
  305. # Append the appservice response by wrapping each result in another dictionary.
  306. devices.extend(
  307. {"device_id": device_id, "keys": device}
  308. for device_id, device in appservice_devices.items()
  309. )
  310. # TODO Handle cross-signing keys.
  311. return {
  312. "user_id": user_id,
  313. "stream_id": stream_id,
  314. "devices": devices,
  315. "master_key": master_key,
  316. "self_signing_key": self_signing_key,
  317. }
  318. async def handle_room_un_partial_stated(self, room_id: str) -> None:
  319. """Handles sending appropriate device list updates in a room that has
  320. gone from partial to full state.
  321. """
  322. # TODO(faster_joins): worker mode support
  323. # https://github.com/matrix-org/synapse/issues/12994
  324. logger.error(
  325. "Trying handling device list state for partial join: not supported on workers."
  326. )
  327. DEVICE_MSGS_DELETE_BATCH_LIMIT = 100
  328. async def _delete_device_messages(
  329. self,
  330. task: ScheduledTask,
  331. ) -> Tuple[TaskStatus, Optional[JsonMapping], Optional[str]]:
  332. """Scheduler task to delete device messages in batch of `DEVICE_MSGS_DELETE_BATCH_LIMIT`."""
  333. assert task.params is not None
  334. user_id = task.params["user_id"]
  335. device_id = task.params["device_id"]
  336. up_to_stream_id = task.params["up_to_stream_id"]
  337. res = await self.store.delete_messages_for_device(
  338. user_id=user_id,
  339. device_id=device_id,
  340. up_to_stream_id=up_to_stream_id,
  341. limit=DeviceHandler.DEVICE_MSGS_DELETE_BATCH_LIMIT,
  342. )
  343. if res < DeviceHandler.DEVICE_MSGS_DELETE_BATCH_LIMIT:
  344. return TaskStatus.COMPLETE, None, None
  345. else:
  346. # There is probably still device messages to be deleted, let's keep the task active and it will be run
  347. # again in a subsequent scheduler loop run (probably the next one, if not too many tasks are running).
  348. return TaskStatus.ACTIVE, None, None
  349. class DeviceHandler(DeviceWorkerHandler):
  350. device_list_updater: "DeviceListUpdater"
  351. def __init__(self, hs: "HomeServer"):
  352. super().__init__(hs)
  353. self.federation_sender = hs.get_federation_sender()
  354. self._account_data_handler = hs.get_account_data_handler()
  355. self._storage_controllers = hs.get_storage_controllers()
  356. self.db_pool = hs.get_datastores().main.db_pool
  357. self.device_list_updater = DeviceListUpdater(hs, self)
  358. federation_registry = hs.get_federation_registry()
  359. federation_registry.register_edu_handler(
  360. EduTypes.DEVICE_LIST_UPDATE,
  361. self.device_list_updater.incoming_device_list_update,
  362. )
  363. # Whether `_handle_new_device_update_async` is currently processing.
  364. self._handle_new_device_update_is_processing = False
  365. # If a new device update may have happened while the loop was
  366. # processing.
  367. self._handle_new_device_update_new_data = False
  368. # On start up check if there are any updates pending.
  369. hs.get_reactor().callWhenRunning(self._handle_new_device_update_async)
  370. self._delete_stale_devices_after = hs.config.server.delete_stale_devices_after
  371. # Ideally we would run this on a worker and condition this on the
  372. # "run_background_tasks_on" setting, but this would mean making the notification
  373. # of device list changes over federation work on workers, which is nontrivial.
  374. if self._delete_stale_devices_after is not None:
  375. self.clock.looping_call(
  376. run_as_background_process,
  377. DELETE_STALE_DEVICES_INTERVAL_MS,
  378. "delete_stale_devices",
  379. self._delete_stale_devices,
  380. )
  381. def _check_device_name_length(self, name: Optional[str]) -> None:
  382. """
  383. Checks whether a device name is longer than the maximum allowed length.
  384. Args:
  385. name: The name of the device.
  386. Raises:
  387. SynapseError: if the device name is too long.
  388. """
  389. if name and len(name) > MAX_DEVICE_DISPLAY_NAME_LEN:
  390. raise SynapseError(
  391. 400,
  392. "Device display name is too long (max %i)"
  393. % (MAX_DEVICE_DISPLAY_NAME_LEN,),
  394. errcode=Codes.TOO_LARGE,
  395. )
  396. async def check_device_registered(
  397. self,
  398. user_id: str,
  399. device_id: Optional[str],
  400. initial_device_display_name: Optional[str] = None,
  401. auth_provider_id: Optional[str] = None,
  402. auth_provider_session_id: Optional[str] = None,
  403. ) -> str:
  404. """
  405. If the given device has not been registered, register it with the
  406. supplied display name.
  407. If no device_id is supplied, we make one up.
  408. Args:
  409. user_id: @user:id
  410. device_id: device id supplied by client
  411. initial_device_display_name: device display name from client
  412. auth_provider_id: The SSO IdP the user used, if any.
  413. auth_provider_session_id: The session ID (sid) got from the SSO IdP.
  414. Returns:
  415. device id (generated if none was supplied)
  416. """
  417. self._check_device_name_length(initial_device_display_name)
  418. if device_id is not None:
  419. new_device = await self.store.store_device(
  420. user_id=user_id,
  421. device_id=device_id,
  422. initial_device_display_name=initial_device_display_name,
  423. auth_provider_id=auth_provider_id,
  424. auth_provider_session_id=auth_provider_session_id,
  425. )
  426. if new_device:
  427. await self.notify_device_update(user_id, [device_id])
  428. return device_id
  429. # if the device id is not specified, we'll autogen one, but loop a few
  430. # times in case of a clash.
  431. attempts = 0
  432. while attempts < 5:
  433. new_device_id = stringutils.random_string(10).upper()
  434. new_device = await self.store.store_device(
  435. user_id=user_id,
  436. device_id=new_device_id,
  437. initial_device_display_name=initial_device_display_name,
  438. auth_provider_id=auth_provider_id,
  439. auth_provider_session_id=auth_provider_session_id,
  440. )
  441. if new_device:
  442. await self.notify_device_update(user_id, [new_device_id])
  443. return new_device_id
  444. attempts += 1
  445. raise errors.StoreError(500, "Couldn't generate a device ID.")
  446. async def _delete_stale_devices(self) -> None:
  447. """Background task that deletes devices which haven't been accessed for more than
  448. a configured time period.
  449. """
  450. # We should only be running this job if the config option is defined.
  451. assert self._delete_stale_devices_after is not None
  452. now_ms = self.clock.time_msec()
  453. since_ms = now_ms - self._delete_stale_devices_after
  454. devices = await self.store.get_local_devices_not_accessed_since(since_ms)
  455. for user_id, user_devices in devices.items():
  456. await self.delete_devices(user_id, user_devices)
  457. @trace
  458. async def delete_all_devices_for_user(
  459. self, user_id: str, except_device_id: Optional[str] = None
  460. ) -> None:
  461. """Delete all of the user's devices
  462. Args:
  463. user_id: The user to remove all devices from
  464. except_device_id: optional device id which should not be deleted
  465. """
  466. device_map = await self.store.get_devices_by_user(user_id)
  467. device_ids = list(device_map)
  468. if except_device_id is not None:
  469. device_ids = [d for d in device_ids if d != except_device_id]
  470. await self.delete_devices(user_id, device_ids)
  471. async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
  472. """Delete several devices
  473. Args:
  474. user_id: The user to delete devices from.
  475. device_ids: The list of device IDs to delete
  476. """
  477. to_device_stream_id = self._event_sources.get_current_token().to_device_key
  478. try:
  479. await self.store.delete_devices(user_id, device_ids)
  480. except errors.StoreError as e:
  481. if e.code == 404:
  482. # no match
  483. set_tag("error", True)
  484. set_tag("reason", "User doesn't have that device id.")
  485. else:
  486. raise
  487. # Delete data specific to each device. Not optimised as it is not
  488. # considered as part of a critical path.
  489. for device_id in device_ids:
  490. await self._auth_handler.delete_access_tokens_for_user(
  491. user_id, device_id=device_id
  492. )
  493. await self.store.delete_e2e_keys_by_device(
  494. user_id=user_id, device_id=device_id
  495. )
  496. if self.hs.config.experimental.msc3890_enabled:
  497. # Remove any local notification settings for this device in accordance
  498. # with MSC3890.
  499. await self._account_data_handler.remove_account_data_for_user(
  500. user_id,
  501. f"org.matrix.msc3890.local_notification_settings.{device_id}",
  502. )
  503. # Delete device messages asynchronously and in batches using the task scheduler
  504. await self._task_scheduler.schedule_task(
  505. DELETE_DEVICE_MSGS_TASK_NAME,
  506. resource_id=device_id,
  507. params={
  508. "user_id": user_id,
  509. "device_id": device_id,
  510. "up_to_stream_id": to_device_stream_id,
  511. },
  512. )
  513. # Pushers are deleted after `delete_access_tokens_for_user` is called so that
  514. # modules using `on_logged_out` hook can use them if needed.
  515. await self.hs.get_pusherpool().remove_pushers_by_devices(user_id, device_ids)
  516. await self.notify_device_update(user_id, device_ids)
  517. async def update_device(self, user_id: str, device_id: str, content: dict) -> None:
  518. """Update the given device
  519. Args:
  520. user_id: The user to update devices of.
  521. device_id: The device to update.
  522. content: body of update request
  523. """
  524. # Reject a new displayname which is too long.
  525. new_display_name = content.get("display_name")
  526. self._check_device_name_length(new_display_name)
  527. try:
  528. await self.store.update_device(
  529. user_id, device_id, new_display_name=new_display_name
  530. )
  531. await self.notify_device_update(user_id, [device_id])
  532. except errors.StoreError as e:
  533. if e.code == 404:
  534. raise errors.NotFoundError()
  535. else:
  536. raise
  537. @trace
  538. @measure_func("notify_device_update")
  539. async def notify_device_update(
  540. self, user_id: str, device_ids: StrCollection
  541. ) -> None:
  542. """Notify that a user's device(s) has changed. Pokes the notifier, and
  543. remote servers if the user is local.
  544. Args:
  545. user_id: The Matrix ID of the user who's device list has been updated.
  546. device_ids: The device IDs that have changed.
  547. """
  548. if not device_ids:
  549. # No changes to notify about, so this is a no-op.
  550. return
  551. room_ids = await self.store.get_rooms_for_user(user_id)
  552. position = await self.store.add_device_change_to_streams(
  553. user_id,
  554. device_ids,
  555. room_ids=room_ids,
  556. )
  557. if not position:
  558. # This should only happen if there are no updates, so we bail.
  559. return
  560. for device_id in device_ids:
  561. logger.debug(
  562. "Notifying about update %r/%r, ID: %r", user_id, device_id, position
  563. )
  564. # specify the user ID too since the user should always get their own device list
  565. # updates, even if they aren't in any rooms.
  566. self.notifier.on_new_event(
  567. StreamKeyType.DEVICE_LIST, position, users={user_id}, rooms=room_ids
  568. )
  569. # We may need to do some processing asynchronously for local user IDs.
  570. if self.hs.is_mine_id(user_id):
  571. self._handle_new_device_update_async()
  572. async def notify_user_signature_update(
  573. self, from_user_id: str, user_ids: List[str]
  574. ) -> None:
  575. """Notify a user that they have made new signatures of other users.
  576. Args:
  577. from_user_id: the user who made the signature
  578. user_ids: the users IDs that have new signatures
  579. """
  580. position = await self.store.add_user_signature_change_to_streams(
  581. from_user_id, user_ids
  582. )
  583. self.notifier.on_new_event(
  584. StreamKeyType.DEVICE_LIST, position, users=[from_user_id]
  585. )
  586. async def store_dehydrated_device(
  587. self,
  588. user_id: str,
  589. device_id: Optional[str],
  590. device_data: JsonDict,
  591. initial_device_display_name: Optional[str] = None,
  592. keys_for_device: Optional[JsonDict] = None,
  593. ) -> str:
  594. """Store a dehydrated device for a user, optionally storing the keys associated with
  595. it as well. If the user had a previous dehydrated device, it is removed.
  596. Args:
  597. user_id: the user that we are storing the device for
  598. device_id: device id supplied by client
  599. device_data: the dehydrated device information
  600. initial_device_display_name: The display name to use for the device
  601. keys_for_device: keys for the dehydrated device
  602. Returns:
  603. device id of the dehydrated device
  604. """
  605. device_id = await self.check_device_registered(
  606. user_id,
  607. device_id,
  608. initial_device_display_name,
  609. )
  610. time_now = self.clock.time_msec()
  611. old_device_id = await self.store.store_dehydrated_device(
  612. user_id, device_id, device_data, time_now, keys_for_device
  613. )
  614. if old_device_id is not None:
  615. await self.delete_devices(user_id, [old_device_id])
  616. return device_id
  617. async def rehydrate_device(
  618. self, user_id: str, access_token: str, device_id: str
  619. ) -> dict:
  620. """Process a rehydration request from the user.
  621. Args:
  622. user_id: the user who is rehydrating the device
  623. access_token: the access token used for the request
  624. device_id: the ID of the device that will be rehydrated
  625. Returns:
  626. a dict containing {"success": True}
  627. """
  628. success = await self.store.remove_dehydrated_device(user_id, device_id)
  629. if not success:
  630. raise errors.NotFoundError()
  631. # If the dehydrated device was successfully deleted (the device ID
  632. # matched the stored dehydrated device), then modify the access
  633. # token and refresh token to use the dehydrated device's ID and
  634. # copy the old device display name to the dehydrated device,
  635. # and destroy the old device ID
  636. old_device_id = await self.store.set_device_for_access_token(
  637. access_token, device_id
  638. )
  639. await self.store.set_device_for_refresh_token(user_id, old_device_id, device_id)
  640. old_device = await self.store.get_device(user_id, old_device_id)
  641. if old_device is None:
  642. raise errors.NotFoundError()
  643. await self.store.update_device(user_id, device_id, old_device["display_name"])
  644. # can't call self.delete_device because that will clobber the
  645. # access token so call the storage layer directly
  646. await self.store.delete_devices(user_id, [old_device_id])
  647. await self.store.delete_e2e_keys_by_device(
  648. user_id=user_id, device_id=old_device_id
  649. )
  650. # tell everyone that the old device is gone and that the dehydrated
  651. # device has a new display name
  652. await self.notify_device_update(user_id, [old_device_id, device_id])
  653. return {"success": True}
  654. async def delete_dehydrated_device(self, user_id: str, device_id: str) -> None:
  655. """
  656. Delete a stored dehydrated device.
  657. Args:
  658. user_id: the user_id to delete the device from
  659. device_id: id of the dehydrated device to delete
  660. """
  661. success = await self.store.remove_dehydrated_device(user_id, device_id)
  662. if not success:
  663. raise errors.NotFoundError()
  664. await self.delete_devices(user_id, [device_id])
  665. await self.store.delete_e2e_keys_by_device(user_id=user_id, device_id=device_id)
  666. @wrap_as_background_process("_handle_new_device_update_async")
  667. async def _handle_new_device_update_async(self) -> None:
  668. """Called when we have a new local device list update that we need to
  669. send out over federation.
  670. This happens in the background so as not to block the original request
  671. that generated the device update.
  672. """
  673. if self._handle_new_device_update_is_processing:
  674. self._handle_new_device_update_new_data = True
  675. return
  676. self._handle_new_device_update_is_processing = True
  677. # The stream ID we processed previous iteration (if any), and the set of
  678. # hosts we've already poked about for this update. This is so that we
  679. # don't poke the same remote server about the same update repeatedly.
  680. current_stream_id = None
  681. hosts_already_sent_to: Set[str] = set()
  682. try:
  683. stream_id, room_id = await self.store.get_device_change_last_converted_pos()
  684. while True:
  685. self._handle_new_device_update_new_data = False
  686. max_stream_id = self.store.get_device_stream_token()
  687. rows = await self.store.get_uncoverted_outbound_room_pokes(
  688. stream_id, room_id
  689. )
  690. if not rows:
  691. # If the DB returned nothing then there is nothing left to
  692. # do, *unless* a new device list update happened during the
  693. # DB query.
  694. # Advance `(stream_id, room_id)`.
  695. # `max_stream_id` comes from *before* the query for unconverted
  696. # rows, which means that any unconverted rows must have a larger
  697. # stream ID.
  698. if max_stream_id > stream_id:
  699. stream_id, room_id = max_stream_id, ""
  700. await self.store.set_device_change_last_converted_pos(
  701. stream_id, room_id
  702. )
  703. else:
  704. assert max_stream_id == stream_id
  705. # Avoid moving `room_id` backwards.
  706. pass
  707. if self._handle_new_device_update_new_data:
  708. continue
  709. else:
  710. return
  711. for user_id, device_id, room_id, stream_id, opentracing_context in rows:
  712. hosts = set()
  713. # Ignore any users that aren't ours
  714. if self.hs.is_mine_id(user_id):
  715. hosts = set(
  716. await self._storage_controllers.state.get_current_hosts_in_room_or_partial_state_approximation(
  717. room_id
  718. )
  719. )
  720. hosts.discard(self.server_name)
  721. # For rooms with partial state, `hosts` is merely an
  722. # approximation. When we transition to a full state room, we
  723. # will have to send out device list updates to any servers we
  724. # missed.
  725. # Check if we've already sent this update to some hosts
  726. if current_stream_id == stream_id:
  727. hosts -= hosts_already_sent_to
  728. await self.store.add_device_list_outbound_pokes(
  729. user_id=user_id,
  730. device_id=device_id,
  731. room_id=room_id,
  732. hosts=hosts,
  733. context=opentracing_context,
  734. )
  735. # Notify replication that we've updated the device list stream.
  736. self.notifier.notify_replication()
  737. if hosts:
  738. logger.info(
  739. "Sending device list update notif for %r to: %r",
  740. user_id,
  741. hosts,
  742. )
  743. await self.federation_sender.send_device_messages(
  744. hosts, immediate=False
  745. )
  746. # TODO: when called, this isn't in a logging context.
  747. # This leads to log spam, sentry event spam, and massive
  748. # memory usage.
  749. # See https://github.com/matrix-org/synapse/issues/12552.
  750. # log_kv(
  751. # {"message": "sent device update to host", "host": host}
  752. # )
  753. if current_stream_id != stream_id:
  754. # Clear the set of hosts we've already sent to as we're
  755. # processing a new update.
  756. hosts_already_sent_to.clear()
  757. hosts_already_sent_to.update(hosts)
  758. current_stream_id = stream_id
  759. # Advance `(stream_id, room_id)`.
  760. _, _, room_id, stream_id, _ = rows[-1]
  761. await self.store.set_device_change_last_converted_pos(
  762. stream_id, room_id
  763. )
  764. finally:
  765. self._handle_new_device_update_is_processing = False
  766. async def handle_room_un_partial_stated(self, room_id: str) -> None:
  767. """Handles sending appropriate device list updates in a room that has
  768. gone from partial to full state.
  769. """
  770. # We defer to the device list updater to handle pending remote device
  771. # list updates.
  772. await self.device_list_updater.handle_room_un_partial_stated(room_id)
  773. # Replay local updates.
  774. (
  775. join_event_id,
  776. device_lists_stream_id,
  777. ) = await self.store.get_join_event_id_and_device_lists_stream_id_for_partial_state(
  778. room_id
  779. )
  780. # Get the local device list changes that have happened in the room since
  781. # we started joining. If there are no updates there's nothing left to do.
  782. changes = await self.store.get_device_list_changes_in_room(
  783. room_id, device_lists_stream_id
  784. )
  785. local_changes = {(u, d) for u, d in changes if self.hs.is_mine_id(u)}
  786. if not local_changes:
  787. return
  788. # Note: We have persisted the full state at this point, we just haven't
  789. # cleared the `partial_room` flag.
  790. join_state_ids = await self._state_storage.get_state_ids_for_event(
  791. join_event_id, await_full_state=False
  792. )
  793. current_state_ids = await self.store.get_partial_current_state_ids(room_id)
  794. # Now we need to work out all servers that might have been in the room
  795. # at any point during our join.
  796. # First we look for any membership states that have changed between the
  797. # initial join and now...
  798. all_keys = set(join_state_ids)
  799. all_keys.update(current_state_ids)
  800. potentially_changed_hosts = set()
  801. for etype, state_key in all_keys:
  802. if etype != EventTypes.Member:
  803. continue
  804. prev = join_state_ids.get((etype, state_key))
  805. current = current_state_ids.get((etype, state_key))
  806. if prev != current:
  807. potentially_changed_hosts.add(get_domain_from_id(state_key))
  808. # ... then we add all the hosts that are currently joined to the room...
  809. current_hosts_in_room = await self.store.get_current_hosts_in_room(room_id)
  810. potentially_changed_hosts.update(current_hosts_in_room)
  811. # ... and finally we remove any hosts that we were told about, as we
  812. # will have sent device list updates to those hosts when they happened.
  813. known_hosts_at_join = await self.store.get_partial_state_servers_at_join(
  814. room_id
  815. )
  816. assert known_hosts_at_join is not None
  817. potentially_changed_hosts.difference_update(known_hosts_at_join)
  818. potentially_changed_hosts.discard(self.server_name)
  819. if not potentially_changed_hosts:
  820. # Nothing to do.
  821. return
  822. logger.info(
  823. "Found %d changed hosts to send device list updates to",
  824. len(potentially_changed_hosts),
  825. )
  826. for user_id, device_id in local_changes:
  827. await self.store.add_device_list_outbound_pokes(
  828. user_id=user_id,
  829. device_id=device_id,
  830. room_id=room_id,
  831. hosts=potentially_changed_hosts,
  832. context=None,
  833. )
  834. # Notify things that device lists need to be sent out.
  835. self.notifier.notify_replication()
  836. await self.federation_sender.send_device_messages(
  837. potentially_changed_hosts, immediate=False
  838. )
  839. def _update_device_from_client_ips(
  840. device: JsonDict, client_ips: Mapping[Tuple[str, str], Mapping[str, Any]]
  841. ) -> None:
  842. ip = client_ips.get((device["user_id"], device["device_id"]), {})
  843. device.update(
  844. {
  845. "last_seen_user_agent": ip.get("user_agent"),
  846. "last_seen_ts": ip.get("last_seen"),
  847. "last_seen_ip": ip.get("ip"),
  848. }
  849. )
  850. class DeviceListWorkerUpdater:
  851. "Handles incoming device list updates from federation and contacts the main process over replication"
  852. def __init__(self, hs: "HomeServer"):
  853. from synapse.replication.http.devices import (
  854. ReplicationMultiUserDevicesResyncRestServlet,
  855. )
  856. self._multi_user_device_resync_client = (
  857. ReplicationMultiUserDevicesResyncRestServlet.make_client(hs)
  858. )
  859. async def multi_user_device_resync(
  860. self, user_ids: List[str], mark_failed_as_stale: bool = True
  861. ) -> Dict[str, Optional[JsonMapping]]:
  862. """
  863. Like `user_device_resync` but operates on multiple users **from the same origin**
  864. at once.
  865. Returns:
  866. Dict from User ID to the same Dict as `user_device_resync`.
  867. """
  868. # mark_failed_as_stale is not sent. Ensure this doesn't break expectations.
  869. assert mark_failed_as_stale
  870. if not user_ids:
  871. # Shortcut empty requests
  872. return {}
  873. return await self._multi_user_device_resync_client(user_ids=user_ids)
  874. class DeviceListUpdater(DeviceListWorkerUpdater):
  875. "Handles incoming device list updates from federation and updates the DB"
  876. def __init__(self, hs: "HomeServer", device_handler: DeviceHandler):
  877. self.store = hs.get_datastores().main
  878. self.federation = hs.get_federation_client()
  879. self.clock = hs.get_clock()
  880. self.device_handler = device_handler
  881. self._notifier = hs.get_notifier()
  882. self._remote_edu_linearizer = Linearizer(name="remote_device_list")
  883. self._resync_linearizer = Linearizer(name="remote_device_resync")
  884. # user_id -> list of updates waiting to be handled.
  885. self._pending_updates: Dict[
  886. str, List[Tuple[str, str, Iterable[str], JsonDict]]
  887. ] = {}
  888. # Recently seen stream ids. We don't bother keeping these in the DB,
  889. # but they're useful to have them about to reduce the number of spurious
  890. # resyncs.
  891. self._seen_updates: ExpiringCache[str, Set[str]] = ExpiringCache(
  892. cache_name="device_update_edu",
  893. clock=self.clock,
  894. max_len=10000,
  895. expiry_ms=30 * 60 * 1000,
  896. iterable=True,
  897. )
  898. # Attempt to resync out of sync device lists every 30s.
  899. self._resync_retry_in_progress = False
  900. self.clock.looping_call(
  901. run_as_background_process,
  902. 30 * 1000,
  903. func=self._maybe_retry_device_resync,
  904. desc="_maybe_retry_device_resync",
  905. )
  906. @trace
  907. async def incoming_device_list_update(
  908. self, origin: str, edu_content: JsonDict
  909. ) -> None:
  910. """Called on incoming device list update from federation. Responsible
  911. for parsing the EDU and adding to pending updates list.
  912. """
  913. set_tag("origin", origin)
  914. set_tag("edu_content", str(edu_content))
  915. user_id = edu_content.pop("user_id")
  916. device_id = edu_content.pop("device_id")
  917. stream_id = str(edu_content.pop("stream_id")) # They may come as ints
  918. prev_ids = edu_content.pop("prev_id", [])
  919. if not isinstance(prev_ids, list):
  920. raise SynapseError(
  921. 400, "Device list update had an invalid 'prev_ids' field"
  922. )
  923. prev_ids = [str(p) for p in prev_ids] # They may come as ints
  924. if get_domain_from_id(user_id) != origin:
  925. # TODO: Raise?
  926. logger.warning(
  927. "Got device list update edu for %r/%r from %r",
  928. user_id,
  929. device_id,
  930. origin,
  931. )
  932. set_tag("error", True)
  933. log_kv(
  934. {
  935. "message": "Got a device list update edu from a user and "
  936. "device which does not match the origin of the request.",
  937. "user_id": user_id,
  938. "device_id": device_id,
  939. }
  940. )
  941. return
  942. # Check if we are partially joining any rooms. If so we need to store
  943. # all device list updates so that we can handle them correctly once we
  944. # know who is in the room.
  945. # TODO(faster_joins): this fetches and processes a bunch of data that we don't
  946. # use. Could be replaced by a tighter query e.g.
  947. # SELECT EXISTS(SELECT 1 FROM partial_state_rooms)
  948. partial_rooms = await self.store.get_partial_state_room_resync_info()
  949. if partial_rooms:
  950. await self.store.add_remote_device_list_to_pending(
  951. user_id,
  952. device_id,
  953. )
  954. self._notifier.notify_replication()
  955. room_ids = await self.store.get_rooms_for_user(user_id)
  956. if not room_ids:
  957. # We don't share any rooms with this user. Ignore update, as we
  958. # probably won't get any further updates.
  959. set_tag("error", True)
  960. log_kv(
  961. {
  962. "message": "Got an update from a user for which "
  963. "we don't share any rooms",
  964. "other user_id": user_id,
  965. }
  966. )
  967. logger.warning(
  968. "Got device list update edu for %r/%r, but don't share a room",
  969. user_id,
  970. device_id,
  971. )
  972. return
  973. logger.debug("Received device list update for %r/%r", user_id, device_id)
  974. self._pending_updates.setdefault(user_id, []).append(
  975. (device_id, stream_id, prev_ids, edu_content)
  976. )
  977. await self._handle_device_updates(user_id)
  978. @measure_func("_incoming_device_list_update")
  979. async def _handle_device_updates(self, user_id: str) -> None:
  980. "Actually handle pending updates."
  981. async with self._remote_edu_linearizer.queue(user_id):
  982. pending_updates = self._pending_updates.pop(user_id, [])
  983. if not pending_updates:
  984. # This can happen since we batch updates
  985. return
  986. for device_id, stream_id, prev_ids, _ in pending_updates:
  987. logger.debug(
  988. "Handling update %r/%r, ID: %r, prev: %r ",
  989. user_id,
  990. device_id,
  991. stream_id,
  992. prev_ids,
  993. )
  994. # Given a list of updates we check if we need to resync. This
  995. # happens if we've missed updates.
  996. resync = await self._need_to_do_resync(user_id, pending_updates)
  997. if logger.isEnabledFor(logging.INFO):
  998. logger.info(
  999. "Received device list update for %s, requiring resync: %s. Devices: %s",
  1000. user_id,
  1001. resync,
  1002. ", ".join(u[0] for u in pending_updates),
  1003. )
  1004. if resync:
  1005. # We mark as stale up front in case we get restarted.
  1006. await self.store.mark_remote_users_device_caches_as_stale([user_id])
  1007. run_as_background_process(
  1008. "_maybe_retry_device_resync",
  1009. self.multi_user_device_resync,
  1010. [user_id],
  1011. False,
  1012. )
  1013. else:
  1014. # Simply update the single device, since we know that is the only
  1015. # change (because of the single prev_id matching the current cache)
  1016. for device_id, stream_id, _, content in pending_updates:
  1017. await self.store.update_remote_device_list_cache_entry(
  1018. user_id, device_id, content, stream_id
  1019. )
  1020. await self.device_handler.notify_device_update(
  1021. user_id, [device_id for device_id, _, _, _ in pending_updates]
  1022. )
  1023. self._seen_updates.setdefault(user_id, set()).update(
  1024. stream_id for _, stream_id, _, _ in pending_updates
  1025. )
  1026. async def _need_to_do_resync(
  1027. self, user_id: str, updates: Iterable[Tuple[str, str, Iterable[str], JsonDict]]
  1028. ) -> bool:
  1029. """Given a list of updates for a user figure out if we need to do a full
  1030. resync, or whether we have enough data that we can just apply the delta.
  1031. """
  1032. seen_updates: Set[str] = self._seen_updates.get(user_id, set())
  1033. extremity = await self.store.get_device_list_last_stream_id_for_remote(user_id)
  1034. logger.debug("Current extremity for %r: %r", user_id, extremity)
  1035. stream_id_in_updates = set() # stream_ids in updates list
  1036. for _, stream_id, prev_ids, _ in updates:
  1037. if not prev_ids:
  1038. # We always do a resync if there are no previous IDs
  1039. return True
  1040. for prev_id in prev_ids:
  1041. if prev_id == extremity:
  1042. continue
  1043. elif prev_id in seen_updates:
  1044. continue
  1045. elif prev_id in stream_id_in_updates:
  1046. continue
  1047. else:
  1048. return True
  1049. stream_id_in_updates.add(stream_id)
  1050. return False
  1051. @trace
  1052. async def _maybe_retry_device_resync(self) -> None:
  1053. """Retry to resync device lists that are out of sync, except if another retry is
  1054. in progress.
  1055. """
  1056. if self._resync_retry_in_progress:
  1057. return
  1058. try:
  1059. # Prevent another call of this function to retry resyncing device lists so
  1060. # we don't send too many requests.
  1061. self._resync_retry_in_progress = True
  1062. # Get all of the users that need resyncing.
  1063. need_resync = await self.store.get_user_ids_requiring_device_list_resync()
  1064. # Filter out users whose host is marked as "down" up front.
  1065. hosts = await filter_destinations_by_retry_limiter(
  1066. {get_domain_from_id(u) for u in need_resync}, self.clock, self.store
  1067. )
  1068. hosts = set(hosts)
  1069. # Iterate over the set of user IDs.
  1070. for user_id in need_resync:
  1071. if get_domain_from_id(user_id) not in hosts:
  1072. continue
  1073. try:
  1074. # Try to resync the current user's devices list.
  1075. result = (await self.multi_user_device_resync([user_id], False))[
  1076. user_id
  1077. ]
  1078. # user_device_resync only returns a result if it managed to
  1079. # successfully resync and update the database. Updating the table
  1080. # of users requiring resync isn't necessary here as
  1081. # user_device_resync already does it (through
  1082. # self.store.update_remote_device_list_cache).
  1083. if result:
  1084. logger.debug(
  1085. "Successfully resynced the device list for %s",
  1086. user_id,
  1087. )
  1088. except Exception as e:
  1089. # If there was an issue resyncing this user, e.g. if the remote
  1090. # server sent a malformed result, just log the error instead of
  1091. # aborting all the subsequent resyncs.
  1092. logger.debug(
  1093. "Could not resync the device list for %s: %s",
  1094. user_id,
  1095. e,
  1096. )
  1097. finally:
  1098. # Allow future calls to retry resyncinc out of sync device lists.
  1099. self._resync_retry_in_progress = False
  1100. async def multi_user_device_resync(
  1101. self, user_ids: List[str], mark_failed_as_stale: bool = True
  1102. ) -> Dict[str, Optional[JsonMapping]]:
  1103. """
  1104. Like `user_device_resync` but operates on multiple users **from the same origin**
  1105. at once.
  1106. Returns:
  1107. Dict from User ID to the same Dict as `user_device_resync`.
  1108. """
  1109. if not user_ids:
  1110. return {}
  1111. origins = {UserID.from_string(user_id).domain for user_id in user_ids}
  1112. if len(origins) != 1:
  1113. raise InvalidAPICallError(f"Only one origin permitted, got {origins!r}")
  1114. result = {}
  1115. failed = set()
  1116. # TODO(Perf): Actually batch these up
  1117. for user_id in user_ids:
  1118. async with self._resync_linearizer.queue(user_id):
  1119. (
  1120. user_result,
  1121. user_failed,
  1122. ) = await self._user_device_resync_returning_failed(user_id)
  1123. result[user_id] = user_result
  1124. if user_failed:
  1125. failed.add(user_id)
  1126. if mark_failed_as_stale:
  1127. await self.store.mark_remote_users_device_caches_as_stale(failed)
  1128. return result
  1129. async def _user_device_resync_returning_failed(
  1130. self, user_id: str
  1131. ) -> Tuple[Optional[JsonMapping], bool]:
  1132. """Fetches all devices for a user and updates the device cache with them.
  1133. Args:
  1134. user_id: The user's id whose device_list will be updated.
  1135. Returns:
  1136. - A dict with device info as under the "devices" in the result of this
  1137. request:
  1138. https://matrix.org/docs/spec/server_server/r0.1.2#get-matrix-federation-v1-user-devices-userid
  1139. None when we weren't able to fetch the device info for some reason,
  1140. e.g. due to a connection problem.
  1141. - True iff the resync failed and the device list should be marked as stale.
  1142. """
  1143. # Check that we haven't gone and fetched the devices since we last
  1144. # checked if we needed to resync these device lists.
  1145. if await self.store.get_users_whose_devices_are_cached([user_id]):
  1146. cached = await self.store.get_cached_devices_for_user(user_id)
  1147. return cached, False
  1148. logger.debug("Attempting to resync the device list for %s", user_id)
  1149. log_kv({"message": "Doing resync to update device list."})
  1150. # Fetch all devices for the user.
  1151. origin = get_domain_from_id(user_id)
  1152. try:
  1153. result = await self.federation.query_user_devices(origin, user_id)
  1154. except NotRetryingDestination:
  1155. return None, True
  1156. except (RequestSendFailed, HttpResponseException) as e:
  1157. logger.warning(
  1158. "Failed to handle device list update for %s: %s",
  1159. user_id,
  1160. e,
  1161. )
  1162. # We abort on exceptions rather than accepting the update
  1163. # as otherwise synapse will 'forget' that its device list
  1164. # is out of date. If we bail then we will retry the resync
  1165. # next time we get a device list update for this user_id.
  1166. # This makes it more likely that the device lists will
  1167. # eventually become consistent.
  1168. return None, True
  1169. except FederationDeniedError as e:
  1170. set_tag("error", True)
  1171. log_kv({"reason": "FederationDeniedError"})
  1172. logger.info(e)
  1173. return None, False
  1174. except Exception as e:
  1175. set_tag("error", True)
  1176. log_kv(
  1177. {"message": "Exception raised by federation request", "exception": e}
  1178. )
  1179. logger.exception("Failed to handle device list update for %s", user_id)
  1180. return None, True
  1181. log_kv({"result": result})
  1182. stream_id = result["stream_id"]
  1183. devices = result["devices"]
  1184. # Get the master key and the self-signing key for this user if provided in the
  1185. # response (None if not in the response).
  1186. # The response will not contain the user signing key, as this key is only used by
  1187. # its owner, thus it doesn't make sense to send it over federation.
  1188. master_key = result.get("master_key")
  1189. self_signing_key = result.get("self_signing_key")
  1190. ignore_devices = False
  1191. # If the remote server has more than ~1000 devices for this user
  1192. # we assume that something is going horribly wrong (e.g. a bot
  1193. # that logs in and creates a new device every time it tries to
  1194. # send a message). Maintaining lots of devices per user in the
  1195. # cache can cause serious performance issues as if this request
  1196. # takes more than 60s to complete, internal replication from the
  1197. # inbound federation worker to the synapse master may time out
  1198. # causing the inbound federation to fail and causing the remote
  1199. # server to retry, causing a DoS. So in this scenario we give
  1200. # up on storing the total list of devices and only handle the
  1201. # delta instead.
  1202. if len(devices) > 1000:
  1203. logger.warning(
  1204. "Ignoring device list snapshot for %s as it has >1K devs (%d)",
  1205. user_id,
  1206. len(devices),
  1207. )
  1208. devices = []
  1209. ignore_devices = True
  1210. else:
  1211. prev_stream_id = await self.store.get_device_list_last_stream_id_for_remote(
  1212. user_id
  1213. )
  1214. cached_devices = await self.store.get_cached_devices_for_user(user_id)
  1215. # To ensure that a user with no devices is cached, we skip the resync only
  1216. # if we have a stream_id from previously writing a cache entry.
  1217. if prev_stream_id is not None and cached_devices == {
  1218. d["device_id"]: d for d in devices
  1219. }:
  1220. logging.info(
  1221. "Skipping device list resync for %s, as our cache matches already",
  1222. user_id,
  1223. )
  1224. devices = []
  1225. ignore_devices = True
  1226. for device in devices:
  1227. logger.debug(
  1228. "Handling resync update %r/%r, ID: %r",
  1229. user_id,
  1230. device["device_id"],
  1231. stream_id,
  1232. )
  1233. if not ignore_devices:
  1234. await self.store.update_remote_device_list_cache(
  1235. user_id, devices, stream_id
  1236. )
  1237. # mark the cache as valid, whether or not we actually processed any device
  1238. # list updates.
  1239. await self.store.mark_remote_user_device_cache_as_valid(user_id)
  1240. device_ids = [device["device_id"] for device in devices]
  1241. # Handle cross-signing keys.
  1242. cross_signing_device_ids = await self.process_cross_signing_key_update(
  1243. user_id,
  1244. master_key,
  1245. self_signing_key,
  1246. )
  1247. device_ids = device_ids + cross_signing_device_ids
  1248. if device_ids:
  1249. await self.device_handler.notify_device_update(user_id, device_ids)
  1250. # We clobber the seen updates since we've re-synced from a given
  1251. # point.
  1252. self._seen_updates[user_id] = {stream_id}
  1253. return result, False
  1254. async def process_cross_signing_key_update(
  1255. self,
  1256. user_id: str,
  1257. master_key: Optional[JsonDict],
  1258. self_signing_key: Optional[JsonDict],
  1259. ) -> List[str]:
  1260. """Process the given new master and self-signing key for the given remote user.
  1261. Args:
  1262. user_id: The ID of the user these keys are for.
  1263. master_key: The dict of the cross-signing master key as returned by the
  1264. remote server.
  1265. self_signing_key: The dict of the cross-signing self-signing key as returned
  1266. by the remote server.
  1267. Return:
  1268. The device IDs for the given keys.
  1269. """
  1270. device_ids = []
  1271. current_keys_map = await self.store.get_e2e_cross_signing_keys_bulk([user_id])
  1272. current_keys = current_keys_map.get(user_id) or {}
  1273. if master_key and master_key != current_keys.get("master"):
  1274. await self.store.set_e2e_cross_signing_key(user_id, "master", master_key)
  1275. _, verify_key = get_verify_key_from_cross_signing_key(master_key)
  1276. # verify_key is a VerifyKey from signedjson, which uses
  1277. # .version to denote the portion of the key ID after the
  1278. # algorithm and colon, which is the device ID
  1279. device_ids.append(verify_key.version)
  1280. if self_signing_key and self_signing_key != current_keys.get("self_signing"):
  1281. await self.store.set_e2e_cross_signing_key(
  1282. user_id, "self_signing", self_signing_key
  1283. )
  1284. _, verify_key = get_verify_key_from_cross_signing_key(self_signing_key)
  1285. device_ids.append(verify_key.version)
  1286. return device_ids
  1287. async def handle_room_un_partial_stated(self, room_id: str) -> None:
  1288. """Handles sending appropriate device list updates in a room that has
  1289. gone from partial to full state.
  1290. """
  1291. pending_updates = (
  1292. await self.store.get_pending_remote_device_list_updates_for_room(room_id)
  1293. )
  1294. for user_id, device_id in pending_updates:
  1295. logger.info(
  1296. "Got pending device list update in room %s: %s / %s",
  1297. room_id,
  1298. user_id,
  1299. device_id,
  1300. )
  1301. position = await self.store.add_device_change_to_streams(
  1302. user_id,
  1303. [device_id],
  1304. room_ids=[room_id],
  1305. )
  1306. if not position:
  1307. # This should only happen if there are no updates, which
  1308. # shouldn't happen when we've passed in a non-empty set of
  1309. # device IDs.
  1310. continue
  1311. self.device_handler.notifier.on_new_event(
  1312. StreamKeyType.DEVICE_LIST, position, rooms=[room_id]
  1313. )