Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add some limitations to alias creation #5124

Merged
merged 1 commit into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5124.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add some missing limitations to room alias creation.
5 changes: 5 additions & 0 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ listeners:
# Used by phonehome stats to group together related servers.
#server_context: context

# Whether to require a user to be in the room to add an alias to it.
# Defaults to 'true'.
#
#require_membership_for_aliases: false


## TLS ##

Expand Down
11 changes: 11 additions & 0 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def read_config(self, config):
# sending out any replication updates.
self.replication_torture_level = config.get("replication_torture_level")

# Whether to require a user to be in the room to add an alias to it.
# Defaults to True.
self.require_membership_for_aliases = config.get(
"require_membership_for_aliases", True,
)

self.listeners = []
for listener in config.get("listeners", []):
if not isinstance(listener.get("port", None), int):
Expand Down Expand Up @@ -490,6 +496,11 @@ def default_config(self, server_name, data_dir_path, **kwargs):

# Used by phonehome stats to group together related servers.
#server_context: context

# Whether to require a user to be in the room to add an alias to it.
# Defaults to 'true'.
#
#require_membership_for_aliases: false
""" % locals()

def read_arguments(self, args):
Expand Down
22 changes: 21 additions & 1 deletion synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@


class DirectoryHandler(BaseHandler):
MAX_ALIAS_LENGTH = 255

def __init__(self, hs):
super(DirectoryHandler, self).__init__(hs)

self.state = hs.get_state_handler()
self.appservice_handler = hs.get_application_service_handler()
self.event_creation_handler = hs.get_event_creation_handler()
self.store = hs.get_datastore()
self.config = hs.config
self.enable_room_list_search = hs.config.enable_room_list_search
self.require_membership = hs.config.require_membership_for_aliases

self.federation = hs.get_federation_client()
hs.get_federation_registry().register_query_handler(
Expand Down Expand Up @@ -83,7 +86,7 @@ def _create_association(self, room_alias, room_id, servers=None, creator=None):

@defer.inlineCallbacks
def create_association(self, requester, room_alias, room_id, servers=None,
send_event=True):
send_event=True, check_membership=True):
"""Attempt to create a new alias

Args:
Expand All @@ -93,13 +96,22 @@ def create_association(self, requester, room_alias, room_id, servers=None,
servers (list[str]|None): List of servers that others servers
should try and join via
send_event (bool): Whether to send an updated m.room.aliases event
check_membership (bool): Whether to check if the user is in the room
before the alias can be set (if the server's config requires it).

Returns:
Deferred
"""

user_id = requester.user.to_string()

if len(room_alias.to_string()) > self.MAX_ALIAS_LENGTH:
raise SynapseError(
babolivier marked this conversation as resolved.
Show resolved Hide resolved
400,
"Can't create aliases longer than %s characters" % self.MAX_ALIAS_LENGTH,
Codes.INVALID_PARAM,
)

service = requester.app_service
if service:
if not service.is_interested_in_alias(room_alias.to_string()):
Expand All @@ -108,6 +120,14 @@ def create_association(self, requester, room_alias, room_id, servers=None,
" this kind of alias.", errcode=Codes.EXCLUSIVE
)
else:
if self.require_membership and check_membership:
rooms_for_user = yield self.store.get_rooms_for_user(user_id)
if room_id not in rooms_for_user:
raise AuthError(
403,
"You must be in the room to create an alias for it",
)

if not self.spam_checker.user_may_create_room_alias(user_id, room_alias):
raise AuthError(
403, "This user is not permitted to create this alias",
Expand Down
3 changes: 2 additions & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def _move_aliases_to_new_room(
yield directory_handler.create_association(
requester, RoomAlias.from_string(alias),
new_room_id, servers=(self.hs.hostname, ),
send_event=False,
send_event=False, check_membership=False,
)
logger.info("Moved alias %s to new room", alias)
except SynapseError as e:
Expand Down Expand Up @@ -538,6 +538,7 @@ def create_room(self, requester, config, ratelimit=True,
room_alias=room_alias,
servers=[self.hs.hostname],
send_event=False,
check_membership=False,
)

preset_config = config.get(
Expand Down