2018-10-17 09:14:04 -06:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2021-11-19 08:19:32 -07:00
|
|
|
# Copyright 2021 Matrix.org Foundation C.I.C.
|
2018-10-17 09:14:04 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2022-04-11 10:07:23 -06:00
|
|
|
from typing import Any, List
|
2021-11-19 08:19:32 -07:00
|
|
|
|
2022-01-05 04:41:49 -07:00
|
|
|
from matrix_common.regex import glob_to_regex
|
|
|
|
|
2021-11-19 08:19:32 -07:00
|
|
|
from synapse.types import JsonDict
|
2018-10-17 09:14:04 -06:00
|
|
|
|
|
|
|
from ._base import Config, ConfigError
|
|
|
|
|
|
|
|
|
|
|
|
class RoomDirectoryConfig(Config):
|
2019-10-10 02:39:35 -06:00
|
|
|
section = "roomdirectory"
|
|
|
|
|
2022-04-11 10:07:23 -06:00
|
|
|
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
|
2019-03-19 10:50:51 -06:00
|
|
|
self.enable_room_list_search = config.get("enable_room_list_search", True)
|
|
|
|
|
2019-02-15 03:53:39 -07:00
|
|
|
alias_creation_rules = config.get("alias_creation_rules")
|
2018-10-17 09:14:04 -06:00
|
|
|
|
2019-02-15 03:53:39 -07:00
|
|
|
if alias_creation_rules is not None:
|
|
|
|
self._alias_creation_rules = [
|
|
|
|
_RoomDirectoryRule("alias_creation_rules", rule)
|
|
|
|
for rule in alias_creation_rules
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
self._alias_creation_rules = [
|
|
|
|
_RoomDirectoryRule("alias_creation_rules", {"action": "allow"})
|
|
|
|
]
|
2018-10-17 09:14:04 -06:00
|
|
|
|
2019-02-15 03:53:39 -07:00
|
|
|
room_list_publication_rules = config.get("room_list_publication_rules")
|
2019-02-14 09:02:23 -07:00
|
|
|
|
2019-02-15 03:53:39 -07:00
|
|
|
if room_list_publication_rules is not None:
|
|
|
|
self._room_list_publication_rules = [
|
|
|
|
_RoomDirectoryRule("room_list_publication_rules", rule)
|
|
|
|
for rule in room_list_publication_rules
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
self._room_list_publication_rules = [
|
|
|
|
_RoomDirectoryRule("room_list_publication_rules", {"action": "allow"})
|
|
|
|
]
|
2019-02-14 09:02:23 -07:00
|
|
|
|
2021-11-19 08:19:32 -07:00
|
|
|
def is_alias_creation_allowed(self, user_id: str, room_id: str, alias: str) -> bool:
|
2018-10-17 09:14:04 -06:00
|
|
|
"""Checks if the given user is allowed to create the given alias
|
|
|
|
|
|
|
|
Args:
|
2021-11-19 08:19:32 -07:00
|
|
|
user_id: The user to check.
|
|
|
|
room_id: The room ID for the alias.
|
|
|
|
alias: The alias being created.
|
2018-10-17 09:14:04 -06:00
|
|
|
|
|
|
|
Returns:
|
2021-11-19 08:19:32 -07:00
|
|
|
True if user is allowed to create the alias
|
2018-10-17 09:14:04 -06:00
|
|
|
"""
|
|
|
|
for rule in self._alias_creation_rules:
|
2019-02-14 09:02:23 -07:00
|
|
|
if rule.matches(user_id, room_id, [alias]):
|
|
|
|
return rule.action == "allow"
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2021-11-19 08:19:32 -07:00
|
|
|
def is_publishing_room_allowed(
|
|
|
|
self, user_id: str, room_id: str, aliases: List[str]
|
|
|
|
) -> bool:
|
2019-02-14 09:02:23 -07:00
|
|
|
"""Checks if the given user is allowed to publish the room
|
|
|
|
|
|
|
|
Args:
|
2021-11-19 08:19:32 -07:00
|
|
|
user_id: The user ID publishing the room.
|
|
|
|
room_id: The room being published.
|
|
|
|
aliases: any local aliases associated with the room
|
2019-02-14 09:02:23 -07:00
|
|
|
|
|
|
|
Returns:
|
2021-11-19 08:19:32 -07:00
|
|
|
True if user can publish room
|
2019-02-14 09:02:23 -07:00
|
|
|
"""
|
|
|
|
for rule in self._room_list_publication_rules:
|
|
|
|
if rule.matches(user_id, room_id, aliases):
|
2018-10-25 08:25:21 -06:00
|
|
|
return rule.action == "allow"
|
2018-10-17 09:14:04 -06:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-09-04 04:54:56 -06:00
|
|
|
class _RoomDirectoryRule:
|
2019-02-14 09:02:23 -07:00
|
|
|
"""Helper class to test whether a room directory action is allowed, like
|
|
|
|
creating an alias or publishing a room.
|
|
|
|
"""
|
|
|
|
|
2021-11-19 08:19:32 -07:00
|
|
|
def __init__(self, option_name: str, rule: JsonDict):
|
2019-02-14 11:07:24 -07:00
|
|
|
"""
|
|
|
|
Args:
|
2021-11-19 08:19:32 -07:00
|
|
|
option_name: Name of the config option this rule belongs to
|
|
|
|
rule: The rule as specified in the config
|
2019-02-14 11:07:24 -07:00
|
|
|
"""
|
|
|
|
|
2018-10-17 09:14:04 -06:00
|
|
|
action = rule["action"]
|
2019-02-14 09:02:23 -07:00
|
|
|
user_id = rule.get("user_id", "*")
|
|
|
|
room_id = rule.get("room_id", "*")
|
|
|
|
alias = rule.get("alias", "*")
|
2018-10-17 09:14:04 -06:00
|
|
|
|
2018-10-25 08:25:21 -06:00
|
|
|
if action in ("allow", "deny"):
|
2018-10-17 09:14:04 -06:00
|
|
|
self.action = action
|
|
|
|
else:
|
|
|
|
raise ConfigError(
|
2019-11-21 05:00:14 -07:00
|
|
|
"%s rules can only have action of 'allow' or 'deny'" % (option_name,)
|
2018-10-17 09:14:04 -06:00
|
|
|
)
|
|
|
|
|
2019-02-14 09:02:23 -07:00
|
|
|
self._alias_matches_all = alias == "*"
|
|
|
|
|
2018-10-17 09:14:04 -06:00
|
|
|
try:
|
|
|
|
self._user_id_regex = glob_to_regex(user_id)
|
|
|
|
self._alias_regex = glob_to_regex(alias)
|
2019-02-14 09:02:23 -07:00
|
|
|
self._room_id_regex = glob_to_regex(room_id)
|
2018-10-17 09:14:04 -06:00
|
|
|
except Exception as e:
|
2020-12-08 07:04:35 -07:00
|
|
|
raise ConfigError("Failed to parse glob into regex") from e
|
2018-10-17 09:14:04 -06:00
|
|
|
|
2021-11-19 08:19:32 -07:00
|
|
|
def matches(self, user_id: str, room_id: str, aliases: List[str]) -> bool:
|
2019-02-14 09:02:23 -07:00
|
|
|
"""Tests if this rule matches the given user_id, room_id and aliases.
|
2018-10-17 09:14:04 -06:00
|
|
|
|
|
|
|
Args:
|
2021-11-19 08:19:32 -07:00
|
|
|
user_id: The user ID to check.
|
|
|
|
room_id: The room ID to check.
|
|
|
|
aliases: The associated aliases to the room. Will be a single element
|
|
|
|
for testing alias creation, and can be empty for testing room
|
|
|
|
publishing.
|
2018-10-17 09:14:04 -06:00
|
|
|
|
|
|
|
Returns:
|
2021-11-19 08:19:32 -07:00
|
|
|
True if the rule matches.
|
2018-10-17 09:14:04 -06:00
|
|
|
"""
|
|
|
|
|
2018-10-19 03:26:50 -06:00
|
|
|
# Note: The regexes are anchored at both ends
|
|
|
|
if not self._user_id_regex.match(user_id):
|
2018-10-17 09:14:04 -06:00
|
|
|
return False
|
|
|
|
|
2019-02-15 03:17:43 -07:00
|
|
|
if not self._room_id_regex.match(room_id):
|
|
|
|
return False
|
2019-02-14 11:16:32 -07:00
|
|
|
|
2019-02-15 03:17:43 -07:00
|
|
|
# We only have alias checks left, so we can short circuit if the alias
|
|
|
|
# rule matches everything.
|
|
|
|
if self._alias_matches_all:
|
|
|
|
return True
|
2019-02-14 09:02:23 -07:00
|
|
|
|
2019-02-15 03:17:43 -07:00
|
|
|
# If we are not given any aliases then this rule only matches if the
|
|
|
|
# alias glob matches all aliases, which we checked above.
|
|
|
|
if not aliases:
|
2018-10-17 09:14:04 -06:00
|
|
|
return False
|
|
|
|
|
2019-02-15 03:17:43 -07:00
|
|
|
# Otherwise, we just need one alias to match
|
|
|
|
for alias in aliases:
|
|
|
|
if self._alias_regex.match(alias):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|