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

Commit 7911e28

Browse files
Prevent federation user keys query from returning device names if disallowed (#14304)
1 parent 730b13d commit 7911e28

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

changelog.d/14304.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug introduced in 1.34.0 where device names would be returned via a federation user key query request when `allow_device_name_lookup_over_federation` was set to `false`.

synapse/handlers/e2e_keys.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
class E2eKeysHandler:
5151
def __init__(self, hs: "HomeServer"):
52+
self.config = hs.config
5253
self.store = hs.get_datastores().main
5354
self.federation = hs.get_federation_client()
5455
self.device_handler = hs.get_device_handler()
@@ -431,13 +432,17 @@ async def get_cross_signing_keys_from_cache(
431432
@trace
432433
@cancellable
433434
async def query_local_devices(
434-
self, query: Mapping[str, Optional[List[str]]]
435+
self,
436+
query: Mapping[str, Optional[List[str]]],
437+
include_displaynames: bool = True,
435438
) -> Dict[str, Dict[str, dict]]:
436439
"""Get E2E device keys for local users
437440
438441
Args:
439442
query: map from user_id to a list
440443
of devices to query (None for all devices)
444+
include_displaynames: Whether to include device displaynames in the returned
445+
device details.
441446
442447
Returns:
443448
A map from user_id -> device_id -> device details
@@ -469,7 +474,9 @@ async def query_local_devices(
469474
# make sure that each queried user appears in the result dict
470475
result_dict[user_id] = {}
471476

472-
results = await self.store.get_e2e_device_keys_for_cs_api(local_query)
477+
results = await self.store.get_e2e_device_keys_for_cs_api(
478+
local_query, include_displaynames
479+
)
473480

474481
# Build the result structure
475482
for user_id, device_keys in results.items():
@@ -482,11 +489,33 @@ async def query_local_devices(
482489
async def on_federation_query_client_keys(
483490
self, query_body: Dict[str, Dict[str, Optional[List[str]]]]
484491
) -> JsonDict:
485-
"""Handle a device key query from a federated server"""
492+
"""Handle a device key query from a federated server:
493+
494+
Handles the path: GET /_matrix/federation/v1/users/keys/query
495+
496+
Args:
497+
query_body: The body of the query request. Should contain a key
498+
"device_keys" that map to a dictionary of user ID's -> list of
499+
device IDs. If the list of device IDs is empty, all devices of
500+
that user will be queried.
501+
502+
Returns:
503+
A json dictionary containing the following:
504+
- device_keys: A dictionary containing the requested device information.
505+
- master_keys: An optional dictionary of user ID -> master cross-signing
506+
key info.
507+
- self_signing_key: An optional dictionary of user ID -> self-signing
508+
key info.
509+
"""
486510
device_keys_query: Dict[str, Optional[List[str]]] = query_body.get(
487511
"device_keys", {}
488512
)
489-
res = await self.query_local_devices(device_keys_query)
513+
res = await self.query_local_devices(
514+
device_keys_query,
515+
include_displaynames=(
516+
self.config.federation.allow_device_name_lookup_over_federation
517+
),
518+
)
490519
ret = {"device_keys": res}
491520

492521
# add in the cross-signing keys

synapse/storage/databases/main/end_to_end_keys.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,15 @@ async def get_e2e_device_keys_for_federation_query(
139139
@trace
140140
@cancellable
141141
async def get_e2e_device_keys_for_cs_api(
142-
self, query_list: List[Tuple[str, Optional[str]]]
142+
self,
143+
query_list: List[Tuple[str, Optional[str]]],
144+
include_displaynames: bool = True,
143145
) -> Dict[str, Dict[str, JsonDict]]:
144146
"""Fetch a list of device keys, formatted suitably for the C/S API.
145147
Args:
146-
query_list(list): List of pairs of user_ids and device_ids.
148+
query_list: List of pairs of user_ids and device_ids.
149+
include_displaynames: Whether to include the displayname of returned devices
150+
(if one exists).
147151
Returns:
148152
Dict mapping from user-id to dict mapping from device_id to
149153
key data. The key data will be a dict in the same format as the
@@ -166,9 +170,12 @@ async def get_e2e_device_keys_for_cs_api(
166170
continue
167171

168172
r["unsigned"] = {}
169-
display_name = device_info.display_name
170-
if display_name is not None:
171-
r["unsigned"]["device_display_name"] = display_name
173+
if include_displaynames:
174+
# Include the device's display name in the "unsigned" dictionary
175+
display_name = device_info.display_name
176+
if display_name is not None:
177+
r["unsigned"]["device_display_name"] = display_name
178+
172179
rv[user_id][device_id] = r
173180

174181
return rv

0 commit comments

Comments
 (0)