This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow a user who could join a restricted room to see it in spaces summary. #9922
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bce96c2
Allow a user who could join a restricted room to see it in spaces sum…
clokep 0a881ac
Check if the origin server can access a room when queried over federa…
clokep 45ad823
Check if an origin server has access to any spaces for a restricted r…
clokep 5225ef1
Do not revisit any of the rooms received over federation.
clokep 31bb2ad
Fix filtering for world readable rooms.
clokep 417aa6c
Abstract out a helper for checking if a space is in a list.
clokep 36d3932
Return the allowed spaces over federation and filter on it.
clokep 9157200
Remove unused variable.
clokep c97917b
Fix typo.
clokep ce8efa9
Split the method in two.
clokep 83974e8
Further split the methods.
clokep 3bd3fe0
Merge branch 'clokep/check-restricted-join-rules-2' into clokep/restr…
clokep 225e06d
Clarify comment.
clokep 7c28513
Clarify comments about when a room can be included.
clokep 7c0ec1a
Fix-up some type hints (and fix a bug about processed rooms).
clokep 4888f02
Further split the join rules checks.
clokep 9ba347b
Fix typo.
clokep fe1eb18
Remove some duplicated code.
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Experimental support to allow a user who could join a restricted room to view it in the spaces summary. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
# 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. | ||
from typing import TYPE_CHECKING, Optional | ||
from typing import TYPE_CHECKING, Collection, Optional | ||
|
||
from synapse.api.constants import EventTypes, JoinRules, Membership | ||
from synapse.api.errors import AuthError | ||
|
@@ -59,32 +59,76 @@ async def check_restricted_join_rules( | |
): | ||
return | ||
|
||
# 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 | ||
|
||
# Get the spaces which allow access to this room and check if the user is | ||
# in any of them. | ||
allowed_spaces = await self.get_spaces_that_allow_join(state_ids) | ||
if not await self.is_user_in_rooms(allowed_spaces, user_id): | ||
raise AuthError( | ||
403, | ||
"You do not belong to any of the required spaces to join this room.", | ||
) | ||
|
||
async def has_restricted_join_rules( | ||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, state_ids: StateMap[str], room_version: RoomVersion | ||
) -> bool: | ||
""" | ||
Return if the room has the proper join rules set for access via spaces. | ||
|
||
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. | ||
""" | ||
# This only applies to room versions which support the new join rule. | ||
if not room_version.msc3083_join_rules: | ||
return | ||
return False | ||
|
||
# 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 | ||
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 | ||
|
||
async def get_spaces_that_allow_join( | ||
self, state_ids: StateMap[str] | ||
) -> Collection[str]: | ||
""" | ||
Generate a list of spaces which allow access to a room. | ||
|
||
Args: | ||
state_ids: The state of the room as it currently is. | ||
|
||
Returns: | ||
A collection of spaces which provide membership to the room. | ||
""" | ||
# 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 () | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be tempted to also do a check of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we'll do the check twice for every caller? 😕 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh, true. IDK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of now we end up re-fetching the join rules event twice too which is unfortunate. |
||
|
||
# If the join rule is not restricted, this doesn't apply. | ||
join_rules_event = await self._store.get_event(join_rules_event_id) | ||
if join_rules_event.content.get("join_rule") != JoinRules.MSC3083_RESTRICTED: | ||
return | ||
|
||
# If allowed is of the wrong form, then only allow invited users. | ||
allowed_spaces = join_rules_event.content.get("allow", []) | ||
if not isinstance(allowed_spaces, list): | ||
allowed_spaces = () | ||
|
||
# Get the list of joined rooms and see if there's an overlap. | ||
if allowed_spaces: | ||
joined_rooms = await self._store.get_rooms_for_user(user_id) | ||
else: | ||
joined_rooms = () | ||
return () | ||
|
||
# Pull out the other room IDs, invalid data gets filtered. | ||
result = [] | ||
for space in allowed_spaces: | ||
if not isinstance(space, dict): | ||
continue | ||
|
@@ -93,13 +137,31 @@ async def check_restricted_join_rules( | |
if not isinstance(space_id, str): | ||
continue | ||
|
||
# The user was joined to one of the spaces specified, they can join | ||
# this room! | ||
if space_id in joined_rooms: | ||
return | ||
result.append(space_id) | ||
|
||
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 | ||
|
||
# The user was not in any of the required spaces. | ||
raise AuthError( | ||
403, | ||
"You do not belong to any of the required spaces to join this room.", | ||
) | ||
# The user was not in any of the rooms. | ||
return False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.