event_auth.py 12 KB

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