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

Commit 7f93eb1

Browse files
authored
pass room_version into compute_event_signature (#6807)
1 parent a5afdd1 commit 7f93eb1

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

changelog.d/6807.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactoring work in preparation for changing the event redaction algorithm.

synapse/crypto/event_signing.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
2+
#
33
# Copyright 2014-2016 OpenMarket Ltd
4+
# Copyright 2020 The Matrix.org Foundation C.I.C.
45
#
56
# Licensed under the Apache License, Version 2.0 (the "License");
67
# you may not use this file except in compliance with the License.
@@ -17,6 +18,7 @@
1718
import collections.abc
1819
import hashlib
1920
import logging
21+
from typing import Dict
2022

2123
from canonicaljson import encode_canonical_json
2224
from signedjson.sign import sign_json
@@ -115,18 +117,28 @@ def compute_event_reference_hash(event, hash_algorithm=hashlib.sha256):
115117
return hashed.name, hashed.digest()
116118

117119

118-
def compute_event_signature(event_dict, signature_name, signing_key):
120+
def compute_event_signature(
121+
room_version: RoomVersion,
122+
event_dict: JsonDict,
123+
signature_name: str,
124+
signing_key: SigningKey,
125+
) -> Dict[str, Dict[str, str]]:
119126
"""Compute the signature of the event for the given name and key.
120127
121128
Args:
122-
event_dict (dict): The event as a dict
123-
signature_name (str): The name of the entity signing the event
129+
room_version: the version of the room that this event is in.
130+
(the room version determines the redaction algorithm and hence the
131+
json to be signed)
132+
133+
event_dict: The event as a dict
134+
135+
signature_name: The name of the entity signing the event
124136
(typically the server's hostname).
125-
signing_key (syutil.crypto.SigningKey): The key to sign with
137+
138+
signing_key: The key to sign with
126139
127140
Returns:
128-
dict[str, dict[str, str]]: Returns a dictionary in the same format of
129-
an event's signatures field.
141+
a dictionary in the same format of an event's signatures field.
130142
"""
131143
redact_json = prune_event_dict(event_dict)
132144
redact_json.pop("age_ts", None)
@@ -161,5 +173,5 @@ def add_hashes_and_signatures(
161173
event_dict.setdefault("hashes", {})[name] = encode_base64(digest)
162174

163175
event_dict["signatures"] = compute_event_signature(
164-
event_dict, signature_name=signature_name, signing_key=signing_key
176+
room_version, event_dict, signature_name=signature_name, signing_key=signing_key
165177
)

synapse/handlers/federation.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,10 @@ def on_invite_request(
15281528

15291529
event.signatures.update(
15301530
compute_event_signature(
1531-
event.get_pdu_json(), self.hs.hostname, self.hs.config.signing_key[0]
1531+
room_version,
1532+
event.get_pdu_json(),
1533+
self.hs.hostname,
1534+
self.hs.config.signing_key[0],
15321535
)
15331536
)
15341537

0 commit comments

Comments
 (0)