From 732d63d6b9e5767dd897909456d8a0f13feaab10 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 16 Mar 2021 15:39:27 -0400 Subject: [PATCH 1/6] only save remote cross-signing keys if they're different from the current ones --- synapse/handlers/device.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/device.py b/synapse/handlers/device.py index df3cdc8fba11..7d9b641fc6cf 100644 --- a/synapse/handlers/device.py +++ b/synapse/handlers/device.py @@ -973,14 +973,17 @@ async def process_cross_signing_key_update( """ device_ids = [] - if master_key: + current_keys = await self.store.get_e2e_cross_signing_keys_bulk([user_id]) + current_keys = current_keys.get(user_id) + + if master_key and master_key != current_keys.get("master"): await self.store.set_e2e_cross_signing_key(user_id, "master", master_key) _, verify_key = get_verify_key_from_cross_signing_key(master_key) # verify_key is a VerifyKey from signedjson, which uses # .version to denote the portion of the key ID after the # algorithm and colon, which is the device ID device_ids.append(verify_key.version) - if self_signing_key: + if self_signing_key and self_signing_key != current_keys.get("self_signing"): await self.store.set_e2e_cross_signing_key( user_id, "self_signing", self_signing_key ) From 6629ae6e4c7e141ebf1346ec5292b2b783f14550 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 16 Mar 2021 15:47:47 -0400 Subject: [PATCH 2/6] add changelog --- changelog.d/9634.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/9634.misc diff --git a/changelog.d/9634.misc b/changelog.d/9634.misc new file mode 100644 index 000000000000..8e54c480f948 --- /dev/null +++ b/changelog.d/9634.misc @@ -0,0 +1 @@ +Only save remote cross-signing keys if they're different from the current ones. From 3d7d358b38f11ae7d2999048afb70c56018c18da Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 16 Mar 2021 17:25:36 -0400 Subject: [PATCH 3/6] make mypy happy --- synapse/handlers/device.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/device.py b/synapse/handlers/device.py index 7d9b641fc6cf..0de004b6c932 100644 --- a/synapse/handlers/device.py +++ b/synapse/handlers/device.py @@ -973,8 +973,8 @@ async def process_cross_signing_key_update( """ device_ids = [] - current_keys = await self.store.get_e2e_cross_signing_keys_bulk([user_id]) - current_keys = current_keys.get(user_id) + current_keys_map = await self.store.get_e2e_cross_signing_keys_bulk([user_id]) + current_keys = current_keys_map.get(user_id) or {} if master_key and master_key != current_keys.get("master"): await self.store.set_e2e_cross_signing_key(user_id, "master", master_key) From a1b2c78670b8bea3f5d96076ea62fa01308f6462 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 16 Mar 2021 17:26:10 -0400 Subject: [PATCH 4/6] also don't notify if devices are unchanged --- changelog.d/9634.misc | 3 ++- synapse/handlers/device.py | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/changelog.d/9634.misc b/changelog.d/9634.misc index 8e54c480f948..f27edf0f079c 100644 --- a/changelog.d/9634.misc +++ b/changelog.d/9634.misc @@ -1 +1,2 @@ -Only save remote cross-signing keys if they're different from the current ones. +Only save remote cross-signing and device keys if they're different from the +current ones. diff --git a/synapse/handlers/device.py b/synapse/handlers/device.py index 0de004b6c932..bb1d868d1806 100644 --- a/synapse/handlers/device.py +++ b/synapse/handlers/device.py @@ -907,6 +907,7 @@ async def user_device_resync( master_key = result.get("master_key") self_signing_key = result.get("self_signing_key") + ignore_devices = False # If the remote server has more than ~1000 devices for this user # we assume that something is going horribly wrong (e.g. a bot # that logs in and creates a new device every time it tries to @@ -925,6 +926,12 @@ async def user_device_resync( len(devices), ) devices = [] + ignore_devices = True + else: + cached_devices = await self.store.get_cached_devices_for_user(user_id) + if cached_devices == {d["device_id"]:d for d in devices}: + devices = [] + ignore_devices = True for device in devices: logger.debug( @@ -934,7 +941,8 @@ async def user_device_resync( stream_id, ) - await self.store.update_remote_device_list_cache(user_id, devices, stream_id) + if not ignore_devices: + await self.store.update_remote_device_list_cache(user_id, devices, stream_id) device_ids = [device["device_id"] for device in devices] # Handle cross-signing keys. @@ -945,7 +953,8 @@ async def user_device_resync( ) device_ids = device_ids + cross_signing_device_ids - await self.device_handler.notify_device_update(user_id, device_ids) + if device_ids: + await self.device_handler.notify_device_update(user_id, device_ids) # We clobber the seen updates since we've re-synced from a given # point. From b0b6efb7c7140a9172d78923234ba9727fcee206 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 16 Mar 2021 17:31:42 -0400 Subject: [PATCH 5/6] run black --- synapse/handlers/device.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/device.py b/synapse/handlers/device.py index bb1d868d1806..4a079c2ebecd 100644 --- a/synapse/handlers/device.py +++ b/synapse/handlers/device.py @@ -929,7 +929,7 @@ async def user_device_resync( ignore_devices = True else: cached_devices = await self.store.get_cached_devices_for_user(user_id) - if cached_devices == {d["device_id"]:d for d in devices}: + if cached_devices == {d["device_id"]: d for d in devices}: devices = [] ignore_devices = True @@ -942,7 +942,9 @@ async def user_device_resync( ) if not ignore_devices: - await self.store.update_remote_device_list_cache(user_id, devices, stream_id) + await self.store.update_remote_device_list_cache( + user_id, devices, stream_id + ) device_ids = [device["device_id"] for device in devices] # Handle cross-signing keys. From e70fe4e8051bb64859b4453c29d8971524fbc065 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 17 Mar 2021 07:13:25 -0400 Subject: [PATCH 6/6] Update changelog. --- changelog.d/9634.misc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/changelog.d/9634.misc b/changelog.d/9634.misc index f27edf0f079c..59ac42cb832a 100644 --- a/changelog.d/9634.misc +++ b/changelog.d/9634.misc @@ -1,2 +1 @@ -Only save remote cross-signing and device keys if they're different from the -current ones. +Only save remote cross-signing and device keys if they're different from the current ones.