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

Query missing cross-signing keys on local sig upload #7289

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8348481
Query missing cross-signing keys on local sig upload
anoadragon453 Apr 16, 2020
1063495
Add changelog
anoadragon453 Apr 16, 2020
cc86457
Save retrieved keys to the db
anoadragon453 Apr 16, 2020
c265bc7
lint
anoadragon453 Apr 16, 2020
39ed9f6
Fix and de-brittle remote result dict processing
anoadragon453 Apr 16, 2020
fd8d154
Use query_user_devices instead, assume only master, self_signing key …
anoadragon453 Apr 16, 2020
759b6b0
Make changelog more useful
anoadragon453 Apr 16, 2020
03d2c8c
Remove very specific exception handling
anoadragon453 Apr 16, 2020
b386658
Wrap get_verify_key_from_cross_signing_key in a try/except
anoadragon453 Apr 16, 2020
bd9a671
Note that _get_e2e_cross_signing_verify_key can raise a SynapseError
anoadragon453 Apr 16, 2020
745e653
lint
anoadragon453 Apr 16, 2020
f8b6f14
Add comment explaining why this is useful
anoadragon453 Apr 16, 2020
37ae643
Only fetch master and self_signing key types
anoadragon453 Apr 16, 2020
83861c3
Fix log statements, docstrings
anoadragon453 Apr 17, 2020
671178b
Remove extraneous items from remote query try/except
anoadragon453 Apr 17, 2020
2d88b5d
lint
anoadragon453 Apr 17, 2020
f417300
Factor key retrieval out into a separate function
anoadragon453 Apr 17, 2020
2f87051
Send device updates, modeled after SigningKeyEduUpdater._handle_signi…
anoadragon453 Apr 17, 2020
5990d1c
Update method docstring
anoadragon453 Apr 17, 2020
4f8ba5c
Remove extraneous key_id and verify_key
anoadragon453 Apr 20, 2020
9240abc
Update changelog
anoadragon453 Apr 20, 2020
3282423
Update changelog
anoadragon453 Apr 20, 2020
95dd9d5
Resolve review comments
anoadragon453 Apr 20, 2020
4f41f37
lint
anoadragon453 Apr 20, 2020
6d559ba
Update changelog.d/7289.bugfix
anoadragon453 Apr 21, 2020
1b4dda5
Refactor _get_e2e_cross_signing_verify_key
anoadragon453 Apr 21, 2020
7cb1e48
Refactor and add validation to _retrieve_cross_signing_keys_for_remot…
anoadragon453 Apr 21, 2020
74eaac0
Improve details of query_client_keys and query_user_devices docstrings
anoadragon453 Apr 21, 2020
b08b7c7
lint
anoadragon453 Apr 21, 2020
de29d1f
Merge branch 'anoa/query_cross_signing_keys_key_upload' of github.com…
anoadragon453 Apr 21, 2020
8484a72
Address review comments
anoadragon453 Apr 22, 2020
2932b9b
JSON brace endings on separate lines
anoadragon453 Apr 22, 2020
ebea2ee
Spaces and braces
anoadragon453 Apr 22, 2020
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
2 changes: 1 addition & 1 deletion changelog.d/7289.bugfix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fix a bug with cross-signing devices with remote users when they did not share a room with any user on the local homeserver.
Fix a bug with cross-signing devices belonging to remote users who did not share a room with any user on the local homeserver.
25 changes: 19 additions & 6 deletions synapse/federation/transport/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ def query_client_keys(self, destination, query_content, timeout):
"<user_id>": {
"<device_id>": {...}
} }
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
"master_keys": {
"master_key": {
"<user_id>": {...}
} }
"self_signing_keys": {
"self_signing_key": {
"<user_id>": {...}
} } }
} }

Args:
destination(str): The server to query.
Expand All @@ -436,9 +436,22 @@ def query_user_devices(self, destination, user_id, timeout):
{
"stream_id": "...",
"devices": [ { ... } ],
"master_key": { ... },
"self_signing_key: { ... }
}
"master_key": {
"user_id": "<user_id>",
"usage": [...],
"keys": {...},
"signatures": {
"<user_id>": {...}
}
},
"self_signing_key": {
"user_id": "<user_id>",
"usage": [...],
"keys": {...},
"signatures": {
"<user_id>": {...}
}
} }
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved

Args:
destination(str): The server to query.
Expand Down
93 changes: 53 additions & 40 deletions synapse/handlers/e2e_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,40 +989,33 @@ def _get_e2e_cross_signing_verify_key(
user_id, key_type, from_user_id
)

if key:
# We found a copy of this key in our database. Decode and return it
key_id, verify_key = get_verify_key_from_cross_signing_key(key)
return key, key_id, verify_key

# If we couldn't find the key locally, and we're looking for keys of
# another user then attempt to fetch the missing key from the remote
# user's server.
#
# We may run into this in possible edge cases where a user tries to
# cross-sign a remote user, but does not share any rooms with them yet.
# Thus, we would not have their key list yet. We fetch the key here,
# Thus, we would not have their key list yet. We instead fetch the key,
# store it and notify clients of new, associated device IDs.
if (
key is None
and not self.is_mine(user)
# We only get "master" and "self_signing" keys from remote servers
and key_type in ["master", "self_signing"]
):
(
key,
key_id,
verify_key,
) = yield self._retrieve_cross_signing_keys_for_remote_user(user, key_type)
if self.is_mine(user) or key_type not in ["master", "self_signing"]:
# Note that master and self_signing keys are the only cross-signing keys we
# can request over federation
return
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved

(
key,
key_id,
verify_key,
) = yield self._retrieve_cross_signing_keys_for_remote_user(user, key_type)

if key is None:
logger.warning("No %s key found for %s", key_type, user_id)
raise NotFoundError("No %s key found for %s" % (key_type, user_id))

try:
key_id, verify_key = get_verify_key_from_cross_signing_key(key)
except ValueError as e:
logger.warning(
"Invalid %s key retrieved: %s - %s %s", key_type, key, type(e), e,
)
raise SynapseError(
502, "Invalid %s key retrieved from remote server" % (key_type,)
)

return key, key_id, verify_key

@defer.inlineCallbacks
Expand Down Expand Up @@ -1058,22 +1051,33 @@ def _retrieve_cross_signing_keys_for_remote_user(
return None, None, None

# Process each of the retrieved cross-signing keys
final_key = None
final_key_id = None
final_verify_key = None
device_ids = []
desired_key = None
desired_key_id = None
desired_verify_key = None
retrieved_device_ids = []
for key_type in ["master", "self_signing"]:
key_content = remote_result.get(key_type + "_key")
if not key_content:
continue

# At the same time, store this key in the db for
# subsequent queries
yield self.store.set_e2e_cross_signing_key(
user.to_string(), key_type, key_content
)
# Ensure these keys belong to the correct user
if "user_id" not in key_content:
logger.warning(
"Invalid %s key retrieved, missing user_id field: %s",
key_type,
key_content,
)
continue
if user.to_string() != key_content["user_id"]:
logger.warning(
"Found %s key of user %s when querying for keys of user %s",
key_type,
key_content["user_id"],
user.to_string(),
)
continue

# Note down the device ID attached to this key
# Validate the key contents
try:
# verify_key is a VerifyKey from signedjson, which uses
# .version to denote the portion of the key ID after the
Expand All @@ -1088,19 +1092,28 @@ def _retrieve_cross_signing_keys_for_remote_user(
e,
)
continue
device_ids.append(verify_key.version)

# Note down the device ID attached to this key
retrieved_device_ids.append(verify_key.version)

# If this is the desired key type, save it and its ID/VerifyKey
if key_type == desired_key_type:
final_key = key_content
final_verify_key = verify_key
final_key_id = key_id
desired_key = key_content
desired_verify_key = verify_key
desired_key_id = key_id

# At the same time, store this key in the db for subsequent queries
yield self.store.set_e2e_cross_signing_key(
user.to_string(), key_type, key_content
)

# Notify clients that new devices for this user have been discovered
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
if device_ids:
yield self.device_handler.notify_device_update(user.to_string(), device_ids)
if retrieved_device_ids:
yield self.device_handler.notify_device_update(
user.to_string(), retrieved_device_ids
)

return final_key, final_key_id, final_verify_key
return desired_key, desired_key_id, desired_verify_key


def _check_cross_signing_key(key, user_id, key_type, signing_key=None):
Expand Down