|
19 | 19 | from synapse.api.errors import AuthError |
20 | 20 | from synapse.api.room_versions import RoomVersions |
21 | 21 | from synapse.events import make_event_from_dict |
| 22 | +from synapse.types import get_domain_from_id |
22 | 23 |
|
23 | 24 |
|
24 | 25 | class EventAuthTestCase(unittest.TestCase): |
@@ -51,7 +52,7 @@ def test_random_users_cannot_send_state_before_first_pl(self): |
51 | 52 | _random_state_event(joiner), |
52 | 53 | auth_events, |
53 | 54 | do_sig_check=False, |
54 | | - ), |
| 55 | + ) |
55 | 56 |
|
56 | 57 | def test_state_default_level(self): |
57 | 58 | """ |
@@ -87,6 +88,83 @@ def test_state_default_level(self): |
87 | 88 | RoomVersions.V1, _random_state_event(king), auth_events, do_sig_check=False, |
88 | 89 | ) |
89 | 90 |
|
| 91 | + def test_alias_event(self): |
| 92 | + """Alias events have special behavior up through room version 6.""" |
| 93 | + creator = "@creator:example.com" |
| 94 | + other = "@other:example.com" |
| 95 | + auth_events = { |
| 96 | + ("m.room.create", ""): _create_event(creator), |
| 97 | + ("m.room.member", creator): _join_event(creator), |
| 98 | + } |
| 99 | + |
| 100 | + # creator should be able to send aliases |
| 101 | + event_auth.check( |
| 102 | + RoomVersions.V1, _alias_event(creator), auth_events, do_sig_check=False, |
| 103 | + ) |
| 104 | + |
| 105 | + # Reject an event with no state key. |
| 106 | + with self.assertRaises(AuthError): |
| 107 | + event_auth.check( |
| 108 | + RoomVersions.V1, |
| 109 | + _alias_event(creator, state_key=""), |
| 110 | + auth_events, |
| 111 | + do_sig_check=False, |
| 112 | + ) |
| 113 | + |
| 114 | + # If the domain of the sender does not match the state key, reject. |
| 115 | + with self.assertRaises(AuthError): |
| 116 | + event_auth.check( |
| 117 | + RoomVersions.V1, |
| 118 | + _alias_event(creator, state_key="test.com"), |
| 119 | + auth_events, |
| 120 | + do_sig_check=False, |
| 121 | + ) |
| 122 | + |
| 123 | + # Note that the member does *not* need to be in the room. |
| 124 | + event_auth.check( |
| 125 | + RoomVersions.V1, _alias_event(other), auth_events, do_sig_check=False, |
| 126 | + ) |
| 127 | + |
| 128 | + def test_msc2432_alias_event(self): |
| 129 | + """After MSC2432, alias events have no special behavior.""" |
| 130 | + creator = "@creator:example.com" |
| 131 | + other = "@other:example.com" |
| 132 | + auth_events = { |
| 133 | + ("m.room.create", ""): _create_event(creator), |
| 134 | + ("m.room.member", creator): _join_event(creator), |
| 135 | + } |
| 136 | + |
| 137 | + # creator should be able to send aliases |
| 138 | + event_auth.check( |
| 139 | + RoomVersions.MSC2432_DEV, |
| 140 | + _alias_event(creator), |
| 141 | + auth_events, |
| 142 | + do_sig_check=False, |
| 143 | + ) |
| 144 | + |
| 145 | + # No particular checks are done on the state key. |
| 146 | + event_auth.check( |
| 147 | + RoomVersions.MSC2432_DEV, |
| 148 | + _alias_event(creator, state_key=""), |
| 149 | + auth_events, |
| 150 | + do_sig_check=False, |
| 151 | + ) |
| 152 | + event_auth.check( |
| 153 | + RoomVersions.MSC2432_DEV, |
| 154 | + _alias_event(creator, state_key="test.com"), |
| 155 | + auth_events, |
| 156 | + do_sig_check=False, |
| 157 | + ) |
| 158 | + |
| 159 | + # Per standard auth rules, the member must be in the room. |
| 160 | + with self.assertRaises(AuthError): |
| 161 | + event_auth.check( |
| 162 | + RoomVersions.MSC2432_DEV, |
| 163 | + _alias_event(other), |
| 164 | + auth_events, |
| 165 | + do_sig_check=False, |
| 166 | + ) |
| 167 | + |
90 | 168 |
|
91 | 169 | # helpers for making events |
92 | 170 |
|
@@ -131,6 +209,19 @@ def _power_levels_event(sender, content): |
131 | 209 | ) |
132 | 210 |
|
133 | 211 |
|
| 212 | +def _alias_event(sender, **kwargs): |
| 213 | + data = { |
| 214 | + "room_id": TEST_ROOM_ID, |
| 215 | + "event_id": _get_event_id(), |
| 216 | + "type": "m.room.aliases", |
| 217 | + "sender": sender, |
| 218 | + "state_key": get_domain_from_id(sender), |
| 219 | + "content": {"aliases": []}, |
| 220 | + } |
| 221 | + data.update(**kwargs) |
| 222 | + return make_event_from_dict(data) |
| 223 | + |
| 224 | + |
134 | 225 | def _random_state_event(sender): |
135 | 226 | return make_event_from_dict( |
136 | 227 | { |
|
0 commit comments