File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ Fix the 'Login as a user' Admin API not checking if the user exists before issuing an access token.
Original file line number Diff line number Diff line change @@ -1144,6 +1144,7 @@ def __init__(self, hs: "HomeServer"):
11441144 self .store = hs .get_datastores ().main
11451145 self .auth = hs .get_auth ()
11461146 self .auth_handler = hs .get_auth_handler ()
1147+ self .admin_handler = hs .get_admin_handler ()
11471148 self .is_mine_id = hs .is_mine_id
11481149
11491150 async def on_POST (
@@ -1158,6 +1159,12 @@ async def on_POST(
11581159 HTTPStatus .BAD_REQUEST , "Only local users can be logged in as"
11591160 )
11601161
1162+ # Validate user_id
1163+ UserID .from_string (user_id )
1164+ _user_info_dict = await self .store .get_user_by_id (user_id )
1165+ if not _user_info_dict :
1166+ raise NotFoundError ("User not found" )
1167+
11611168 body = parse_json_object_from_request (request , allow_empty_body = True )
11621169
11631170 valid_until_ms = body .get ("valid_until_ms" )
Original file line number Diff line number Diff line change @@ -4288,6 +4288,17 @@ def test_not_admin(self) -> None:
42884288
42894289 self .assertEqual (403 , channel .code , msg = channel .json_body )
42904290
4291+ def test_no_user (self ) -> None :
4292+ """Try to log in as a user that doesn't exist."""
4293+ channel = self .make_request (
4294+ "POST" ,
4295+ "/_synapse/admin/v1/users/%s/login" % urllib .parse .quote ("@ghost:test" ),
4296+ b"{}" ,
4297+ access_token = self .admin_user_tok ,
4298+ )
4299+ self .assertEqual (404 , channel .code , msg = channel .json_body )
4300+ self .assertEqual (Codes .NOT_FOUND , channel .json_body ["errcode" ])
4301+
42914302 def test_send_event (self ) -> None :
42924303 """Test that sending event as a user works."""
42934304 # Create a room.
You can’t perform that action at this time.
0 commit comments