25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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