forked from MirrorHub/synapse
Add configurable room list publishing rules
This allows specifying who and what is allowed to be published onto the public room list
This commit is contained in:
parent
c6e75c9f2d
commit
eaf4d11af9
5 changed files with 200 additions and 22 deletions
|
@ -23,70 +23,121 @@ class RoomDirectoryConfig(Config):
|
||||||
alias_creation_rules = config["alias_creation_rules"]
|
alias_creation_rules = config["alias_creation_rules"]
|
||||||
|
|
||||||
self._alias_creation_rules = [
|
self._alias_creation_rules = [
|
||||||
_AliasRule(rule)
|
_RoomDirectoryRule("alias_creation_rules", rule)
|
||||||
for rule in alias_creation_rules
|
for rule in alias_creation_rules
|
||||||
]
|
]
|
||||||
|
|
||||||
|
room_list_publication_rules = config["room_list_publication_rules"]
|
||||||
|
|
||||||
|
self._room_list_publication_rules = [
|
||||||
|
_RoomDirectoryRule("room_list_publication_rules", rule)
|
||||||
|
for rule in room_list_publication_rules
|
||||||
|
]
|
||||||
|
|
||||||
def default_config(self, config_dir_path, server_name, **kwargs):
|
def default_config(self, config_dir_path, server_name, **kwargs):
|
||||||
return """
|
return """
|
||||||
# The `alias_creation` option controls who's allowed to create aliases
|
# The `alias_creation` option controls who's allowed to create aliases
|
||||||
# on this server.
|
# on this server.
|
||||||
#
|
#
|
||||||
# The format of this option is a list of rules that contain globs that
|
# 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
|
# match against user_id, room_id and the new alias (fully qualified with
|
||||||
# name). The action in the first rule that matches is taken, which can
|
# server name). The action in the first rule that matches is taken,
|
||||||
# currently either be "allow" or "deny".
|
# which can currently either be "allow" or "deny".
|
||||||
|
#
|
||||||
|
# Missing user_id/room_id/alias fields default to "*".
|
||||||
#
|
#
|
||||||
# If no rules match the request is denied.
|
# If no rules match the request is denied.
|
||||||
alias_creation_rules:
|
alias_creation_rules:
|
||||||
- user_id: "*"
|
- user_id: "*"
|
||||||
alias: "*"
|
alias: "*" # This matches alias being created
|
||||||
|
room_id: "*"
|
||||||
|
action: allow
|
||||||
|
|
||||||
|
# The `room_list_publication_rules` option control who and what can be
|
||||||
|
# published in the public room list.
|
||||||
|
#
|
||||||
|
# The format of this option is the same as that for
|
||||||
|
# `alias_creation_rules`
|
||||||
|
room_list_publication_rules:
|
||||||
|
- user_id: "*"
|
||||||
|
alias: "*" # This matches any local or canonical alias
|
||||||
|
# associated with the room
|
||||||
|
room_id: "*"
|
||||||
action: allow
|
action: allow
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def is_alias_creation_allowed(self, user_id, alias):
|
def is_alias_creation_allowed(self, user_id, room_id, alias):
|
||||||
"""Checks if the given user is allowed to create the given alias
|
"""Checks if the given user is allowed to create the given alias
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (str)
|
user_id (str)
|
||||||
|
room_id (str)
|
||||||
alias (str)
|
alias (str)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
boolean: True if user is allowed to crate the alias
|
boolean: True if user is allowed to crate the alias
|
||||||
"""
|
"""
|
||||||
for rule in self._alias_creation_rules:
|
for rule in self._alias_creation_rules:
|
||||||
if rule.matches(user_id, alias):
|
if rule.matches(user_id, room_id, [alias]):
|
||||||
|
return rule.action == "allow"
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def is_publishing_room_allowed(self, user_id, room_id, aliases):
|
||||||
|
"""Checks if the given user is allowed to publish the room
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_id (str)
|
||||||
|
room_id (str)
|
||||||
|
aliases (list[str]): any local aliases associated with the room
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
boolean: True if user can publish room
|
||||||
|
"""
|
||||||
|
for rule in self._room_list_publication_rules:
|
||||||
|
if rule.matches(user_id, room_id, aliases):
|
||||||
return rule.action == "allow"
|
return rule.action == "allow"
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class _AliasRule(object):
|
class _RoomDirectoryRule(object):
|
||||||
def __init__(self, rule):
|
"""Helper class to test whether a room directory action is allowed, like
|
||||||
|
creating an alias or publishing a room.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, option_name, rule):
|
||||||
action = rule["action"]
|
action = rule["action"]
|
||||||
user_id = rule["user_id"]
|
user_id = rule.get("user_id", "*")
|
||||||
alias = rule["alias"]
|
room_id = rule.get("room_id", "*")
|
||||||
|
alias = rule.get("alias", "*")
|
||||||
|
|
||||||
if action in ("allow", "deny"):
|
if action in ("allow", "deny"):
|
||||||
self.action = action
|
self.action = action
|
||||||
else:
|
else:
|
||||||
raise ConfigError(
|
raise ConfigError(
|
||||||
"alias_creation_rules rules can only have action of 'allow'"
|
"%s rules can only have action of 'allow'"
|
||||||
" or 'deny'"
|
" or 'deny'" % (option_name,)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self._alias_matches_all = alias == "*"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._user_id_regex = glob_to_regex(user_id)
|
self._user_id_regex = glob_to_regex(user_id)
|
||||||
self._alias_regex = glob_to_regex(alias)
|
self._alias_regex = glob_to_regex(alias)
|
||||||
|
self._room_id_regex = glob_to_regex(room_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ConfigError("Failed to parse glob into regex: %s", e)
|
raise ConfigError("Failed to parse glob into regex: %s", e)
|
||||||
|
|
||||||
def matches(self, user_id, alias):
|
def matches(self, user_id, room_id, aliases):
|
||||||
"""Tests if this rule matches the given user_id and alias.
|
"""Tests if this rule matches the given user_id, room_id and aliases.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (str)
|
user_id (str)
|
||||||
alias (str)
|
room_id (str)
|
||||||
|
aliases (list[str]): The associated aliases to the room. Will be a
|
||||||
|
single element for testing alias creation, and can be empty for
|
||||||
|
testing room publishing.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
boolean
|
boolean
|
||||||
|
@ -96,7 +147,16 @@ class _AliasRule(object):
|
||||||
if not self._user_id_regex.match(user_id):
|
if not self._user_id_regex.match(user_id):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not self._alias_regex.match(alias):
|
# If we are not given any aliases then this rule only matches if the
|
||||||
|
# alias glob matches all aliases
|
||||||
|
if not aliases and not self._alias_matches_all:
|
||||||
|
return False
|
||||||
|
|
||||||
|
for alias in aliases:
|
||||||
|
if not self._alias_regex.match(alias):
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not self._room_id_regex.match(room_id):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -112,7 +112,9 @@ class DirectoryHandler(BaseHandler):
|
||||||
403, "This user is not permitted to create this alias",
|
403, "This user is not permitted to create this alias",
|
||||||
)
|
)
|
||||||
|
|
||||||
if not self.config.is_alias_creation_allowed(user_id, room_alias.to_string()):
|
if not self.config.is_alias_creation_allowed(
|
||||||
|
user_id, room_id, room_alias.to_string(),
|
||||||
|
):
|
||||||
# Lets just return a generic message, as there may be all sorts of
|
# Lets just return a generic message, as there may be all sorts of
|
||||||
# reasons why we said no. TODO: Allow configurable error messages
|
# reasons why we said no. TODO: Allow configurable error messages
|
||||||
# per alias creation rule?
|
# per alias creation rule?
|
||||||
|
@ -395,9 +397,9 @@ class DirectoryHandler(BaseHandler):
|
||||||
room_id (str)
|
room_id (str)
|
||||||
visibility (str): "public" or "private"
|
visibility (str): "public" or "private"
|
||||||
"""
|
"""
|
||||||
if not self.spam_checker.user_may_publish_room(
|
user_id = requester.user.to_string()
|
||||||
requester.user.to_string(), room_id
|
|
||||||
):
|
if not self.spam_checker.user_may_publish_room(user_id, room_id):
|
||||||
raise AuthError(
|
raise AuthError(
|
||||||
403,
|
403,
|
||||||
"This user is not permitted to publish rooms to the room list"
|
"This user is not permitted to publish rooms to the room list"
|
||||||
|
@ -415,7 +417,24 @@ class DirectoryHandler(BaseHandler):
|
||||||
|
|
||||||
yield self.auth.check_can_change_room_list(room_id, requester.user)
|
yield self.auth.check_can_change_room_list(room_id, requester.user)
|
||||||
|
|
||||||
yield self.store.set_room_is_public(room_id, visibility == "public")
|
room_aliases = yield self.store.get_aliases_for_room(room_id)
|
||||||
|
canonical_alias = yield self.store.get_canonical_alias_for_room(room_id)
|
||||||
|
if canonical_alias:
|
||||||
|
room_aliases.append(canonical_alias)
|
||||||
|
|
||||||
|
making_public = visibility == "public"
|
||||||
|
|
||||||
|
if making_public and not self.config.is_publishing_room_allowed(
|
||||||
|
user_id, room_id, room_aliases,
|
||||||
|
):
|
||||||
|
# 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 publish room",
|
||||||
|
)
|
||||||
|
|
||||||
|
yield self.store.set_room_is_public(room_id, making_public)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def edit_published_appservice_room_list(self, appservice_id, network_id,
|
def edit_published_appservice_room_list(self, appservice_id, network_id,
|
||||||
|
|
|
@ -548,6 +548,31 @@ class StateGroupWorkerStore(EventsWorkerStore, SQLBaseStore):
|
||||||
_get_filtered_current_state_ids_txn,
|
_get_filtered_current_state_ids_txn,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def get_canonical_alias_for_room(self, room_id):
|
||||||
|
"""Get canonical alias for room, if any
|
||||||
|
|
||||||
|
Args:
|
||||||
|
room_id (str)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Deferred[str|None]: The canonical alias, if any
|
||||||
|
"""
|
||||||
|
|
||||||
|
state = yield self.get_filtered_current_state_ids(room_id, StateFilter.from_types(
|
||||||
|
[(EventTypes.CanonicalAlias, "")]
|
||||||
|
))
|
||||||
|
|
||||||
|
event_id = state.get((EventTypes.CanonicalAlias, ""))
|
||||||
|
if not event_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
event = yield self.get_event(event_id, allow_none=True)
|
||||||
|
if not event:
|
||||||
|
return
|
||||||
|
|
||||||
|
defer.returnValue(event.content.get("canonical_alias"))
|
||||||
|
|
||||||
@cached(max_entries=10000, iterable=True)
|
@cached(max_entries=10000, iterable=True)
|
||||||
def get_state_group_delta(self, state_group):
|
def get_state_group_delta(self, state_group):
|
||||||
"""Given a state group try to return a previous group and a delta between
|
"""Given a state group try to return a previous group and a delta between
|
||||||
|
|
|
@ -36,6 +36,8 @@ class RoomDirectoryConfigTestCase(unittest.TestCase):
|
||||||
- user_id: "@gah:example.com"
|
- user_id: "@gah:example.com"
|
||||||
alias: "#goo:example.com"
|
alias: "#goo:example.com"
|
||||||
action: "allow"
|
action: "allow"
|
||||||
|
|
||||||
|
room_list_publication_rules: []
|
||||||
""")
|
""")
|
||||||
|
|
||||||
rd_config = RoomDirectoryConfig()
|
rd_config = RoomDirectoryConfig()
|
||||||
|
@ -43,25 +45,96 @@ class RoomDirectoryConfigTestCase(unittest.TestCase):
|
||||||
|
|
||||||
self.assertFalse(rd_config.is_alias_creation_allowed(
|
self.assertFalse(rd_config.is_alias_creation_allowed(
|
||||||
user_id="@bob:example.com",
|
user_id="@bob:example.com",
|
||||||
|
room_id="!test",
|
||||||
alias="#test:example.com",
|
alias="#test:example.com",
|
||||||
))
|
))
|
||||||
|
|
||||||
self.assertTrue(rd_config.is_alias_creation_allowed(
|
self.assertTrue(rd_config.is_alias_creation_allowed(
|
||||||
user_id="@test:example.com",
|
user_id="@test:example.com",
|
||||||
|
room_id="!test",
|
||||||
alias="#unofficial_st:example.com",
|
alias="#unofficial_st:example.com",
|
||||||
))
|
))
|
||||||
|
|
||||||
self.assertTrue(rd_config.is_alias_creation_allowed(
|
self.assertTrue(rd_config.is_alias_creation_allowed(
|
||||||
user_id="@foobar:example.com",
|
user_id="@foobar:example.com",
|
||||||
|
room_id="!test",
|
||||||
alias="#test:example.com",
|
alias="#test:example.com",
|
||||||
))
|
))
|
||||||
|
|
||||||
self.assertTrue(rd_config.is_alias_creation_allowed(
|
self.assertTrue(rd_config.is_alias_creation_allowed(
|
||||||
user_id="@gah:example.com",
|
user_id="@gah:example.com",
|
||||||
|
room_id="!test",
|
||||||
alias="#goo:example.com",
|
alias="#goo:example.com",
|
||||||
))
|
))
|
||||||
|
|
||||||
self.assertFalse(rd_config.is_alias_creation_allowed(
|
self.assertFalse(rd_config.is_alias_creation_allowed(
|
||||||
user_id="@test:example.com",
|
user_id="@test:example.com",
|
||||||
|
room_id="!test",
|
||||||
alias="#test:example.com",
|
alias="#test:example.com",
|
||||||
))
|
))
|
||||||
|
|
||||||
|
def test_room_publish_acl(self):
|
||||||
|
config = yaml.load("""
|
||||||
|
alias_creation_rules: []
|
||||||
|
|
||||||
|
room_list_publication_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"
|
||||||
|
- room_id: "!test-deny"
|
||||||
|
action: "deny"
|
||||||
|
""")
|
||||||
|
|
||||||
|
rd_config = RoomDirectoryConfig()
|
||||||
|
rd_config.read_config(config)
|
||||||
|
|
||||||
|
self.assertFalse(rd_config.is_publishing_room_allowed(
|
||||||
|
user_id="@bob:example.com",
|
||||||
|
room_id="!test",
|
||||||
|
aliases=["#test:example.com"],
|
||||||
|
))
|
||||||
|
|
||||||
|
self.assertTrue(rd_config.is_publishing_room_allowed(
|
||||||
|
user_id="@test:example.com",
|
||||||
|
room_id="!test",
|
||||||
|
aliases=["#unofficial_st:example.com"],
|
||||||
|
))
|
||||||
|
|
||||||
|
self.assertTrue(rd_config.is_publishing_room_allowed(
|
||||||
|
user_id="@foobar:example.com",
|
||||||
|
room_id="!test",
|
||||||
|
aliases=[],
|
||||||
|
))
|
||||||
|
|
||||||
|
self.assertTrue(rd_config.is_publishing_room_allowed(
|
||||||
|
user_id="@gah:example.com",
|
||||||
|
room_id="!test",
|
||||||
|
aliases=["#goo:example.com"],
|
||||||
|
))
|
||||||
|
|
||||||
|
self.assertFalse(rd_config.is_publishing_room_allowed(
|
||||||
|
user_id="@test:example.com",
|
||||||
|
room_id="!test",
|
||||||
|
aliases=["#test:example.com"],
|
||||||
|
))
|
||||||
|
|
||||||
|
self.assertTrue(rd_config.is_publishing_room_allowed(
|
||||||
|
user_id="@foobar:example.com",
|
||||||
|
room_id="!test-deny",
|
||||||
|
aliases=[],
|
||||||
|
))
|
||||||
|
|
||||||
|
self.assertFalse(rd_config.is_publishing_room_allowed(
|
||||||
|
user_id="@gah:example.com",
|
||||||
|
room_id="!test-deny",
|
||||||
|
aliases=[],
|
||||||
|
))
|
||||||
|
|
|
@ -121,6 +121,7 @@ class TestCreateAliasACL(unittest.HomeserverTestCase):
|
||||||
"action": "allow",
|
"action": "allow",
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
config["room_list_publication_rules"] = []
|
||||||
|
|
||||||
rd_config = RoomDirectoryConfig()
|
rd_config = RoomDirectoryConfig()
|
||||||
rd_config.read_config(config)
|
rd_config.read_config(config)
|
||||||
|
|
Loading…
Reference in a new issue