directory.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. # Copyright 2014-2016 OpenMarket Ltd
  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. import string
  16. from typing import TYPE_CHECKING, Iterable, List, Optional
  17. from synapse.api.constants import MAX_ALIAS_LENGTH, EventTypes
  18. from synapse.api.errors import (
  19. AuthError,
  20. CodeMessageException,
  21. Codes,
  22. NotFoundError,
  23. RequestSendFailed,
  24. ShadowBanError,
  25. StoreError,
  26. SynapseError,
  27. )
  28. from synapse.appservice import ApplicationService
  29. from synapse.storage.databases.main.directory import RoomAliasMapping
  30. from synapse.types import JsonDict, Requester, RoomAlias, UserID, get_domain_from_id
  31. if TYPE_CHECKING:
  32. from synapse.server import HomeServer
  33. logger = logging.getLogger(__name__)
  34. class DirectoryHandler:
  35. def __init__(self, hs: "HomeServer"):
  36. self.auth = hs.get_auth()
  37. self.hs = hs
  38. self.state = hs.get_state_handler()
  39. self.appservice_handler = hs.get_application_service_handler()
  40. self.event_creation_handler = hs.get_event_creation_handler()
  41. self.store = hs.get_datastores().main
  42. self.config = hs.config
  43. self.enable_room_list_search = hs.config.roomdirectory.enable_room_list_search
  44. self.require_membership = hs.config.server.require_membership_for_aliases
  45. self.third_party_event_rules = hs.get_third_party_event_rules()
  46. self.server_name = hs.hostname
  47. self.federation = hs.get_federation_client()
  48. hs.get_federation_registry().register_query_handler(
  49. "directory", self.on_directory_query
  50. )
  51. self.spam_checker = hs.get_spam_checker()
  52. async def _create_association(
  53. self,
  54. room_alias: RoomAlias,
  55. room_id: str,
  56. servers: Optional[Iterable[str]] = None,
  57. creator: Optional[str] = None,
  58. ) -> None:
  59. # general association creation for both human users and app services
  60. for wchar in string.whitespace:
  61. if wchar in room_alias.localpart:
  62. raise SynapseError(400, "Invalid characters in room alias")
  63. if ":" in room_alias.localpart:
  64. raise SynapseError(400, "Invalid character in room alias localpart: ':'.")
  65. if not self.hs.is_mine(room_alias):
  66. raise SynapseError(400, "Room alias must be local")
  67. # TODO(erikj): Change this.
  68. # TODO(erikj): Add transactions.
  69. # TODO(erikj): Check if there is a current association.
  70. if not servers:
  71. users = await self.store.get_users_in_room(room_id)
  72. servers = {get_domain_from_id(u) for u in users}
  73. if not servers:
  74. raise SynapseError(400, "Failed to get server list")
  75. await self.store.create_room_alias_association(
  76. room_alias, room_id, servers, creator=creator
  77. )
  78. async def create_association(
  79. self,
  80. requester: Requester,
  81. room_alias: RoomAlias,
  82. room_id: str,
  83. servers: Optional[List[str]] = None,
  84. check_membership: bool = True,
  85. ) -> None:
  86. """Attempt to create a new alias
  87. Args:
  88. requester
  89. room_alias
  90. room_id
  91. servers: Iterable of servers that others servers should try and join via
  92. check_membership: Whether to check if the user is in the room
  93. before the alias can be set (if the server's config requires it).
  94. """
  95. user_id = requester.user.to_string()
  96. room_alias_str = room_alias.to_string()
  97. if len(room_alias_str) > MAX_ALIAS_LENGTH:
  98. raise SynapseError(
  99. 400,
  100. "Can't create aliases longer than %s characters" % MAX_ALIAS_LENGTH,
  101. Codes.INVALID_PARAM,
  102. )
  103. service = requester.app_service
  104. if service:
  105. if not service.is_room_alias_in_namespace(room_alias_str):
  106. raise SynapseError(
  107. 400,
  108. "This application service has not reserved this kind of alias.",
  109. errcode=Codes.EXCLUSIVE,
  110. )
  111. else:
  112. # Server admins are not subject to the same constraints as normal
  113. # users when creating an alias (e.g. being in the room).
  114. is_admin = await self.auth.is_server_admin(requester.user)
  115. if (self.require_membership and check_membership) and not is_admin:
  116. rooms_for_user = await self.store.get_rooms_for_user(user_id)
  117. if room_id not in rooms_for_user:
  118. raise AuthError(
  119. 403, "You must be in the room to create an alias for it"
  120. )
  121. if not await self.spam_checker.user_may_create_room_alias(
  122. user_id, room_alias
  123. ):
  124. raise AuthError(403, "This user is not permitted to create this alias")
  125. if not self.config.roomdirectory.is_alias_creation_allowed(
  126. user_id, room_id, room_alias_str
  127. ):
  128. # Let's just return a generic message, as there may be all sorts of
  129. # reasons why we said no. TODO: Allow configurable error messages
  130. # per alias creation rule?
  131. raise SynapseError(403, "Not allowed to create alias")
  132. can_create = self.can_modify_alias(room_alias, user_id=user_id)
  133. if not can_create:
  134. raise AuthError(
  135. 400,
  136. "This alias is reserved by an application service.",
  137. errcode=Codes.EXCLUSIVE,
  138. )
  139. await self._create_association(room_alias, room_id, servers, creator=user_id)
  140. async def delete_association(
  141. self, requester: Requester, room_alias: RoomAlias
  142. ) -> str:
  143. """Remove an alias from the directory
  144. (this is only meant for human users; AS users should call
  145. delete_appservice_association)
  146. Args:
  147. requester
  148. room_alias
  149. Returns:
  150. room id that the alias used to point to
  151. Raises:
  152. NotFoundError: if the alias doesn't exist
  153. AuthError: if the user doesn't have perms to delete the alias (ie, the user
  154. is neither the creator of the alias, nor a server admin.
  155. SynapseError: if the alias belongs to an AS
  156. """
  157. user_id = requester.user.to_string()
  158. try:
  159. can_delete = await self._user_can_delete_alias(room_alias, user_id)
  160. except StoreError as e:
  161. if e.code == 404:
  162. raise NotFoundError("Unknown room alias")
  163. raise
  164. if not can_delete:
  165. raise AuthError(403, "You don't have permission to delete the alias.")
  166. can_delete = self.can_modify_alias(room_alias, user_id=user_id)
  167. if not can_delete:
  168. raise SynapseError(
  169. 400,
  170. "This alias is reserved by an application service.",
  171. errcode=Codes.EXCLUSIVE,
  172. )
  173. room_id = await self._delete_association(room_alias)
  174. if room_id is None:
  175. # It's possible someone else deleted the association after the
  176. # checks above, but before we did the deletion.
  177. raise NotFoundError("Unknown room alias")
  178. try:
  179. await self._update_canonical_alias(requester, user_id, room_id, room_alias)
  180. except ShadowBanError as e:
  181. logger.info("Failed to update alias events due to shadow-ban: %s", e)
  182. except AuthError as e:
  183. logger.info("Failed to update alias events: %s", e)
  184. return room_id
  185. async def delete_appservice_association(
  186. self, service: ApplicationService, room_alias: RoomAlias
  187. ) -> None:
  188. if not service.is_room_alias_in_namespace(room_alias.to_string()):
  189. raise SynapseError(
  190. 400,
  191. "This application service has not reserved this kind of alias",
  192. errcode=Codes.EXCLUSIVE,
  193. )
  194. await self._delete_association(room_alias)
  195. async def _delete_association(self, room_alias: RoomAlias) -> Optional[str]:
  196. if not self.hs.is_mine(room_alias):
  197. raise SynapseError(400, "Room alias must be local")
  198. room_id = await self.store.delete_room_alias(room_alias)
  199. return room_id
  200. async def get_association(self, room_alias: RoomAlias) -> JsonDict:
  201. room_id = None
  202. if self.hs.is_mine(room_alias):
  203. result: Optional[
  204. RoomAliasMapping
  205. ] = await self.get_association_from_room_alias(room_alias)
  206. if result:
  207. room_id = result.room_id
  208. servers = result.servers
  209. else:
  210. try:
  211. fed_result: Optional[JsonDict] = await self.federation.make_query(
  212. destination=room_alias.domain,
  213. query_type="directory",
  214. args={"room_alias": room_alias.to_string()},
  215. retry_on_dns_fail=False,
  216. ignore_backoff=True,
  217. )
  218. except RequestSendFailed:
  219. raise SynapseError(502, "Failed to fetch alias")
  220. except CodeMessageException as e:
  221. logging.warning("Error retrieving alias")
  222. if e.code == 404:
  223. fed_result = None
  224. else:
  225. raise SynapseError(502, "Failed to fetch alias")
  226. if fed_result and "room_id" in fed_result and "servers" in fed_result:
  227. room_id = fed_result["room_id"]
  228. servers = fed_result["servers"]
  229. if not room_id:
  230. raise SynapseError(
  231. 404,
  232. "Room alias %s not found" % (room_alias.to_string(),),
  233. Codes.NOT_FOUND,
  234. )
  235. users = await self.store.get_users_in_room(room_id)
  236. extra_servers = {get_domain_from_id(u) for u in users}
  237. servers_set = set(extra_servers) | set(servers)
  238. # If this server is in the list of servers, return it first.
  239. if self.server_name in servers_set:
  240. servers = [self.server_name] + [
  241. s for s in servers_set if s != self.server_name
  242. ]
  243. else:
  244. servers = list(servers_set)
  245. return {"room_id": room_id, "servers": servers}
  246. async def on_directory_query(self, args: JsonDict) -> JsonDict:
  247. room_alias = RoomAlias.from_string(args["room_alias"])
  248. if not self.hs.is_mine(room_alias):
  249. raise SynapseError(400, "Room Alias is not hosted on this homeserver")
  250. result = await self.get_association_from_room_alias(room_alias)
  251. if result is not None:
  252. return {"room_id": result.room_id, "servers": result.servers}
  253. else:
  254. raise SynapseError(
  255. 404,
  256. "Room alias %r not found" % (room_alias.to_string(),),
  257. Codes.NOT_FOUND,
  258. )
  259. async def _update_canonical_alias(
  260. self, requester: Requester, user_id: str, room_id: str, room_alias: RoomAlias
  261. ) -> None:
  262. """
  263. Send an updated canonical alias event if the removed alias was set as
  264. the canonical alias or listed in the alt_aliases field.
  265. Raises:
  266. ShadowBanError if the requester has been shadow-banned.
  267. """
  268. alias_event = await self.state.get_current_state(
  269. room_id, EventTypes.CanonicalAlias, ""
  270. )
  271. # There is no canonical alias, nothing to do.
  272. if not alias_event:
  273. return
  274. # Obtain a mutable version of the event content.
  275. content = dict(alias_event.content)
  276. send_update = False
  277. # Remove the alias property if it matches the removed alias.
  278. alias_str = room_alias.to_string()
  279. if alias_event.content.get("alias", "") == alias_str:
  280. send_update = True
  281. content.pop("alias", "")
  282. # Filter the alt_aliases property for the removed alias. Note that the
  283. # value is not modified if alt_aliases is of an unexpected form.
  284. alt_aliases = content.get("alt_aliases")
  285. if isinstance(alt_aliases, (list, tuple)) and alias_str in alt_aliases:
  286. send_update = True
  287. alt_aliases = [alias for alias in alt_aliases if alias != alias_str]
  288. if alt_aliases:
  289. content["alt_aliases"] = alt_aliases
  290. else:
  291. del content["alt_aliases"]
  292. if send_update:
  293. await self.event_creation_handler.create_and_send_nonmember_event(
  294. requester,
  295. {
  296. "type": EventTypes.CanonicalAlias,
  297. "state_key": "",
  298. "room_id": room_id,
  299. "sender": user_id,
  300. "content": content,
  301. },
  302. ratelimit=False,
  303. )
  304. async def get_association_from_room_alias(
  305. self, room_alias: RoomAlias
  306. ) -> Optional[RoomAliasMapping]:
  307. result = await self.store.get_association_from_room_alias(room_alias)
  308. if not result:
  309. # Query AS to see if it exists
  310. as_handler = self.appservice_handler
  311. result = await as_handler.query_room_alias_exists(room_alias)
  312. return result
  313. def can_modify_alias(self, alias: RoomAlias, user_id: Optional[str] = None) -> bool:
  314. # Any application service "interested" in an alias they are regexing on
  315. # can modify the alias.
  316. # Users can only modify the alias if ALL the interested services have
  317. # non-exclusive locks on the alias (or there are no interested services)
  318. services = self.store.get_app_services()
  319. interested_services = [
  320. s for s in services if s.is_room_alias_in_namespace(alias.to_string())
  321. ]
  322. for service in interested_services:
  323. if user_id == service.sender:
  324. # this user IS the app service so they can do whatever they like
  325. return True
  326. elif service.is_exclusive_alias(alias.to_string()):
  327. # another service has an exclusive lock on this alias.
  328. return False
  329. # either no interested services, or no service with an exclusive lock
  330. return True
  331. async def _user_can_delete_alias(self, alias: RoomAlias, user_id: str) -> bool:
  332. """Determine whether a user can delete an alias.
  333. One of the following must be true:
  334. 1. The user created the alias.
  335. 2. The user is a server administrator.
  336. 3. The user has a power-level sufficient to send a canonical alias event
  337. for the current room.
  338. """
  339. creator = await self.store.get_room_alias_creator(alias.to_string())
  340. if creator == user_id:
  341. return True
  342. # Resolve the alias to the corresponding room.
  343. room_mapping = await self.get_association(alias)
  344. room_id = room_mapping["room_id"]
  345. if not room_id:
  346. return False
  347. return await self.auth.check_can_change_room_list(
  348. room_id, UserID.from_string(user_id)
  349. )
  350. async def edit_published_room_list(
  351. self, requester: Requester, room_id: str, visibility: str
  352. ) -> None:
  353. """Edit the entry of the room in the published room list.
  354. requester
  355. room_id
  356. visibility: "public" or "private"
  357. """
  358. user_id = requester.user.to_string()
  359. if not await self.spam_checker.user_may_publish_room(user_id, room_id):
  360. raise AuthError(
  361. 403, "This user is not permitted to publish rooms to the room list"
  362. )
  363. if requester.is_guest:
  364. raise AuthError(403, "Guests cannot edit the published room list")
  365. if visibility not in ["public", "private"]:
  366. raise SynapseError(400, "Invalid visibility setting")
  367. if visibility == "public" and not self.enable_room_list_search:
  368. # The room list has been disabled.
  369. raise AuthError(
  370. 403, "This user is not permitted to publish rooms to the room list"
  371. )
  372. room = await self.store.get_room(room_id)
  373. if room is None:
  374. raise SynapseError(400, "Unknown room")
  375. can_change_room_list = await self.auth.check_can_change_room_list(
  376. room_id, requester.user
  377. )
  378. if not can_change_room_list:
  379. raise AuthError(
  380. 403,
  381. "This server requires you to be a moderator in the room to"
  382. " edit its room list entry",
  383. )
  384. making_public = visibility == "public"
  385. if making_public:
  386. room_aliases = await self.store.get_aliases_for_room(room_id)
  387. canonical_alias = await self.store.get_canonical_alias_for_room(room_id)
  388. if canonical_alias:
  389. room_aliases.append(canonical_alias)
  390. if not self.config.roomdirectory.is_publishing_room_allowed(
  391. user_id, room_id, room_aliases
  392. ):
  393. # Let's just return a generic message, as there may be all sorts of
  394. # reasons why we said no. TODO: Allow configurable error messages
  395. # per alias creation rule?
  396. raise SynapseError(403, "Not allowed to publish room")
  397. # Check if publishing is blocked by a third party module
  398. allowed_by_third_party_rules = await (
  399. self.third_party_event_rules.check_visibility_can_be_modified(
  400. room_id, visibility
  401. )
  402. )
  403. if not allowed_by_third_party_rules:
  404. raise SynapseError(403, "Not allowed to publish room")
  405. await self.store.set_room_is_public(room_id, making_public)
  406. async def edit_published_appservice_room_list(
  407. self, appservice_id: str, network_id: str, room_id: str, visibility: str
  408. ) -> None:
  409. """Add or remove a room from the appservice/network specific public
  410. room list.
  411. Args:
  412. appservice_id: ID of the appservice that owns the list
  413. network_id: The ID of the network the list is associated with
  414. room_id
  415. visibility: either "public" or "private"
  416. """
  417. if visibility not in ["public", "private"]:
  418. raise SynapseError(400, "Invalid visibility setting")
  419. await self.store.set_room_is_public_appservice(
  420. room_id, appservice_id, network_id, visibility == "public"
  421. )
  422. async def get_aliases_for_room(
  423. self, requester: Requester, room_id: str
  424. ) -> List[str]:
  425. """
  426. Get a list of the aliases that currently point to this room on this server
  427. """
  428. # allow access to server admins and current members of the room
  429. is_admin = await self.auth.is_server_admin(requester.user)
  430. if not is_admin:
  431. await self.auth.check_user_in_room_or_world_readable(
  432. room_id, requester.user.to_string()
  433. )
  434. return await self.store.get_aliases_for_room(room_id)