Skip to content

Commit 16245f0

Browse files
reivilibresandhose
andauthored
Fix the 'Login as a user' Admin API not checking if the user exists before issuing an access token. (#18518)
Fixes: #18503 --------- Signed-off-by: Olivier 'reivilibre <oliverw@matrix.org> Co-authored-by: Quentin Gliech <quenting@element.io>
1 parent 4500652 commit 16245f0

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

changelog.d/18518.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the 'Login as a user' Admin API not checking if the user exists before issuing an access token.

synapse/rest/admin/users.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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")

tests/rest/admin/test_user.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

0 commit comments

Comments
 (0)