Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Always communicate device OTK counts to clients #10485

Merged
merged 6 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10485.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug where Synapse would signal "no change" in device one-time key counts, whereas in reality the number of keys had been exhausted.
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ class ToDeviceEventTypes:
RoomKeyRequest = "m.room_key_request"


class DeviceKeyAlgorithms:
"""Spec'd algorithms for the generation of per-device keys"""

ED25519 = "ed25519"
CURVE25519 = "curve25519"
SIGNED_CURVE25519 = "signed_curve25519"


class EduTypes:
Presence = "m.presence"

Expand Down
3 changes: 3 additions & 0 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,9 @@ async def generate_sync_result(
one_time_key_counts: JsonDict = {}
unused_fallback_key_types: List[str] = []
if device_id:
# TODO: If this is an incremental sync and there's been no change in a count
# since the provided since token, then simply omit that algorithm's entry
# altogether.
richvdh marked this conversation as resolved.
Show resolved Hide resolved
one_time_key_counts = await self.store.count_e2e_one_time_keys(
user_id, device_id
)
Expand Down
9 changes: 8 additions & 1 deletion synapse/storage/databases/main/end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from twisted.enterprise.adbapi import Connection

from synapse.api.constants import DeviceKeyAlgorithms
from synapse.logging.opentracing import log_kv, set_tag, trace
from synapse.storage._base import SQLBaseStore, db_to_json
from synapse.storage.database import DatabasePool, make_in_list_sql_clause
Expand Down Expand Up @@ -381,9 +382,15 @@ def _count_e2e_one_time_keys(txn):
" GROUP BY algorithm"
)
txn.execute(sql, (user_id, device_id))
result = {}

# Initially set the key count to 0. This ensures that the client will always
# receive *some count*, even if it's 0.
result = {DeviceKeyAlgorithms.SIGNED_CURVE25519: 0}

# Override entries with the count of any keys we pulled from the database
for algorithm, key_count in txn:
result[algorithm] = key_count

return result

return await self.db_pool.runInteraction(
Expand Down