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

221 lines
7.6 KiB

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