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

Commit 9dde9c9

Browse files
authored
Implement MSC2176: Updated redaction rules (#8984)
An experimental room version ("org.matrix.msc2176") contains the new redaction rules for testing.
1 parent 111b673 commit 9dde9c9

File tree

5 files changed

+206
-30
lines changed

5 files changed

+206
-30
lines changed

changelog.d/8984.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement [MSC2176](https://github.com/matrix-org/matrix-doc/pull/2176) in an experimental room version.

synapse/api/room_versions.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ class RoomDisposition:
5151
class RoomVersion:
5252
"""An object which describes the unique attributes of a room version."""
5353

54-
identifier = attr.ib() # str; the identifier for this version
55-
disposition = attr.ib() # str; one of the RoomDispositions
56-
event_format = attr.ib() # int; one of the EventFormatVersions
57-
state_res = attr.ib() # int; one of the StateResolutionVersions
58-
enforce_key_validity = attr.ib() # bool
54+
identifier = attr.ib(type=str) # the identifier for this version
55+
disposition = attr.ib(type=str) # one of the RoomDispositions
56+
event_format = attr.ib(type=int) # one of the EventFormatVersions
57+
state_res = attr.ib(type=int) # one of the StateResolutionVersions
58+
enforce_key_validity = attr.ib(type=bool)
5959

6060
# bool: before MSC2261/MSC2432, m.room.aliases had special auth rules and redaction rules
6161
special_case_aliases_auth = attr.ib(type=bool)
@@ -64,9 +64,11 @@ class RoomVersion:
6464
# * Floats
6565
# * NaN, Infinity, -Infinity
6666
strict_canonicaljson = attr.ib(type=bool)
67-
# bool: MSC2209: Check 'notifications' key while verifying
67+
# MSC2209: Check 'notifications' key while verifying
6868
# m.room.power_levels auth rules.
6969
limit_notifications_power_levels = attr.ib(type=bool)
70+
# MSC2174/MSC2176: Apply updated redaction rules algorithm.
71+
msc2176_redaction_rules = attr.ib(type=bool)
7072

7173

7274
class RoomVersions:
@@ -79,6 +81,7 @@ class RoomVersions:
7981
special_case_aliases_auth=True,
8082
strict_canonicaljson=False,
8183
limit_notifications_power_levels=False,
84+
msc2176_redaction_rules=False,
8285
)
8386
V2 = RoomVersion(
8487
"2",
@@ -89,6 +92,7 @@ class RoomVersions:
8992
special_case_aliases_auth=True,
9093
strict_canonicaljson=False,
9194
limit_notifications_power_levels=False,
95+
msc2176_redaction_rules=False,
9296
)
9397
V3 = RoomVersion(
9498
"3",
@@ -99,6 +103,7 @@ class RoomVersions:
99103
special_case_aliases_auth=True,
100104
strict_canonicaljson=False,
101105
limit_notifications_power_levels=False,
106+
msc2176_redaction_rules=False,
102107
)
103108
V4 = RoomVersion(
104109
"4",
@@ -109,6 +114,7 @@ class RoomVersions:
109114
special_case_aliases_auth=True,
110115
strict_canonicaljson=False,
111116
limit_notifications_power_levels=False,
117+
msc2176_redaction_rules=False,
112118
)
113119
V5 = RoomVersion(
114120
"5",
@@ -119,6 +125,7 @@ class RoomVersions:
119125
special_case_aliases_auth=True,
120126
strict_canonicaljson=False,
121127
limit_notifications_power_levels=False,
128+
msc2176_redaction_rules=False,
122129
)
123130
V6 = RoomVersion(
124131
"6",
@@ -129,6 +136,18 @@ class RoomVersions:
129136
special_case_aliases_auth=False,
130137
strict_canonicaljson=True,
131138
limit_notifications_power_levels=True,
139+
msc2176_redaction_rules=False,
140+
)
141+
MSC2176 = RoomVersion(
142+
"org.matrix.msc2176",
143+
RoomDisposition.UNSTABLE,
144+
EventFormatVersions.V3,
145+
StateResolutionVersions.V2,
146+
enforce_key_validity=True,
147+
special_case_aliases_auth=False,
148+
strict_canonicaljson=True,
149+
limit_notifications_power_levels=True,
150+
msc2176_redaction_rules=True,
132151
)
133152

134153

@@ -141,5 +160,6 @@ class RoomVersions:
141160
RoomVersions.V4,
142161
RoomVersions.V5,
143162
RoomVersions.V6,
163+
RoomVersions.MSC2176,
144164
)
145165
} # type: Dict[str, RoomVersion]

synapse/events/utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ def prune_event_dict(room_version: RoomVersion, event_dict: dict) -> dict:
7979
"state_key",
8080
"depth",
8181
"prev_events",
82-
"prev_state",
8382
"auth_events",
8483
"origin",
8584
"origin_server_ts",
86-
"membership",
8785
]
8886

87+
# Room versions from before MSC2176 had additional allowed keys.
88+
if not room_version.msc2176_redaction_rules:
89+
allowed_keys.extend(["prev_state", "membership"])
90+
8991
event_type = event_dict["type"]
9092

9193
new_content = {}
@@ -98,6 +100,10 @@ def add_fields(*fields):
98100
if event_type == EventTypes.Member:
99101
add_fields("membership")
100102
elif event_type == EventTypes.Create:
103+
# MSC2176 rules state that create events cannot be redacted.
104+
if room_version.msc2176_redaction_rules:
105+
return event_dict
106+
101107
add_fields("creator")
102108
elif event_type == EventTypes.JoinRules:
103109
add_fields("join_rule")
@@ -112,10 +118,16 @@ def add_fields(*fields):
112118
"kick",
113119
"redact",
114120
)
121+
122+
if room_version.msc2176_redaction_rules:
123+
add_fields("invite")
124+
115125
elif event_type == EventTypes.Aliases and room_version.special_case_aliases_auth:
116126
add_fields("aliases")
117127
elif event_type == EventTypes.RoomHistoryVisibility:
118128
add_fields("history_visibility")
129+
elif event_type == EventTypes.Redaction and room_version.msc2176_redaction_rules:
130+
add_fields("redacts")
119131

120132
allowed_fields = {k: v for k, v in event_dict.items() if k in allowed_keys}
121133

synapse/handlers/room.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ async def clone_existing_room(
365365
creation_content = {
366366
"room_version": new_room_version.identifier,
367367
"predecessor": {"room_id": old_room_id, "event_id": tombstone_event_id},
368-
}
368+
} # type: JsonDict
369369

370370
# Check if old room was non-federatable
371371

0 commit comments

Comments
 (0)