Skip to content

Commit 0ae1f10

Browse files
committed
Update KeyUploadServlet to handle case where client sends device_keys: null (#19023)
1 parent 527e831 commit 0ae1f10

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

changelog.d/19023.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug introduced in 1.139.1 where a client could receive an Internal Server Error if they set `device_keys: null` in the request to [`POST /_matrix/client/v3/keys/upload`](https://spec.matrix.org/v1.16/client-server-api/#post_matrixclientv3keysupload).

synapse/rest/client/keys.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,21 +270,21 @@ async def on_POST(
270270
400, "To upload keys, you must pass device_id when authenticating"
271271
)
272272

273-
if "device_keys" in body:
273+
if "device_keys" in body and isinstance(body["device_keys"], dict):
274274
# Validate the provided `user_id` and `device_id` fields in
275275
# `device_keys` match that of the requesting user. We can't do
276276
# this directly in the pydantic model as we don't have access
277277
# to the requester yet.
278278
#
279279
# TODO: We could use ValidationInfo when we switch to Pydantic v2.
280280
# https://docs.pydantic.dev/latest/concepts/validators/#validation-info
281-
if body["device_keys"]["user_id"] != user_id:
281+
if body["device_keys"].get("user_id") != user_id:
282282
raise SynapseError(
283283
code=HTTPStatus.BAD_REQUEST,
284284
errcode=Codes.BAD_JSON,
285285
msg="Provided `user_id` in `device_keys` does not match that of the authenticated user",
286286
)
287-
if body["device_keys"]["device_id"] != device_id:
287+
if body["device_keys"].get("device_id") != device_id:
288288
raise SynapseError(
289289
code=HTTPStatus.BAD_REQUEST,
290290
errcode=Codes.BAD_JSON,

tests/rest/client/test_keys.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ def test_upload_keys_fails_on_invalid_user_id_or_device_id(self) -> None:
160160
channel.result,
161161
)
162162

163+
def test_upload_keys_succeeds_when_fields_are_explicitly_set_to_null(self) -> None:
164+
"""
165+
This is a regression test for https://github.com/element-hq/synapse/pull/19023.
166+
"""
167+
device_id = "DEVICE_ID"
168+
self.register_user("alice", "wonderland")
169+
alice_token = self.login("alice", "wonderland", device_id=device_id)
170+
171+
channel = self.make_request(
172+
"POST",
173+
"/_matrix/client/v3/keys/upload",
174+
{
175+
"device_keys": None,
176+
"one_time_keys": None,
177+
"fallback_keys": None,
178+
},
179+
alice_token,
180+
)
181+
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
182+
163183

164184
class KeyQueryTestCase(unittest.HomeserverTestCase):
165185
servlets = [

0 commit comments

Comments
 (0)