test_event_auth.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. # Copyright 2018 New Vector Ltd
  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 Optional
  16. from synapse import event_auth
  17. from synapse.api.constants import EventContentFields
  18. from synapse.api.errors import AuthError
  19. from synapse.api.room_versions import RoomVersions
  20. from synapse.events import EventBase, make_event_from_dict
  21. from synapse.types import JsonDict, get_domain_from_id
  22. class EventAuthTestCase(unittest.TestCase):
  23. def test_random_users_cannot_send_state_before_first_pl(self):
  24. """
  25. Check that, before the first PL lands, the creator is the only user
  26. that can send a state event.
  27. """
  28. creator = "@creator:example.com"
  29. joiner = "@joiner:example.com"
  30. auth_events = {
  31. ("m.room.create", ""): _create_event(creator),
  32. ("m.room.member", creator): _join_event(creator),
  33. ("m.room.member", joiner): _join_event(joiner),
  34. }
  35. # creator should be able to send state
  36. event_auth.check_auth_rules_for_event(
  37. RoomVersions.V1,
  38. _random_state_event(creator),
  39. auth_events,
  40. )
  41. # joiner should not be able to send state
  42. self.assertRaises(
  43. AuthError,
  44. event_auth.check_auth_rules_for_event,
  45. RoomVersions.V1,
  46. _random_state_event(joiner),
  47. auth_events,
  48. )
  49. def test_state_default_level(self):
  50. """
  51. Check that users above the state_default level can send state and
  52. those below cannot
  53. """
  54. creator = "@creator:example.com"
  55. pleb = "@joiner:example.com"
  56. king = "@joiner2:example.com"
  57. auth_events = {
  58. ("m.room.create", ""): _create_event(creator),
  59. ("m.room.member", creator): _join_event(creator),
  60. ("m.room.power_levels", ""): _power_levels_event(
  61. creator, {"state_default": "30", "users": {pleb: "29", king: "30"}}
  62. ),
  63. ("m.room.member", pleb): _join_event(pleb),
  64. ("m.room.member", king): _join_event(king),
  65. }
  66. # pleb should not be able to send state
  67. self.assertRaises(
  68. AuthError,
  69. event_auth.check_auth_rules_for_event,
  70. RoomVersions.V1,
  71. _random_state_event(pleb),
  72. auth_events,
  73. ),
  74. # king should be able to send state
  75. event_auth.check_auth_rules_for_event(
  76. RoomVersions.V1,
  77. _random_state_event(king),
  78. auth_events,
  79. )
  80. def test_alias_event(self):
  81. """Alias events have special behavior up through room version 6."""
  82. creator = "@creator:example.com"
  83. other = "@other:example.com"
  84. auth_events = {
  85. ("m.room.create", ""): _create_event(creator),
  86. ("m.room.member", creator): _join_event(creator),
  87. }
  88. # creator should be able to send aliases
  89. event_auth.check_auth_rules_for_event(
  90. RoomVersions.V1,
  91. _alias_event(creator),
  92. auth_events,
  93. )
  94. # Reject an event with no state key.
  95. with self.assertRaises(AuthError):
  96. event_auth.check_auth_rules_for_event(
  97. RoomVersions.V1,
  98. _alias_event(creator, state_key=""),
  99. auth_events,
  100. )
  101. # If the domain of the sender does not match the state key, reject.
  102. with self.assertRaises(AuthError):
  103. event_auth.check_auth_rules_for_event(
  104. RoomVersions.V1,
  105. _alias_event(creator, state_key="test.com"),
  106. auth_events,
  107. )
  108. # Note that the member does *not* need to be in the room.
  109. event_auth.check_auth_rules_for_event(
  110. RoomVersions.V1,
  111. _alias_event(other),
  112. auth_events,
  113. )
  114. def test_msc2432_alias_event(self):
  115. """After MSC2432, alias events have no special behavior."""
  116. creator = "@creator:example.com"
  117. other = "@other:example.com"
  118. auth_events = {
  119. ("m.room.create", ""): _create_event(creator),
  120. ("m.room.member", creator): _join_event(creator),
  121. }
  122. # creator should be able to send aliases
  123. event_auth.check_auth_rules_for_event(
  124. RoomVersions.V6,
  125. _alias_event(creator),
  126. auth_events,
  127. )
  128. # No particular checks are done on the state key.
  129. event_auth.check_auth_rules_for_event(
  130. RoomVersions.V6,
  131. _alias_event(creator, state_key=""),
  132. auth_events,
  133. )
  134. event_auth.check_auth_rules_for_event(
  135. RoomVersions.V6,
  136. _alias_event(creator, state_key="test.com"),
  137. auth_events,
  138. )
  139. # Per standard auth rules, the member must be in the room.
  140. with self.assertRaises(AuthError):
  141. event_auth.check_auth_rules_for_event(
  142. RoomVersions.V6,
  143. _alias_event(other),
  144. auth_events,
  145. )
  146. def test_msc2209(self):
  147. """
  148. Notifications power levels get checked due to MSC2209.
  149. """
  150. creator = "@creator:example.com"
  151. pleb = "@joiner:example.com"
  152. auth_events = {
  153. ("m.room.create", ""): _create_event(creator),
  154. ("m.room.member", creator): _join_event(creator),
  155. ("m.room.power_levels", ""): _power_levels_event(
  156. creator, {"state_default": "30", "users": {pleb: "30"}}
  157. ),
  158. ("m.room.member", pleb): _join_event(pleb),
  159. }
  160. # pleb should be able to modify the notifications power level.
  161. event_auth.check_auth_rules_for_event(
  162. RoomVersions.V1,
  163. _power_levels_event(pleb, {"notifications": {"room": 100}}),
  164. auth_events,
  165. )
  166. # But an MSC2209 room rejects this change.
  167. with self.assertRaises(AuthError):
  168. event_auth.check_auth_rules_for_event(
  169. RoomVersions.V6,
  170. _power_levels_event(pleb, {"notifications": {"room": 100}}),
  171. auth_events,
  172. )
  173. def test_join_rules_public(self):
  174. """
  175. Test joining a public room.
  176. """
  177. creator = "@creator:example.com"
  178. pleb = "@joiner:example.com"
  179. auth_events = {
  180. ("m.room.create", ""): _create_event(creator),
  181. ("m.room.member", creator): _join_event(creator),
  182. ("m.room.join_rules", ""): _join_rules_event(creator, "public"),
  183. }
  184. # Check join.
  185. event_auth.check_auth_rules_for_event(
  186. RoomVersions.V6,
  187. _join_event(pleb),
  188. auth_events,
  189. )
  190. # A user cannot be force-joined to a room.
  191. with self.assertRaises(AuthError):
  192. event_auth.check_auth_rules_for_event(
  193. RoomVersions.V6,
  194. _member_event(pleb, "join", sender=creator),
  195. auth_events,
  196. )
  197. # Banned should be rejected.
  198. auth_events[("m.room.member", pleb)] = _member_event(pleb, "ban")
  199. with self.assertRaises(AuthError):
  200. event_auth.check_auth_rules_for_event(
  201. RoomVersions.V6,
  202. _join_event(pleb),
  203. auth_events,
  204. )
  205. # A user who left can re-join.
  206. auth_events[("m.room.member", pleb)] = _member_event(pleb, "leave")
  207. event_auth.check_auth_rules_for_event(
  208. RoomVersions.V6,
  209. _join_event(pleb),
  210. auth_events,
  211. )
  212. # A user can send a join if they're in the room.
  213. auth_events[("m.room.member", pleb)] = _member_event(pleb, "join")
  214. event_auth.check_auth_rules_for_event(
  215. RoomVersions.V6,
  216. _join_event(pleb),
  217. auth_events,
  218. )
  219. # A user can accept an invite.
  220. auth_events[("m.room.member", pleb)] = _member_event(
  221. pleb, "invite", sender=creator
  222. )
  223. event_auth.check_auth_rules_for_event(
  224. RoomVersions.V6,
  225. _join_event(pleb),
  226. auth_events,
  227. )
  228. def test_join_rules_invite(self):
  229. """
  230. Test joining an invite only room.
  231. """
  232. creator = "@creator:example.com"
  233. pleb = "@joiner:example.com"
  234. auth_events = {
  235. ("m.room.create", ""): _create_event(creator),
  236. ("m.room.member", creator): _join_event(creator),
  237. ("m.room.join_rules", ""): _join_rules_event(creator, "invite"),
  238. }
  239. # A join without an invite is rejected.
  240. with self.assertRaises(AuthError):
  241. event_auth.check_auth_rules_for_event(
  242. RoomVersions.V6,
  243. _join_event(pleb),
  244. auth_events,
  245. )
  246. # A user cannot be force-joined to a room.
  247. with self.assertRaises(AuthError):
  248. event_auth.check_auth_rules_for_event(
  249. RoomVersions.V6,
  250. _member_event(pleb, "join", sender=creator),
  251. auth_events,
  252. )
  253. # Banned should be rejected.
  254. auth_events[("m.room.member", pleb)] = _member_event(pleb, "ban")
  255. with self.assertRaises(AuthError):
  256. event_auth.check_auth_rules_for_event(
  257. RoomVersions.V6,
  258. _join_event(pleb),
  259. auth_events,
  260. )
  261. # A user who left cannot re-join.
  262. auth_events[("m.room.member", pleb)] = _member_event(pleb, "leave")
  263. with self.assertRaises(AuthError):
  264. event_auth.check_auth_rules_for_event(
  265. RoomVersions.V6,
  266. _join_event(pleb),
  267. auth_events,
  268. )
  269. # A user can send a join if they're in the room.
  270. auth_events[("m.room.member", pleb)] = _member_event(pleb, "join")
  271. event_auth.check_auth_rules_for_event(
  272. RoomVersions.V6,
  273. _join_event(pleb),
  274. auth_events,
  275. )
  276. # A user can accept an invite.
  277. auth_events[("m.room.member", pleb)] = _member_event(
  278. pleb, "invite", sender=creator
  279. )
  280. event_auth.check_auth_rules_for_event(
  281. RoomVersions.V6,
  282. _join_event(pleb),
  283. auth_events,
  284. )
  285. def test_join_rules_msc3083_restricted(self):
  286. """
  287. Test joining a restricted room from MSC3083.
  288. This is similar to the public test, but has some additional checks on
  289. signatures.
  290. The checks which care about signatures fake them by simply adding an
  291. object of the proper form, not generating valid signatures.
  292. """
  293. creator = "@creator:example.com"
  294. pleb = "@joiner:example.com"
  295. auth_events = {
  296. ("m.room.create", ""): _create_event(creator),
  297. ("m.room.member", creator): _join_event(creator),
  298. ("m.room.power_levels", ""): _power_levels_event(creator, {"invite": 0}),
  299. ("m.room.join_rules", ""): _join_rules_event(creator, "restricted"),
  300. }
  301. # Older room versions don't understand this join rule
  302. with self.assertRaises(AuthError):
  303. event_auth.check_auth_rules_for_event(
  304. RoomVersions.V6,
  305. _join_event(pleb),
  306. auth_events,
  307. )
  308. # A properly formatted join event should work.
  309. authorised_join_event = _join_event(
  310. pleb,
  311. additional_content={
  312. EventContentFields.AUTHORISING_USER: "@creator:example.com"
  313. },
  314. )
  315. event_auth.check_auth_rules_for_event(
  316. RoomVersions.V8,
  317. authorised_join_event,
  318. auth_events,
  319. )
  320. # A join issued by a specific user works (i.e. the power level checks
  321. # are done properly).
  322. pl_auth_events = auth_events.copy()
  323. pl_auth_events[("m.room.power_levels", "")] = _power_levels_event(
  324. creator, {"invite": 100, "users": {"@inviter:foo.test": 150}}
  325. )
  326. pl_auth_events[("m.room.member", "@inviter:foo.test")] = _join_event(
  327. "@inviter:foo.test"
  328. )
  329. event_auth.check_auth_rules_for_event(
  330. RoomVersions.V8,
  331. _join_event(
  332. pleb,
  333. additional_content={
  334. EventContentFields.AUTHORISING_USER: "@inviter:foo.test"
  335. },
  336. ),
  337. pl_auth_events,
  338. )
  339. # A join which is missing an authorised server is rejected.
  340. with self.assertRaises(AuthError):
  341. event_auth.check_auth_rules_for_event(
  342. RoomVersions.V8,
  343. _join_event(pleb),
  344. auth_events,
  345. )
  346. # An join authorised by a user who is not in the room is rejected.
  347. pl_auth_events = auth_events.copy()
  348. pl_auth_events[("m.room.power_levels", "")] = _power_levels_event(
  349. creator, {"invite": 100, "users": {"@other:example.com": 150}}
  350. )
  351. with self.assertRaises(AuthError):
  352. event_auth.check_auth_rules_for_event(
  353. RoomVersions.V8,
  354. _join_event(
  355. pleb,
  356. additional_content={
  357. EventContentFields.AUTHORISING_USER: "@other:example.com"
  358. },
  359. ),
  360. auth_events,
  361. )
  362. # A user cannot be force-joined to a room. (This uses an event which
  363. # *would* be valid, but is sent be a different user.)
  364. with self.assertRaises(AuthError):
  365. event_auth.check_auth_rules_for_event(
  366. RoomVersions.V8,
  367. _member_event(
  368. pleb,
  369. "join",
  370. sender=creator,
  371. additional_content={
  372. EventContentFields.AUTHORISING_USER: "@inviter:foo.test"
  373. },
  374. ),
  375. auth_events,
  376. )
  377. # Banned should be rejected.
  378. auth_events[("m.room.member", pleb)] = _member_event(pleb, "ban")
  379. with self.assertRaises(AuthError):
  380. event_auth.check_auth_rules_for_event(
  381. RoomVersions.V8,
  382. authorised_join_event,
  383. auth_events,
  384. )
  385. # A user who left can re-join.
  386. auth_events[("m.room.member", pleb)] = _member_event(pleb, "leave")
  387. event_auth.check_auth_rules_for_event(
  388. RoomVersions.V8,
  389. authorised_join_event,
  390. auth_events,
  391. )
  392. # A user can send a join if they're in the room. (This doesn't need to
  393. # be authorised since the user is already joined.)
  394. auth_events[("m.room.member", pleb)] = _member_event(pleb, "join")
  395. event_auth.check_auth_rules_for_event(
  396. RoomVersions.V8,
  397. _join_event(pleb),
  398. auth_events,
  399. )
  400. # A user can accept an invite. (This doesn't need to be authorised since
  401. # the user was invited.)
  402. auth_events[("m.room.member", pleb)] = _member_event(
  403. pleb, "invite", sender=creator
  404. )
  405. event_auth.check_auth_rules_for_event(
  406. RoomVersions.V8,
  407. _join_event(pleb),
  408. auth_events,
  409. )
  410. # helpers for making events
  411. TEST_ROOM_ID = "!test:room"
  412. def _create_event(user_id: str) -> EventBase:
  413. return make_event_from_dict(
  414. {
  415. "room_id": TEST_ROOM_ID,
  416. "event_id": _get_event_id(),
  417. "type": "m.room.create",
  418. "sender": user_id,
  419. "content": {"creator": user_id},
  420. }
  421. )
  422. def _member_event(
  423. user_id: str,
  424. membership: str,
  425. sender: Optional[str] = None,
  426. additional_content: Optional[dict] = None,
  427. ) -> EventBase:
  428. return make_event_from_dict(
  429. {
  430. "room_id": TEST_ROOM_ID,
  431. "event_id": _get_event_id(),
  432. "type": "m.room.member",
  433. "sender": sender or user_id,
  434. "state_key": user_id,
  435. "content": {"membership": membership, **(additional_content or {})},
  436. "prev_events": [],
  437. }
  438. )
  439. def _join_event(user_id: str, additional_content: Optional[dict] = None) -> EventBase:
  440. return _member_event(user_id, "join", additional_content=additional_content)
  441. def _power_levels_event(sender: str, content: JsonDict) -> EventBase:
  442. return make_event_from_dict(
  443. {
  444. "room_id": TEST_ROOM_ID,
  445. "event_id": _get_event_id(),
  446. "type": "m.room.power_levels",
  447. "sender": sender,
  448. "state_key": "",
  449. "content": content,
  450. }
  451. )
  452. def _alias_event(sender: str, **kwargs) -> EventBase:
  453. data = {
  454. "room_id": TEST_ROOM_ID,
  455. "event_id": _get_event_id(),
  456. "type": "m.room.aliases",
  457. "sender": sender,
  458. "state_key": get_domain_from_id(sender),
  459. "content": {"aliases": []},
  460. }
  461. data.update(**kwargs)
  462. return make_event_from_dict(data)
  463. def _random_state_event(sender: str) -> EventBase:
  464. return make_event_from_dict(
  465. {
  466. "room_id": TEST_ROOM_ID,
  467. "event_id": _get_event_id(),
  468. "type": "test.state",
  469. "sender": sender,
  470. "state_key": "",
  471. "content": {"membership": "join"},
  472. }
  473. )
  474. def _join_rules_event(sender: str, join_rule: str) -> EventBase:
  475. return make_event_from_dict(
  476. {
  477. "room_id": TEST_ROOM_ID,
  478. "event_id": _get_event_id(),
  479. "type": "m.room.join_rules",
  480. "sender": sender,
  481. "state_key": "",
  482. "content": {
  483. "join_rule": join_rule,
  484. },
  485. }
  486. )
  487. event_count = 0
  488. def _get_event_id() -> str:
  489. global event_count
  490. c = event_count
  491. event_count += 1
  492. return "!%i:example.com" % (c,)