From ebb76e69173bd7365c1d7d60d326defaf06cac4a Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 20 Jul 2023 17:25:19 +0100 Subject: If we are acting on a PDU, keep the "lpdu" hash intact. To match the logic defined by section 9.1.1 of draft-ralston-mimi-linearized-matrix-03. --- synapse/crypto/event_signing.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/synapse/crypto/event_signing.py b/synapse/crypto/event_signing.py index 34432891da..78032cd6eb 100644 --- a/synapse/crypto/event_signing.py +++ b/synapse/crypto/event_signing.py @@ -96,9 +96,6 @@ def check_event_content_hash( # Check the content hash of the LPDU, if this was sent via a hub. if event.room_version.linearized_matrix and event.hub_server: - # TODO(LM) I don't think this works since _check_dict_hash always strips - # all hashes (via compute_content_hash)? - # hashes must be a dictionary to have passed _check_dict_hash above. lpdu_hashes = hashes.get("lpdu") return _check_dict_hash( @@ -131,7 +128,19 @@ def compute_content_hash( event_dict.pop("age_ts", None) event_dict.pop("unsigned", None) event_dict.pop("signatures", None) - event_dict.pop("hashes", None) + + hashes = event_dict.pop("hashes", {}) + + # If we are calculating the content hash of an LPDU, `hashes` + # should be removed entirely. + if "hub_server" not in event_dict: + # Otherwise, we are calculating the content hash of a PDU. In that + # case, we keep the "lpdu" field inside "hashes", if it exists. + sha256_lpdu_hash = hashes.get("lpdu", {}).get("sha256") + if sha256_lpdu_hash is not None: + lpdu_dict = event_dict.setdefault("hashes", {}).setdefault("lpdu", {}) + lpdu_dict["sha256"] = sha256_lpdu_hash + event_dict.pop("outlier", None) event_dict.pop("destinations", None) -- cgit 1.4.1