test_event_auth.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, _random_state_event(king), auth_events, do_sig_check=False,
  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(
  90. RoomVersions.V1, _alias_event(creator), auth_events, do_sig_check=False,
  91. )
  92. # Reject an event with no state key.
  93. with self.assertRaises(AuthError):
  94. event_auth.check(
  95. RoomVersions.V1,
  96. _alias_event(creator, state_key=""),
  97. auth_events,
  98. do_sig_check=False,
  99. )
  100. # If the domain of the sender does not match the state key, reject.
  101. with self.assertRaises(AuthError):
  102. event_auth.check(
  103. RoomVersions.V1,
  104. _alias_event(creator, state_key="test.com"),
  105. auth_events,
  106. do_sig_check=False,
  107. )
  108. # Note that the member does *not* need to be in the room.
  109. event_auth.check(
  110. RoomVersions.V1, _alias_event(other), auth_events, do_sig_check=False,
  111. )
  112. def test_msc2432_alias_event(self):
  113. """After MSC2432, alias events have no special behavior."""
  114. creator = "@creator:example.com"
  115. other = "@other:example.com"
  116. auth_events = {
  117. ("m.room.create", ""): _create_event(creator),
  118. ("m.room.member", creator): _join_event(creator),
  119. }
  120. # creator should be able to send aliases
  121. event_auth.check(
  122. RoomVersions.MSC2432_DEV,
  123. _alias_event(creator),
  124. auth_events,
  125. do_sig_check=False,
  126. )
  127. # No particular checks are done on the state key.
  128. event_auth.check(
  129. RoomVersions.MSC2432_DEV,
  130. _alias_event(creator, state_key=""),
  131. auth_events,
  132. do_sig_check=False,
  133. )
  134. event_auth.check(
  135. RoomVersions.MSC2432_DEV,
  136. _alias_event(creator, state_key="test.com"),
  137. auth_events,
  138. do_sig_check=False,
  139. )
  140. # Per standard auth rules, the member must be in the room.
  141. with self.assertRaises(AuthError):
  142. event_auth.check(
  143. RoomVersions.MSC2432_DEV,
  144. _alias_event(other),
  145. auth_events,
  146. do_sig_check=False,
  147. )
  148. # helpers for making events
  149. TEST_ROOM_ID = "!test:room"
  150. def _create_event(user_id):
  151. return make_event_from_dict(
  152. {
  153. "room_id": TEST_ROOM_ID,
  154. "event_id": _get_event_id(),
  155. "type": "m.room.create",
  156. "sender": user_id,
  157. "content": {"creator": user_id},
  158. }
  159. )
  160. def _join_event(user_id):
  161. return make_event_from_dict(
  162. {
  163. "room_id": TEST_ROOM_ID,
  164. "event_id": _get_event_id(),
  165. "type": "m.room.member",
  166. "sender": user_id,
  167. "state_key": user_id,
  168. "content": {"membership": "join"},
  169. }
  170. )
  171. def _power_levels_event(sender, content):
  172. return make_event_from_dict(
  173. {
  174. "room_id": TEST_ROOM_ID,
  175. "event_id": _get_event_id(),
  176. "type": "m.room.power_levels",
  177. "sender": sender,
  178. "state_key": "",
  179. "content": content,
  180. }
  181. )
  182. def _alias_event(sender, **kwargs):
  183. data = {
  184. "room_id": TEST_ROOM_ID,
  185. "event_id": _get_event_id(),
  186. "type": "m.room.aliases",
  187. "sender": sender,
  188. "state_key": get_domain_from_id(sender),
  189. "content": {"aliases": []},
  190. }
  191. data.update(**kwargs)
  192. return make_event_from_dict(data)
  193. def _random_state_event(sender):
  194. return make_event_from_dict(
  195. {
  196. "room_id": TEST_ROOM_ID,
  197. "event_id": _get_event_id(),
  198. "type": "test.state",
  199. "sender": sender,
  200. "state_key": "",
  201. "content": {"membership": "join"},
  202. }
  203. )
  204. event_count = 0
  205. def _get_event_id():
  206. global event_count
  207. c = event_count
  208. event_count += 1
  209. return "!%i:example.com" % (c,)