room_member.py 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  1. # Copyright 2016-2020 The Matrix.org Foundation C.I.C.
  2. # Copyright 2020 Sorunome
  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 abc
  16. import logging
  17. import random
  18. from http import HTTPStatus
  19. from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple
  20. from synapse import types
  21. from synapse.api.constants import (
  22. AccountDataTypes,
  23. EventContentFields,
  24. EventTypes,
  25. GuestAccess,
  26. Membership,
  27. )
  28. from synapse.api.errors import (
  29. AuthError,
  30. Codes,
  31. LimitExceededError,
  32. ShadowBanError,
  33. SynapseError,
  34. )
  35. from synapse.api.ratelimiting import Ratelimiter
  36. from synapse.event_auth import get_named_level, get_power_level_event
  37. from synapse.events import EventBase
  38. from synapse.events.snapshot import EventContext
  39. from synapse.handlers.profile import MAX_AVATAR_URL_LEN, MAX_DISPLAYNAME_LEN
  40. from synapse.storage.state import StateFilter
  41. from synapse.types import (
  42. JsonDict,
  43. Requester,
  44. RoomAlias,
  45. RoomID,
  46. StateMap,
  47. UserID,
  48. create_requester,
  49. get_domain_from_id,
  50. )
  51. from synapse.util.async_helpers import Linearizer
  52. from synapse.util.distributor import user_left_room
  53. if TYPE_CHECKING:
  54. from synapse.server import HomeServer
  55. logger = logging.getLogger(__name__)
  56. class RoomMemberHandler(metaclass=abc.ABCMeta):
  57. # TODO(paul): This handler currently contains a messy conflation of
  58. # low-level API that works on UserID objects and so on, and REST-level
  59. # API that takes ID strings and returns pagination chunks. These concerns
  60. # ought to be separated out a lot better.
  61. def __init__(self, hs: "HomeServer"):
  62. self.hs = hs
  63. self.store = hs.get_datastores().main
  64. self.auth = hs.get_auth()
  65. self.state_handler = hs.get_state_handler()
  66. self.config = hs.config
  67. self._server_name = hs.hostname
  68. self.federation_handler = hs.get_federation_handler()
  69. self.directory_handler = hs.get_directory_handler()
  70. self.identity_handler = hs.get_identity_handler()
  71. self.registration_handler = hs.get_registration_handler()
  72. self.profile_handler = hs.get_profile_handler()
  73. self.event_creation_handler = hs.get_event_creation_handler()
  74. self.account_data_handler = hs.get_account_data_handler()
  75. self.event_auth_handler = hs.get_event_auth_handler()
  76. self.member_linearizer: Linearizer = Linearizer(name="member")
  77. self.member_as_limiter = Linearizer(max_count=10, name="member_as_limiter")
  78. self.clock = hs.get_clock()
  79. self.spam_checker = hs.get_spam_checker()
  80. self.third_party_event_rules = hs.get_third_party_event_rules()
  81. self._server_notices_mxid = self.config.servernotices.server_notices_mxid
  82. self._enable_lookup = hs.config.registration.enable_3pid_lookup
  83. self.allow_per_room_profiles = self.config.server.allow_per_room_profiles
  84. self._join_rate_limiter_local = Ratelimiter(
  85. store=self.store,
  86. clock=self.clock,
  87. rate_hz=hs.config.ratelimiting.rc_joins_local.per_second,
  88. burst_count=hs.config.ratelimiting.rc_joins_local.burst_count,
  89. )
  90. self._join_rate_limiter_remote = Ratelimiter(
  91. store=self.store,
  92. clock=self.clock,
  93. rate_hz=hs.config.ratelimiting.rc_joins_remote.per_second,
  94. burst_count=hs.config.ratelimiting.rc_joins_remote.burst_count,
  95. )
  96. self._invites_per_room_limiter = Ratelimiter(
  97. store=self.store,
  98. clock=self.clock,
  99. rate_hz=hs.config.ratelimiting.rc_invites_per_room.per_second,
  100. burst_count=hs.config.ratelimiting.rc_invites_per_room.burst_count,
  101. )
  102. self._invites_per_user_limiter = Ratelimiter(
  103. store=self.store,
  104. clock=self.clock,
  105. rate_hz=hs.config.ratelimiting.rc_invites_per_user.per_second,
  106. burst_count=hs.config.ratelimiting.rc_invites_per_user.burst_count,
  107. )
  108. self._third_party_invite_limiter = Ratelimiter(
  109. store=self.store,
  110. clock=self.clock,
  111. rate_hz=hs.config.ratelimiting.rc_third_party_invite.per_second,
  112. burst_count=hs.config.ratelimiting.rc_third_party_invite.burst_count,
  113. )
  114. self.request_ratelimiter = hs.get_request_ratelimiter()
  115. @abc.abstractmethod
  116. async def _remote_join(
  117. self,
  118. requester: Requester,
  119. remote_room_hosts: List[str],
  120. room_id: str,
  121. user: UserID,
  122. content: dict,
  123. ) -> Tuple[str, int]:
  124. """Try and join a room that this server is not in
  125. Args:
  126. requester
  127. remote_room_hosts: List of servers that can be used to join via.
  128. room_id: Room that we are trying to join
  129. user: User who is trying to join
  130. content: A dict that should be used as the content of the join event.
  131. """
  132. raise NotImplementedError()
  133. @abc.abstractmethod
  134. async def remote_knock(
  135. self,
  136. remote_room_hosts: List[str],
  137. room_id: str,
  138. user: UserID,
  139. content: dict,
  140. ) -> Tuple[str, int]:
  141. """Try and knock on a room that this server is not in
  142. Args:
  143. remote_room_hosts: List of servers that can be used to knock via.
  144. room_id: Room that we are trying to knock on.
  145. user: User who is trying to knock.
  146. content: A dict that should be used as the content of the knock event.
  147. """
  148. raise NotImplementedError()
  149. @abc.abstractmethod
  150. async def remote_reject_invite(
  151. self,
  152. invite_event_id: str,
  153. txn_id: Optional[str],
  154. requester: Requester,
  155. content: JsonDict,
  156. ) -> Tuple[str, int]:
  157. """
  158. Rejects an out-of-band invite we have received from a remote server
  159. Args:
  160. invite_event_id: ID of the invite to be rejected
  161. txn_id: optional transaction ID supplied by the client
  162. requester: user making the rejection request, according to the access token
  163. content: additional content to include in the rejection event.
  164. Normally an empty dict.
  165. Returns:
  166. event id, stream_id of the leave event
  167. """
  168. raise NotImplementedError()
  169. @abc.abstractmethod
  170. async def remote_rescind_knock(
  171. self,
  172. knock_event_id: str,
  173. txn_id: Optional[str],
  174. requester: Requester,
  175. content: JsonDict,
  176. ) -> Tuple[str, int]:
  177. """Rescind a local knock made on a remote room.
  178. Args:
  179. knock_event_id: The ID of the knock event to rescind.
  180. txn_id: An optional transaction ID supplied by the client.
  181. requester: The user making the request, according to the access token.
  182. content: The content of the generated leave event.
  183. Returns:
  184. A tuple containing (event_id, stream_id of the leave event).
  185. """
  186. raise NotImplementedError()
  187. @abc.abstractmethod
  188. async def _user_left_room(self, target: UserID, room_id: str) -> None:
  189. """Notifies distributor on master process that the user has left the
  190. room.
  191. Args:
  192. target
  193. room_id
  194. """
  195. raise NotImplementedError()
  196. @abc.abstractmethod
  197. async def forget(self, user: UserID, room_id: str) -> None:
  198. raise NotImplementedError()
  199. async def ratelimit_multiple_invites(
  200. self,
  201. requester: Optional[Requester],
  202. room_id: Optional[str],
  203. n_invites: int,
  204. update: bool = True,
  205. ) -> None:
  206. """Ratelimit more than one invite sent by the given requester in the given room.
  207. Args:
  208. requester: The requester sending the invites.
  209. room_id: The room the invites are being sent in.
  210. n_invites: The amount of invites to ratelimit for.
  211. update: Whether to update the ratelimiter's cache.
  212. Raises:
  213. LimitExceededError: The requester can't send that many invites in the room.
  214. """
  215. await self._invites_per_room_limiter.ratelimit(
  216. requester,
  217. room_id,
  218. update=update,
  219. n_actions=n_invites,
  220. )
  221. async def ratelimit_invite(
  222. self,
  223. requester: Optional[Requester],
  224. room_id: Optional[str],
  225. invitee_user_id: str,
  226. ) -> None:
  227. """Ratelimit invites by room and by target user.
  228. If room ID is missing then we just rate limit by target user.
  229. """
  230. if room_id:
  231. await self._invites_per_room_limiter.ratelimit(requester, room_id)
  232. await self._invites_per_user_limiter.ratelimit(requester, invitee_user_id)
  233. async def _local_membership_update(
  234. self,
  235. requester: Requester,
  236. target: UserID,
  237. room_id: str,
  238. membership: str,
  239. allow_no_prev_events: bool = False,
  240. prev_event_ids: Optional[List[str]] = None,
  241. state_event_ids: Optional[List[str]] = None,
  242. txn_id: Optional[str] = None,
  243. ratelimit: bool = True,
  244. content: Optional[dict] = None,
  245. require_consent: bool = True,
  246. outlier: bool = False,
  247. historical: bool = False,
  248. ) -> Tuple[str, int]:
  249. """
  250. Internal membership update function to get an existing event or create
  251. and persist a new event for the new membership change.
  252. Args:
  253. requester:
  254. target:
  255. room_id:
  256. membership:
  257. allow_no_prev_events: Whether to allow this event to be created an empty
  258. list of prev_events. Normally this is prohibited just because most
  259. events should have a prev_event and we should only use this in special
  260. cases like MSC2716.
  261. prev_event_ids: The event IDs to use as the prev events
  262. state_event_ids:
  263. The full state at a given event. This is used particularly by the MSC2716
  264. /batch_send endpoint. One use case is the historical `state_events_at_start`;
  265. since each is marked as an `outlier`, the `EventContext.for_outlier()` won't
  266. have any `state_ids` set and therefore can't derive any state even though the
  267. prev_events are set so we need to set them ourself via this argument.
  268. This should normally be left as None, which will cause the auth_event_ids
  269. to be calculated based on the room state at the prev_events.
  270. txn_id:
  271. ratelimit:
  272. content:
  273. require_consent:
  274. outlier: Indicates whether the event is an `outlier`, i.e. if
  275. it's from an arbitrary point and floating in the DAG as
  276. opposed to being inline with the current DAG.
  277. historical: Indicates whether the message is being inserted
  278. back in time around some existing events. This is used to skip
  279. a few checks and mark the event as backfilled.
  280. Returns:
  281. Tuple of event ID and stream ordering position
  282. """
  283. user_id = target.to_string()
  284. if content is None:
  285. content = {}
  286. content["membership"] = membership
  287. if requester.is_guest:
  288. content["kind"] = "guest"
  289. # Check if we already have an event with a matching transaction ID. (We
  290. # do this check just before we persist an event as well, but may as well
  291. # do it up front for efficiency.)
  292. if txn_id and requester.access_token_id:
  293. existing_event_id = await self.store.get_event_id_from_transaction_id(
  294. room_id,
  295. requester.user.to_string(),
  296. requester.access_token_id,
  297. txn_id,
  298. )
  299. if existing_event_id:
  300. event_pos = await self.store.get_position_for_event(existing_event_id)
  301. return existing_event_id, event_pos.stream
  302. event, context = await self.event_creation_handler.create_event(
  303. requester,
  304. {
  305. "type": EventTypes.Member,
  306. "content": content,
  307. "room_id": room_id,
  308. "sender": requester.user.to_string(),
  309. "state_key": user_id,
  310. # For backwards compatibility:
  311. "membership": membership,
  312. },
  313. txn_id=txn_id,
  314. allow_no_prev_events=allow_no_prev_events,
  315. prev_event_ids=prev_event_ids,
  316. state_event_ids=state_event_ids,
  317. require_consent=require_consent,
  318. outlier=outlier,
  319. historical=historical,
  320. )
  321. prev_state_ids = await context.get_prev_state_ids(
  322. StateFilter.from_types([(EventTypes.Member, None)])
  323. )
  324. prev_member_event_id = prev_state_ids.get((EventTypes.Member, user_id), None)
  325. if event.membership == Membership.JOIN:
  326. newly_joined = True
  327. if prev_member_event_id:
  328. prev_member_event = await self.store.get_event(prev_member_event_id)
  329. newly_joined = prev_member_event.membership != Membership.JOIN
  330. # Only rate-limit if the user actually joined the room, otherwise we'll end
  331. # up blocking profile updates.
  332. if newly_joined and ratelimit:
  333. time_now_s = self.clock.time()
  334. (
  335. allowed,
  336. time_allowed,
  337. ) = await self._join_rate_limiter_local.can_do_action(requester)
  338. if not allowed:
  339. raise LimitExceededError(
  340. retry_after_ms=int(1000 * (time_allowed - time_now_s))
  341. )
  342. result_event = await self.event_creation_handler.handle_new_client_event(
  343. requester,
  344. event,
  345. context,
  346. extra_users=[target],
  347. ratelimit=ratelimit,
  348. )
  349. if event.membership == Membership.LEAVE:
  350. if prev_member_event_id:
  351. prev_member_event = await self.store.get_event(prev_member_event_id)
  352. if prev_member_event.membership == Membership.JOIN:
  353. await self._user_left_room(target, room_id)
  354. # we know it was persisted, so should have a stream ordering
  355. assert result_event.internal_metadata.stream_ordering
  356. return result_event.event_id, result_event.internal_metadata.stream_ordering
  357. async def copy_room_tags_and_direct_to_room(
  358. self, old_room_id: str, new_room_id: str, user_id: str
  359. ) -> None:
  360. """Copies the tags and direct room state from one room to another.
  361. Args:
  362. old_room_id: The room ID of the old room.
  363. new_room_id: The room ID of the new room.
  364. user_id: The user's ID.
  365. """
  366. # Retrieve user account data for predecessor room
  367. user_account_data, _ = await self.store.get_account_data_for_user(user_id)
  368. # Copy direct message state if applicable
  369. direct_rooms = user_account_data.get(AccountDataTypes.DIRECT, {})
  370. # Check which key this room is under
  371. if isinstance(direct_rooms, dict):
  372. for key, room_id_list in direct_rooms.items():
  373. if old_room_id in room_id_list and new_room_id not in room_id_list:
  374. # Add new room_id to this key
  375. direct_rooms[key].append(new_room_id)
  376. # Save back to user's m.direct account data
  377. await self.account_data_handler.add_account_data_for_user(
  378. user_id, AccountDataTypes.DIRECT, direct_rooms
  379. )
  380. break
  381. # Copy room tags if applicable
  382. room_tags = await self.store.get_tags_for_room(user_id, old_room_id)
  383. # Copy each room tag to the new room
  384. for tag, tag_content in room_tags.items():
  385. await self.account_data_handler.add_tag_to_room(
  386. user_id, new_room_id, tag, tag_content
  387. )
  388. async def update_membership(
  389. self,
  390. requester: Requester,
  391. target: UserID,
  392. room_id: str,
  393. action: str,
  394. txn_id: Optional[str] = None,
  395. remote_room_hosts: Optional[List[str]] = None,
  396. third_party_signed: Optional[dict] = None,
  397. ratelimit: bool = True,
  398. content: Optional[dict] = None,
  399. new_room: bool = False,
  400. require_consent: bool = True,
  401. outlier: bool = False,
  402. historical: bool = False,
  403. allow_no_prev_events: bool = False,
  404. prev_event_ids: Optional[List[str]] = None,
  405. state_event_ids: Optional[List[str]] = None,
  406. ) -> Tuple[str, int]:
  407. """Update a user's membership in a room.
  408. Params:
  409. requester: The user who is performing the update.
  410. target: The user whose membership is being updated.
  411. room_id: The room ID whose membership is being updated.
  412. action: The membership change, see synapse.api.constants.Membership.
  413. txn_id: The transaction ID, if given.
  414. remote_room_hosts: Remote servers to send the update to.
  415. third_party_signed: Information from a 3PID invite.
  416. ratelimit: Whether to rate limit the request.
  417. content: The content of the created event.
  418. new_room: Whether the membership update is happening in the context of a room
  419. creation.
  420. require_consent: Whether consent is required.
  421. outlier: Indicates whether the event is an `outlier`, i.e. if
  422. it's from an arbitrary point and floating in the DAG as
  423. opposed to being inline with the current DAG.
  424. historical: Indicates whether the message is being inserted
  425. back in time around some existing events. This is used to skip
  426. a few checks and mark the event as backfilled.
  427. allow_no_prev_events: Whether to allow this event to be created an empty
  428. list of prev_events. Normally this is prohibited just because most
  429. events should have a prev_event and we should only use this in special
  430. cases like MSC2716.
  431. prev_event_ids: The event IDs to use as the prev events
  432. state_event_ids:
  433. The full state at a given event. This is used particularly by the MSC2716
  434. /batch_send endpoint. One use case is the historical `state_events_at_start`;
  435. since each is marked as an `outlier`, the `EventContext.for_outlier()` won't
  436. have any `state_ids` set and therefore can't derive any state even though the
  437. prev_events are set so we need to set them ourself via this argument.
  438. This should normally be left as None, which will cause the auth_event_ids
  439. to be calculated based on the room state at the prev_events.
  440. Returns:
  441. A tuple of the new event ID and stream ID.
  442. Raises:
  443. ShadowBanError if a shadow-banned requester attempts to send an invite.
  444. """
  445. if action == Membership.INVITE and requester.shadow_banned:
  446. # We randomly sleep a bit just to annoy the requester.
  447. await self.clock.sleep(random.randint(1, 10))
  448. raise ShadowBanError()
  449. key = (room_id,)
  450. as_id = object()
  451. if requester.app_service:
  452. as_id = requester.app_service.id
  453. # We first linearise by the application service (to try to limit concurrent joins
  454. # by application services), and then by room ID.
  455. async with self.member_as_limiter.queue(as_id):
  456. async with self.member_linearizer.queue(key):
  457. result = await self.update_membership_locked(
  458. requester,
  459. target,
  460. room_id,
  461. action,
  462. txn_id=txn_id,
  463. remote_room_hosts=remote_room_hosts,
  464. third_party_signed=third_party_signed,
  465. ratelimit=ratelimit,
  466. content=content,
  467. new_room=new_room,
  468. require_consent=require_consent,
  469. outlier=outlier,
  470. historical=historical,
  471. allow_no_prev_events=allow_no_prev_events,
  472. prev_event_ids=prev_event_ids,
  473. state_event_ids=state_event_ids,
  474. )
  475. return result
  476. async def update_membership_locked(
  477. self,
  478. requester: Requester,
  479. target: UserID,
  480. room_id: str,
  481. action: str,
  482. txn_id: Optional[str] = None,
  483. remote_room_hosts: Optional[List[str]] = None,
  484. third_party_signed: Optional[dict] = None,
  485. ratelimit: bool = True,
  486. content: Optional[dict] = None,
  487. new_room: bool = False,
  488. require_consent: bool = True,
  489. outlier: bool = False,
  490. historical: bool = False,
  491. allow_no_prev_events: bool = False,
  492. prev_event_ids: Optional[List[str]] = None,
  493. state_event_ids: Optional[List[str]] = None,
  494. ) -> Tuple[str, int]:
  495. """Helper for update_membership.
  496. Assumes that the membership linearizer is already held for the room.
  497. Args:
  498. requester:
  499. target:
  500. room_id:
  501. action:
  502. txn_id:
  503. remote_room_hosts:
  504. third_party_signed:
  505. ratelimit:
  506. content:
  507. new_room: Whether the membership update is happening in the context of a room
  508. creation.
  509. require_consent:
  510. outlier: Indicates whether the event is an `outlier`, i.e. if
  511. it's from an arbitrary point and floating in the DAG as
  512. opposed to being inline with the current DAG.
  513. historical: Indicates whether the message is being inserted
  514. back in time around some existing events. This is used to skip
  515. a few checks and mark the event as backfilled.
  516. allow_no_prev_events: Whether to allow this event to be created an empty
  517. list of prev_events. Normally this is prohibited just because most
  518. events should have a prev_event and we should only use this in special
  519. cases like MSC2716.
  520. prev_event_ids: The event IDs to use as the prev events
  521. state_event_ids:
  522. The full state at a given event. This is used particularly by the MSC2716
  523. /batch_send endpoint. One use case is the historical `state_events_at_start`;
  524. since each is marked as an `outlier`, the `EventContext.for_outlier()` won't
  525. have any `state_ids` set and therefore can't derive any state even though the
  526. prev_events are set so we need to set them ourself via this argument.
  527. This should normally be left as None, which will cause the auth_event_ids
  528. to be calculated based on the room state at the prev_events.
  529. Returns:
  530. A tuple of the new event ID and stream ID.
  531. """
  532. content_specified = bool(content)
  533. if content is None:
  534. content = {}
  535. else:
  536. # We do a copy here as we potentially change some keys
  537. # later on.
  538. content = dict(content)
  539. # allow the server notices mxid to set room-level profile
  540. is_requester_server_notices_user = (
  541. self._server_notices_mxid is not None
  542. and requester.user.to_string() == self._server_notices_mxid
  543. )
  544. if (
  545. not self.allow_per_room_profiles and not is_requester_server_notices_user
  546. ) or requester.shadow_banned:
  547. # Strip profile data, knowing that new profile data will be added to the
  548. # event's content in event_creation_handler.create_event() using the target's
  549. # global profile.
  550. content.pop("displayname", None)
  551. content.pop("avatar_url", None)
  552. if len(content.get("displayname") or "") > MAX_DISPLAYNAME_LEN:
  553. raise SynapseError(
  554. 400,
  555. f"Displayname is too long (max {MAX_DISPLAYNAME_LEN})",
  556. errcode=Codes.BAD_JSON,
  557. )
  558. if len(content.get("avatar_url") or "") > MAX_AVATAR_URL_LEN:
  559. raise SynapseError(
  560. 400,
  561. f"Avatar URL is too long (max {MAX_AVATAR_URL_LEN})",
  562. errcode=Codes.BAD_JSON,
  563. )
  564. if "avatar_url" in content:
  565. if not await self.profile_handler.check_avatar_size_and_mime_type(
  566. content["avatar_url"],
  567. ):
  568. raise SynapseError(403, "This avatar is not allowed", Codes.FORBIDDEN)
  569. # The event content should *not* include the authorising user as
  570. # it won't be properly signed. Strip it out since it might come
  571. # back from a client updating a display name / avatar.
  572. #
  573. # This only applies to restricted rooms, but there should be no reason
  574. # for a client to include it. Unconditionally remove it.
  575. content.pop(EventContentFields.AUTHORISING_USER, None)
  576. effective_membership_state = action
  577. if action in ["kick", "unban"]:
  578. effective_membership_state = "leave"
  579. # if this is a join with a 3pid signature, we may need to turn a 3pid
  580. # invite into a normal invite before we can handle the join.
  581. if third_party_signed is not None:
  582. await self.federation_handler.exchange_third_party_invite(
  583. third_party_signed["sender"],
  584. target.to_string(),
  585. room_id,
  586. third_party_signed,
  587. )
  588. if not remote_room_hosts:
  589. remote_room_hosts = []
  590. if effective_membership_state not in ("leave", "ban"):
  591. is_blocked = await self.store.is_room_blocked(room_id)
  592. if is_blocked:
  593. raise SynapseError(403, "This room has been blocked on this server")
  594. if effective_membership_state == Membership.INVITE:
  595. target_id = target.to_string()
  596. if ratelimit:
  597. await self.ratelimit_invite(requester, room_id, target_id)
  598. # block any attempts to invite the server notices mxid
  599. if target_id == self._server_notices_mxid:
  600. raise SynapseError(HTTPStatus.FORBIDDEN, "Cannot invite this user")
  601. block_invite = False
  602. if (
  603. self._server_notices_mxid is not None
  604. and requester.user.to_string() == self._server_notices_mxid
  605. ):
  606. # allow the server notices mxid to send invites
  607. is_requester_admin = True
  608. else:
  609. is_requester_admin = await self.auth.is_server_admin(requester.user)
  610. if not is_requester_admin:
  611. if self.config.server.block_non_admin_invites:
  612. logger.info(
  613. "Blocking invite: user is not admin and non-admin "
  614. "invites disabled"
  615. )
  616. block_invite = True
  617. if not await self.spam_checker.user_may_invite(
  618. requester.user.to_string(), target_id, room_id
  619. ):
  620. logger.info("Blocking invite due to spam checker")
  621. block_invite = True
  622. if block_invite:
  623. raise SynapseError(403, "Invites have been disabled on this server")
  624. # An empty prev_events list is allowed as long as the auth_event_ids are present
  625. if prev_event_ids is not None:
  626. return await self._local_membership_update(
  627. requester=requester,
  628. target=target,
  629. room_id=room_id,
  630. membership=effective_membership_state,
  631. txn_id=txn_id,
  632. ratelimit=ratelimit,
  633. allow_no_prev_events=allow_no_prev_events,
  634. prev_event_ids=prev_event_ids,
  635. state_event_ids=state_event_ids,
  636. content=content,
  637. require_consent=require_consent,
  638. outlier=outlier,
  639. historical=historical,
  640. )
  641. latest_event_ids = await self.store.get_prev_events_for_room(room_id)
  642. current_state_ids = await self.state_handler.get_current_state_ids(
  643. room_id, latest_event_ids=latest_event_ids
  644. )
  645. # TODO: Refactor into dictionary of explicitly allowed transitions
  646. # between old and new state, with specific error messages for some
  647. # transitions and generic otherwise
  648. old_state_id = current_state_ids.get((EventTypes.Member, target.to_string()))
  649. if old_state_id:
  650. old_state = await self.store.get_event(old_state_id, allow_none=True)
  651. old_membership = old_state.content.get("membership") if old_state else None
  652. if action == "unban" and old_membership != "ban":
  653. raise SynapseError(
  654. 403,
  655. "Cannot unban user who was not banned"
  656. " (membership=%s)" % old_membership,
  657. errcode=Codes.BAD_STATE,
  658. )
  659. if old_membership == "ban" and action not in ["ban", "unban", "leave"]:
  660. raise SynapseError(
  661. 403,
  662. "Cannot %s user who was banned" % (action,),
  663. errcode=Codes.BAD_STATE,
  664. )
  665. if old_state:
  666. same_content = content == old_state.content
  667. same_membership = old_membership == effective_membership_state
  668. same_sender = requester.user.to_string() == old_state.sender
  669. if same_sender and same_membership and same_content:
  670. # duplicate event.
  671. # we know it was persisted, so must have a stream ordering.
  672. assert old_state.internal_metadata.stream_ordering
  673. return (
  674. old_state.event_id,
  675. old_state.internal_metadata.stream_ordering,
  676. )
  677. if old_membership in ["ban", "leave"] and action == "kick":
  678. raise AuthError(403, "The target user is not in the room")
  679. # we don't allow people to reject invites to the server notice
  680. # room, but they can leave it once they are joined.
  681. if (
  682. old_membership == Membership.INVITE
  683. and effective_membership_state == Membership.LEAVE
  684. ):
  685. is_blocked = await self._is_server_notice_room(room_id)
  686. if is_blocked:
  687. raise SynapseError(
  688. HTTPStatus.FORBIDDEN,
  689. "You cannot reject this invite",
  690. errcode=Codes.CANNOT_LEAVE_SERVER_NOTICE_ROOM,
  691. )
  692. else:
  693. if action == "kick":
  694. raise AuthError(403, "The target user is not in the room")
  695. is_host_in_room = await self._is_host_in_room(current_state_ids)
  696. if effective_membership_state == Membership.JOIN:
  697. if requester.is_guest:
  698. guest_can_join = await self._can_guest_join(current_state_ids)
  699. if not guest_can_join:
  700. # This should be an auth check, but guests are a local concept,
  701. # so don't really fit into the general auth process.
  702. raise AuthError(403, "Guest access not allowed")
  703. # Figure out whether the user is a server admin to determine whether they
  704. # should be able to bypass the spam checker.
  705. if (
  706. self._server_notices_mxid is not None
  707. and requester.user.to_string() == self._server_notices_mxid
  708. ):
  709. # allow the server notices mxid to join rooms
  710. bypass_spam_checker = True
  711. else:
  712. bypass_spam_checker = await self.auth.is_server_admin(requester.user)
  713. inviter = await self._get_inviter(target.to_string(), room_id)
  714. if (
  715. not bypass_spam_checker
  716. # We assume that if the spam checker allowed the user to create
  717. # a room then they're allowed to join it.
  718. and not new_room
  719. and not await self.spam_checker.user_may_join_room(
  720. target.to_string(), room_id, is_invited=inviter is not None
  721. )
  722. ):
  723. raise SynapseError(403, "Not allowed to join this room")
  724. # Check if a remote join should be performed.
  725. remote_join, remote_room_hosts = await self._should_perform_remote_join(
  726. target.to_string(), room_id, remote_room_hosts, content, is_host_in_room
  727. )
  728. if remote_join:
  729. if ratelimit:
  730. time_now_s = self.clock.time()
  731. (
  732. allowed,
  733. time_allowed,
  734. ) = await self._join_rate_limiter_remote.can_do_action(
  735. requester,
  736. )
  737. if not allowed:
  738. raise LimitExceededError(
  739. retry_after_ms=int(1000 * (time_allowed - time_now_s))
  740. )
  741. inviter = await self._get_inviter(target.to_string(), room_id)
  742. if inviter and not self.hs.is_mine(inviter):
  743. remote_room_hosts.append(inviter.domain)
  744. content["membership"] = Membership.JOIN
  745. profile = self.profile_handler
  746. if not content_specified:
  747. content["displayname"] = await profile.get_displayname(target)
  748. content["avatar_url"] = await profile.get_avatar_url(target)
  749. if requester.is_guest:
  750. content["kind"] = "guest"
  751. remote_join_response = await self._remote_join(
  752. requester, remote_room_hosts, room_id, target, content
  753. )
  754. return remote_join_response
  755. elif effective_membership_state == Membership.LEAVE:
  756. if not is_host_in_room:
  757. # Figure out the user's current membership state for the room
  758. (
  759. current_membership_type,
  760. current_membership_event_id,
  761. ) = await self.store.get_local_current_membership_for_user_in_room(
  762. target.to_string(), room_id
  763. )
  764. if not current_membership_type or not current_membership_event_id:
  765. logger.info(
  766. "%s sent a leave request to %s, but that is not an active room "
  767. "on this server, or there is no pending invite or knock",
  768. target,
  769. room_id,
  770. )
  771. raise SynapseError(404, "Not a known room")
  772. # perhaps we've been invited
  773. if current_membership_type == Membership.INVITE:
  774. invite = await self.store.get_event(current_membership_event_id)
  775. logger.info(
  776. "%s rejects invite to %s from %s",
  777. target,
  778. room_id,
  779. invite.sender,
  780. )
  781. if not self.hs.is_mine_id(invite.sender):
  782. # send the rejection to the inviter's HS (with fallback to
  783. # local event)
  784. return await self.remote_reject_invite(
  785. invite.event_id,
  786. txn_id,
  787. requester,
  788. content,
  789. )
  790. # the inviter was on our server, but has now left. Carry on
  791. # with the normal rejection codepath, which will also send the
  792. # rejection out to any other servers we believe are still in the room.
  793. # thanks to overzealous cleaning up of event_forward_extremities in
  794. # `delete_old_current_state_events`, it's possible to end up with no
  795. # forward extremities here. If that happens, let's just hang the
  796. # rejection off the invite event.
  797. #
  798. # see: https://github.com/matrix-org/synapse/issues/7139
  799. if len(latest_event_ids) == 0:
  800. latest_event_ids = [invite.event_id]
  801. # or perhaps this is a remote room that a local user has knocked on
  802. elif current_membership_type == Membership.KNOCK:
  803. knock = await self.store.get_event(current_membership_event_id)
  804. return await self.remote_rescind_knock(
  805. knock.event_id, txn_id, requester, content
  806. )
  807. elif effective_membership_state == Membership.KNOCK:
  808. if not is_host_in_room:
  809. # The knock needs to be sent over federation instead
  810. remote_room_hosts.append(get_domain_from_id(room_id))
  811. content["membership"] = Membership.KNOCK
  812. profile = self.profile_handler
  813. if "displayname" not in content:
  814. content["displayname"] = await profile.get_displayname(target)
  815. if "avatar_url" not in content:
  816. content["avatar_url"] = await profile.get_avatar_url(target)
  817. return await self.remote_knock(
  818. remote_room_hosts, room_id, target, content
  819. )
  820. return await self._local_membership_update(
  821. requester=requester,
  822. target=target,
  823. room_id=room_id,
  824. membership=effective_membership_state,
  825. txn_id=txn_id,
  826. ratelimit=ratelimit,
  827. prev_event_ids=latest_event_ids,
  828. state_event_ids=state_event_ids,
  829. content=content,
  830. require_consent=require_consent,
  831. outlier=outlier,
  832. )
  833. async def _should_perform_remote_join(
  834. self,
  835. user_id: str,
  836. room_id: str,
  837. remote_room_hosts: List[str],
  838. content: JsonDict,
  839. is_host_in_room: bool,
  840. ) -> Tuple[bool, List[str]]:
  841. """
  842. Check whether the server should do a remote join (as opposed to a local
  843. join) for a user.
  844. Generally a remote join is used if:
  845. * The server is not yet in the room.
  846. * The server is in the room, the room has restricted join rules, the user
  847. is not joined or invited to the room, and the server does not have
  848. another user who is capable of issuing invites.
  849. Args:
  850. user_id: The user joining the room.
  851. room_id: The room being joined.
  852. remote_room_hosts: A list of remote room hosts.
  853. content: The content to use as the event body of the join. This may
  854. be modified.
  855. is_host_in_room: True if the host is in the room.
  856. Returns:
  857. A tuple of:
  858. True if a remote join should be performed. False if the join can be
  859. done locally.
  860. A list of remote room hosts to use. This is an empty list if a
  861. local join is to be done.
  862. """
  863. # If the host isn't in the room, pass through the prospective hosts.
  864. if not is_host_in_room:
  865. return True, remote_room_hosts
  866. # If the host is in the room, but not one of the authorised hosts
  867. # for restricted join rules, a remote join must be used.
  868. room_version = await self.store.get_room_version(room_id)
  869. current_state_ids = await self.store.get_current_state_ids(room_id)
  870. # If restricted join rules are not being used, a local join can always
  871. # be used.
  872. if not await self.event_auth_handler.has_restricted_join_rules(
  873. current_state_ids, room_version
  874. ):
  875. return False, []
  876. # If the user is invited to the room or already joined, the join
  877. # event can always be issued locally.
  878. prev_member_event_id = current_state_ids.get((EventTypes.Member, user_id), None)
  879. prev_member_event = None
  880. if prev_member_event_id:
  881. prev_member_event = await self.store.get_event(prev_member_event_id)
  882. if prev_member_event.membership in (
  883. Membership.JOIN,
  884. Membership.INVITE,
  885. ):
  886. return False, []
  887. # If the local host has a user who can issue invites, then a local
  888. # join can be done.
  889. #
  890. # If not, generate a new list of remote hosts based on which
  891. # can issue invites.
  892. event_map = await self.store.get_events(current_state_ids.values())
  893. current_state = {
  894. state_key: event_map[event_id]
  895. for state_key, event_id in current_state_ids.items()
  896. }
  897. allowed_servers = get_servers_from_users(
  898. get_users_which_can_issue_invite(current_state)
  899. )
  900. # If the local server is not one of allowed servers, then a remote
  901. # join must be done. Return the list of prospective servers based on
  902. # which can issue invites.
  903. if self.hs.hostname not in allowed_servers:
  904. return True, list(allowed_servers)
  905. # Ensure the member should be allowed access via membership in a room.
  906. await self.event_auth_handler.check_restricted_join_rules(
  907. current_state_ids, room_version, user_id, prev_member_event
  908. )
  909. # If this is going to be a local join, additional information must
  910. # be included in the event content in order to efficiently validate
  911. # the event.
  912. content[
  913. EventContentFields.AUTHORISING_USER
  914. ] = await self.event_auth_handler.get_user_which_could_invite(
  915. room_id,
  916. current_state_ids,
  917. )
  918. return False, []
  919. async def transfer_room_state_on_room_upgrade(
  920. self, old_room_id: str, room_id: str
  921. ) -> None:
  922. """Upon our server becoming aware of an upgraded room, either by upgrading a room
  923. ourselves or joining one, we can transfer over information from the previous room.
  924. Copies user state (tags/push rules) for every local user that was in the old room, as
  925. well as migrating the room directory state.
  926. Args:
  927. old_room_id: The ID of the old room
  928. room_id: The ID of the new room
  929. """
  930. logger.info("Transferring room state from %s to %s", old_room_id, room_id)
  931. # Find all local users that were in the old room and copy over each user's state
  932. users = await self.store.get_users_in_room(old_room_id)
  933. await self.copy_user_state_on_room_upgrade(old_room_id, room_id, users)
  934. # Add new room to the room directory if the old room was there
  935. # Remove old room from the room directory
  936. old_room = await self.store.get_room(old_room_id)
  937. if old_room is not None and old_room["is_public"]:
  938. await self.store.set_room_is_public(old_room_id, False)
  939. await self.store.set_room_is_public(room_id, True)
  940. # Transfer alias mappings in the room directory
  941. await self.store.update_aliases_for_room(old_room_id, room_id)
  942. async def copy_user_state_on_room_upgrade(
  943. self, old_room_id: str, new_room_id: str, user_ids: Iterable[str]
  944. ) -> None:
  945. """Copy user-specific information when they join a new room when that new room is the
  946. result of a room upgrade
  947. Args:
  948. old_room_id: The ID of upgraded room
  949. new_room_id: The ID of the new room
  950. user_ids: User IDs to copy state for
  951. """
  952. logger.debug(
  953. "Copying over room tags and push rules from %s to %s for users %s",
  954. old_room_id,
  955. new_room_id,
  956. user_ids,
  957. )
  958. for user_id in user_ids:
  959. try:
  960. # It is an upgraded room. Copy over old tags
  961. await self.copy_room_tags_and_direct_to_room(
  962. old_room_id, new_room_id, user_id
  963. )
  964. # Copy over push rules
  965. await self.store.copy_push_rules_from_room_to_room_for_user(
  966. old_room_id, new_room_id, user_id
  967. )
  968. except Exception:
  969. logger.exception(
  970. "Error copying tags and/or push rules from rooms %s to %s for user %s. "
  971. "Skipping...",
  972. old_room_id,
  973. new_room_id,
  974. user_id,
  975. )
  976. continue
  977. async def send_membership_event(
  978. self,
  979. requester: Optional[Requester],
  980. event: EventBase,
  981. context: EventContext,
  982. ratelimit: bool = True,
  983. ) -> None:
  984. """
  985. Change the membership status of a user in a room.
  986. Args:
  987. requester: The local user who requested the membership
  988. event. If None, certain checks, like whether this homeserver can
  989. act as the sender, will be skipped.
  990. event: The membership event.
  991. context: The context of the event.
  992. ratelimit: Whether to rate limit this request.
  993. Raises:
  994. SynapseError if there was a problem changing the membership.
  995. """
  996. target_user = UserID.from_string(event.state_key)
  997. room_id = event.room_id
  998. if requester is not None:
  999. sender = UserID.from_string(event.sender)
  1000. assert (
  1001. sender == requester.user
  1002. ), "Sender (%s) must be same as requester (%s)" % (sender, requester.user)
  1003. assert self.hs.is_mine(sender), "Sender must be our own: %s" % (sender,)
  1004. else:
  1005. requester = types.create_requester(target_user)
  1006. prev_state_ids = await context.get_prev_state_ids(
  1007. StateFilter.from_types([(EventTypes.GuestAccess, None)])
  1008. )
  1009. if event.membership == Membership.JOIN:
  1010. if requester.is_guest:
  1011. guest_can_join = await self._can_guest_join(prev_state_ids)
  1012. if not guest_can_join:
  1013. # This should be an auth check, but guests are a local concept,
  1014. # so don't really fit into the general auth process.
  1015. raise AuthError(403, "Guest access not allowed")
  1016. if event.membership not in (Membership.LEAVE, Membership.BAN):
  1017. is_blocked = await self.store.is_room_blocked(room_id)
  1018. if is_blocked:
  1019. raise SynapseError(403, "This room has been blocked on this server")
  1020. event = await self.event_creation_handler.handle_new_client_event(
  1021. requester, event, context, extra_users=[target_user], ratelimit=ratelimit
  1022. )
  1023. prev_member_event_id = prev_state_ids.get(
  1024. (EventTypes.Member, event.state_key), None
  1025. )
  1026. if event.membership == Membership.LEAVE:
  1027. if prev_member_event_id:
  1028. prev_member_event = await self.store.get_event(prev_member_event_id)
  1029. if prev_member_event.membership == Membership.JOIN:
  1030. await self._user_left_room(target_user, room_id)
  1031. async def _can_guest_join(self, current_state_ids: StateMap[str]) -> bool:
  1032. """
  1033. Returns whether a guest can join a room based on its current state.
  1034. """
  1035. guest_access_id = current_state_ids.get((EventTypes.GuestAccess, ""), None)
  1036. if not guest_access_id:
  1037. return False
  1038. guest_access = await self.store.get_event(guest_access_id)
  1039. return bool(
  1040. guest_access
  1041. and guest_access.content
  1042. and guest_access.content.get(EventContentFields.GUEST_ACCESS)
  1043. == GuestAccess.CAN_JOIN
  1044. )
  1045. async def kick_guest_users(self, current_state: Iterable[EventBase]) -> None:
  1046. """Kick any local guest users from the room.
  1047. This is called when the room state changes from guests allowed to not-allowed.
  1048. Params:
  1049. current_state: the current state of the room. We will iterate this to look
  1050. for guest users to kick.
  1051. """
  1052. for member_event in current_state:
  1053. try:
  1054. if member_event.type != EventTypes.Member:
  1055. continue
  1056. if not self.hs.is_mine_id(member_event.state_key):
  1057. continue
  1058. if member_event.content["membership"] not in {
  1059. Membership.JOIN,
  1060. Membership.INVITE,
  1061. }:
  1062. continue
  1063. if (
  1064. "kind" not in member_event.content
  1065. or member_event.content["kind"] != "guest"
  1066. ):
  1067. continue
  1068. # We make the user choose to leave, rather than have the
  1069. # event-sender kick them. This is partially because we don't
  1070. # need to worry about power levels, and partially because guest
  1071. # users are a concept which doesn't hugely work over federation,
  1072. # and having homeservers have their own users leave keeps more
  1073. # of that decision-making and control local to the guest-having
  1074. # homeserver.
  1075. target_user = UserID.from_string(member_event.state_key)
  1076. requester = create_requester(
  1077. target_user, is_guest=True, authenticated_entity=self._server_name
  1078. )
  1079. handler = self.hs.get_room_member_handler()
  1080. await handler.update_membership(
  1081. requester,
  1082. target_user,
  1083. member_event.room_id,
  1084. "leave",
  1085. ratelimit=False,
  1086. require_consent=False,
  1087. )
  1088. except Exception as e:
  1089. logger.exception("Error kicking guest user: %s" % (e,))
  1090. async def lookup_room_alias(
  1091. self, room_alias: RoomAlias
  1092. ) -> Tuple[RoomID, List[str]]:
  1093. """
  1094. Get the room ID associated with a room alias.
  1095. Args:
  1096. room_alias: The alias to look up.
  1097. Returns:
  1098. A tuple of:
  1099. The room ID as a RoomID object.
  1100. Hosts likely to be participating in the room ([str]).
  1101. Raises:
  1102. SynapseError if room alias could not be found.
  1103. """
  1104. directory_handler = self.directory_handler
  1105. mapping = await directory_handler.get_association(room_alias)
  1106. if not mapping:
  1107. raise SynapseError(404, "No such room alias")
  1108. room_id = mapping["room_id"]
  1109. servers = mapping["servers"]
  1110. # put the server which owns the alias at the front of the server list.
  1111. if room_alias.domain in servers:
  1112. servers.remove(room_alias.domain)
  1113. servers.insert(0, room_alias.domain)
  1114. return RoomID.from_string(room_id), servers
  1115. async def _get_inviter(self, user_id: str, room_id: str) -> Optional[UserID]:
  1116. invite = await self.store.get_invite_for_local_user_in_room(
  1117. user_id=user_id, room_id=room_id
  1118. )
  1119. if invite:
  1120. return UserID.from_string(invite.sender)
  1121. return None
  1122. async def do_3pid_invite(
  1123. self,
  1124. room_id: str,
  1125. inviter: UserID,
  1126. medium: str,
  1127. address: str,
  1128. id_server: str,
  1129. requester: Requester,
  1130. txn_id: Optional[str],
  1131. id_access_token: Optional[str] = None,
  1132. ) -> int:
  1133. """Invite a 3PID to a room.
  1134. Args:
  1135. room_id: The room to invite the 3PID to.
  1136. inviter: The user sending the invite.
  1137. medium: The 3PID's medium.
  1138. address: The 3PID's address.
  1139. id_server: The identity server to use.
  1140. requester: The user making the request.
  1141. txn_id: The transaction ID this is part of, or None if this is not
  1142. part of a transaction.
  1143. id_access_token: The optional identity server access token.
  1144. Returns:
  1145. The new stream ID.
  1146. Raises:
  1147. ShadowBanError if the requester has been shadow-banned.
  1148. """
  1149. if self.config.server.block_non_admin_invites:
  1150. is_requester_admin = await self.auth.is_server_admin(requester.user)
  1151. if not is_requester_admin:
  1152. raise SynapseError(
  1153. 403, "Invites have been disabled on this server", Codes.FORBIDDEN
  1154. )
  1155. if requester.shadow_banned:
  1156. # We randomly sleep a bit just to annoy the requester.
  1157. await self.clock.sleep(random.randint(1, 10))
  1158. raise ShadowBanError()
  1159. # We need to rate limit *before* we send out any 3PID invites, so we
  1160. # can't just rely on the standard ratelimiting of events.
  1161. await self._third_party_invite_limiter.ratelimit(requester)
  1162. can_invite = await self.third_party_event_rules.check_threepid_can_be_invited(
  1163. medium, address, room_id
  1164. )
  1165. if not can_invite:
  1166. raise SynapseError(
  1167. 403,
  1168. "This third-party identifier can not be invited in this room",
  1169. Codes.FORBIDDEN,
  1170. )
  1171. if not self._enable_lookup:
  1172. raise SynapseError(
  1173. 403, "Looking up third-party identifiers is denied from this server"
  1174. )
  1175. invitee = await self.identity_handler.lookup_3pid(
  1176. id_server, medium, address, id_access_token
  1177. )
  1178. if invitee:
  1179. # Note that update_membership with an action of "invite" can raise
  1180. # a ShadowBanError, but this was done above already.
  1181. # We don't check the invite against the spamchecker(s) here (through
  1182. # user_may_invite) because we'll do it further down the line anyway (in
  1183. # update_membership_locked).
  1184. _, stream_id = await self.update_membership(
  1185. requester, UserID.from_string(invitee), room_id, "invite", txn_id=txn_id
  1186. )
  1187. else:
  1188. # Check if the spamchecker(s) allow this invite to go through.
  1189. if not await self.spam_checker.user_may_send_3pid_invite(
  1190. inviter_userid=requester.user.to_string(),
  1191. medium=medium,
  1192. address=address,
  1193. room_id=room_id,
  1194. ):
  1195. raise SynapseError(403, "Cannot send threepid invite")
  1196. stream_id = await self._make_and_store_3pid_invite(
  1197. requester,
  1198. id_server,
  1199. medium,
  1200. address,
  1201. room_id,
  1202. inviter,
  1203. txn_id=txn_id,
  1204. id_access_token=id_access_token,
  1205. )
  1206. return stream_id
  1207. async def _make_and_store_3pid_invite(
  1208. self,
  1209. requester: Requester,
  1210. id_server: str,
  1211. medium: str,
  1212. address: str,
  1213. room_id: str,
  1214. user: UserID,
  1215. txn_id: Optional[str],
  1216. id_access_token: Optional[str] = None,
  1217. ) -> int:
  1218. room_state = await self.state_handler.get_current_state(room_id)
  1219. inviter_display_name = ""
  1220. inviter_avatar_url = ""
  1221. member_event = room_state.get((EventTypes.Member, user.to_string()))
  1222. if member_event:
  1223. inviter_display_name = member_event.content.get("displayname", "")
  1224. inviter_avatar_url = member_event.content.get("avatar_url", "")
  1225. # if user has no display name, default to their MXID
  1226. if not inviter_display_name:
  1227. inviter_display_name = user.to_string()
  1228. canonical_room_alias = ""
  1229. canonical_alias_event = room_state.get((EventTypes.CanonicalAlias, ""))
  1230. if canonical_alias_event:
  1231. canonical_room_alias = canonical_alias_event.content.get("alias", "")
  1232. room_name = ""
  1233. room_name_event = room_state.get((EventTypes.Name, ""))
  1234. if room_name_event:
  1235. room_name = room_name_event.content.get("name", "")
  1236. room_type = None
  1237. room_create_event = room_state.get((EventTypes.Create, ""))
  1238. if room_create_event:
  1239. room_type = room_create_event.content.get(EventContentFields.ROOM_TYPE)
  1240. room_join_rules = ""
  1241. join_rules_event = room_state.get((EventTypes.JoinRules, ""))
  1242. if join_rules_event:
  1243. room_join_rules = join_rules_event.content.get("join_rule", "")
  1244. room_avatar_url = ""
  1245. room_avatar_event = room_state.get((EventTypes.RoomAvatar, ""))
  1246. if room_avatar_event:
  1247. room_avatar_url = room_avatar_event.content.get("url", "")
  1248. (
  1249. token,
  1250. public_keys,
  1251. fallback_public_key,
  1252. display_name,
  1253. ) = await self.identity_handler.ask_id_server_for_third_party_invite(
  1254. requester=requester,
  1255. id_server=id_server,
  1256. medium=medium,
  1257. address=address,
  1258. room_id=room_id,
  1259. inviter_user_id=user.to_string(),
  1260. room_alias=canonical_room_alias,
  1261. room_avatar_url=room_avatar_url,
  1262. room_join_rules=room_join_rules,
  1263. room_name=room_name,
  1264. room_type=room_type,
  1265. inviter_display_name=inviter_display_name,
  1266. inviter_avatar_url=inviter_avatar_url,
  1267. id_access_token=id_access_token,
  1268. )
  1269. (
  1270. event,
  1271. stream_id,
  1272. ) = await self.event_creation_handler.create_and_send_nonmember_event(
  1273. requester,
  1274. {
  1275. "type": EventTypes.ThirdPartyInvite,
  1276. "content": {
  1277. "display_name": display_name,
  1278. "public_keys": public_keys,
  1279. # For backwards compatibility:
  1280. "key_validity_url": fallback_public_key["key_validity_url"],
  1281. "public_key": fallback_public_key["public_key"],
  1282. },
  1283. "room_id": room_id,
  1284. "sender": user.to_string(),
  1285. "state_key": token,
  1286. },
  1287. ratelimit=False,
  1288. txn_id=txn_id,
  1289. )
  1290. return stream_id
  1291. async def _is_host_in_room(self, current_state_ids: StateMap[str]) -> bool:
  1292. # Have we just created the room, and is this about to be the very
  1293. # first member event?
  1294. create_event_id = current_state_ids.get(("m.room.create", ""))
  1295. if len(current_state_ids) == 1 and create_event_id:
  1296. # We can only get here if we're in the process of creating the room
  1297. return True
  1298. for etype, state_key in current_state_ids:
  1299. if etype != EventTypes.Member or not self.hs.is_mine_id(state_key):
  1300. continue
  1301. event_id = current_state_ids[(etype, state_key)]
  1302. event = await self.store.get_event(event_id, allow_none=True)
  1303. if not event:
  1304. continue
  1305. if event.membership == Membership.JOIN:
  1306. return True
  1307. return False
  1308. async def _is_server_notice_room(self, room_id: str) -> bool:
  1309. if self._server_notices_mxid is None:
  1310. return False
  1311. user_ids = await self.store.get_users_in_room(room_id)
  1312. return self._server_notices_mxid in user_ids
  1313. class RoomMemberMasterHandler(RoomMemberHandler):
  1314. def __init__(self, hs: "HomeServer"):
  1315. super().__init__(hs)
  1316. self.distributor = hs.get_distributor()
  1317. self.distributor.declare("user_left_room")
  1318. async def _is_remote_room_too_complex(
  1319. self, room_id: str, remote_room_hosts: List[str]
  1320. ) -> Optional[bool]:
  1321. """
  1322. Check if complexity of a remote room is too great.
  1323. Args:
  1324. room_id
  1325. remote_room_hosts
  1326. Returns: bool of whether the complexity is too great, or None
  1327. if unable to be fetched
  1328. """
  1329. max_complexity = self.hs.config.server.limit_remote_rooms.complexity
  1330. complexity = await self.federation_handler.get_room_complexity(
  1331. remote_room_hosts, room_id
  1332. )
  1333. if complexity:
  1334. return complexity["v1"] > max_complexity
  1335. return None
  1336. async def _is_local_room_too_complex(self, room_id: str) -> bool:
  1337. """
  1338. Check if the complexity of a local room is too great.
  1339. Args:
  1340. room_id: The room ID to check for complexity.
  1341. """
  1342. max_complexity = self.hs.config.server.limit_remote_rooms.complexity
  1343. complexity = await self.store.get_room_complexity(room_id)
  1344. return complexity["v1"] > max_complexity
  1345. async def _remote_join(
  1346. self,
  1347. requester: Requester,
  1348. remote_room_hosts: List[str],
  1349. room_id: str,
  1350. user: UserID,
  1351. content: dict,
  1352. ) -> Tuple[str, int]:
  1353. """Implements RoomMemberHandler._remote_join"""
  1354. # filter ourselves out of remote_room_hosts: do_invite_join ignores it
  1355. # and if it is the only entry we'd like to return a 404 rather than a
  1356. # 500.
  1357. remote_room_hosts = [
  1358. host for host in remote_room_hosts if host != self.hs.hostname
  1359. ]
  1360. if len(remote_room_hosts) == 0:
  1361. raise SynapseError(404, "No known servers")
  1362. check_complexity = self.hs.config.server.limit_remote_rooms.enabled
  1363. if (
  1364. check_complexity
  1365. and self.hs.config.server.limit_remote_rooms.admins_can_join
  1366. ):
  1367. check_complexity = not await self.auth.is_server_admin(user)
  1368. if check_complexity:
  1369. # Fetch the room complexity
  1370. too_complex = await self._is_remote_room_too_complex(
  1371. room_id, remote_room_hosts
  1372. )
  1373. if too_complex is True:
  1374. raise SynapseError(
  1375. code=400,
  1376. msg=self.hs.config.server.limit_remote_rooms.complexity_error,
  1377. errcode=Codes.RESOURCE_LIMIT_EXCEEDED,
  1378. )
  1379. # We don't do an auth check if we are doing an invite
  1380. # join dance for now, since we're kinda implicitly checking
  1381. # that we are allowed to join when we decide whether or not we
  1382. # need to do the invite/join dance.
  1383. event_id, stream_id = await self.federation_handler.do_invite_join(
  1384. remote_room_hosts, room_id, user.to_string(), content
  1385. )
  1386. # Check the room we just joined wasn't too large, if we didn't fetch the
  1387. # complexity of it before.
  1388. if check_complexity:
  1389. if too_complex is False:
  1390. # We checked, and we're under the limit.
  1391. return event_id, stream_id
  1392. # Check again, but with the local state events
  1393. too_complex = await self._is_local_room_too_complex(room_id)
  1394. if too_complex is False:
  1395. # We're under the limit.
  1396. return event_id, stream_id
  1397. # The room is too large. Leave.
  1398. requester = types.create_requester(
  1399. user, authenticated_entity=self._server_name
  1400. )
  1401. await self.update_membership(
  1402. requester=requester, target=user, room_id=room_id, action="leave"
  1403. )
  1404. raise SynapseError(
  1405. code=400,
  1406. msg=self.hs.config.server.limit_remote_rooms.complexity_error,
  1407. errcode=Codes.RESOURCE_LIMIT_EXCEEDED,
  1408. )
  1409. return event_id, stream_id
  1410. async def remote_reject_invite(
  1411. self,
  1412. invite_event_id: str,
  1413. txn_id: Optional[str],
  1414. requester: Requester,
  1415. content: JsonDict,
  1416. ) -> Tuple[str, int]:
  1417. """
  1418. Rejects an out-of-band invite received from a remote user
  1419. Implements RoomMemberHandler.remote_reject_invite
  1420. """
  1421. invite_event = await self.store.get_event(invite_event_id)
  1422. room_id = invite_event.room_id
  1423. target_user = invite_event.state_key
  1424. # first of all, try doing a rejection via the inviting server
  1425. fed_handler = self.federation_handler
  1426. try:
  1427. inviter_id = UserID.from_string(invite_event.sender)
  1428. event, stream_id = await fed_handler.do_remotely_reject_invite(
  1429. [inviter_id.domain], room_id, target_user, content=content
  1430. )
  1431. return event.event_id, stream_id
  1432. except Exception as e:
  1433. # if we were unable to reject the invite, we will generate our own
  1434. # leave event.
  1435. #
  1436. # The 'except' clause is very broad, but we need to
  1437. # capture everything from DNS failures upwards
  1438. #
  1439. logger.warning("Failed to reject invite: %s", e)
  1440. return await self._generate_local_out_of_band_leave(
  1441. invite_event, txn_id, requester, content
  1442. )
  1443. async def remote_rescind_knock(
  1444. self,
  1445. knock_event_id: str,
  1446. txn_id: Optional[str],
  1447. requester: Requester,
  1448. content: JsonDict,
  1449. ) -> Tuple[str, int]:
  1450. """
  1451. Rescinds a local knock made on a remote room
  1452. Args:
  1453. knock_event_id: The ID of the knock event to rescind.
  1454. txn_id: The transaction ID to use.
  1455. requester: The originator of the request.
  1456. content: The content of the leave event.
  1457. Implements RoomMemberHandler.remote_rescind_knock
  1458. """
  1459. # TODO: We don't yet support rescinding knocks over federation
  1460. # as we don't know which homeserver to send it to. An obvious
  1461. # candidate is the remote homeserver we originally knocked through,
  1462. # however we don't currently store that information.
  1463. # Just rescind the knock locally
  1464. knock_event = await self.store.get_event(knock_event_id)
  1465. return await self._generate_local_out_of_band_leave(
  1466. knock_event, txn_id, requester, content
  1467. )
  1468. async def _generate_local_out_of_band_leave(
  1469. self,
  1470. previous_membership_event: EventBase,
  1471. txn_id: Optional[str],
  1472. requester: Requester,
  1473. content: JsonDict,
  1474. ) -> Tuple[str, int]:
  1475. """Generate a local leave event for a room
  1476. This can be called after we e.g fail to reject an invite via a remote server.
  1477. It generates an out-of-band membership event locally.
  1478. Args:
  1479. previous_membership_event: the previous membership event for this user
  1480. txn_id: optional transaction ID supplied by the client
  1481. requester: user making the request, according to the access token
  1482. content: additional content to include in the leave event.
  1483. Normally an empty dict.
  1484. Returns:
  1485. A tuple containing (event_id, stream_id of the leave event)
  1486. """
  1487. room_id = previous_membership_event.room_id
  1488. target_user = previous_membership_event.state_key
  1489. content["membership"] = Membership.LEAVE
  1490. event_dict = {
  1491. "type": EventTypes.Member,
  1492. "room_id": room_id,
  1493. "sender": target_user,
  1494. "content": content,
  1495. "state_key": target_user,
  1496. }
  1497. # the auth events for the new event are the same as that of the previous event, plus
  1498. # the event itself.
  1499. #
  1500. # the prev_events consist solely of the previous membership event.
  1501. prev_event_ids = [previous_membership_event.event_id]
  1502. auth_event_ids = (
  1503. list(previous_membership_event.auth_event_ids()) + prev_event_ids
  1504. )
  1505. event, context = await self.event_creation_handler.create_event(
  1506. requester,
  1507. event_dict,
  1508. txn_id=txn_id,
  1509. prev_event_ids=prev_event_ids,
  1510. auth_event_ids=auth_event_ids,
  1511. outlier=True,
  1512. )
  1513. event.internal_metadata.out_of_band_membership = True
  1514. result_event = await self.event_creation_handler.handle_new_client_event(
  1515. requester,
  1516. event,
  1517. context,
  1518. extra_users=[UserID.from_string(target_user)],
  1519. )
  1520. # we know it was persisted, so must have a stream ordering
  1521. assert result_event.internal_metadata.stream_ordering
  1522. return result_event.event_id, result_event.internal_metadata.stream_ordering
  1523. async def remote_knock(
  1524. self,
  1525. remote_room_hosts: List[str],
  1526. room_id: str,
  1527. user: UserID,
  1528. content: dict,
  1529. ) -> Tuple[str, int]:
  1530. """Sends a knock to a room. Attempts to do so via one remote out of a given list.
  1531. Args:
  1532. remote_room_hosts: A list of homeservers to try knocking through.
  1533. room_id: The ID of the room to knock on.
  1534. user: The user to knock on behalf of.
  1535. content: The content of the knock event.
  1536. Returns:
  1537. A tuple of (event ID, stream ID).
  1538. """
  1539. # filter ourselves out of remote_room_hosts
  1540. remote_room_hosts = [
  1541. host for host in remote_room_hosts if host != self.hs.hostname
  1542. ]
  1543. if len(remote_room_hosts) == 0:
  1544. raise SynapseError(404, "No known servers")
  1545. return await self.federation_handler.do_knock(
  1546. remote_room_hosts, room_id, user.to_string(), content=content
  1547. )
  1548. async def _user_left_room(self, target: UserID, room_id: str) -> None:
  1549. """Implements RoomMemberHandler._user_left_room"""
  1550. user_left_room(self.distributor, target, room_id)
  1551. async def forget(self, user: UserID, room_id: str) -> None:
  1552. user_id = user.to_string()
  1553. member = await self.state_handler.get_current_state(
  1554. room_id=room_id, event_type=EventTypes.Member, state_key=user_id
  1555. )
  1556. membership = member.membership if member else None
  1557. if membership is not None and membership not in [
  1558. Membership.LEAVE,
  1559. Membership.BAN,
  1560. ]:
  1561. raise SynapseError(400, "User %s in room %s" % (user_id, room_id))
  1562. if membership:
  1563. await self.store.forget(user_id, room_id)
  1564. def get_users_which_can_issue_invite(auth_events: StateMap[EventBase]) -> List[str]:
  1565. """
  1566. Return the list of users which can issue invites.
  1567. This is done by exploring the joined users and comparing their power levels
  1568. to the necessyar power level to issue an invite.
  1569. Args:
  1570. auth_events: state in force at this point in the room
  1571. Returns:
  1572. The users which can issue invites.
  1573. """
  1574. invite_level = get_named_level(auth_events, "invite", 0)
  1575. users_default_level = get_named_level(auth_events, "users_default", 0)
  1576. power_level_event = get_power_level_event(auth_events)
  1577. # Custom power-levels for users.
  1578. if power_level_event:
  1579. users = power_level_event.content.get("users", {})
  1580. else:
  1581. users = {}
  1582. result = []
  1583. # Check which members are able to invite by ensuring they're joined and have
  1584. # the necessary power level.
  1585. for (event_type, state_key), event in auth_events.items():
  1586. if event_type != EventTypes.Member:
  1587. continue
  1588. if event.membership != Membership.JOIN:
  1589. continue
  1590. # Check if the user has a custom power level.
  1591. if users.get(state_key, users_default_level) >= invite_level:
  1592. result.append(state_key)
  1593. return result
  1594. def get_servers_from_users(users: List[str]) -> Set[str]:
  1595. """
  1596. Resolve a list of users into their servers.
  1597. Args:
  1598. users: A list of users.
  1599. Returns:
  1600. A set of servers.
  1601. """
  1602. servers = set()
  1603. for user in users:
  1604. try:
  1605. servers.add(get_domain_from_id(user))
  1606. except SynapseError:
  1607. pass
  1608. return servers