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

Commit c684dfc

Browse files
committed
Mainline some more
1 parent b8b3e6a commit c684dfc

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

synapse/logging/context.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
def get_thread_resource_usage() -> "Optional[resource.struct_rusage]":
5656
return resource.getrusage(RUSAGE_THREAD)
5757

58-
5958
except Exception:
6059
# If the system doesn't support resource.getrusage(RUSAGE_THREAD) then we
6160
# won't track resource usage.

synapse/rest/client/register.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515
import logging
1616
import random
17-
import re
1817
from typing import TYPE_CHECKING, List, Optional, Tuple
1918

2019
from twisted.web.server import Request
@@ -469,22 +468,13 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
469468
else:
470469
should_issue_refresh_token = False
471470

472-
# We don't care about usernames for this deployment. In fact, the act
473-
# of checking whether they exist already can leak metadata about
474-
# which users are already registered.
475-
#
476-
# Usernames are already derived via the provided email.
477-
# So, if they're not necessary, just ignore them.
478-
#
479-
# (we do still allow appservices to set them below)
471+
# Pull out the provided username and do basic sanity checks early since
472+
# the auth layer will store these in sessions.
480473
desired_username = None
481-
482-
desired_display_name = body.get("display_name")
483-
484-
# We need to retrieve the password early in order to pass it to
485-
# application service registration
486-
# This is specific to shadow server registration of users via an AS
487-
password = body.pop("password", None)
474+
if "username" in body:
475+
if not isinstance(body["username"], str) or len(body["username"]) > 512:
476+
raise SynapseError(400, "Invalid username")
477+
desired_username = body["username"]
488478

489479
# fork off as soon as possible for ASes which have completely
490480
# different registration flows to normal users
@@ -503,7 +493,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
503493
# Set the desired user according to the AS API (which uses the
504494
# 'user' key not 'username'). Since this is a new addition, we'll
505495
# fallback to 'username' if they gave one.
506-
desired_username = body.get("user", body.get("username"))
496+
desired_username = body.get("user", desired_username)
507497

508498
# XXX we should check that desired_username is valid. Currently
509499
# we give appservices carte blanche for any insanity in mxids,
@@ -533,6 +523,16 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
533523
if not self._registration_enabled:
534524
raise SynapseError(403, "Registration has been disabled", Codes.FORBIDDEN)
535525

526+
# For regular registration, convert the provided username to lowercase
527+
# before attempting to register it. This should mean that people who try
528+
# to register with upper-case in their usernames don't get a nasty surprise.
529+
#
530+
# Note that we treat usernames case-insensitively in login, so they are
531+
# free to carry on imagining that their username is CrAzYh4cKeR if that
532+
# keeps them happy.
533+
if desired_username is not None:
534+
desired_username = desired_username.lower()
535+
536536
# Check if this account is upgrading from a guest account.
537537
guest_access_token = body.get("guest_access_token", None)
538538

@@ -541,6 +541,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
541541
# Note that we remove the password from the body since the auth layer
542542
# will store the body in the session and we don't want a plaintext
543543
# password store there.
544+
password = body.pop("password", None)
544545
if password is not None:
545546
if not isinstance(password, str) or len(password) > 512:
546547
raise SynapseError(400, "Invalid password")
@@ -626,15 +627,6 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
626627
Codes.THREEPID_DENIED,
627628
)
628629

629-
existingUid = await self.store.get_user_id_by_threepid(
630-
medium, address
631-
)
632-
633-
if existingUid is not None:
634-
raise SynapseError(
635-
400, "%s is already in use" % medium, Codes.THREEPID_IN_USE
636-
)
637-
638630
if registered_user_id is not None:
639631
logger.info(
640632
"Already registered user ID %r for this session", registered_user_id
@@ -703,6 +695,20 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
703695
session_id
704696
)
705697

698+
# TODO: This won't be needed anymore once https://github.com/matrix-org/matrix-dinsic/issues/793
699+
# is resolved.
700+
desired_display_name = body.get("display_name")
701+
if auth_result:
702+
if LoginType.EMAIL_IDENTITY in auth_result:
703+
address = auth_result[LoginType.EMAIL_IDENTITY]["address"]
704+
if (
705+
self.hs.config.registration.register_just_use_email_for_display_name
706+
):
707+
desired_display_name = address
708+
else:
709+
# Custom mapping between email address and display name
710+
desired_display_name = _map_email_to_displayname(address)
711+
706712
registered_user_id = await self.registration_handler.register_user(
707713
localpart=desired_username,
708714
password_hash=password_hash,

synapse/util/caches/lrucache.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def _get_size_of(val: Any, *, recurse: bool = True) -> int:
6868
sizer.exclude_refs((), None, "")
6969
return sizer.asizeof(val, limit=100 if recurse else 0)
7070

71-
7271
except ImportError:
7372

7473
def _get_size_of(val: Any, *, recurse: bool = True) -> int:

0 commit comments

Comments
 (0)