room_directory.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Copyright 2018 New Vector Ltd
  2. # Copyright 2021 Matrix.org Foundation C.I.C.
  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. from typing import List
  16. from matrix_common.regex import glob_to_regex
  17. from synapse.types import JsonDict
  18. from ._base import Config, ConfigError
  19. class RoomDirectoryConfig(Config):
  20. section = "roomdirectory"
  21. def read_config(self, config, **kwargs) -> None:
  22. self.enable_room_list_search = config.get("enable_room_list_search", True)
  23. alias_creation_rules = config.get("alias_creation_rules")
  24. if alias_creation_rules is not None:
  25. self._alias_creation_rules = [
  26. _RoomDirectoryRule("alias_creation_rules", rule)
  27. for rule in alias_creation_rules
  28. ]
  29. else:
  30. self._alias_creation_rules = [
  31. _RoomDirectoryRule("alias_creation_rules", {"action": "allow"})
  32. ]
  33. room_list_publication_rules = config.get("room_list_publication_rules")
  34. if room_list_publication_rules is not None:
  35. self._room_list_publication_rules = [
  36. _RoomDirectoryRule("room_list_publication_rules", rule)
  37. for rule in room_list_publication_rules
  38. ]
  39. else:
  40. self._room_list_publication_rules = [
  41. _RoomDirectoryRule("room_list_publication_rules", {"action": "allow"})
  42. ]
  43. def generate_config_section(self, config_dir_path, server_name, **kwargs) -> str:
  44. return """
  45. # Uncomment to disable searching the public room list. When disabled
  46. # blocks searching local and remote room lists for local and remote
  47. # users by always returning an empty list for all queries.
  48. #
  49. #enable_room_list_search: false
  50. # The `alias_creation` option controls who's allowed to create aliases
  51. # on this server.
  52. #
  53. # The format of this option is a list of rules that contain globs that
  54. # match against user_id, room_id and the new alias (fully qualified with
  55. # server name). The action in the first rule that matches is taken,
  56. # which can currently either be "allow" or "deny".
  57. #
  58. # Missing user_id/room_id/alias fields default to "*".
  59. #
  60. # If no rules match the request is denied. An empty list means no one
  61. # can create aliases.
  62. #
  63. # Options for the rules include:
  64. #
  65. # user_id: Matches against the creator of the alias
  66. # alias: Matches against the alias being created
  67. # room_id: Matches against the room ID the alias is being pointed at
  68. # action: Whether to "allow" or "deny" the request if the rule matches
  69. #
  70. # The default is:
  71. #
  72. #alias_creation_rules:
  73. # - user_id: "*"
  74. # alias: "*"
  75. # room_id: "*"
  76. # action: allow
  77. # The `room_list_publication_rules` option controls who can publish and
  78. # which rooms can be published in the public room list.
  79. #
  80. # The format of this option is the same as that for
  81. # `alias_creation_rules`.
  82. #
  83. # If the room has one or more aliases associated with it, only one of
  84. # the aliases needs to match the alias rule. If there are no aliases
  85. # then only rules with `alias: *` match.
  86. #
  87. # If no rules match the request is denied. An empty list means no one
  88. # can publish rooms.
  89. #
  90. # Options for the rules include:
  91. #
  92. # user_id: Matches against the creator of the alias
  93. # room_id: Matches against the room ID being published
  94. # alias: Matches against any current local or canonical aliases
  95. # associated with the room
  96. # action: Whether to "allow" or "deny" the request if the rule matches
  97. #
  98. # The default is:
  99. #
  100. #room_list_publication_rules:
  101. # - user_id: "*"
  102. # alias: "*"
  103. # room_id: "*"
  104. # action: allow
  105. """
  106. def is_alias_creation_allowed(self, user_id: str, room_id: str, alias: str) -> bool:
  107. """Checks if the given user is allowed to create the given alias
  108. Args:
  109. user_id: The user to check.
  110. room_id: The room ID for the alias.
  111. alias: The alias being created.
  112. Returns:
  113. True if user is allowed to create the alias
  114. """
  115. for rule in self._alias_creation_rules:
  116. if rule.matches(user_id, room_id, [alias]):
  117. return rule.action == "allow"
  118. return False
  119. def is_publishing_room_allowed(
  120. self, user_id: str, room_id: str, aliases: List[str]
  121. ) -> bool:
  122. """Checks if the given user is allowed to publish the room
  123. Args:
  124. user_id: The user ID publishing the room.
  125. room_id: The room being published.
  126. aliases: any local aliases associated with the room
  127. Returns:
  128. True if user can publish room
  129. """
  130. for rule in self._room_list_publication_rules:
  131. if rule.matches(user_id, room_id, aliases):
  132. return rule.action == "allow"
  133. return False
  134. class _RoomDirectoryRule:
  135. """Helper class to test whether a room directory action is allowed, like
  136. creating an alias or publishing a room.
  137. """
  138. def __init__(self, option_name: str, rule: JsonDict):
  139. """
  140. Args:
  141. option_name: Name of the config option this rule belongs to
  142. rule: The rule as specified in the config
  143. """
  144. action = rule["action"]
  145. user_id = rule.get("user_id", "*")
  146. room_id = rule.get("room_id", "*")
  147. alias = rule.get("alias", "*")
  148. if action in ("allow", "deny"):
  149. self.action = action
  150. else:
  151. raise ConfigError(
  152. "%s rules can only have action of 'allow' or 'deny'" % (option_name,)
  153. )
  154. self._alias_matches_all = alias == "*"
  155. try:
  156. self._user_id_regex = glob_to_regex(user_id)
  157. self._alias_regex = glob_to_regex(alias)
  158. self._room_id_regex = glob_to_regex(room_id)
  159. except Exception as e:
  160. raise ConfigError("Failed to parse glob into regex") from e
  161. def matches(self, user_id: str, room_id: str, aliases: List[str]) -> bool:
  162. """Tests if this rule matches the given user_id, room_id and aliases.
  163. Args:
  164. user_id: The user ID to check.
  165. room_id: The room ID to check.
  166. aliases: The associated aliases to the room. Will be a single element
  167. for testing alias creation, and can be empty for testing room
  168. publishing.
  169. Returns:
  170. True if the rule matches.
  171. """
  172. # Note: The regexes are anchored at both ends
  173. if not self._user_id_regex.match(user_id):
  174. return False
  175. if not self._room_id_regex.match(room_id):
  176. return False
  177. # We only have alias checks left, so we can short circuit if the alias
  178. # rule matches everything.
  179. if self._alias_matches_all:
  180. return True
  181. # If we are not given any aliases then this rule only matches if the
  182. # alias glob matches all aliases, which we checked above.
  183. if not aliases:
  184. return False
  185. # Otherwise, we just need one alias to match
  186. for alias in aliases:
  187. if self._alias_regex.match(alias):
  188. return True
  189. return False