event_auth.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. # Copyright 2021 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 logging
  15. from typing import TYPE_CHECKING, List, Mapping, Optional, Union
  16. from synapse import event_auth
  17. from synapse.api.constants import (
  18. EventTypes,
  19. JoinRules,
  20. Membership,
  21. RestrictedJoinRuleTypes,
  22. )
  23. from synapse.api.errors import AuthError, Codes, SynapseError
  24. from synapse.api.room_versions import RoomVersion
  25. from synapse.event_auth import (
  26. check_state_dependent_auth_rules,
  27. check_state_independent_auth_rules,
  28. )
  29. from synapse.events import EventBase
  30. from synapse.events.builder import EventBuilder
  31. from synapse.types import StateMap, StrCollection, get_domain_from_id
  32. if TYPE_CHECKING:
  33. from synapse.server import HomeServer
  34. logger = logging.getLogger(__name__)
  35. class EventAuthHandler:
  36. """
  37. This class contains methods for authenticating events added to room graphs.
  38. """
  39. def __init__(self, hs: "HomeServer"):
  40. self._clock = hs.get_clock()
  41. self._store = hs.get_datastores().main
  42. self._state_storage_controller = hs.get_storage_controllers().state
  43. self._server_name = hs.hostname
  44. async def check_auth_rules_from_context(
  45. self,
  46. event: EventBase,
  47. batched_auth_events: Optional[Mapping[str, EventBase]] = None,
  48. ) -> None:
  49. """Check an event passes the auth rules at its own auth events
  50. Args:
  51. event: event to be authed
  52. batched_auth_events: if the event being authed is part of a batch, any events
  53. from the same batch that may be necessary to auth the current event
  54. """
  55. await check_state_independent_auth_rules(
  56. self._store, event, batched_auth_events
  57. )
  58. auth_event_ids = event.auth_event_ids()
  59. if batched_auth_events:
  60. # Copy the batched auth events to avoid mutating them.
  61. auth_events_by_id = dict(batched_auth_events)
  62. needed_auth_event_ids = set(auth_event_ids) - set(batched_auth_events)
  63. if needed_auth_event_ids:
  64. auth_events_by_id.update(
  65. await self._store.get_events(needed_auth_event_ids)
  66. )
  67. else:
  68. auth_events_by_id = await self._store.get_events(auth_event_ids)
  69. check_state_dependent_auth_rules(event, auth_events_by_id.values())
  70. def compute_auth_events(
  71. self,
  72. event: Union[EventBase, EventBuilder],
  73. current_state_ids: StateMap[str],
  74. for_verification: bool = False,
  75. ) -> List[str]:
  76. """Given an event and current state return the list of event IDs used
  77. to auth an event.
  78. If `for_verification` is False then only return auth events that
  79. should be added to the event's `auth_events`.
  80. Returns:
  81. List of event IDs.
  82. """
  83. if event.type == EventTypes.Create:
  84. return []
  85. # Currently we ignore the `for_verification` flag even though there are
  86. # some situations where we can drop particular auth events when adding
  87. # to the event's `auth_events` (e.g. joins pointing to previous joins
  88. # when room is publicly joinable). Dropping event IDs has the
  89. # advantage that the auth chain for the room grows slower, but we use
  90. # the auth chain in state resolution v2 to order events, which means
  91. # care must be taken if dropping events to ensure that it doesn't
  92. # introduce undesirable "state reset" behaviour.
  93. #
  94. # All of which sounds a bit tricky so we don't bother for now.
  95. auth_ids = []
  96. for etype, state_key in event_auth.auth_types_for_event(
  97. event.room_version, event
  98. ):
  99. auth_ev_id = current_state_ids.get((etype, state_key))
  100. if auth_ev_id:
  101. auth_ids.append(auth_ev_id)
  102. return auth_ids
  103. async def get_user_which_could_invite(
  104. self, room_id: str, current_state_ids: StateMap[str]
  105. ) -> str:
  106. """
  107. Searches the room state for a local user who has the power level necessary
  108. to invite other users.
  109. Args:
  110. room_id: The room ID under search.
  111. current_state_ids: The current state of the room.
  112. Returns:
  113. The MXID of the user which could issue an invite.
  114. Raises:
  115. SynapseError if no appropriate user is found.
  116. """
  117. power_level_event_id = current_state_ids.get((EventTypes.PowerLevels, ""))
  118. invite_level = 0
  119. users_default_level = 0
  120. if power_level_event_id:
  121. power_level_event = await self._store.get_event(power_level_event_id)
  122. invite_level = power_level_event.content.get("invite", invite_level)
  123. users_default_level = power_level_event.content.get(
  124. "users_default", users_default_level
  125. )
  126. users = power_level_event.content.get("users", {})
  127. else:
  128. users = {}
  129. # Find the user with the highest power level (only interested in local
  130. # users).
  131. local_users_in_room = await self._store.get_local_users_in_room(room_id)
  132. chosen_user = max(
  133. local_users_in_room,
  134. key=lambda user: users.get(user, users_default_level),
  135. default=None,
  136. )
  137. # Return the chosen if they can issue invites.
  138. user_power_level = users.get(chosen_user, users_default_level)
  139. if chosen_user and user_power_level >= invite_level:
  140. logger.debug(
  141. "Found a user who can issue invites %s with power level %d >= invite level %d",
  142. chosen_user,
  143. user_power_level,
  144. invite_level,
  145. )
  146. return chosen_user
  147. # No user was found.
  148. raise SynapseError(
  149. 400,
  150. "Unable to find a user which could issue an invite",
  151. Codes.UNABLE_TO_GRANT_JOIN,
  152. )
  153. async def is_host_in_room(self, room_id: str, host: str) -> bool:
  154. return await self._store.is_host_joined(room_id, host)
  155. async def assert_host_in_room(
  156. self, room_id: str, host: str, allow_partial_state_rooms: bool = False
  157. ) -> None:
  158. """
  159. Asserts that the host is in the room, or raises an AuthError.
  160. If the room is partial-stated, we raise an AuthError with the
  161. UNABLE_DUE_TO_PARTIAL_STATE error code, unless `allow_partial_state_rooms` is true.
  162. If allow_partial_state_rooms is True and the room is partial-stated,
  163. this function may return an incorrect result as we are not able to fully
  164. track server membership in a room without full state.
  165. """
  166. if await self._store.is_partial_state_room(room_id):
  167. if allow_partial_state_rooms:
  168. current_hosts = await self._state_storage_controller.get_current_hosts_in_room_or_partial_state_approximation(
  169. room_id
  170. )
  171. if host not in current_hosts:
  172. raise AuthError(403, "Host not in room (partial-state approx).")
  173. else:
  174. raise AuthError(
  175. 403,
  176. "Unable to authorise you right now; room is partial-stated here.",
  177. errcode=Codes.UNABLE_DUE_TO_PARTIAL_STATE,
  178. )
  179. else:
  180. if not await self.is_host_in_room(room_id, host):
  181. raise AuthError(403, "Host not in room.")
  182. async def check_restricted_join_rules(
  183. self,
  184. state_ids: StateMap[str],
  185. room_version: RoomVersion,
  186. user_id: str,
  187. prev_membership: Optional[str],
  188. ) -> None:
  189. """
  190. Check whether a user can join a room without an invite due to restricted join rules.
  191. When joining a room with restricted joined rules (as defined in MSC3083),
  192. the membership of rooms must be checked during a room join.
  193. Args:
  194. state_ids: The state of the room as it currently is.
  195. room_version: The room version of the room being joined.
  196. user_id: The user joining the room.
  197. prev_membership: The current membership state for this user. `None` if the
  198. user has never joined the room (equivalent to "leave").
  199. Raises:
  200. AuthError if the user cannot join the room.
  201. """
  202. # If the member is invited or currently joined, then nothing to do.
  203. if prev_membership in (Membership.JOIN, Membership.INVITE):
  204. return
  205. # This is not a room with a restricted join rule, so we don't need to do the
  206. # restricted room specific checks.
  207. #
  208. # Note: We'll be applying the standard join rule checks later, which will
  209. # catch the cases of e.g. trying to join private rooms without an invite.
  210. if not await self.has_restricted_join_rules(state_ids, room_version):
  211. return
  212. # Get the rooms which allow access to this room and check if the user is
  213. # in any of them.
  214. allowed_rooms = await self.get_rooms_that_allow_join(state_ids)
  215. if not await self.is_user_in_rooms(allowed_rooms, user_id):
  216. # If this is a remote request, the user might be in an allowed room
  217. # that we do not know about.
  218. if get_domain_from_id(user_id) != self._server_name:
  219. for room_id in allowed_rooms:
  220. if not await self._store.is_host_joined(room_id, self._server_name):
  221. raise SynapseError(
  222. 400,
  223. f"Unable to check if {user_id} is in allowed rooms.",
  224. Codes.UNABLE_AUTHORISE_JOIN,
  225. )
  226. raise AuthError(
  227. 403,
  228. "You do not belong to any of the required rooms/spaces to join this room.",
  229. )
  230. async def has_restricted_join_rules(
  231. self, partial_state_ids: StateMap[str], room_version: RoomVersion
  232. ) -> bool:
  233. """
  234. Return if the room has the proper join rules set for access via rooms.
  235. Args:
  236. state_ids: The state of the room as it currently is. May be full or partial
  237. state.
  238. room_version: The room version of the room to query.
  239. Returns:
  240. True if the proper room version and join rules are set for restricted access.
  241. """
  242. # This only applies to room versions which support the new join rule.
  243. if not room_version.msc3083_join_rules:
  244. return False
  245. # If there's no join rule, then it defaults to invite (so this doesn't apply).
  246. join_rules_event_id = partial_state_ids.get((EventTypes.JoinRules, ""), None)
  247. if not join_rules_event_id:
  248. return False
  249. # If the join rule is not restricted, this doesn't apply.
  250. join_rules_event = await self._store.get_event(join_rules_event_id)
  251. content_join_rule = join_rules_event.content.get("join_rule")
  252. if content_join_rule == JoinRules.RESTRICTED:
  253. return True
  254. # also check for MSC3787 behaviour
  255. if room_version.msc3787_knock_restricted_join_rule:
  256. return content_join_rule == JoinRules.KNOCK_RESTRICTED
  257. return False
  258. async def get_rooms_that_allow_join(
  259. self, state_ids: StateMap[str]
  260. ) -> StrCollection:
  261. """
  262. Generate a list of rooms in which membership allows access to a room.
  263. Args:
  264. state_ids: The current state of the room the user wishes to join
  265. Returns:
  266. A collection of room IDs. Membership in any of the rooms in the list grants the ability to join the target room.
  267. """
  268. # If there's no join rule, then it defaults to invite (so this doesn't apply).
  269. join_rules_event_id = state_ids.get((EventTypes.JoinRules, ""), None)
  270. if not join_rules_event_id:
  271. return ()
  272. # If the join rule is not restricted, this doesn't apply.
  273. join_rules_event = await self._store.get_event(join_rules_event_id)
  274. # If allowed is of the wrong form, then only allow invited users.
  275. allow_list = join_rules_event.content.get("allow", [])
  276. if not isinstance(allow_list, list):
  277. return ()
  278. # Pull out the other room IDs, invalid data gets filtered.
  279. result = []
  280. for allow in allow_list:
  281. if not isinstance(allow, dict):
  282. continue
  283. # If the type is unexpected, skip it.
  284. if allow.get("type") != RestrictedJoinRuleTypes.ROOM_MEMBERSHIP:
  285. continue
  286. room_id = allow.get("room_id")
  287. if not isinstance(room_id, str):
  288. continue
  289. result.append(room_id)
  290. return result
  291. async def is_user_in_rooms(self, room_ids: StrCollection, user_id: str) -> bool:
  292. """
  293. Check whether a user is a member of any of the provided rooms.
  294. Args:
  295. room_ids: The rooms to check for membership.
  296. user_id: The user to check.
  297. Returns:
  298. True if the user is in any of the rooms, false otherwise.
  299. """
  300. if not room_ids:
  301. return False
  302. # Get the list of joined rooms and see if there's an overlap.
  303. joined_rooms = await self._store.get_rooms_for_user(user_id)
  304. # Check each room and see if the user is in it.
  305. for room_id in room_ids:
  306. if room_id in joined_rooms:
  307. return True
  308. # The user was not in any of the rooms.
  309. return False