You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

357 lines
14 KiB

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