diff --git a/synapse/handlers/e2e_room_keys.py b/synapse/handlers/e2e_room_keys.py index 9e40ee7961..ea604584e6 100644 --- a/synapse/handlers/e2e_room_keys.py +++ b/synapse/handlers/e2e_room_keys.py @@ -100,6 +100,8 @@ class E2eRoomKeysHandler(object): rooms session_id(string): session ID to delete keys for, for None to delete keys for all sessions + Raises: + NotFoundError: if the backup version does not exist Returns: A dict containing the count and hash for the backup version """ @@ -216,7 +218,7 @@ class E2eRoomKeysHandler(object): if changed: version_hash = version_hash + 1 yield self.store.update_e2e_room_keys_version( - user_id, version, {"hash": str(version_hash)} + user_id, version, None, version_hash ) count = yield self.store.count_e2e_room_keys(user_id, version) @@ -366,10 +368,6 @@ class E2eRoomKeysHandler(object): if old_info["algorithm"] != version_info["algorithm"]: raise SynapseError(400, "Algorithm does not match", Codes.INVALID_PARAM) - # don't allow the user to set the hash - if "hash" in version_info: - del version_info["hash"] - yield self.store.update_e2e_room_keys_version( user_id, version, version_info ) diff --git a/synapse/storage/e2e_room_keys.py b/synapse/storage/e2e_room_keys.py index a10ee71fe7..94a6a7300c 100644 --- a/synapse/storage/e2e_room_keys.py +++ b/synapse/storage/e2e_room_keys.py @@ -169,7 +169,7 @@ class EndToEndRoomKeyStore(SQLBaseStore): @staticmethod def _get_e2e_room_keys_multi_txn(txn, user_id, version, room_keys): - if not len(room_keys): + if not room_keys: return {} where_clauses = [] @@ -185,6 +185,10 @@ class EndToEndRoomKeyStore(SQLBaseStore): % (",".join(["?" for _ in sessions]),) ) + # check if we're actually querying something + if not where_clauses: + return {} + sql = """ SELECT room_id, session_id, first_message_index, forwarded_count, is_verified, session_data @@ -282,6 +286,7 @@ class EndToEndRoomKeyStore(SQLBaseStore): version(str) algorithm(str) auth_data(object): opaque dict supplied by the client + hash(int): tag of the keys in the backup """ def _get_e2e_room_keys_version_info_txn(txn): @@ -351,27 +356,29 @@ class EndToEndRoomKeyStore(SQLBaseStore): "create_e2e_room_keys_version_txn", _create_e2e_room_keys_version_txn ) - def update_e2e_room_keys_version(self, user_id, version, info): + def update_e2e_room_keys_version(self, user_id, version, info=None, version_hash=None): """Update a given backup version Args: user_id(str): the user whose backup version we're updating version(str): the version ID of the backup version we're updating info(dict): the new backup version info to store + version_hash(str): tag of the keys in the backup """ updatevalues = {} - if "auth_data" in info: + if info and "auth_data" in info: updatevalues["auth_data"] = json.dumps(info["auth_data"]) - if "hash" in info: - updatevalues["hash"] = info["hash"] + if version_hash: + updatevalues["hash"] = version_hash - return self._simple_update( - table="e2e_room_keys_versions", - keyvalues={"user_id": user_id, "version": version}, - updatevalues=updatevalues, - desc="update_e2e_room_keys_version", - ) + if updatevalues: + return self._simple_update( + table="e2e_room_keys_versions", + keyvalues={"user_id": user_id, "version": version}, + updatevalues=updatevalues, + desc="update_e2e_room_keys_version", + ) def delete_e2e_room_keys_version(self, user_id, version=None): """Delete a given backup version of the user's room keys.