From c540f7ffb76db6e7eb0dba6315a2aca56c15e8a3 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 3 Dec 2019 22:16:15 +0000 Subject: [PATCH 1/2] Fix exception when a cross-signed device is deleted (hopefully) --- changelog.d/6462.bugfix | 1 + .../data_stores/main/end_to_end_keys.py | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 changelog.d/6462.bugfix diff --git a/changelog.d/6462.bugfix b/changelog.d/6462.bugfix new file mode 100644 index 000000000000..c4359395266f --- /dev/null +++ b/changelog.d/6462.bugfix @@ -0,0 +1 @@ +Fix bug which lead to exceptions being thrown in a loop when a cross-signed device is deleted. diff --git a/synapse/storage/data_stores/main/end_to_end_keys.py b/synapse/storage/data_stores/main/end_to_end_keys.py index 073412a78de0..81bedf2e9744 100644 --- a/synapse/storage/data_stores/main/end_to_end_keys.py +++ b/synapse/storage/data_stores/main/end_to_end_keys.py @@ -145,13 +145,28 @@ def _get_e2e_device_keys_txn( txn.execute(signature_sql, signature_query_params) rows = self.cursor_to_dict(txn) + # add each cross-signing signature to the correct device in the result dict. for row in rows: + signing_user_id = row["user_id"] + signing_key_id = row["key_id"] target_user_id = row["target_user_id"] target_device_id = row["target_device_id"] - if target_user_id in result and target_device_id in result[target_user_id]: - result[target_user_id][target_device_id].setdefault( - "signatures", {} - ).setdefault(row["user_id"], {})[row["key_id"]] = row["signature"] + signature = row["signature"] + + target_user_result = result.get(target_user_id) + if not target_user_result: + continue + + target_device_result = result.get(target_device_id) + if not target_device_result: + # note that target_device_result will be None for deleted devices. + continue + + target_device_signatures = target_device_result.setdefault("signatures", {}) + signing_user_signatures = target_device_signatures.setdefault( + signing_user_id, {} + ) + signing_user_signatures[signing_key_id] = signature log_kv(result) return result From 7ef631a45da671841b19a233000e3dbb2bc26d5c Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Tue, 3 Dec 2019 21:14:52 -0500 Subject: [PATCH 2/2] fetch from the right dict --- synapse/storage/data_stores/main/end_to_end_keys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/data_stores/main/end_to_end_keys.py b/synapse/storage/data_stores/main/end_to_end_keys.py index 81bedf2e9744..8481c9892e95 100644 --- a/synapse/storage/data_stores/main/end_to_end_keys.py +++ b/synapse/storage/data_stores/main/end_to_end_keys.py @@ -157,7 +157,7 @@ def _get_e2e_device_keys_txn( if not target_user_result: continue - target_device_result = result.get(target_device_id) + target_device_result = target_user_result.get(target_device_id) if not target_device_result: # note that target_device_result will be None for deleted devices. continue