forked from MirrorHub/synapse
Merge pull request #4051 from matrix-org/erikj/alias_disallow_list
Add config option to control alias creation
This commit is contained in:
commit
c85e063302
8 changed files with 256 additions and 15 deletions
1
changelog.d/4051.feature
Normal file
1
changelog.d/4051.feature
Normal file
|
@ -0,0 +1 @@
|
|||
Add config option to control alias creation
|
|
@ -31,6 +31,7 @@ from .push import PushConfig
|
|||
from .ratelimiting import RatelimitConfig
|
||||
from .registration import RegistrationConfig
|
||||
from .repository import ContentRepositoryConfig
|
||||
from .room_directory import RoomDirectoryConfig
|
||||
from .saml2 import SAML2Config
|
||||
from .server import ServerConfig
|
||||
from .server_notices_config import ServerNoticesConfig
|
||||
|
@ -49,7 +50,7 @@ class HomeServerConfig(TlsConfig, ServerConfig, DatabaseConfig, LoggingConfig,
|
|||
WorkerConfig, PasswordAuthProviderConfig, PushConfig,
|
||||
SpamCheckerConfig, GroupsConfig, UserDirectoryConfig,
|
||||
ConsentConfig,
|
||||
ServerNoticesConfig,
|
||||
ServerNoticesConfig, RoomDirectoryConfig,
|
||||
):
|
||||
pass
|
||||
|
||||
|
|
102
synapse/config/room_directory.py
Normal file
102
synapse/config/room_directory.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2018 New Vector Ltd
|
||||
#
|
||||
# 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.
|
||||
|
||||
from synapse.util import glob_to_regex
|
||||
|
||||
from ._base import Config, ConfigError
|
||||
|
||||
|
||||
class RoomDirectoryConfig(Config):
|
||||
def read_config(self, config):
|
||||
alias_creation_rules = config["alias_creation_rules"]
|
||||
|
||||
self._alias_creation_rules = [
|
||||
_AliasRule(rule)
|
||||
for rule in alias_creation_rules
|
||||
]
|
||||
|
||||
def default_config(self, config_dir_path, server_name, **kwargs):
|
||||
return """
|
||||
# The `alias_creation` option controls who's allowed to create aliases
|
||||
# on this server.
|
||||
#
|
||||
# The format of this option is a list of rules that contain globs that
|
||||
# match against user_id and the new alias (fully qualified with server
|
||||
# name). The action in the first rule that matches is taken, which can
|
||||
# currently either be "allow" or "deny".
|
||||
#
|
||||
# If no rules match the request is denied.
|
||||
alias_creation_rules:
|
||||
- user_id: "*"
|
||||
alias: "*"
|
||||
action: allow
|
||||
"""
|
||||
|
||||
def is_alias_creation_allowed(self, user_id, alias):
|
||||
"""Checks if the given user is allowed to create the given alias
|
||||
|
||||
Args:
|
||||
user_id (str)
|
||||
alias (str)
|
||||
|
||||
Returns:
|
||||
boolean: True if user is allowed to crate the alias
|
||||
"""
|
||||
for rule in self._alias_creation_rules:
|
||||
if rule.matches(user_id, alias):
|
||||
return rule.action == "allow"
|
||||
|
||||
return False
|
||||
|
||||
|
||||
class _AliasRule(object):
|
||||
def __init__(self, rule):
|
||||
action = rule["action"]
|
||||
user_id = rule["user_id"]
|
||||
alias = rule["alias"]
|
||||
|
||||
if action in ("allow", "deny"):
|
||||
self.action = action
|
||||
else:
|
||||
raise ConfigError(
|
||||
"alias_creation_rules rules can only have action of 'allow'"
|
||||
" or 'deny'"
|
||||
)
|
||||
|
||||
try:
|
||||
self._user_id_regex = glob_to_regex(user_id)
|
||||
self._alias_regex = glob_to_regex(alias)
|
||||
except Exception as e:
|
||||
raise ConfigError("Failed to parse glob into regex: %s", e)
|
||||
|
||||
def matches(self, user_id, alias):
|
||||
"""Tests if this rule matches the given user_id and alias.
|
||||
|
||||
Args:
|
||||
user_id (str)
|
||||
alias (str)
|
||||
|
||||
Returns:
|
||||
boolean
|
||||
"""
|
||||
|
||||
# Note: The regexes are anchored at both ends
|
||||
if not self._user_id_regex.match(user_id):
|
||||
return False
|
||||
|
||||
if not self._alias_regex.match(alias):
|
||||
return False
|
||||
|
||||
return True
|
|
@ -14,7 +14,6 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import logging
|
||||
import re
|
||||
|
||||
import six
|
||||
from six import iteritems
|
||||
|
@ -44,6 +43,7 @@ from synapse.replication.http.federation import (
|
|||
ReplicationGetQueryRestServlet,
|
||||
)
|
||||
from synapse.types import get_domain_from_id
|
||||
from synapse.util import glob_to_regex
|
||||
from synapse.util.async_helpers import Linearizer, concurrently_execute
|
||||
from synapse.util.caches.response_cache import ResponseCache
|
||||
from synapse.util.logcontext import nested_logging_context
|
||||
|
@ -729,22 +729,10 @@ def _acl_entry_matches(server_name, acl_entry):
|
|||
if not isinstance(acl_entry, six.string_types):
|
||||
logger.warn("Ignoring non-str ACL entry '%s' (is %s)", acl_entry, type(acl_entry))
|
||||
return False
|
||||
regex = _glob_to_regex(acl_entry)
|
||||
regex = glob_to_regex(acl_entry)
|
||||
return regex.match(server_name)
|
||||
|
||||
|
||||
def _glob_to_regex(glob):
|
||||
res = ''
|
||||
for c in glob:
|
||||
if c == '*':
|
||||
res = res + '.*'
|
||||
elif c == '?':
|
||||
res = res + '.'
|
||||
else:
|
||||
res = res + re.escape(c)
|
||||
return re.compile(res + "\\Z", re.IGNORECASE)
|
||||
|
||||
|
||||
class FederationHandlerRegistry(object):
|
||||
"""Allows classes to register themselves as handlers for a given EDU or
|
||||
query type for incoming federation traffic.
|
||||
|
|
|
@ -43,6 +43,7 @@ class DirectoryHandler(BaseHandler):
|
|||
self.state = hs.get_state_handler()
|
||||
self.appservice_handler = hs.get_application_service_handler()
|
||||
self.event_creation_handler = hs.get_event_creation_handler()
|
||||
self.config = hs.config
|
||||
|
||||
self.federation = hs.get_federation_client()
|
||||
hs.get_federation_registry().register_query_handler(
|
||||
|
@ -111,6 +112,14 @@ class DirectoryHandler(BaseHandler):
|
|||
403, "This user is not permitted to create this alias",
|
||||
)
|
||||
|
||||
if not self.config.is_alias_creation_allowed(user_id, room_alias.to_string()):
|
||||
# Lets just return a generic message, as there may be all sorts of
|
||||
# reasons why we said no. TODO: Allow configurable error messages
|
||||
# per alias creation rule?
|
||||
raise SynapseError(
|
||||
403, "Not allowed to create alias",
|
||||
)
|
||||
|
||||
can_create = yield self.can_modify_alias(
|
||||
room_alias,
|
||||
user_id=user_id
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import re
|
||||
from itertools import islice
|
||||
|
||||
import attr
|
||||
|
@ -138,3 +139,27 @@ def log_failure(failure, msg, consumeErrors=True):
|
|||
|
||||
if not consumeErrors:
|
||||
return failure
|
||||
|
||||
|
||||
def glob_to_regex(glob):
|
||||
"""Converts a glob to a compiled regex object.
|
||||
|
||||
The regex is anchored at the beginning and end of the string.
|
||||
|
||||
Args:
|
||||
glob (str)
|
||||
|
||||
Returns:
|
||||
re.RegexObject
|
||||
"""
|
||||
res = ''
|
||||
for c in glob:
|
||||
if c == '*':
|
||||
res = res + '.*'
|
||||
elif c == '?':
|
||||
res = res + '.'
|
||||
else:
|
||||
res = res + re.escape(c)
|
||||
|
||||
# \A anchors at start of string, \Z at end of string
|
||||
return re.compile(r"\A" + res + r"\Z", re.IGNORECASE)
|
||||
|
|
67
tests/config/test_room_directory.py
Normal file
67
tests/config/test_room_directory.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2018 New Vector Ltd
|
||||
#
|
||||
# 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.
|
||||
|
||||
import yaml
|
||||
|
||||
from synapse.config.room_directory import RoomDirectoryConfig
|
||||
|
||||
from tests import unittest
|
||||
|
||||
|
||||
class RoomDirectoryConfigTestCase(unittest.TestCase):
|
||||
def test_alias_creation_acl(self):
|
||||
config = yaml.load("""
|
||||
alias_creation_rules:
|
||||
- user_id: "*bob*"
|
||||
alias: "*"
|
||||
action: "deny"
|
||||
- user_id: "*"
|
||||
alias: "#unofficial_*"
|
||||
action: "allow"
|
||||
- user_id: "@foo*:example.com"
|
||||
alias: "*"
|
||||
action: "allow"
|
||||
- user_id: "@gah:example.com"
|
||||
alias: "#goo:example.com"
|
||||
action: "allow"
|
||||
""")
|
||||
|
||||
rd_config = RoomDirectoryConfig()
|
||||
rd_config.read_config(config)
|
||||
|
||||
self.assertFalse(rd_config.is_alias_creation_allowed(
|
||||
user_id="@bob:example.com",
|
||||
alias="#test:example.com",
|
||||
))
|
||||
|
||||
self.assertTrue(rd_config.is_alias_creation_allowed(
|
||||
user_id="@test:example.com",
|
||||
alias="#unofficial_st:example.com",
|
||||
))
|
||||
|
||||
self.assertTrue(rd_config.is_alias_creation_allowed(
|
||||
user_id="@foobar:example.com",
|
||||
alias="#test:example.com",
|
||||
))
|
||||
|
||||
self.assertTrue(rd_config.is_alias_creation_allowed(
|
||||
user_id="@gah:example.com",
|
||||
alias="#goo:example.com",
|
||||
))
|
||||
|
||||
self.assertFalse(rd_config.is_alias_creation_allowed(
|
||||
user_id="@test:example.com",
|
||||
alias="#test:example.com",
|
||||
))
|
|
@ -18,7 +18,9 @@ from mock import Mock
|
|||
|
||||
from twisted.internet import defer
|
||||
|
||||
from synapse.config.room_directory import RoomDirectoryConfig
|
||||
from synapse.handlers.directory import DirectoryHandler
|
||||
from synapse.rest.client.v1 import directory, room
|
||||
from synapse.types import RoomAlias
|
||||
|
||||
from tests import unittest
|
||||
|
@ -102,3 +104,49 @@ class DirectoryTestCase(unittest.TestCase):
|
|||
)
|
||||
|
||||
self.assertEquals({"room_id": "!8765asdf:test", "servers": ["test"]}, response)
|
||||
|
||||
|
||||
class TestCreateAliasACL(unittest.HomeserverTestCase):
|
||||
user_id = "@test:test"
|
||||
|
||||
servlets = [directory.register_servlets, room.register_servlets]
|
||||
|
||||
def prepare(self, hs, reactor, clock):
|
||||
# We cheekily override the config to add custom alias creation rules
|
||||
config = {}
|
||||
config["alias_creation_rules"] = [
|
||||
{
|
||||
"user_id": "*",
|
||||
"alias": "#unofficial_*",
|
||||
"action": "allow",
|
||||
}
|
||||
]
|
||||
|
||||
rd_config = RoomDirectoryConfig()
|
||||
rd_config.read_config(config)
|
||||
|
||||
self.hs.config.is_alias_creation_allowed = rd_config.is_alias_creation_allowed
|
||||
|
||||
return hs
|
||||
|
||||
def test_denied(self):
|
||||
room_id = self.helper.create_room_as(self.user_id)
|
||||
|
||||
request, channel = self.make_request(
|
||||
"PUT",
|
||||
b"directory/room/%23test%3Atest",
|
||||
('{"room_id":"%s"}' % (room_id,)).encode('ascii'),
|
||||
)
|
||||
self.render(request)
|
||||
self.assertEquals(403, channel.code, channel.result)
|
||||
|
||||
def test_allowed(self):
|
||||
room_id = self.helper.create_room_as(self.user_id)
|
||||
|
||||
request, channel = self.make_request(
|
||||
"PUT",
|
||||
b"directory/room/%23unofficial_test%3Atest",
|
||||
('{"room_id":"%s"}' % (room_id,)).encode('ascii'),
|
||||
)
|
||||
self.render(request)
|
||||
self.assertEquals(200, channel.code, channel.result)
|
||||
|
|
Loading…
Reference in a new issue