test_event_auth.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. # helpers for making events
  187. TEST_ROOM_ID = "!test:room"
  188. def _create_event(user_id):
  189. return make_event_from_dict(
  190. {
  191. "room_id": TEST_ROOM_ID,
  192. "event_id": _get_event_id(),
  193. "type": "m.room.create",
  194. "sender": user_id,
  195. "content": {"creator": user_id},
  196. }
  197. )
  198. def _join_event(user_id):
  199. return make_event_from_dict(
  200. {
  201. "room_id": TEST_ROOM_ID,
  202. "event_id": _get_event_id(),
  203. "type": "m.room.member",
  204. "sender": user_id,
  205. "state_key": user_id,
  206. "content": {"membership": "join"},
  207. }
  208. )
  209. def _power_levels_event(sender, content):
  210. return make_event_from_dict(
  211. {
  212. "room_id": TEST_ROOM_ID,
  213. "event_id": _get_event_id(),
  214. "type": "m.room.power_levels",
  215. "sender": sender,
  216. "state_key": "",
  217. "content": content,
  218. }
  219. )
  220. def _alias_event(sender, **kwargs):
  221. data = {
  222. "room_id": TEST_ROOM_ID,
  223. "event_id": _get_event_id(),
  224. "type": "m.room.aliases",
  225. "sender": sender,
  226. "state_key": get_domain_from_id(sender),
  227. "content": {"aliases": []},
  228. }
  229. data.update(**kwargs)
  230. return make_event_from_dict(data)
  231. def _random_state_event(sender):
  232. return make_event_from_dict(
  233. {
  234. "room_id": TEST_ROOM_ID,
  235. "event_id": _get_event_id(),
  236. "type": "test.state",
  237. "sender": sender,
  238. "state_key": "",
  239. "content": {"membership": "join"},
  240. }
  241. )
  242. event_count = 0
  243. def _get_event_id():
  244. global event_count
  245. c = event_count
  246. event_count += 1
  247. return "!%i:example.com" % (c,)