1
0

test_event_auth.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. # Copyright 2018-2022 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import unittest
  15. from typing import Collection, Dict, Iterable, List, Optional
  16. from parameterized import parameterized
  17. from synapse import event_auth
  18. from synapse.api.constants import EventContentFields
  19. from synapse.api.errors import AuthError, SynapseError
  20. from synapse.api.room_versions import EventFormatVersions, RoomVersion, RoomVersions
  21. from synapse.events import EventBase, make_event_from_dict
  22. from synapse.storage.databases.main.events_worker import EventRedactBehaviour
  23. from synapse.types import JsonDict, get_domain_from_id
  24. from tests.test_utils import get_awaitable_result
  25. class _StubEventSourceStore:
  26. """A stub implementation of the EventSourceStore"""
  27. def __init__(self):
  28. self._store: Dict[str, EventBase] = {}
  29. def add_event(self, event: EventBase):
  30. self._store[event.event_id] = event
  31. def add_events(self, events: Iterable[EventBase]):
  32. for event in events:
  33. self._store[event.event_id] = event
  34. async def get_events(
  35. self,
  36. event_ids: Collection[str],
  37. redact_behaviour: EventRedactBehaviour,
  38. get_prev_content: bool = False,
  39. allow_rejected: bool = False,
  40. ) -> Dict[str, EventBase]:
  41. assert allow_rejected
  42. assert not get_prev_content
  43. assert redact_behaviour == EventRedactBehaviour.as_is
  44. results = {}
  45. for e in event_ids:
  46. if e in self._store:
  47. results[e] = self._store[e]
  48. return results
  49. class EventAuthTestCase(unittest.TestCase):
  50. def test_rejected_auth_events(self):
  51. """
  52. Events that refer to rejected events in their auth events are rejected
  53. """
  54. creator = "@creator:example.com"
  55. auth_events = [
  56. _create_event(RoomVersions.V9, creator),
  57. _join_event(RoomVersions.V9, creator),
  58. ]
  59. event_store = _StubEventSourceStore()
  60. event_store.add_events(auth_events)
  61. # creator should be able to send state
  62. event = _random_state_event(RoomVersions.V9, creator, auth_events)
  63. get_awaitable_result(
  64. event_auth.check_state_independent_auth_rules(event_store, event)
  65. )
  66. event_auth.check_state_dependent_auth_rules(event, auth_events)
  67. # ... but a rejected join_rules event should cause it to be rejected
  68. rejected_join_rules = _join_rules_event(
  69. RoomVersions.V9,
  70. creator,
  71. "public",
  72. )
  73. rejected_join_rules.rejected_reason = "stinky"
  74. auth_events.append(rejected_join_rules)
  75. event_store.add_event(rejected_join_rules)
  76. with self.assertRaises(AuthError):
  77. get_awaitable_result(
  78. event_auth.check_state_independent_auth_rules(
  79. event_store,
  80. _random_state_event(RoomVersions.V9, creator),
  81. )
  82. )
  83. # ... even if there is *also* a good join rules
  84. auth_events.append(_join_rules_event(RoomVersions.V9, creator, "public"))
  85. event_store.add_event(rejected_join_rules)
  86. with self.assertRaises(AuthError):
  87. get_awaitable_result(
  88. event_auth.check_state_independent_auth_rules(
  89. event_store,
  90. _random_state_event(RoomVersions.V9, creator),
  91. )
  92. )
  93. def test_create_event_with_prev_events(self):
  94. """A create event with prev_events should be rejected
  95. https://spec.matrix.org/v1.3/rooms/v9/#authorization-rules
  96. 1: If type is m.room.create:
  97. 1. If it has any previous events, reject.
  98. """
  99. creator = f"@creator:{TEST_DOMAIN}"
  100. # we make both a good event and a bad event, to check that we are rejecting
  101. # the bad event for the reason we think we are.
  102. good_event = make_event_from_dict(
  103. {
  104. "room_id": TEST_ROOM_ID,
  105. "type": "m.room.create",
  106. "state_key": "",
  107. "sender": creator,
  108. "content": {
  109. "creator": creator,
  110. "room_version": RoomVersions.V9.identifier,
  111. },
  112. "auth_events": [],
  113. "prev_events": [],
  114. },
  115. room_version=RoomVersions.V9,
  116. )
  117. bad_event = make_event_from_dict(
  118. {**good_event.get_dict(), "prev_events": ["$fakeevent"]},
  119. room_version=RoomVersions.V9,
  120. )
  121. event_store = _StubEventSourceStore()
  122. get_awaitable_result(
  123. event_auth.check_state_independent_auth_rules(event_store, good_event)
  124. )
  125. with self.assertRaises(AuthError):
  126. get_awaitable_result(
  127. event_auth.check_state_independent_auth_rules(event_store, bad_event)
  128. )
  129. def test_duplicate_auth_events(self):
  130. """Events with duplicate auth_events should be rejected
  131. https://spec.matrix.org/v1.3/rooms/v9/#authorization-rules
  132. 2. Reject if event has auth_events that:
  133. 1. have duplicate entries for a given type and state_key pair
  134. """
  135. creator = "@creator:example.com"
  136. create_event = _create_event(RoomVersions.V9, creator)
  137. join_event1 = _join_event(RoomVersions.V9, creator)
  138. pl_event = _power_levels_event(
  139. RoomVersions.V9,
  140. creator,
  141. {"state_default": 30, "users": {"creator": 100}},
  142. )
  143. # create a second join event, so that we can make a duplicate
  144. join_event2 = _join_event(RoomVersions.V9, creator)
  145. event_store = _StubEventSourceStore()
  146. event_store.add_events([create_event, join_event1, join_event2, pl_event])
  147. good_event = _random_state_event(
  148. RoomVersions.V9, creator, [create_event, join_event2, pl_event]
  149. )
  150. bad_event = _random_state_event(
  151. RoomVersions.V9, creator, [create_event, join_event1, join_event2, pl_event]
  152. )
  153. # a variation: two instances of the *same* event
  154. bad_event2 = _random_state_event(
  155. RoomVersions.V9, creator, [create_event, join_event2, join_event2, pl_event]
  156. )
  157. get_awaitable_result(
  158. event_auth.check_state_independent_auth_rules(event_store, good_event)
  159. )
  160. with self.assertRaises(AuthError):
  161. get_awaitable_result(
  162. event_auth.check_state_independent_auth_rules(event_store, bad_event)
  163. )
  164. with self.assertRaises(AuthError):
  165. get_awaitable_result(
  166. event_auth.check_state_independent_auth_rules(event_store, bad_event2)
  167. )
  168. def test_unexpected_auth_events(self):
  169. """Events with excess auth_events should be rejected
  170. https://spec.matrix.org/v1.3/rooms/v9/#authorization-rules
  171. 2. Reject if event has auth_events that:
  172. 2. have entries whose type and state_key don’t match those specified by the
  173. auth events selection algorithm described in the server specification.
  174. """
  175. creator = "@creator:example.com"
  176. create_event = _create_event(RoomVersions.V9, creator)
  177. join_event = _join_event(RoomVersions.V9, creator)
  178. pl_event = _power_levels_event(
  179. RoomVersions.V9,
  180. creator,
  181. {"state_default": 30, "users": {"creator": 100}},
  182. )
  183. join_rules_event = _join_rules_event(RoomVersions.V9, creator, "public")
  184. event_store = _StubEventSourceStore()
  185. event_store.add_events([create_event, join_event, pl_event, join_rules_event])
  186. good_event = _random_state_event(
  187. RoomVersions.V9, creator, [create_event, join_event, pl_event]
  188. )
  189. # join rules should *not* be included in the auth events.
  190. bad_event = _random_state_event(
  191. RoomVersions.V9,
  192. creator,
  193. [create_event, join_event, pl_event, join_rules_event],
  194. )
  195. get_awaitable_result(
  196. event_auth.check_state_independent_auth_rules(event_store, good_event)
  197. )
  198. with self.assertRaises(AuthError):
  199. get_awaitable_result(
  200. event_auth.check_state_independent_auth_rules(event_store, bad_event)
  201. )
  202. def test_random_users_cannot_send_state_before_first_pl(self):
  203. """
  204. Check that, before the first PL lands, the creator is the only user
  205. that can send a state event.
  206. """
  207. creator = "@creator:example.com"
  208. joiner = "@joiner:example.com"
  209. auth_events = [
  210. _create_event(RoomVersions.V1, creator),
  211. _join_event(RoomVersions.V1, creator),
  212. _join_event(RoomVersions.V1, joiner),
  213. ]
  214. # creator should be able to send state
  215. event_auth.check_state_dependent_auth_rules(
  216. _random_state_event(RoomVersions.V1, creator),
  217. auth_events,
  218. )
  219. # joiner should not be able to send state
  220. self.assertRaises(
  221. AuthError,
  222. event_auth.check_state_dependent_auth_rules,
  223. _random_state_event(RoomVersions.V1, joiner),
  224. auth_events,
  225. )
  226. def test_state_default_level(self):
  227. """
  228. Check that users above the state_default level can send state and
  229. those below cannot
  230. """
  231. creator = "@creator:example.com"
  232. pleb = "@joiner:example.com"
  233. king = "@joiner2:example.com"
  234. auth_events = [
  235. _create_event(RoomVersions.V1, creator),
  236. _join_event(RoomVersions.V1, creator),
  237. _power_levels_event(
  238. RoomVersions.V1,
  239. creator,
  240. {"state_default": "30", "users": {pleb: "29", king: "30"}},
  241. ),
  242. _join_event(RoomVersions.V1, pleb),
  243. _join_event(RoomVersions.V1, king),
  244. ]
  245. # pleb should not be able to send state
  246. self.assertRaises(
  247. AuthError,
  248. event_auth.check_state_dependent_auth_rules,
  249. _random_state_event(RoomVersions.V1, pleb),
  250. auth_events,
  251. ),
  252. # king should be able to send state
  253. event_auth.check_state_dependent_auth_rules(
  254. _random_state_event(RoomVersions.V1, king),
  255. auth_events,
  256. )
  257. def test_alias_event(self):
  258. """Alias events have special behavior up through room version 6."""
  259. creator = "@creator:example.com"
  260. other = "@other:example.com"
  261. auth_events = [
  262. _create_event(RoomVersions.V1, creator),
  263. _join_event(RoomVersions.V1, creator),
  264. ]
  265. # creator should be able to send aliases
  266. event_auth.check_state_dependent_auth_rules(
  267. _alias_event(RoomVersions.V1, creator),
  268. auth_events,
  269. )
  270. # Reject an event with no state key.
  271. with self.assertRaises(AuthError):
  272. event_auth.check_state_dependent_auth_rules(
  273. _alias_event(RoomVersions.V1, creator, state_key=""),
  274. auth_events,
  275. )
  276. # If the domain of the sender does not match the state key, reject.
  277. with self.assertRaises(AuthError):
  278. event_auth.check_state_dependent_auth_rules(
  279. _alias_event(RoomVersions.V1, creator, state_key="test.com"),
  280. auth_events,
  281. )
  282. # Note that the member does *not* need to be in the room.
  283. event_auth.check_state_dependent_auth_rules(
  284. _alias_event(RoomVersions.V1, other),
  285. auth_events,
  286. )
  287. def test_msc2432_alias_event(self):
  288. """After MSC2432, alias events have no special behavior."""
  289. creator = "@creator:example.com"
  290. other = "@other:example.com"
  291. auth_events = [
  292. _create_event(RoomVersions.V6, creator),
  293. _join_event(RoomVersions.V6, creator),
  294. ]
  295. # creator should be able to send aliases
  296. event_auth.check_state_dependent_auth_rules(
  297. _alias_event(RoomVersions.V6, creator),
  298. auth_events,
  299. )
  300. # No particular checks are done on the state key.
  301. event_auth.check_state_dependent_auth_rules(
  302. _alias_event(RoomVersions.V6, creator, state_key=""),
  303. auth_events,
  304. )
  305. event_auth.check_state_dependent_auth_rules(
  306. _alias_event(RoomVersions.V6, creator, state_key="test.com"),
  307. auth_events,
  308. )
  309. # Per standard auth rules, the member must be in the room.
  310. with self.assertRaises(AuthError):
  311. event_auth.check_state_dependent_auth_rules(
  312. _alias_event(RoomVersions.V6, other),
  313. auth_events,
  314. )
  315. @parameterized.expand([(RoomVersions.V1, True), (RoomVersions.V6, False)])
  316. def test_notifications(self, room_version: RoomVersion, allow_modification: bool):
  317. """
  318. Notifications power levels get checked due to MSC2209.
  319. """
  320. creator = "@creator:example.com"
  321. pleb = "@joiner:example.com"
  322. auth_events = [
  323. _create_event(room_version, creator),
  324. _join_event(room_version, creator),
  325. _power_levels_event(
  326. room_version, creator, {"state_default": "30", "users": {pleb: "30"}}
  327. ),
  328. _join_event(room_version, pleb),
  329. ]
  330. pl_event = _power_levels_event(
  331. room_version, pleb, {"notifications": {"room": 100}}
  332. )
  333. # on room V1, pleb should be able to modify the notifications power level.
  334. if allow_modification:
  335. event_auth.check_state_dependent_auth_rules(pl_event, auth_events)
  336. else:
  337. # But an MSC2209 room rejects this change.
  338. with self.assertRaises(AuthError):
  339. event_auth.check_state_dependent_auth_rules(pl_event, auth_events)
  340. def test_join_rules_public(self):
  341. """
  342. Test joining a public room.
  343. """
  344. creator = "@creator:example.com"
  345. pleb = "@joiner:example.com"
  346. auth_events = {
  347. ("m.room.create", ""): _create_event(RoomVersions.V6, creator),
  348. ("m.room.member", creator): _join_event(RoomVersions.V6, creator),
  349. ("m.room.join_rules", ""): _join_rules_event(
  350. RoomVersions.V6, creator, "public"
  351. ),
  352. }
  353. # Check join.
  354. event_auth.check_state_dependent_auth_rules(
  355. _join_event(RoomVersions.V6, pleb),
  356. auth_events.values(),
  357. )
  358. # A user cannot be force-joined to a room.
  359. with self.assertRaises(AuthError):
  360. event_auth.check_state_dependent_auth_rules(
  361. _member_event(RoomVersions.V6, pleb, "join", sender=creator),
  362. auth_events.values(),
  363. )
  364. # Banned should be rejected.
  365. auth_events[("m.room.member", pleb)] = _member_event(
  366. RoomVersions.V6, pleb, "ban"
  367. )
  368. with self.assertRaises(AuthError):
  369. event_auth.check_state_dependent_auth_rules(
  370. _join_event(RoomVersions.V6, pleb),
  371. auth_events.values(),
  372. )
  373. # A user who left can re-join.
  374. auth_events[("m.room.member", pleb)] = _member_event(
  375. RoomVersions.V6, pleb, "leave"
  376. )
  377. event_auth.check_state_dependent_auth_rules(
  378. _join_event(RoomVersions.V6, pleb),
  379. auth_events.values(),
  380. )
  381. # A user can send a join if they're in the room.
  382. auth_events[("m.room.member", pleb)] = _member_event(
  383. RoomVersions.V6, pleb, "join"
  384. )
  385. event_auth.check_state_dependent_auth_rules(
  386. _join_event(RoomVersions.V6, pleb),
  387. auth_events.values(),
  388. )
  389. # A user can accept an invite.
  390. auth_events[("m.room.member", pleb)] = _member_event(
  391. RoomVersions.V6, pleb, "invite", sender=creator
  392. )
  393. event_auth.check_state_dependent_auth_rules(
  394. _join_event(RoomVersions.V6, pleb),
  395. auth_events.values(),
  396. )
  397. def test_join_rules_invite(self):
  398. """
  399. Test joining an invite only room.
  400. """
  401. creator = "@creator:example.com"
  402. pleb = "@joiner:example.com"
  403. auth_events = {
  404. ("m.room.create", ""): _create_event(RoomVersions.V6, creator),
  405. ("m.room.member", creator): _join_event(RoomVersions.V6, creator),
  406. ("m.room.join_rules", ""): _join_rules_event(
  407. RoomVersions.V6, creator, "invite"
  408. ),
  409. }
  410. # A join without an invite is rejected.
  411. with self.assertRaises(AuthError):
  412. event_auth.check_state_dependent_auth_rules(
  413. _join_event(RoomVersions.V6, pleb),
  414. auth_events.values(),
  415. )
  416. # A user cannot be force-joined to a room.
  417. with self.assertRaises(AuthError):
  418. event_auth.check_state_dependent_auth_rules(
  419. _member_event(RoomVersions.V6, pleb, "join", sender=creator),
  420. auth_events.values(),
  421. )
  422. # Banned should be rejected.
  423. auth_events[("m.room.member", pleb)] = _member_event(
  424. RoomVersions.V6, pleb, "ban"
  425. )
  426. with self.assertRaises(AuthError):
  427. event_auth.check_state_dependent_auth_rules(
  428. _join_event(RoomVersions.V6, pleb),
  429. auth_events.values(),
  430. )
  431. # A user who left cannot re-join.
  432. auth_events[("m.room.member", pleb)] = _member_event(
  433. RoomVersions.V6, pleb, "leave"
  434. )
  435. with self.assertRaises(AuthError):
  436. event_auth.check_state_dependent_auth_rules(
  437. _join_event(RoomVersions.V6, pleb),
  438. auth_events.values(),
  439. )
  440. # A user can send a join if they're in the room.
  441. auth_events[("m.room.member", pleb)] = _member_event(
  442. RoomVersions.V6, pleb, "join"
  443. )
  444. event_auth.check_state_dependent_auth_rules(
  445. _join_event(RoomVersions.V6, pleb),
  446. auth_events.values(),
  447. )
  448. # A user can accept an invite.
  449. auth_events[("m.room.member", pleb)] = _member_event(
  450. RoomVersions.V6, pleb, "invite", sender=creator
  451. )
  452. event_auth.check_state_dependent_auth_rules(
  453. _join_event(RoomVersions.V6, pleb),
  454. auth_events.values(),
  455. )
  456. def test_join_rules_restricted_old_room(self) -> None:
  457. """Old room versions should reject joins to restricted rooms"""
  458. creator = "@creator:example.com"
  459. pleb = "@joiner:example.com"
  460. auth_events = {
  461. ("m.room.create", ""): _create_event(RoomVersions.V6, creator),
  462. ("m.room.member", creator): _join_event(RoomVersions.V6, creator),
  463. ("m.room.power_levels", ""): _power_levels_event(
  464. RoomVersions.V6, creator, {"invite": 0}
  465. ),
  466. ("m.room.join_rules", ""): _join_rules_event(
  467. RoomVersions.V6, creator, "restricted"
  468. ),
  469. }
  470. with self.assertRaises(AuthError):
  471. event_auth.check_state_dependent_auth_rules(
  472. _join_event(RoomVersions.V6, pleb),
  473. auth_events.values(),
  474. )
  475. def test_join_rules_msc3083_restricted(self) -> None:
  476. """
  477. Test joining a restricted room from MSC3083.
  478. This is similar to the public test, but has some additional checks on
  479. signatures.
  480. The checks which care about signatures fake them by simply adding an
  481. object of the proper form, not generating valid signatures.
  482. """
  483. creator = "@creator:example.com"
  484. pleb = "@joiner:example.com"
  485. auth_events = {
  486. ("m.room.create", ""): _create_event(RoomVersions.V8, creator),
  487. ("m.room.member", creator): _join_event(RoomVersions.V8, creator),
  488. ("m.room.power_levels", ""): _power_levels_event(
  489. RoomVersions.V8, creator, {"invite": 0}
  490. ),
  491. ("m.room.join_rules", ""): _join_rules_event(
  492. RoomVersions.V8, creator, "restricted"
  493. ),
  494. }
  495. # A properly formatted join event should work.
  496. authorised_join_event = _join_event(
  497. RoomVersions.V8,
  498. pleb,
  499. additional_content={
  500. EventContentFields.AUTHORISING_USER: "@creator:example.com"
  501. },
  502. )
  503. event_auth.check_state_dependent_auth_rules(
  504. authorised_join_event,
  505. auth_events.values(),
  506. )
  507. # A join issued by a specific user works (i.e. the power level checks
  508. # are done properly).
  509. pl_auth_events = auth_events.copy()
  510. pl_auth_events[("m.room.power_levels", "")] = _power_levels_event(
  511. RoomVersions.V8,
  512. creator,
  513. {"invite": 100, "users": {"@inviter:foo.test": 150}},
  514. )
  515. pl_auth_events[("m.room.member", "@inviter:foo.test")] = _join_event(
  516. RoomVersions.V8, "@inviter:foo.test"
  517. )
  518. event_auth.check_state_dependent_auth_rules(
  519. _join_event(
  520. RoomVersions.V8,
  521. pleb,
  522. additional_content={
  523. EventContentFields.AUTHORISING_USER: "@inviter:foo.test"
  524. },
  525. ),
  526. pl_auth_events.values(),
  527. )
  528. # A join which is missing an authorised server is rejected.
  529. with self.assertRaises(AuthError):
  530. event_auth.check_state_dependent_auth_rules(
  531. _join_event(RoomVersions.V8, pleb),
  532. auth_events.values(),
  533. )
  534. # An join authorised by a user who is not in the room is rejected.
  535. pl_auth_events = auth_events.copy()
  536. pl_auth_events[("m.room.power_levels", "")] = _power_levels_event(
  537. RoomVersions.V8,
  538. creator,
  539. {"invite": 100, "users": {"@other:example.com": 150}},
  540. )
  541. with self.assertRaises(AuthError):
  542. event_auth.check_state_dependent_auth_rules(
  543. _join_event(
  544. RoomVersions.V8,
  545. pleb,
  546. additional_content={
  547. EventContentFields.AUTHORISING_USER: "@other:example.com"
  548. },
  549. ),
  550. auth_events.values(),
  551. )
  552. # A user cannot be force-joined to a room. (This uses an event which
  553. # *would* be valid, but is sent be a different user.)
  554. with self.assertRaises(AuthError):
  555. event_auth.check_state_dependent_auth_rules(
  556. _member_event(
  557. RoomVersions.V8,
  558. pleb,
  559. "join",
  560. sender=creator,
  561. additional_content={
  562. EventContentFields.AUTHORISING_USER: "@inviter:foo.test"
  563. },
  564. ),
  565. auth_events.values(),
  566. )
  567. # Banned should be rejected.
  568. auth_events[("m.room.member", pleb)] = _member_event(
  569. RoomVersions.V8, pleb, "ban"
  570. )
  571. with self.assertRaises(AuthError):
  572. event_auth.check_state_dependent_auth_rules(
  573. authorised_join_event,
  574. auth_events.values(),
  575. )
  576. # A user who left can re-join.
  577. auth_events[("m.room.member", pleb)] = _member_event(
  578. RoomVersions.V8, pleb, "leave"
  579. )
  580. event_auth.check_state_dependent_auth_rules(
  581. authorised_join_event,
  582. auth_events.values(),
  583. )
  584. # A user can send a join if they're in the room. (This doesn't need to
  585. # be authorised since the user is already joined.)
  586. auth_events[("m.room.member", pleb)] = _member_event(
  587. RoomVersions.V8, pleb, "join"
  588. )
  589. event_auth.check_state_dependent_auth_rules(
  590. _join_event(RoomVersions.V8, pleb),
  591. auth_events.values(),
  592. )
  593. # A user can accept an invite. (This doesn't need to be authorised since
  594. # the user was invited.)
  595. auth_events[("m.room.member", pleb)] = _member_event(
  596. RoomVersions.V8, pleb, "invite", sender=creator
  597. )
  598. event_auth.check_state_dependent_auth_rules(
  599. _join_event(RoomVersions.V8, pleb),
  600. auth_events.values(),
  601. )
  602. def test_room_v10_rejects_string_power_levels(self) -> None:
  603. pl_event_content = {"users_default": "42"}
  604. pl_event = make_event_from_dict(
  605. {
  606. "room_id": TEST_ROOM_ID,
  607. **_maybe_get_event_id_dict_for_room_version(RoomVersions.V10),
  608. "type": "m.room.power_levels",
  609. "sender": "@test:test.com",
  610. "state_key": "",
  611. "content": pl_event_content,
  612. "signatures": {"test.com": {"ed25519:0": "some9signature"}},
  613. },
  614. room_version=RoomVersions.V10,
  615. )
  616. pl_event2_content = {"events": {"m.room.name": "42", "m.room.power_levels": 42}}
  617. pl_event2 = make_event_from_dict(
  618. {
  619. "room_id": TEST_ROOM_ID,
  620. **_maybe_get_event_id_dict_for_room_version(RoomVersions.V10),
  621. "type": "m.room.power_levels",
  622. "sender": "@test:test.com",
  623. "state_key": "",
  624. "content": pl_event2_content,
  625. "signatures": {"test.com": {"ed25519:0": "some9signature"}},
  626. },
  627. room_version=RoomVersions.V10,
  628. )
  629. with self.assertRaises(SynapseError):
  630. event_auth._check_power_levels(
  631. pl_event.room_version, pl_event, {("fake_type", "fake_key"): pl_event2}
  632. )
  633. with self.assertRaises(SynapseError):
  634. event_auth._check_power_levels(
  635. pl_event.room_version, pl_event2, {("fake_type", "fake_key"): pl_event}
  636. )
  637. # helpers for making events
  638. TEST_DOMAIN = "example.com"
  639. TEST_ROOM_ID = f"!test_room:{TEST_DOMAIN}"
  640. def _create_event(
  641. room_version: RoomVersion,
  642. user_id: str,
  643. ) -> EventBase:
  644. return make_event_from_dict(
  645. {
  646. "room_id": TEST_ROOM_ID,
  647. **_maybe_get_event_id_dict_for_room_version(room_version),
  648. "type": "m.room.create",
  649. "state_key": "",
  650. "sender": user_id,
  651. "content": {"creator": user_id},
  652. "auth_events": [],
  653. },
  654. room_version=room_version,
  655. )
  656. def _member_event(
  657. room_version: RoomVersion,
  658. user_id: str,
  659. membership: str,
  660. sender: Optional[str] = None,
  661. additional_content: Optional[dict] = None,
  662. ) -> EventBase:
  663. return make_event_from_dict(
  664. {
  665. "room_id": TEST_ROOM_ID,
  666. **_maybe_get_event_id_dict_for_room_version(room_version),
  667. "type": "m.room.member",
  668. "sender": sender or user_id,
  669. "state_key": user_id,
  670. "content": {"membership": membership, **(additional_content or {})},
  671. "auth_events": [],
  672. "prev_events": [],
  673. },
  674. room_version=room_version,
  675. )
  676. def _join_event(
  677. room_version: RoomVersion,
  678. user_id: str,
  679. additional_content: Optional[dict] = None,
  680. ) -> EventBase:
  681. return _member_event(
  682. room_version,
  683. user_id,
  684. "join",
  685. additional_content=additional_content,
  686. )
  687. def _power_levels_event(
  688. room_version: RoomVersion,
  689. sender: str,
  690. content: JsonDict,
  691. ) -> EventBase:
  692. return make_event_from_dict(
  693. {
  694. "room_id": TEST_ROOM_ID,
  695. **_maybe_get_event_id_dict_for_room_version(room_version),
  696. "type": "m.room.power_levels",
  697. "sender": sender,
  698. "state_key": "",
  699. "content": content,
  700. },
  701. room_version=room_version,
  702. )
  703. def _alias_event(room_version: RoomVersion, sender: str, **kwargs) -> EventBase:
  704. data = {
  705. "room_id": TEST_ROOM_ID,
  706. **_maybe_get_event_id_dict_for_room_version(room_version),
  707. "type": "m.room.aliases",
  708. "sender": sender,
  709. "state_key": get_domain_from_id(sender),
  710. "content": {"aliases": []},
  711. }
  712. data.update(**kwargs)
  713. return make_event_from_dict(data, room_version=room_version)
  714. def _build_auth_dict_for_room_version(
  715. room_version: RoomVersion, auth_events: Iterable[EventBase]
  716. ) -> List:
  717. if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
  718. return [(e.event_id, "not_used") for e in auth_events]
  719. else:
  720. return [e.event_id for e in auth_events]
  721. def _random_state_event(
  722. room_version: RoomVersion,
  723. sender: str,
  724. auth_events: Optional[Iterable[EventBase]] = None,
  725. ) -> EventBase:
  726. if auth_events is None:
  727. auth_events = []
  728. return make_event_from_dict(
  729. {
  730. "room_id": TEST_ROOM_ID,
  731. **_maybe_get_event_id_dict_for_room_version(room_version),
  732. "type": "test.state",
  733. "sender": sender,
  734. "state_key": "",
  735. "content": {"membership": "join"},
  736. "auth_events": _build_auth_dict_for_room_version(room_version, auth_events),
  737. },
  738. room_version=room_version,
  739. )
  740. def _join_rules_event(
  741. room_version: RoomVersion, sender: str, join_rule: str
  742. ) -> EventBase:
  743. return make_event_from_dict(
  744. {
  745. "room_id": TEST_ROOM_ID,
  746. **_maybe_get_event_id_dict_for_room_version(room_version),
  747. "type": "m.room.join_rules",
  748. "sender": sender,
  749. "state_key": "",
  750. "content": {
  751. "join_rule": join_rule,
  752. },
  753. },
  754. room_version=room_version,
  755. )
  756. event_count = 0
  757. def _maybe_get_event_id_dict_for_room_version(room_version: RoomVersion) -> dict:
  758. """If this room version needs it, generate an event id"""
  759. if room_version.event_format != EventFormatVersions.ROOM_V1_V2:
  760. return {}
  761. global event_count
  762. c = event_count
  763. event_count += 1
  764. return {"event_id": "!%i:example.com" % (c,)}