您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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