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

Commit 37ca592

Browse files
authored
Create function to check for long names in devices (#8364)
* Create a new function to verify that the length of a device name is under a certain threshold. * Refactor old code and tests to use said function. * Verify device name length during registration of device * Add a test for the above Signed-off-by: Dionysis Grigoropoulos <[email protected]>
1 parent 4f3096d commit 37ca592

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

changelog.d/8364.bugfix

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug where during device registration the length of the device name wasn't
2+
limited.

synapse/handlers/device.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from synapse.api import errors
2121
from synapse.api.constants import EventTypes
2222
from synapse.api.errors import (
23+
Codes,
2324
FederationDeniedError,
2425
HttpResponseException,
2526
RequestSendFailed,
@@ -265,6 +266,24 @@ def __init__(self, hs):
265266

266267
hs.get_distributor().observe("user_left_room", self.user_left_room)
267268

269+
def _check_device_name_length(self, name: str):
270+
"""
271+
Checks whether a device name is longer than the maximum allowed length.
272+
273+
Args:
274+
name: The name of the device.
275+
276+
Raises:
277+
SynapseError: if the device name is too long.
278+
"""
279+
if name and len(name) > MAX_DEVICE_DISPLAY_NAME_LEN:
280+
raise SynapseError(
281+
400,
282+
"Device display name is too long (max %i)"
283+
% (MAX_DEVICE_DISPLAY_NAME_LEN,),
284+
errcode=Codes.TOO_LARGE,
285+
)
286+
268287
async def check_device_registered(
269288
self, user_id, device_id, initial_device_display_name=None
270289
):
@@ -282,6 +301,9 @@ async def check_device_registered(
282301
Returns:
283302
str: device id (generated if none was supplied)
284303
"""
304+
305+
self._check_device_name_length(initial_device_display_name)
306+
285307
if device_id is not None:
286308
new_device = await self.store.store_device(
287309
user_id=user_id,
@@ -397,12 +419,8 @@ async def update_device(self, user_id: str, device_id: str, content: dict) -> No
397419

398420
# Reject a new displayname which is too long.
399421
new_display_name = content.get("display_name")
400-
if new_display_name and len(new_display_name) > MAX_DEVICE_DISPLAY_NAME_LEN:
401-
raise SynapseError(
402-
400,
403-
"Device display name is too long (max %i)"
404-
% (MAX_DEVICE_DISPLAY_NAME_LEN,),
405-
)
422+
423+
self._check_device_name_length(new_display_name)
406424

407425
try:
408426
await self.store.update_device(

tests/handlers/test_device.py

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ def prepare(self, reactor, clock, hs):
3535
# These tests assume that it starts 1000 seconds in.
3636
self.reactor.advance(1000)
3737

38+
def test_device_is_created_with_invalid_name(self):
39+
self.get_failure(
40+
self.handler.check_device_registered(
41+
user_id="@boris:foo",
42+
device_id="foo",
43+
initial_device_display_name="a"
44+
* (synapse.handlers.device.MAX_DEVICE_DISPLAY_NAME_LEN + 1),
45+
),
46+
synapse.api.errors.SynapseError,
47+
)
48+
3849
def test_device_is_created_if_doesnt_exist(self):
3950
res = self.get_success(
4051
self.handler.check_device_registered(

tests/rest/admin/test_device.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_update_device_too_long_display_name(self):
221221
self.render(request)
222222

223223
self.assertEqual(400, channel.code, msg=channel.json_body)
224-
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
224+
self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"])
225225

226226
# Ensure the display name was not updated.
227227
request, channel = self.make_request(

0 commit comments

Comments
 (0)