test_event_auth.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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 Any, 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) -> None:
  28. self._store: Dict[str, EventBase] = {}
  29. def add_event(self, event: EventBase) -> None:
  30. self._store[event.event_id] = event
  31. def add_events(self, events: Iterable[EventBase]) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  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) -> None:
  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(
  317. self, room_version: RoomVersion, allow_modification: bool
  318. ) -> None:
  319. """
  320. Notifications power levels get checked due to MSC2209.
  321. """
  322. creator = "@creator:example.com"
  323. pleb = "@joiner:example.com"
  324. auth_events = [
  325. _create_event(room_version, creator),
  326. _join_event(room_version, creator),
  327. _power_levels_event(
  328. room_version, creator, {"state_default": "30", "users": {pleb: "30"}}
  329. ),
  330. _join_event(room_version, pleb),
  331. ]
  332. pl_event = _power_levels_event(
  333. room_version, pleb, {"notifications": {"room": 100}}
  334. )
  335. # on room V1, pleb should be able to modify the notifications power level.
  336. if allow_modification:
  337. event_auth.check_state_dependent_auth_rules(pl_event, auth_events)
  338. else:
  339. # But an MSC2209 room rejects this change.
  340. with self.assertRaises(AuthError):
  341. event_auth.check_state_dependent_auth_rules(pl_event, auth_events)
  342. def test_join_rules_public(self) -> None:
  343. """
  344. Test joining a public room.
  345. """
  346. creator = "@creator:example.com"
  347. pleb = "@joiner:example.com"
  348. auth_events = {
  349. ("m.room.create", ""): _create_event(RoomVersions.V6, creator),
  350. ("m.room.member", creator): _join_event(RoomVersions.V6, creator),
  351. ("m.room.join_rules", ""): _join_rules_event(
  352. RoomVersions.V6, creator, "public"
  353. ),
  354. }
  355. # Check join.
  356. event_auth.check_state_dependent_auth_rules(
  357. _join_event(RoomVersions.V6, pleb),
  358. auth_events.values(),
  359. )
  360. # A user cannot be force-joined to a room.
  361. with self.assertRaises(AuthError):
  362. event_auth.check_state_dependent_auth_rules(
  363. _member_event(RoomVersions.V6, pleb, "join", sender=creator),
  364. auth_events.values(),
  365. )
  366. # Banned should be rejected.
  367. auth_events[("m.room.member", pleb)] = _member_event(
  368. RoomVersions.V6, pleb, "ban"
  369. )
  370. with self.assertRaises(AuthError):
  371. event_auth.check_state_dependent_auth_rules(
  372. _join_event(RoomVersions.V6, pleb),
  373. auth_events.values(),
  374. )
  375. # A user who left can re-join.
  376. auth_events[("m.room.member", pleb)] = _member_event(
  377. RoomVersions.V6, pleb, "leave"
  378. )
  379. event_auth.check_state_dependent_auth_rules(
  380. _join_event(RoomVersions.V6, pleb),
  381. auth_events.values(),
  382. )
  383. # A user can send a join if they're in the room.
  384. auth_events[("m.room.member", pleb)] = _member_event(
  385. RoomVersions.V6, pleb, "join"
  386. )
  387. event_auth.check_state_dependent_auth_rules(
  388. _join_event(RoomVersions.V6, pleb),
  389. auth_events.values(),
  390. )
  391. # A user can accept an invite.
  392. auth_events[("m.room.member", pleb)] = _member_event(
  393. RoomVersions.V6, pleb, "invite", sender=creator
  394. )
  395. event_auth.check_state_dependent_auth_rules(
  396. _join_event(RoomVersions.V6, pleb),
  397. auth_events.values(),
  398. )
  399. def test_join_rules_invite(self) -> None:
  400. """
  401. Test joining an invite only room.
  402. """
  403. creator = "@creator:example.com"
  404. pleb = "@joiner:example.com"
  405. auth_events = {
  406. ("m.room.create", ""): _create_event(RoomVersions.V6, creator),
  407. ("m.room.member", creator): _join_event(RoomVersions.V6, creator),
  408. ("m.room.join_rules", ""): _join_rules_event(
  409. RoomVersions.V6, creator, "invite"
  410. ),
  411. }
  412. # A join without an invite is rejected.
  413. with self.assertRaises(AuthError):
  414. event_auth.check_state_dependent_auth_rules(
  415. _join_event(RoomVersions.V6, pleb),
  416. auth_events.values(),
  417. )
  418. # A user cannot be force-joined to a room.
  419. with self.assertRaises(AuthError):
  420. event_auth.check_state_dependent_auth_rules(
  421. _member_event(RoomVersions.V6, pleb, "join", sender=creator),
  422. auth_events.values(),
  423. )
  424. # Banned should be rejected.
  425. auth_events[("m.room.member", pleb)] = _member_event(
  426. RoomVersions.V6, pleb, "ban"
  427. )
  428. with self.assertRaises(AuthError):
  429. event_auth.check_state_dependent_auth_rules(
  430. _join_event(RoomVersions.V6, pleb),
  431. auth_events.values(),
  432. )
  433. # A user who left cannot re-join.
  434. auth_events[("m.room.member", pleb)] = _member_event(
  435. RoomVersions.V6, pleb, "leave"
  436. )
  437. with self.assertRaises(AuthError):
  438. event_auth.check_state_dependent_auth_rules(
  439. _join_event(RoomVersions.V6, pleb),
  440. auth_events.values(),
  441. )
  442. # A user can send a join if they're in the room.
  443. auth_events[("m.room.member", pleb)] = _member_event(
  444. RoomVersions.V6, pleb, "join"
  445. )
  446. event_auth.check_state_dependent_auth_rules(
  447. _join_event(RoomVersions.V6, pleb),
  448. auth_events.values(),
  449. )
  450. # A user can accept an invite.
  451. auth_events[("m.room.member", pleb)] = _member_event(
  452. RoomVersions.V6, pleb, "invite", sender=creator
  453. )
  454. event_auth.check_state_dependent_auth_rules(
  455. _join_event(RoomVersions.V6, pleb),
  456. auth_events.values(),
  457. )
  458. def test_join_rules_restricted_old_room(self) -> None:
  459. """Old room versions should reject joins to restricted rooms"""
  460. creator = "@creator:example.com"
  461. pleb = "@joiner:example.com"
  462. auth_events = {
  463. ("m.room.create", ""): _create_event(RoomVersions.V6, creator),
  464. ("m.room.member", creator): _join_event(RoomVersions.V6, creator),
  465. ("m.room.power_levels", ""): _power_levels_event(
  466. RoomVersions.V6, creator, {"invite": 0}
  467. ),
  468. ("m.room.join_rules", ""): _join_rules_event(
  469. RoomVersions.V6, creator, "restricted"
  470. ),
  471. }
  472. with self.assertRaises(AuthError):
  473. event_auth.check_state_dependent_auth_rules(
  474. _join_event(RoomVersions.V6, pleb),
  475. auth_events.values(),
  476. )
  477. def test_join_rules_msc3083_restricted(self) -> None:
  478. """
  479. Test joining a restricted room from MSC3083.
  480. This is similar to the public test, but has some additional checks on
  481. signatures.
  482. The checks which care about signatures fake them by simply adding an
  483. object of the proper form, not generating valid signatures.
  484. """
  485. creator = "@creator:example.com"
  486. pleb = "@joiner:example.com"
  487. auth_events = {
  488. ("m.room.create", ""): _create_event(RoomVersions.V8, creator),
  489. ("m.room.member", creator): _join_event(RoomVersions.V8, creator),
  490. ("m.room.power_levels", ""): _power_levels_event(
  491. RoomVersions.V8, creator, {"invite": 0}
  492. ),
  493. ("m.room.join_rules", ""): _join_rules_event(
  494. RoomVersions.V8, creator, "restricted"
  495. ),
  496. }
  497. # A properly formatted join event should work.
  498. authorised_join_event = _join_event(
  499. RoomVersions.V8,
  500. pleb,
  501. additional_content={
  502. EventContentFields.AUTHORISING_USER: "@creator:example.com"
  503. },
  504. )
  505. event_auth.check_state_dependent_auth_rules(
  506. authorised_join_event,
  507. auth_events.values(),
  508. )
  509. # A join issued by a specific user works (i.e. the power level checks
  510. # are done properly).
  511. pl_auth_events = auth_events.copy()
  512. pl_auth_events[("m.room.power_levels", "")] = _power_levels_event(
  513. RoomVersions.V8,
  514. creator,
  515. {"invite": 100, "users": {"@inviter:foo.test": 150}},
  516. )
  517. pl_auth_events[("m.room.member", "@inviter:foo.test")] = _join_event(
  518. RoomVersions.V8, "@inviter:foo.test"
  519. )
  520. event_auth.check_state_dependent_auth_rules(
  521. _join_event(
  522. RoomVersions.V8,
  523. pleb,
  524. additional_content={
  525. EventContentFields.AUTHORISING_USER: "@inviter:foo.test"
  526. },
  527. ),
  528. pl_auth_events.values(),
  529. )
  530. # A join which is missing an authorised server is rejected.
  531. with self.assertRaises(AuthError):
  532. event_auth.check_state_dependent_auth_rules(
  533. _join_event(RoomVersions.V8, pleb),
  534. auth_events.values(),
  535. )
  536. # An join authorised by a user who is not in the room is rejected.
  537. pl_auth_events = auth_events.copy()
  538. pl_auth_events[("m.room.power_levels", "")] = _power_levels_event(
  539. RoomVersions.V8,
  540. creator,
  541. {"invite": 100, "users": {"@other:example.com": 150}},
  542. )
  543. with self.assertRaises(AuthError):
  544. event_auth.check_state_dependent_auth_rules(
  545. _join_event(
  546. RoomVersions.V8,
  547. pleb,
  548. additional_content={
  549. EventContentFields.AUTHORISING_USER: "@other:example.com"
  550. },
  551. ),
  552. auth_events.values(),
  553. )
  554. # A user cannot be force-joined to a room. (This uses an event which
  555. # *would* be valid, but is sent be a different user.)
  556. with self.assertRaises(AuthError):
  557. event_auth.check_state_dependent_auth_rules(
  558. _member_event(
  559. RoomVersions.V8,
  560. pleb,
  561. "join",
  562. sender=creator,
  563. additional_content={
  564. EventContentFields.AUTHORISING_USER: "@inviter:foo.test"
  565. },
  566. ),
  567. auth_events.values(),
  568. )
  569. # Banned should be rejected.
  570. auth_events[("m.room.member", pleb)] = _member_event(
  571. RoomVersions.V8, pleb, "ban"
  572. )
  573. with self.assertRaises(AuthError):
  574. event_auth.check_state_dependent_auth_rules(
  575. authorised_join_event,
  576. auth_events.values(),
  577. )
  578. # A user who left can re-join.
  579. auth_events[("m.room.member", pleb)] = _member_event(
  580. RoomVersions.V8, pleb, "leave"
  581. )
  582. event_auth.check_state_dependent_auth_rules(
  583. authorised_join_event,
  584. auth_events.values(),
  585. )
  586. # A user can send a join if they're in the room. (This doesn't need to
  587. # be authorised since the user is already joined.)
  588. auth_events[("m.room.member", pleb)] = _member_event(
  589. RoomVersions.V8, pleb, "join"
  590. )
  591. event_auth.check_state_dependent_auth_rules(
  592. _join_event(RoomVersions.V8, pleb),
  593. auth_events.values(),
  594. )
  595. # A user can accept an invite. (This doesn't need to be authorised since
  596. # the user was invited.)
  597. auth_events[("m.room.member", pleb)] = _member_event(
  598. RoomVersions.V8, pleb, "invite", sender=creator
  599. )
  600. event_auth.check_state_dependent_auth_rules(
  601. _join_event(RoomVersions.V8, pleb),
  602. auth_events.values(),
  603. )
  604. def test_room_v10_rejects_string_power_levels(self) -> None:
  605. pl_event_content = {"users_default": "42"}
  606. pl_event = make_event_from_dict(
  607. {
  608. "room_id": TEST_ROOM_ID,
  609. **_maybe_get_event_id_dict_for_room_version(RoomVersions.V10),
  610. "type": "m.room.power_levels",
  611. "sender": "@test:test.com",
  612. "state_key": "",
  613. "content": pl_event_content,
  614. "signatures": {"test.com": {"ed25519:0": "some9signature"}},
  615. },
  616. room_version=RoomVersions.V10,
  617. )
  618. pl_event2_content = {"events": {"m.room.name": "42", "m.room.power_levels": 42}}
  619. pl_event2 = make_event_from_dict(
  620. {
  621. "room_id": TEST_ROOM_ID,
  622. **_maybe_get_event_id_dict_for_room_version(RoomVersions.V10),
  623. "type": "m.room.power_levels",
  624. "sender": "@test:test.com",
  625. "state_key": "",
  626. "content": pl_event2_content,
  627. "signatures": {"test.com": {"ed25519:0": "some9signature"}},
  628. },
  629. room_version=RoomVersions.V10,
  630. )
  631. with self.assertRaises(SynapseError):
  632. event_auth._check_power_levels(
  633. pl_event.room_version, pl_event, {("fake_type", "fake_key"): pl_event2}
  634. )
  635. with self.assertRaises(SynapseError):
  636. event_auth._check_power_levels(
  637. pl_event.room_version, pl_event2, {("fake_type", "fake_key"): pl_event}
  638. )
  639. def test_room_v10_rejects_other_non_integer_power_levels(self) -> None:
  640. """We should reject PLs that are non-integer, non-string JSON values.
  641. test_room_v10_rejects_string_power_levels above handles the string case.
  642. """
  643. def create_event(pl_event_content: Dict[str, Any]) -> EventBase:
  644. return make_event_from_dict(
  645. {
  646. "room_id": TEST_ROOM_ID,
  647. **_maybe_get_event_id_dict_for_room_version(RoomVersions.V10),
  648. "type": "m.room.power_levels",
  649. "sender": "@test:test.com",
  650. "state_key": "",
  651. "content": pl_event_content,
  652. "signatures": {"test.com": {"ed25519:0": "some9signature"}},
  653. },
  654. room_version=RoomVersions.V10,
  655. )
  656. contents: Iterable[Dict[str, Any]] = [
  657. {"notifications": {"room": None}},
  658. {"users": {"@alice:wonderland": []}},
  659. {"users_default": {}},
  660. ]
  661. for content in contents:
  662. event = create_event(content)
  663. with self.assertRaises(SynapseError):
  664. event_auth._check_power_levels(event.room_version, event, {})
  665. # helpers for making events
  666. TEST_DOMAIN = "example.com"
  667. TEST_ROOM_ID = f"!test_room:{TEST_DOMAIN}"
  668. def _create_event(
  669. room_version: RoomVersion,
  670. user_id: str,
  671. ) -> EventBase:
  672. return make_event_from_dict(
  673. {
  674. "room_id": TEST_ROOM_ID,
  675. **_maybe_get_event_id_dict_for_room_version(room_version),
  676. "type": "m.room.create",
  677. "state_key": "",
  678. "sender": user_id,
  679. "content": {"creator": user_id},
  680. "auth_events": [],
  681. },
  682. room_version=room_version,
  683. )
  684. def _member_event(
  685. room_version: RoomVersion,
  686. user_id: str,
  687. membership: str,
  688. sender: Optional[str] = None,
  689. additional_content: Optional[dict] = None,
  690. ) -> EventBase:
  691. return make_event_from_dict(
  692. {
  693. "room_id": TEST_ROOM_ID,
  694. **_maybe_get_event_id_dict_for_room_version(room_version),
  695. "type": "m.room.member",
  696. "sender": sender or user_id,
  697. "state_key": user_id,
  698. "content": {"membership": membership, **(additional_content or {})},
  699. "auth_events": [],
  700. "prev_events": [],
  701. },
  702. room_version=room_version,
  703. )
  704. def _join_event(
  705. room_version: RoomVersion,
  706. user_id: str,
  707. additional_content: Optional[dict] = None,
  708. ) -> EventBase:
  709. return _member_event(
  710. room_version,
  711. user_id,
  712. "join",
  713. additional_content=additional_content,
  714. )
  715. def _power_levels_event(
  716. room_version: RoomVersion,
  717. sender: str,
  718. content: JsonDict,
  719. ) -> EventBase:
  720. return make_event_from_dict(
  721. {
  722. "room_id": TEST_ROOM_ID,
  723. **_maybe_get_event_id_dict_for_room_version(room_version),
  724. "type": "m.room.power_levels",
  725. "sender": sender,
  726. "state_key": "",
  727. "content": content,
  728. },
  729. room_version=room_version,
  730. )
  731. def _alias_event(room_version: RoomVersion, sender: str, **kwargs: Any) -> EventBase:
  732. data = {
  733. "room_id": TEST_ROOM_ID,
  734. **_maybe_get_event_id_dict_for_room_version(room_version),
  735. "type": "m.room.aliases",
  736. "sender": sender,
  737. "state_key": get_domain_from_id(sender),
  738. "content": {"aliases": []},
  739. }
  740. data.update(**kwargs)
  741. return make_event_from_dict(data, room_version=room_version)
  742. def _build_auth_dict_for_room_version(
  743. room_version: RoomVersion, auth_events: Iterable[EventBase]
  744. ) -> List:
  745. if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
  746. return [(e.event_id, "not_used") for e in auth_events]
  747. else:
  748. return [e.event_id for e in auth_events]
  749. def _random_state_event(
  750. room_version: RoomVersion,
  751. sender: str,
  752. auth_events: Optional[Iterable[EventBase]] = None,
  753. ) -> EventBase:
  754. if auth_events is None:
  755. auth_events = []
  756. return make_event_from_dict(
  757. {
  758. "room_id": TEST_ROOM_ID,
  759. **_maybe_get_event_id_dict_for_room_version(room_version),
  760. "type": "test.state",
  761. "sender": sender,
  762. "state_key": "",
  763. "content": {"membership": "join"},
  764. "auth_events": _build_auth_dict_for_room_version(room_version, auth_events),
  765. },
  766. room_version=room_version,
  767. )
  768. def _join_rules_event(
  769. room_version: RoomVersion, sender: str, join_rule: str
  770. ) -> EventBase:
  771. return make_event_from_dict(
  772. {
  773. "room_id": TEST_ROOM_ID,
  774. **_maybe_get_event_id_dict_for_room_version(room_version),
  775. "type": "m.room.join_rules",
  776. "sender": sender,
  777. "state_key": "",
  778. "content": {
  779. "join_rule": join_rule,
  780. },
  781. },
  782. room_version=room_version,
  783. )
  784. event_count = 0
  785. def _maybe_get_event_id_dict_for_room_version(room_version: RoomVersion) -> dict:
  786. """If this room version needs it, generate an event id"""
  787. if room_version.event_format != EventFormatVersions.ROOM_V1_V2:
  788. return {}
  789. global event_count
  790. c = event_count
  791. event_count += 1
  792. return {"event_id": "!%i:example.com" % (c,)}