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

Commit

Permalink
Create function to check for long names in devices (#8364)
Browse files Browse the repository at this point in the history
* 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 <dgrig@erethon.com>
  • Loading branch information
Erethon committed Sep 22, 2020
1 parent 4f3096d commit 37ca592
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions changelog.d/8364.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug where during device registration the length of the device name wasn't
limited.
30 changes: 24 additions & 6 deletions synapse/handlers/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from synapse.api import errors
from synapse.api.constants import EventTypes
from synapse.api.errors import (
Codes,
FederationDeniedError,
HttpResponseException,
RequestSendFailed,
Expand Down Expand Up @@ -265,6 +266,24 @@ def __init__(self, hs):

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

def _check_device_name_length(self, name: str):
"""
Checks whether a device name is longer than the maximum allowed length.
Args:
name: The name of the device.
Raises:
SynapseError: if the device name is too long.
"""
if name and len(name) > MAX_DEVICE_DISPLAY_NAME_LEN:
raise SynapseError(
400,
"Device display name is too long (max %i)"
% (MAX_DEVICE_DISPLAY_NAME_LEN,),
errcode=Codes.TOO_LARGE,
)

async def check_device_registered(
self, user_id, device_id, initial_device_display_name=None
):
Expand All @@ -282,6 +301,9 @@ async def check_device_registered(
Returns:
str: device id (generated if none was supplied)
"""

self._check_device_name_length(initial_device_display_name)

if device_id is not None:
new_device = await self.store.store_device(
user_id=user_id,
Expand Down Expand Up @@ -397,12 +419,8 @@ async def update_device(self, user_id: str, device_id: str, content: dict) -> No

# Reject a new displayname which is too long.
new_display_name = content.get("display_name")
if new_display_name and len(new_display_name) > MAX_DEVICE_DISPLAY_NAME_LEN:
raise SynapseError(
400,
"Device display name is too long (max %i)"
% (MAX_DEVICE_DISPLAY_NAME_LEN,),
)

self._check_device_name_length(new_display_name)

try:
await self.store.update_device(
Expand Down
11 changes: 11 additions & 0 deletions tests/handlers/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ def prepare(self, reactor, clock, hs):
# These tests assume that it starts 1000 seconds in.
self.reactor.advance(1000)

def test_device_is_created_with_invalid_name(self):
self.get_failure(
self.handler.check_device_registered(
user_id="@boris:foo",
device_id="foo",
initial_device_display_name="a"
* (synapse.handlers.device.MAX_DEVICE_DISPLAY_NAME_LEN + 1),
),
synapse.api.errors.SynapseError,
)

def test_device_is_created_if_doesnt_exist(self):
res = self.get_success(
self.handler.check_device_registered(
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/admin/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_update_device_too_long_display_name(self):
self.render(request)

self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"])

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

0 comments on commit 37ca592

Please sign in to comment.