2929from synapse .api .errors import HttpResponseException
3030from synapse .handlers .pagination import PurgeStatus
3131from synapse .rest import admin
32- from synapse .rest .client import account , directory , login , profile , room
32+ from synapse .rest .client import account , directory , login , profile , room , sync
3333from synapse .types import JsonDict , RoomAlias , UserID , create_requester
3434from synapse .util .stringutils import random_string
3535
@@ -381,6 +381,8 @@ def test_leave_permissions(self):
381381class RoomsMemberListTestCase (RoomBase ):
382382 """Tests /rooms/$room_id/members/list REST events."""
383383
384+ servlets = RoomBase .servlets + [sync .register_servlets ]
385+
384386 user_id = "@sid1:red"
385387
386388 def test_get_member_list (self ):
@@ -397,6 +399,86 @@ def test_get_member_list_no_permission(self):
397399 channel = self .make_request ("GET" , "/rooms/%s/members" % room_id )
398400 self .assertEquals (403 , channel .code , msg = channel .result ["body" ])
399401
402+ def test_get_member_list_no_permission_with_at_token (self ):
403+ """
404+ Tests that a stranger to the room cannot get the member list
405+ (in the case that they use an at token).
406+ """
407+ room_id = self .helper .create_room_as ("@someone.else:red" )
408+
409+ # first sync to get an at token
410+ channel = self .make_request ("GET" , "/sync" )
411+ self .assertEquals (200 , channel .code )
412+ sync_token = channel .json_body ["next_batch" ]
413+
414+ # check that permission is denied for @sid1:red to get the
415+ # memberships of @someone.else:red's room.
416+ channel = self .make_request (
417+ "GET" ,
418+ f"/rooms/{ room_id } /members?at={ sync_token } " ,
419+ )
420+ self .assertEquals (403 , channel .code , msg = channel .result ["body" ])
421+
422+ def test_get_member_list_no_permission_former_member (self ):
423+ """
424+ Tests that a former member of the room can not get the member list.
425+ """
426+ # create a room, invite the user and the user joins
427+ room_id = self .helper .create_room_as ("@alice:red" )
428+ self .helper .invite (room_id , "@alice:red" , self .user_id )
429+ self .helper .join (room_id , self .user_id )
430+
431+ # check that the user can see the member list to start with
432+ channel = self .make_request ("GET" , "/rooms/%s/members" % room_id )
433+ self .assertEquals (200 , channel .code , msg = channel .result ["body" ])
434+
435+ # ban the user
436+ self .helper .change_membership (room_id , "@alice:red" , self .user_id , "ban" )
437+
438+ # check the user can no longer see the member list
439+ channel = self .make_request ("GET" , "/rooms/%s/members" % room_id )
440+ self .assertEquals (403 , channel .code , msg = channel .result ["body" ])
441+
442+ def test_get_member_list_no_permission_former_member_with_at_token (self ):
443+ """
444+ Tests that a former member of the room can not get the member list
445+ (in the case that they use an at token).
446+ """
447+ # create a room, invite the user and the user joins
448+ room_id = self .helper .create_room_as ("@alice:red" )
449+ self .helper .invite (room_id , "@alice:red" , self .user_id )
450+ self .helper .join (room_id , self .user_id )
451+
452+ # sync to get an at token
453+ channel = self .make_request ("GET" , "/sync" )
454+ self .assertEquals (200 , channel .code )
455+ sync_token = channel .json_body ["next_batch" ]
456+
457+ # check that the user can see the member list to start with
458+ channel = self .make_request (
459+ "GET" , "/rooms/%s/members?at=%s" % (room_id , sync_token )
460+ )
461+ self .assertEquals (200 , channel .code , msg = channel .result ["body" ])
462+
463+ # ban the user (Note: the user is actually allowed to see this event and
464+ # state so that they know they're banned!)
465+ self .helper .change_membership (room_id , "@alice:red" , self .user_id , "ban" )
466+
467+ # invite a third user and let them join
468+ self .helper .invite (room_id , "@alice:red" , "@bob:red" )
469+ self .helper .join (room_id , "@bob:red" )
470+
471+ # now, with the original user, sync again to get a new at token
472+ channel = self .make_request ("GET" , "/sync" )
473+ self .assertEquals (200 , channel .code )
474+ sync_token = channel .json_body ["next_batch" ]
475+
476+ # check the user can no longer see the updated member list
477+ channel = self .make_request (
478+ "GET" , "/rooms/%s/members?at=%s" % (room_id , sync_token )
479+ )
480+ self .assertEquals (403 , channel .code , msg = channel .result ["body" ])
481+
400482 def test_get_member_list_mixed_memberships (self ):
401483 room_creator = "@some_other_guy:red"
402484 room_id = self .helper .create_room_as (room_creator )
0 commit comments