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

Commit 8c75b62

Browse files
Ensure 'deactivated' parameter is a boolean on user admin API, Fix error handling of call to deactivate user (#6990)
1 parent c1156d3 commit 8c75b62

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

changelog.d/6990.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent user from setting 'deactivated' to anything other than a bool on the v2 PUT /users Admin API.

synapse/rest/admin/users.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,16 @@ async def on_PUT(self, request, user_id):
228228
)
229229

230230
if "deactivated" in body:
231-
deactivate = bool(body["deactivated"])
231+
deactivate = body["deactivated"]
232+
if not isinstance(deactivate, bool):
233+
raise SynapseError(
234+
400, "'deactivated' parameter is not of type boolean"
235+
)
236+
232237
if deactivate and not user["deactivated"]:
233-
result = await self.deactivate_account_handler.deactivate_account(
238+
await self.deactivate_account_handler.deactivate_account(
234239
target_user.to_string(), False
235240
)
236-
if not result:
237-
raise SynapseError(500, "Could not deactivate user")
238241

239242
user = await self.admin_handler.get_user(target_user)
240243
return 200, user

synapse/rest/client/v1/login.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ def complete_sso_login(
599599
redirect_url = self._add_login_token_to_redirect_url(
600600
client_redirect_url, login_token
601601
)
602+
# Load page
602603
request.redirect(redirect_url)
603604
finish_request(request)
604605

tests/rest/admin/test_user.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,62 @@ def test_requester_is_admin(self):
507507
self.assertEqual(1, channel.json_body["admin"])
508508
self.assertEqual(0, channel.json_body["is_guest"])
509509
self.assertEqual(1, channel.json_body["deactivated"])
510+
511+
def test_accidental_deactivation_prevention(self):
512+
"""
513+
Ensure an account can't accidentally be deactivated by using a str value
514+
for the deactivated body parameter
515+
"""
516+
self.hs.config.registration_shared_secret = None
517+
518+
# Create user
519+
body = json.dumps({"password": "abc123"})
520+
521+
request, channel = self.make_request(
522+
"PUT",
523+
self.url,
524+
access_token=self.admin_user_tok,
525+
content=body.encode(encoding="utf_8"),
526+
)
527+
self.render(request)
528+
529+
self.assertEqual(201, int(channel.result["code"]), msg=channel.result["body"])
530+
self.assertEqual("@bob:test", channel.json_body["name"])
531+
self.assertEqual("bob", channel.json_body["displayname"])
532+
533+
# Get user
534+
request, channel = self.make_request(
535+
"GET", self.url, access_token=self.admin_user_tok,
536+
)
537+
self.render(request)
538+
539+
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
540+
self.assertEqual("@bob:test", channel.json_body["name"])
541+
self.assertEqual("bob", channel.json_body["displayname"])
542+
self.assertEqual(0, channel.json_body["deactivated"])
543+
544+
# Change password (and use a str for deactivate instead of a bool)
545+
body = json.dumps({"password": "abc123", "deactivated": "false"}) # oops!
546+
547+
request, channel = self.make_request(
548+
"PUT",
549+
self.url,
550+
access_token=self.admin_user_tok,
551+
content=body.encode(encoding="utf_8"),
552+
)
553+
self.render(request)
554+
555+
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
556+
557+
# Check user is not deactivated
558+
request, channel = self.make_request(
559+
"GET", self.url, access_token=self.admin_user_tok,
560+
)
561+
self.render(request)
562+
563+
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
564+
self.assertEqual("@bob:test", channel.json_body["name"])
565+
self.assertEqual("bob", channel.json_body["displayname"])
566+
567+
# Ensure they're still alive
568+
self.assertEqual(0, channel.json_body["deactivated"])

0 commit comments

Comments
 (0)