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

Commit cc51aaa

Browse files
authored
Check for space membership during a remote join of a restricted room. (#9763)
When receiving a /send_join request for a room with join rules set to 'restricted', check if the user is a member of the spaces defined in the 'allow' key of the join rules. This only applies to an experimental room version, as defined in MSC3083.
1 parent 05e8c70 commit cc51aaa

File tree

7 files changed

+238
-131
lines changed

7 files changed

+238
-131
lines changed

changelog.d/9763.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update experimental support for [MSC3083](https://github.com/matrix-org/matrix-doc/pull/3083): restricting room access via group membership.

changelog.d/9800.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update experimental support for [MSC3083](https://github.com/matrix-org/matrix-doc/pull/3083): restricting room access via group membership.

synapse/handlers/event_auth.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2021 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from typing import TYPE_CHECKING
15+
16+
from synapse.api.constants import EventTypes, JoinRules
17+
from synapse.api.room_versions import RoomVersion
18+
from synapse.types import StateMap
19+
20+
if TYPE_CHECKING:
21+
from synapse.server import HomeServer
22+
23+
24+
class EventAuthHandler:
25+
def __init__(self, hs: "HomeServer"):
26+
self._store = hs.get_datastore()
27+
28+
async def can_join_without_invite(
29+
self, state_ids: StateMap[str], room_version: RoomVersion, user_id: str
30+
) -> bool:
31+
"""
32+
Check whether a user can join a room without an invite.
33+
34+
When joining a room with restricted joined rules (as defined in MSC3083),
35+
the membership of spaces must be checked during join.
36+
37+
Args:
38+
state_ids: The state of the room as it currently is.
39+
room_version: The room version of the room being joined.
40+
user_id: The user joining the room.
41+
42+
Returns:
43+
True if the user can join the room, false otherwise.
44+
"""
45+
# This only applies to room versions which support the new join rule.
46+
if not room_version.msc3083_join_rules:
47+
return True
48+
49+
# If there's no join rule, then it defaults to public (so this doesn't apply).
50+
join_rules_event_id = state_ids.get((EventTypes.JoinRules, ""), None)
51+
if not join_rules_event_id:
52+
return True
53+
54+
# If the join rule is not restricted, this doesn't apply.
55+
join_rules_event = await self._store.get_event(join_rules_event_id)
56+
if join_rules_event.content.get("join_rule") != JoinRules.MSC3083_RESTRICTED:
57+
return True
58+
59+
# If allowed is of the wrong form, then only allow invited users.
60+
allowed_spaces = join_rules_event.content.get("allow", [])
61+
if not isinstance(allowed_spaces, list):
62+
return False
63+
64+
# Get the list of joined rooms and see if there's an overlap.
65+
joined_rooms = await self._store.get_rooms_for_user(user_id)
66+
67+
# Pull out the other room IDs, invalid data gets filtered.
68+
for space in allowed_spaces:
69+
if not isinstance(space, dict):
70+
continue
71+
72+
space_id = space.get("space")
73+
if not isinstance(space_id, str):
74+
continue
75+
76+
# The user was joined to one of the spaces specified, they can join
77+
# this room!
78+
if space_id in joined_rooms:
79+
return True
80+
81+
# The user was not in any of the required spaces.
82+
return False

0 commit comments

Comments
 (0)