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

Limit UserIds to a length that fits in a state key #5198

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5198.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent registration for user ids that are to long to fit into a state key. Contributed by Reid Anderson.
3 changes: 3 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
# the maximum length for a room alias is 255 characters
MAX_ALIAS_LENGTH = 255

# the maximum length for a user id is 255 characters
MAX_USERID_LENGTH = 255


class Membership(object):

Expand Down
14 changes: 13 additions & 1 deletion synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
from twisted.internet import defer

from synapse import types
from synapse.api.constants import LoginType
from synapse.api.constants import (
LoginType,
MAX_USERID_LENGTH
)
from synapse.api.errors import (
AuthError,
Codes,
Expand Down Expand Up @@ -123,6 +126,15 @@ def check_username(self, localpart, guest_access_token=None,

self.check_user_id_not_appservice_exclusive(user_id)

if len(user_id) > MAX_ALIAS_LENGTH:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be MAX_USERID_LENGTH

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, this looks fixed on master now synapse/handlers/register.py#L104

raise SynapseError(
400,
"User ID may not be longer than %s characters" % (
MAX_ALIAS_LENGTH,
),
Codes.INVALID_USERNAME
)

users = yield self.store.get_users_by_id_case_insensitive(user_id)
if users:
if not guest_access_token:
Expand Down
7 changes: 7 additions & 0 deletions tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,10 @@ def test_register_support_user(self):
def test_register_not_support_user(self):
res = self.get_success(self.handler.register(localpart='user'))
self.assertFalse(self.store.is_support_user(res[0]))

def test_invalid_user_id_length(self):
invalid_user_id = "x"*257
res = self.get_failure(
self.handler.register(localpart=invalid_user_id),
SynapseError
)