1
0

test_event_auth.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import unittest
  16. from synapse import event_auth
  17. from synapse.api.errors import AuthError
  18. from synapse.api.room_versions import RoomVersions
  19. from synapse.events import make_event_from_dict
  20. from synapse.types import get_domain_from_id
  21. class EventAuthTestCase(unittest.TestCase):
  22. def test_random_users_cannot_send_state_before_first_pl(self):
  23. """
  24. Check that, before the first PL lands, the creator is the only user
  25. that can send a state event.
  26. """
  27. creator = "@creator:example.com"
  28. joiner = "@joiner:example.com"
  29. auth_events = {
  30. ("m.room.create", ""): _create_event(creator),
  31. ("m.room.member", creator): _join_event(creator),
  32. ("m.room.member", joiner): _join_event(joiner),
  33. }
  34. # creator should be able to send state
  35. event_auth.check(
  36. RoomVersions.V1,
  37. _random_state_event(creator),
  38. auth_events,
  39. do_sig_check=False,
  40. )
  41. # joiner should not be able to send state
  42. self.assertRaises(
  43. AuthError,
  44. event_auth.check,
  45. RoomVersions.V1,
  46. _random_state_event(joiner),
  47. auth_events,
  48. do_sig_check=False,
  49. )
  50. def test_state_default_level(self):
  51. """
  52. Check that users above the state_default level can send state and
  53. those below cannot
  54. """
  55. creator = "@creator:example.com"
  56. pleb = "@joiner:example.com"
  57. king = "@joiner2:example.com"
  58. auth_events = {
  59. ("m.room.create", ""): _create_event(creator),
  60. ("m.room.member", creator): _join_event(creator),
  61. ("m.room.power_levels", ""): _power_levels_event(
  62. creator, {"state_default": "30", "users": {pleb: "29", king: "30"}}
  63. ),
  64. ("m.room.member", pleb): _join_event(pleb),
  65. ("m.room.member", king): _join_event(king),
  66. }
  67. # pleb should not be able to send state
  68. self.assertRaises(
  69. AuthError,
  70. event_auth.check,
  71. RoomVersions.V1,
  72. _random_state_event(pleb),
  73. auth_events,
  74. do_sig_check=False,
  75. ),
  76. # king should be able to send state
  77. event_auth.check(
  78. RoomVersions.V1,
  79. _random_state_event(king),
  80. auth_events,
  81. do_sig_check=False,
  82. )
  83. def test_alias_event(self):
  84. """Alias events have special behavior up through room version 6."""
  85. creator = "@creator:example.com"
  86. other = "@other:example.com"
  87. auth_events = {
  88. ("m.room.create", ""): _create_event(creator),
  89. ("m.room.member", creator): _join_event(creator),
  90. }
  91. # creator should be able to send aliases
  92. event_auth.check(
  93. RoomVersions.V1,
  94. _alias_event(creator),
  95. auth_events,
  96. do_sig_check=False,
  97. )
  98. # Reject an event with no state key.
  99. with self.assertRaises(AuthError):
  100. event_auth.check(
  101. RoomVersions.V1,
  102. _alias_event(creator, state_key=""),
  103. auth_events,
  104. do_sig_check=False,
  105. )
  106. # If the domain of the sender does not match the state key, reject.
  107. with self.assertRaises(AuthError):
  108. event_auth.check(
  109. RoomVersions.V1,
  110. _alias_event(creator, state_key="test.com"),
  111. auth_events,
  112. do_sig_check=False,
  113. )
  114. # Note that the member does *not* need to be in the room.
  115. event_auth.check(
  116. RoomVersions.V1,
  117. _alias_event(other),
  118. auth_events,
  119. do_sig_check=False,
  120. )
  121. def test_msc2432_alias_event(self):
  122. """After MSC2432, alias events have no special behavior."""
  123. creator = "@creator:example.com"
  124. other = "@other:example.com"
  125. auth_events = {
  126. ("m.room.create", ""): _create_event(creator),
  127. ("m.room.member", creator): _join_event(creator),
  128. }
  129. # creator should be able to send aliases
  130. event_auth.check(
  131. RoomVersions.V6,
  132. _alias_event(creator),
  133. auth_events,
  134. do_sig_check=False,
  135. )
  136. # No particular checks are done on the state key.
  137. event_auth.check(
  138. RoomVersions.V6,
  139. _alias_event(creator, state_key=""),
  140. auth_events,
  141. do_sig_check=False,
  142. )
  143. event_auth.check(
  144. RoomVersions.V6,
  145. _alias_event(creator, state_key="test.com"),
  146. auth_events,
  147. do_sig_check=False,
  148. )
  149. # Per standard auth rules, the member must be in the room.
  150. with self.assertRaises(AuthError):
  151. event_auth.check(
  152. RoomVersions.V6,
  153. _alias_event(other),
  154. auth_events,
  155. do_sig_check=False,
  156. )
  157. def test_msc2209(self):
  158. """
  159. Notifications power levels get checked due to MSC2209.
  160. """
  161. creator = "@creator:example.com"
  162. pleb = "@joiner:example.com"
  163. auth_events = {
  164. ("m.room.create", ""): _create_event(creator),
  165. ("m.room.member", creator): _join_event(creator),
  166. ("m.room.power_levels", ""): _power_levels_event(
  167. creator, {"state_default": "30", "users": {pleb: "30"}}
  168. ),
  169. ("m.room.member", pleb): _join_event(pleb),
  170. }
  171. # pleb should be able to modify the notifications power level.
  172. event_auth.check(
  173. RoomVersions.V1,
  174. _power_levels_event(pleb, {"notifications": {"room": 100}}),
  175. auth_events,
  176. do_sig_check=False,
  177. )
  178. # But an MSC2209 room rejects this change.
  179. with self.assertRaises(AuthError):
  180. event_auth.check(
  181. RoomVersions.V6,
  182. _power_levels_event(pleb, {"notifications": {"room": 100}}),
  183. auth_events,
  184. do_sig_check=False,
  185. )
  186. def test_join_rules_public(self):
  187. """
  188. Test joining a public room.
  189. """
  190. creator = "@creator:example.com"
  191. pleb = "@joiner:example.com"
  192. auth_events = {
  193. ("m.room.create", ""): _create_event(creator),
  194. ("m.room.member", creator): _join_event(creator),
  195. ("m.room.join_rules", ""): _join_rules_event(creator, "public"),
  196. }
  197. # Check join.
  198. event_auth.check(
  199. RoomVersions.V6,
  200. _join_event(pleb),
  201. auth_events,
  202. do_sig_check=False,
  203. )
  204. # A user cannot be force-joined to a room.
  205. with self.assertRaises(AuthError):
  206. event_auth.check(
  207. RoomVersions.V6,
  208. _member_event(pleb, "join", sender=creator),
  209. auth_events,
  210. do_sig_check=False,
  211. )
  212. # Banned should be rejected.
  213. auth_events[("m.room.member", pleb)] = _member_event(pleb, "ban")
  214. with self.assertRaises(AuthError):
  215. event_auth.check(
  216. RoomVersions.V6,
  217. _join_event(pleb),
  218. auth_events,
  219. do_sig_check=False,
  220. )
  221. # A user who left can re-join.
  222. auth_events[("m.room.member", pleb)] = _member_event(pleb, "leave")
  223. event_auth.check(
  224. RoomVersions.V6,
  225. _join_event(pleb),
  226. auth_events,
  227. do_sig_check=False,
  228. )
  229. # A user can send a join if they're in the room.
  230. auth_events[("m.room.member", pleb)] = _member_event(pleb, "join")
  231. event_auth.check(
  232. RoomVersions.V6,
  233. _join_event(pleb),
  234. auth_events,
  235. do_sig_check=False,
  236. )
  237. # A user can accept an invite.
  238. auth_events[("m.room.member", pleb)] = _member_event(
  239. pleb, "invite", sender=creator
  240. )
  241. event_auth.check(
  242. RoomVersions.V6,
  243. _join_event(pleb),
  244. auth_events,
  245. do_sig_check=False,
  246. )
  247. def test_join_rules_invite(self):
  248. """
  249. Test joining an invite only room.
  250. """
  251. creator = "@creator:example.com"
  252. pleb = "@joiner:example.com"
  253. auth_events = {
  254. ("m.room.create", ""): _create_event(creator),
  255. ("m.room.member", creator): _join_event(creator),
  256. ("m.room.join_rules", ""): _join_rules_event(creator, "invite"),
  257. }
  258. # A join without an invite is rejected.
  259. with self.assertRaises(AuthError):
  260. event_auth.check(
  261. RoomVersions.V6,
  262. _join_event(pleb),
  263. auth_events,
  264. do_sig_check=False,
  265. )
  266. # A user cannot be force-joined to a room.
  267. with self.assertRaises(AuthError):
  268. event_auth.check(
  269. RoomVersions.V6,
  270. _member_event(pleb, "join", sender=creator),
  271. auth_events,
  272. do_sig_check=False,
  273. )
  274. # Banned should be rejected.
  275. auth_events[("m.room.member", pleb)] = _member_event(pleb, "ban")
  276. with self.assertRaises(AuthError):
  277. event_auth.check(
  278. RoomVersions.V6,
  279. _join_event(pleb),
  280. auth_events,
  281. do_sig_check=False,
  282. )
  283. # A user who left cannot re-join.
  284. auth_events[("m.room.member", pleb)] = _member_event(pleb, "leave")
  285. with self.assertRaises(AuthError):
  286. event_auth.check(
  287. RoomVersions.V6,
  288. _join_event(pleb),
  289. auth_events,
  290. do_sig_check=False,
  291. )
  292. # A user can send a join if they're in the room.
  293. auth_events[("m.room.member", pleb)] = _member_event(pleb, "join")
  294. event_auth.check(
  295. RoomVersions.V6,
  296. _join_event(pleb),
  297. auth_events,
  298. do_sig_check=False,
  299. )
  300. # A user can accept an invite.
  301. auth_events[("m.room.member", pleb)] = _member_event(
  302. pleb, "invite", sender=creator
  303. )
  304. event_auth.check(
  305. RoomVersions.V6,
  306. _join_event(pleb),
  307. auth_events,
  308. do_sig_check=False,
  309. )
  310. def test_join_rules_msc3083_restricted(self):
  311. """
  312. Test joining a restricted room from MSC3083.
  313. This is pretty much the same test as public.
  314. """
  315. creator = "@creator:example.com"
  316. pleb = "@joiner:example.com"
  317. auth_events = {
  318. ("m.room.create", ""): _create_event(creator),
  319. ("m.room.member", creator): _join_event(creator),
  320. ("m.room.join_rules", ""): _join_rules_event(creator, "restricted"),
  321. }
  322. # Older room versions don't understand this join rule
  323. with self.assertRaises(AuthError):
  324. event_auth.check(
  325. RoomVersions.V6,
  326. _join_event(pleb),
  327. auth_events,
  328. do_sig_check=False,
  329. )
  330. # Check join.
  331. event_auth.check(
  332. RoomVersions.MSC3083,
  333. _join_event(pleb),
  334. auth_events,
  335. do_sig_check=False,
  336. )
  337. # A user cannot be force-joined to a room.
  338. with self.assertRaises(AuthError):
  339. event_auth.check(
  340. RoomVersions.MSC3083,
  341. _member_event(pleb, "join", sender=creator),
  342. auth_events,
  343. do_sig_check=False,
  344. )
  345. # Banned should be rejected.
  346. auth_events[("m.room.member", pleb)] = _member_event(pleb, "ban")
  347. with self.assertRaises(AuthError):
  348. event_auth.check(
  349. RoomVersions.MSC3083,
  350. _join_event(pleb),
  351. auth_events,
  352. do_sig_check=False,
  353. )
  354. # A user who left can re-join.
  355. auth_events[("m.room.member", pleb)] = _member_event(pleb, "leave")
  356. event_auth.check(
  357. RoomVersions.MSC3083,
  358. _join_event(pleb),
  359. auth_events,
  360. do_sig_check=False,
  361. )
  362. # A user can send a join if they're in the room.
  363. auth_events[("m.room.member", pleb)] = _member_event(pleb, "join")
  364. event_auth.check(
  365. RoomVersions.MSC3083,
  366. _join_event(pleb),
  367. auth_events,
  368. do_sig_check=False,
  369. )
  370. # A user can accept an invite.
  371. auth_events[("m.room.member", pleb)] = _member_event(
  372. pleb, "invite", sender=creator
  373. )
  374. event_auth.check(
  375. RoomVersions.MSC3083,
  376. _join_event(pleb),
  377. auth_events,
  378. do_sig_check=False,
  379. )
  380. # helpers for making events
  381. TEST_ROOM_ID = "!test:room"
  382. def _create_event(user_id):
  383. return make_event_from_dict(
  384. {
  385. "room_id": TEST_ROOM_ID,
  386. "event_id": _get_event_id(),
  387. "type": "m.room.create",
  388. "sender": user_id,
  389. "content": {"creator": user_id},
  390. }
  391. )
  392. def _member_event(user_id, membership, sender=None):
  393. return make_event_from_dict(
  394. {
  395. "room_id": TEST_ROOM_ID,
  396. "event_id": _get_event_id(),
  397. "type": "m.room.member",
  398. "sender": sender or user_id,
  399. "state_key": user_id,
  400. "content": {"membership": membership},
  401. "prev_events": [],
  402. }
  403. )
  404. def _join_event(user_id):
  405. return _member_event(user_id, "join")
  406. def _power_levels_event(sender, content):
  407. return make_event_from_dict(
  408. {
  409. "room_id": TEST_ROOM_ID,
  410. "event_id": _get_event_id(),
  411. "type": "m.room.power_levels",
  412. "sender": sender,
  413. "state_key": "",
  414. "content": content,
  415. }
  416. )
  417. def _alias_event(sender, **kwargs):
  418. data = {
  419. "room_id": TEST_ROOM_ID,
  420. "event_id": _get_event_id(),
  421. "type": "m.room.aliases",
  422. "sender": sender,
  423. "state_key": get_domain_from_id(sender),
  424. "content": {"aliases": []},
  425. }
  426. data.update(**kwargs)
  427. return make_event_from_dict(data)
  428. def _random_state_event(sender):
  429. return make_event_from_dict(
  430. {
  431. "room_id": TEST_ROOM_ID,
  432. "event_id": _get_event_id(),
  433. "type": "test.state",
  434. "sender": sender,
  435. "state_key": "",
  436. "content": {"membership": "join"},
  437. }
  438. )
  439. def _join_rules_event(sender, join_rule):
  440. return make_event_from_dict(
  441. {
  442. "room_id": TEST_ROOM_ID,
  443. "event_id": _get_event_id(),
  444. "type": "m.room.join_rules",
  445. "sender": sender,
  446. "state_key": "",
  447. "content": {
  448. "join_rule": join_rule,
  449. },
  450. }
  451. )
  452. event_count = 0
  453. def _get_event_id():
  454. global event_count
  455. c = event_count
  456. event_count += 1
  457. return "!%i:example.com" % (c,)