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

Commit 7e37d7c

Browse files
committed
Merge branch 'rav/redact_changes/3' into rav/redact_changes/work
2 parents 46a4468 + 540c5e1 commit 7e37d7c

File tree

6 files changed

+49
-41
lines changed

6 files changed

+49
-41
lines changed

changelog.d/6806.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: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020

2121
from canonicaljson import encode_canonical_json
2222
from signedjson.sign import sign_json
23+
from signedjson.types import SigningKey
2324
from unpaddedbase64 import decode_base64, encode_base64
2425

2526
from synapse.api.errors import Codes, SynapseError
27+
from synapse.api.room_versions import RoomVersion
2628
from synapse.events.utils import prune_event, prune_event_dict
29+
from synapse.types import JsonDict
2730

2831
logger = logging.getLogger(__name__)
2932

@@ -137,20 +140,23 @@ def compute_event_signature(event_dict, signature_name, signing_key):
137140

138141

139142
def add_hashes_and_signatures(
140-
event_dict, signature_name, signing_key, hash_algorithm=hashlib.sha256
143+
room_version: RoomVersion,
144+
event_dict: JsonDict,
145+
signature_name: str,
146+
signing_key: SigningKey,
141147
):
142148
"""Add content hash and sign the event
143149
144150
Args:
145-
event_dict (dict): The event to add hashes to and sign
146-
signature_name (str): The name of the entity signing the event
151+
room_version: the version of the room this event is in
152+
153+
event_dict: The event to add hashes to and sign
154+
signature_name: The name of the entity signing the event
147155
(typically the server's hostname).
148-
signing_key (syutil.crypto.SigningKey): The key to sign with
149-
hash_algorithm: A hasher from `hashlib`, e.g. hashlib.sha256, to use
150-
to hash the event
156+
signing_key: The key to sign with
151157
"""
152158

153-
name, digest = compute_content_hash(event_dict, hash_algorithm=hash_algorithm)
159+
name, digest = compute_content_hash(event_dict, hash_algorithm=hashlib.sha256)
154160

155161
event_dict.setdefault("hashes", {})[name] = encode_base64(digest)
156162

synapse/events/builder.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
from typing import Optional
1516

1617
import attr
18+
from nacl.signing import SigningKey
1719

1820
from twisted.internet import defer
1921

@@ -23,13 +25,18 @@
2325
KNOWN_EVENT_FORMAT_VERSIONS,
2426
KNOWN_ROOM_VERSIONS,
2527
EventFormatVersions,
28+
RoomVersion,
2629
)
2730
from synapse.crypto.event_signing import add_hashes_and_signatures
28-
from synapse.types import EventID
31+
from synapse.events import (
32+
EventBase,
33+
_EventInternalMetadata,
34+
event_type_from_format_version,
35+
)
36+
from synapse.types import EventID, JsonDict
37+
from synapse.util import Clock
2938
from synapse.util.stringutils import random_string
3039

31-
from . import _EventInternalMetadata, event_type_from_format_version
32-
3340

3441
@attr.s(slots=True, cmp=False, frozen=True)
3542
class EventBuilder(object):
@@ -40,7 +47,7 @@ class EventBuilder(object):
4047
content/unsigned/internal_metadata fields are still mutable)
4148
4249
Attributes:
43-
format_version (int): Event format version
50+
room_version: Version of the target room
4451
room_id (str)
4552
type (str)
4653
sender (str)
@@ -63,7 +70,7 @@ class EventBuilder(object):
6370
_hostname = attr.ib()
6471
_signing_key = attr.ib()
6572

66-
format_version = attr.ib()
73+
room_version = attr.ib(type=RoomVersion)
6774

6875
room_id = attr.ib()
6976
type = attr.ib()
@@ -108,7 +115,8 @@ def build(self, prev_event_ids):
108115
)
109116
auth_ids = yield self._auth.compute_auth_events(self, state_ids)
110117

111-
if self.format_version == EventFormatVersions.V1:
118+
format_version = self.room_version.event_format
119+
if format_version == EventFormatVersions.V1:
112120
auth_events = yield self._store.add_event_hashes(auth_ids)
113121
prev_events = yield self._store.add_event_hashes(prev_event_ids)
114122
else:
@@ -148,7 +156,7 @@ def build(self, prev_event_ids):
148156
clock=self._clock,
149157
hostname=self._hostname,
150158
signing_key=self._signing_key,
151-
format_version=self.format_version,
159+
room_version=self.room_version,
152160
event_dict=event_dict,
153161
internal_metadata_dict=self.internal_metadata.get_dict(),
154162
)
@@ -201,7 +209,7 @@ def for_room_version(self, room_version, key_values):
201209
clock=self.clock,
202210
hostname=self.hostname,
203211
signing_key=self.signing_key,
204-
format_version=room_version.event_format,
212+
room_version=room_version,
205213
type=key_values["type"],
206214
state_key=key_values.get("state_key"),
207215
room_id=key_values["room_id"],
@@ -214,29 +222,19 @@ def for_room_version(self, room_version, key_values):
214222

215223

216224
def create_local_event_from_event_dict(
217-
clock,
218-
hostname,
219-
signing_key,
220-
format_version,
221-
event_dict,
222-
internal_metadata_dict=None,
223-
):
225+
clock: Clock,
226+
hostname: str,
227+
signing_key: SigningKey,
228+
room_version: RoomVersion,
229+
event_dict: JsonDict,
230+
internal_metadata_dict: Optional[JsonDict] = None,
231+
) -> EventBase:
224232
"""Takes a fully formed event dict, ensuring that fields like `origin`
225233
and `origin_server_ts` have correct values for a locally produced event,
226234
then signs and hashes it.
227-
228-
Args:
229-
clock (Clock)
230-
hostname (str)
231-
signing_key
232-
format_version (int)
233-
event_dict (dict)
234-
internal_metadata_dict (dict|None)
235-
236-
Returns:
237-
FrozenEvent
238235
"""
239236

237+
format_version = room_version.event_format
240238
if format_version not in KNOWN_EVENT_FORMAT_VERSIONS:
241239
raise Exception("No event format defined for version %r" % (format_version,))
242240

@@ -257,7 +255,7 @@ def create_local_event_from_event_dict(
257255

258256
event_dict.setdefault("signatures", {})
259257

260-
add_hashes_and_signatures(event_dict, hostname, signing_key)
258+
add_hashes_and_signatures(room_version, event_dict, hostname, signing_key)
261259
return event_type_from_format_version(format_version)(
262260
event_dict, internal_metadata_dict=internal_metadata_dict
263261
)

synapse/federation/federation_client.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,6 @@ def send_request(destination):
470470
if not room_version:
471471
raise UnsupportedRoomVersionError()
472472

473-
event_format = room_version_to_event_format(room_version_id)
474-
475473
pdu_dict = ret.get("event", None)
476474
if not isinstance(pdu_dict, dict):
477475
raise InvalidResponseError("Bad 'event' field in response")
@@ -490,7 +488,7 @@ def send_request(destination):
490488
self._clock,
491489
self.hostname,
492490
self.signing_key,
493-
format_version=event_format,
491+
room_version=room_version,
494492
event_dict=pdu_dict,
495493
)
496494

tests/crypto/test_event_signing.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import nacl.signing
1818
from unpaddedbase64 import decode_base64
1919

20+
from synapse.api.room_versions import RoomVersions
2021
from synapse.crypto.event_signing import add_hashes_and_signatures
2122
from synapse.events import FrozenEvent
2223

@@ -49,7 +50,9 @@ def test_sign_minimal(self):
4950
"unsigned": {"age_ts": 1000000},
5051
}
5152

52-
add_hashes_and_signatures(event_dict, HOSTNAME, self.signing_key)
53+
add_hashes_and_signatures(
54+
RoomVersions.V1, event_dict, HOSTNAME, self.signing_key
55+
)
5356

5457
event = FrozenEvent(event_dict)
5558

@@ -81,7 +84,9 @@ def test_sign_message(self):
8184
"unsigned": {"age_ts": 1000000},
8285
}
8386

84-
add_hashes_and_signatures(event_dict, HOSTNAME, self.signing_key)
87+
add_hashes_and_signatures(
88+
RoomVersions.V1, event_dict, HOSTNAME, self.signing_key
89+
)
8590

8691
event = FrozenEvent(event_dict)
8792

tests/handlers/test_presence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from signedjson.key import generate_signing_key
2020

2121
from synapse.api.constants import EventTypes, Membership, PresenceState
22-
from synapse.events import room_version_to_event_format
22+
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
2323
from synapse.events.builder import EventBuilder
2424
from synapse.handlers.presence import (
2525
EXTERNAL_PROCESS_EXPIRY,
@@ -597,7 +597,7 @@ def _add_new_user(self, room_id, user_id):
597597
clock=self.clock,
598598
hostname=hostname,
599599
signing_key=self.random_signing_key,
600-
format_version=room_version_to_event_format(room_version),
600+
room_version=KNOWN_ROOM_VERSIONS[room_version],
601601
room_id=room_id,
602602
type=EventTypes.Member,
603603
sender=user_id,

0 commit comments

Comments
 (0)