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

Commit 8c97d58

Browse files
authored
Update MSC3083 support per changes in the MSC. (#10189)
Adds a "type" field and generalize "space" to "room_id".
1 parent fcf3c70 commit 8c97d58

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

changelog.d/10189.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update MSC3083 support for modifications in the MSC.

synapse/api/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class JoinRules:
6565
MSC3083_RESTRICTED = "restricted"
6666

6767

68+
class RestrictedJoinRuleTypes:
69+
"""Understood types for the allow rules in restricted join rules."""
70+
71+
ROOM_MEMBERSHIP = "m.room_membership"
72+
73+
6874
class LoginType:
6975
PASSWORD = "m.login.password"
7076
EMAIL_IDENTITY = "m.login.email.identity"

synapse/handlers/event_auth.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
# limitations under the License.
1414
from typing import TYPE_CHECKING, Collection, Optional
1515

16-
from synapse.api.constants import EventTypes, JoinRules, Membership
16+
from synapse.api.constants import (
17+
EventTypes,
18+
JoinRules,
19+
Membership,
20+
RestrictedJoinRuleTypes,
21+
)
1722
from synapse.api.errors import AuthError
1823
from synapse.api.room_versions import RoomVersion
1924
from synapse.events import EventBase
@@ -42,7 +47,7 @@ async def check_restricted_join_rules(
4247
Check whether a user can join a room without an invite due to restricted join rules.
4348
4449
When joining a room with restricted joined rules (as defined in MSC3083),
45-
the membership of spaces must be checked during a room join.
50+
the membership of rooms must be checked during a room join.
4651
4752
Args:
4853
state_ids: The state of the room as it currently is.
@@ -67,20 +72,20 @@ async def check_restricted_join_rules(
6772
if not await self.has_restricted_join_rules(state_ids, room_version):
6873
return
6974

70-
# Get the spaces which allow access to this room and check if the user is
75+
# Get the rooms which allow access to this room and check if the user is
7176
# in any of them.
72-
allowed_spaces = await self.get_spaces_that_allow_join(state_ids)
73-
if not await self.is_user_in_rooms(allowed_spaces, user_id):
77+
allowed_rooms = await self.get_rooms_that_allow_join(state_ids)
78+
if not await self.is_user_in_rooms(allowed_rooms, user_id):
7479
raise AuthError(
7580
403,
76-
"You do not belong to any of the required spaces to join this room.",
81+
"You do not belong to any of the required rooms to join this room.",
7782
)
7883

7984
async def has_restricted_join_rules(
8085
self, state_ids: StateMap[str], room_version: RoomVersion
8186
) -> bool:
8287
"""
83-
Return if the room has the proper join rules set for access via spaces.
88+
Return if the room has the proper join rules set for access via rooms.
8489
8590
Args:
8691
state_ids: The state of the room as it currently is.
@@ -102,17 +107,17 @@ async def has_restricted_join_rules(
102107
join_rules_event = await self._store.get_event(join_rules_event_id)
103108
return join_rules_event.content.get("join_rule") == JoinRules.MSC3083_RESTRICTED
104109

105-
async def get_spaces_that_allow_join(
110+
async def get_rooms_that_allow_join(
106111
self, state_ids: StateMap[str]
107112
) -> Collection[str]:
108113
"""
109-
Generate a list of spaces which allow access to a room.
114+
Generate a list of rooms in which membership allows access to a room.
110115
111116
Args:
112-
state_ids: The state of the room as it currently is.
117+
state_ids: The current state of the room the user wishes to join
113118
114119
Returns:
115-
A collection of spaces which provide membership to the room.
120+
A collection of room IDs. Membership in any of the rooms in the list grants the ability to join the target room.
116121
"""
117122
# If there's no join rule, then it defaults to invite (so this doesn't apply).
118123
join_rules_event_id = state_ids.get((EventTypes.JoinRules, ""), None)
@@ -123,21 +128,25 @@ async def get_spaces_that_allow_join(
123128
join_rules_event = await self._store.get_event(join_rules_event_id)
124129

125130
# If allowed is of the wrong form, then only allow invited users.
126-
allowed_spaces = join_rules_event.content.get("allow", [])
127-
if not isinstance(allowed_spaces, list):
131+
allow_list = join_rules_event.content.get("allow", [])
132+
if not isinstance(allow_list, list):
128133
return ()
129134

130135
# Pull out the other room IDs, invalid data gets filtered.
131136
result = []
132-
for space in allowed_spaces:
133-
if not isinstance(space, dict):
137+
for allow in allow_list:
138+
if not isinstance(allow, dict):
139+
continue
140+
141+
# If the type is unexpected, skip it.
142+
if allow.get("type") != RestrictedJoinRuleTypes.ROOM_MEMBERSHIP:
134143
continue
135144

136-
space_id = space.get("space")
137-
if not isinstance(space_id, str):
145+
room_id = allow.get("room_id")
146+
if not isinstance(room_id, str):
138147
continue
139148

140-
result.append(space_id)
149+
result.append(room_id)
141150

142151
return result
143152

synapse/handlers/space_summary.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ async def get_space_summary(
160160

161161
# Check if the user is a member of any of the allowed spaces
162162
# from the response.
163-
allowed_spaces = room.get("allowed_spaces")
163+
allowed_rooms = room.get("allowed_spaces")
164164
if (
165165
not include_room
166-
and allowed_spaces
167-
and isinstance(allowed_spaces, list)
166+
and allowed_rooms
167+
and isinstance(allowed_rooms, list)
168168
):
169169
include_room = await self._event_auth_handler.is_user_in_rooms(
170-
allowed_spaces, requester
170+
allowed_rooms, requester
171171
)
172172

173173
# Finally, if this isn't the requested room, check ourselves
@@ -455,11 +455,11 @@ async def _is_room_accessible(
455455
if self._event_auth_handler.has_restricted_join_rules(
456456
state_ids, room_version
457457
):
458-
allowed_spaces = (
459-
await self._event_auth_handler.get_spaces_that_allow_join(state_ids)
458+
allowed_rooms = (
459+
await self._event_auth_handler.get_rooms_that_allow_join(state_ids)
460460
)
461461
if await self._event_auth_handler.is_user_in_rooms(
462-
allowed_spaces, requester
462+
allowed_rooms, requester
463463
):
464464
return True
465465

@@ -475,10 +475,10 @@ async def _is_room_accessible(
475475
if await self._event_auth_handler.has_restricted_join_rules(
476476
state_ids, room_version
477477
):
478-
allowed_spaces = (
479-
await self._event_auth_handler.get_spaces_that_allow_join(state_ids)
478+
allowed_rooms = (
479+
await self._event_auth_handler.get_rooms_that_allow_join(state_ids)
480480
)
481-
for space_id in allowed_spaces:
481+
for space_id in allowed_rooms:
482482
if await self._auth.check_host_in_room(space_id, origin):
483483
return True
484484

@@ -512,11 +512,11 @@ async def _build_room_entry(self, room_id: str) -> JsonDict:
512512
)
513513

514514
room_version = await self._store.get_room_version(room_id)
515-
allowed_spaces = None
515+
allowed_rooms = None
516516
if await self._event_auth_handler.has_restricted_join_rules(
517517
current_state_ids, room_version
518518
):
519-
allowed_spaces = await self._event_auth_handler.get_spaces_that_allow_join(
519+
allowed_rooms = await self._event_auth_handler.get_rooms_that_allow_join(
520520
current_state_ids
521521
)
522522

@@ -533,7 +533,7 @@ async def _build_room_entry(self, room_id: str) -> JsonDict:
533533
"guest_can_join": stats["guest_access"] == "can_join",
534534
"creation_ts": create_event.origin_server_ts,
535535
"room_type": create_event.content.get(EventContentFields.ROOM_TYPE),
536-
"allowed_spaces": allowed_spaces,
536+
"allowed_spaces": allowed_rooms,
537537
}
538538

539539
# Filter out Nones – rather omit the field altogether

0 commit comments

Comments
 (0)