2021-04-23 07:05:51 -04:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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.
|
2021-05-20 11:10:36 -04:00
|
|
|
from typing import TYPE_CHECKING, Collection, Optional
|
2021-04-23 07:05:51 -04:00
|
|
|
|
2021-06-17 12:53:27 -04:00
|
|
|
from synapse.api.constants import (
|
|
|
|
EventTypes,
|
|
|
|
JoinRules,
|
|
|
|
Membership,
|
|
|
|
RestrictedJoinRuleTypes,
|
|
|
|
)
|
2021-05-18 12:17:04 -04:00
|
|
|
from synapse.api.errors import AuthError
|
2021-04-23 07:05:51 -04:00
|
|
|
from synapse.api.room_versions import RoomVersion
|
2021-05-18 12:17:04 -04:00
|
|
|
from synapse.events import EventBase
|
2021-04-23 07:05:51 -04:00
|
|
|
from synapse.types import StateMap
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
|
|
|
|
class EventAuthHandler:
|
|
|
|
"""
|
|
|
|
This class contains methods for authenticating events added to room graphs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._store = hs.get_datastore()
|
|
|
|
|
2021-05-18 12:17:04 -04:00
|
|
|
async def check_restricted_join_rules(
|
|
|
|
self,
|
|
|
|
state_ids: StateMap[str],
|
|
|
|
room_version: RoomVersion,
|
|
|
|
user_id: str,
|
|
|
|
prev_member_event: Optional[EventBase],
|
|
|
|
) -> None:
|
2021-04-23 07:05:51 -04:00
|
|
|
"""
|
2021-05-18 12:17:04 -04:00
|
|
|
Check whether a user can join a room without an invite due to restricted join rules.
|
2021-04-23 07:05:51 -04:00
|
|
|
|
|
|
|
When joining a room with restricted joined rules (as defined in MSC3083),
|
2021-06-17 12:53:27 -04:00
|
|
|
the membership of rooms must be checked during a room join.
|
2021-04-23 07:05:51 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
state_ids: The state of the room as it currently is.
|
|
|
|
room_version: The room version of the room being joined.
|
|
|
|
user_id: The user joining the room.
|
2021-05-18 12:17:04 -04:00
|
|
|
prev_member_event: The current membership event for this user.
|
2021-04-23 07:05:51 -04:00
|
|
|
|
2021-05-18 12:17:04 -04:00
|
|
|
Raises:
|
|
|
|
AuthError if the user cannot join the room.
|
2021-04-23 07:05:51 -04:00
|
|
|
"""
|
2021-05-18 12:17:04 -04:00
|
|
|
# If the member is invited or currently joined, then nothing to do.
|
|
|
|
if prev_member_event and (
|
|
|
|
prev_member_event.membership in (Membership.JOIN, Membership.INVITE)
|
|
|
|
):
|
|
|
|
return
|
|
|
|
|
2021-05-20 11:10:36 -04:00
|
|
|
# This is not a room with a restricted join rule, so we don't need to do the
|
|
|
|
# restricted room specific checks.
|
|
|
|
#
|
|
|
|
# Note: We'll be applying the standard join rule checks later, which will
|
|
|
|
# catch the cases of e.g. trying to join private rooms without an invite.
|
|
|
|
if not await self.has_restricted_join_rules(state_ids, room_version):
|
|
|
|
return
|
|
|
|
|
2021-06-17 12:53:27 -04:00
|
|
|
# Get the rooms which allow access to this room and check if the user is
|
2021-05-20 11:10:36 -04:00
|
|
|
# in any of them.
|
2021-06-17 12:53:27 -04:00
|
|
|
allowed_rooms = await self.get_rooms_that_allow_join(state_ids)
|
|
|
|
if not await self.is_user_in_rooms(allowed_rooms, user_id):
|
2021-05-20 11:10:36 -04:00
|
|
|
raise AuthError(
|
|
|
|
403,
|
2021-06-17 12:53:27 -04:00
|
|
|
"You do not belong to any of the required rooms to join this room.",
|
2021-05-20 11:10:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
async def has_restricted_join_rules(
|
|
|
|
self, state_ids: StateMap[str], room_version: RoomVersion
|
|
|
|
) -> bool:
|
|
|
|
"""
|
2021-06-17 12:53:27 -04:00
|
|
|
Return if the room has the proper join rules set for access via rooms.
|
2021-05-20 11:10:36 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
state_ids: The state of the room as it currently is.
|
|
|
|
room_version: The room version of the room to query.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the proper room version and join rules are set for restricted access.
|
|
|
|
"""
|
2021-04-23 07:05:51 -04:00
|
|
|
# This only applies to room versions which support the new join rule.
|
|
|
|
if not room_version.msc3083_join_rules:
|
2021-05-20 11:10:36 -04:00
|
|
|
return False
|
2021-04-23 07:05:51 -04:00
|
|
|
|
|
|
|
# If there's no join rule, then it defaults to invite (so this doesn't apply).
|
|
|
|
join_rules_event_id = state_ids.get((EventTypes.JoinRules, ""), None)
|
|
|
|
if not join_rules_event_id:
|
2021-05-20 11:10:36 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
# If the join rule is not restricted, this doesn't apply.
|
|
|
|
join_rules_event = await self._store.get_event(join_rules_event_id)
|
|
|
|
return join_rules_event.content.get("join_rule") == JoinRules.MSC3083_RESTRICTED
|
|
|
|
|
2021-06-17 12:53:27 -04:00
|
|
|
async def get_rooms_that_allow_join(
|
2021-05-20 11:10:36 -04:00
|
|
|
self, state_ids: StateMap[str]
|
|
|
|
) -> Collection[str]:
|
|
|
|
"""
|
2021-06-17 12:53:27 -04:00
|
|
|
Generate a list of rooms in which membership allows access to a room.
|
2021-05-20 11:10:36 -04:00
|
|
|
|
|
|
|
Args:
|
2021-06-17 12:53:27 -04:00
|
|
|
state_ids: The current state of the room the user wishes to join
|
2021-05-20 11:10:36 -04:00
|
|
|
|
|
|
|
Returns:
|
2021-06-17 12:53:27 -04:00
|
|
|
A collection of room IDs. Membership in any of the rooms in the list grants the ability to join the target room.
|
2021-05-20 11:10:36 -04:00
|
|
|
"""
|
|
|
|
# If there's no join rule, then it defaults to invite (so this doesn't apply).
|
|
|
|
join_rules_event_id = state_ids.get((EventTypes.JoinRules, ""), None)
|
|
|
|
if not join_rules_event_id:
|
|
|
|
return ()
|
2021-04-23 07:05:51 -04:00
|
|
|
|
|
|
|
# If the join rule is not restricted, this doesn't apply.
|
|
|
|
join_rules_event = await self._store.get_event(join_rules_event_id)
|
|
|
|
|
|
|
|
# If allowed is of the wrong form, then only allow invited users.
|
2021-06-17 12:53:27 -04:00
|
|
|
allow_list = join_rules_event.content.get("allow", [])
|
|
|
|
if not isinstance(allow_list, list):
|
2021-05-20 11:10:36 -04:00
|
|
|
return ()
|
2021-04-23 07:05:51 -04:00
|
|
|
|
|
|
|
# Pull out the other room IDs, invalid data gets filtered.
|
2021-05-20 11:10:36 -04:00
|
|
|
result = []
|
2021-06-17 12:53:27 -04:00
|
|
|
for allow in allow_list:
|
|
|
|
if not isinstance(allow, dict):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# If the type is unexpected, skip it.
|
|
|
|
if allow.get("type") != RestrictedJoinRuleTypes.ROOM_MEMBERSHIP:
|
2021-04-23 07:05:51 -04:00
|
|
|
continue
|
|
|
|
|
2021-06-17 12:53:27 -04:00
|
|
|
room_id = allow.get("room_id")
|
|
|
|
if not isinstance(room_id, str):
|
2021-04-23 07:05:51 -04:00
|
|
|
continue
|
|
|
|
|
2021-06-17 12:53:27 -04:00
|
|
|
result.append(room_id)
|
2021-05-20 11:10:36 -04:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
async def is_user_in_rooms(self, room_ids: Collection[str], user_id: str) -> bool:
|
|
|
|
"""
|
|
|
|
Check whether a user is a member of any of the provided rooms.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_ids: The rooms to check for membership.
|
|
|
|
user_id: The user to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the user is in any of the rooms, false otherwise.
|
|
|
|
"""
|
|
|
|
if not room_ids:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Get the list of joined rooms and see if there's an overlap.
|
|
|
|
joined_rooms = await self._store.get_rooms_for_user(user_id)
|
|
|
|
|
|
|
|
# Check each room and see if the user is in it.
|
|
|
|
for room_id in room_ids:
|
|
|
|
if room_id in joined_rooms:
|
|
|
|
return True
|
2021-04-23 07:05:51 -04:00
|
|
|
|
2021-05-20 11:10:36 -04:00
|
|
|
# The user was not in any of the rooms.
|
|
|
|
return False
|