Allow registering invalid user IDs with admin API

This commit is contained in:
Tulir Asokan 2020-11-18 01:13:27 +02:00
parent c79b058cff
commit 5ec53ac808

View File

@ -70,22 +70,24 @@ class RegistrationHandler(BaseHandler):
self.session_lifetime = hs.config.session_lifetime self.session_lifetime = hs.config.session_lifetime
async def check_username( async def check_username(
self, localpart, guest_access_token=None, assigned_user_id=None self, localpart, guest_access_token=None, assigned_user_id=None, allow_invalid=False
): ):
if types.contains_invalid_mxid_characters(localpart): # meow: allow admins to register invalid user ids
raise SynapseError( if not allow_invalid:
400, if types.contains_invalid_mxid_characters(localpart):
"User ID can only contain characters a-z, 0-9, or '=_-./'", raise SynapseError(
Codes.INVALID_USERNAME, 400,
) "User ID can only contain characters a-z, 0-9, or '=_-./'",
Codes.INVALID_USERNAME,
)
if not localpart: if not localpart:
raise SynapseError(400, "User ID cannot be empty", Codes.INVALID_USERNAME) raise SynapseError(400, "User ID cannot be empty", Codes.INVALID_USERNAME)
if localpart[0] == "_": if localpart[0] == "_":
raise SynapseError( raise SynapseError(
400, "User ID may not begin with _", Codes.INVALID_USERNAME 400, "User ID may not begin with _", Codes.INVALID_USERNAME
) )
user = UserID(localpart, self.hs.hostname) user = UserID(localpart, self.hs.hostname)
user_id = user.to_string() user_id = user.to_string()
@ -99,14 +101,16 @@ class RegistrationHandler(BaseHandler):
"A different user ID has already been registered for this session", "A different user ID has already been registered for this session",
) )
self.check_user_id_not_appservice_exclusive(user_id) # meow: allow admins to register reserved user ids and long user ids
if not allow_invalid:
self.check_user_id_not_appservice_exclusive(user_id)
if len(user_id) > MAX_USERID_LENGTH: if len(user_id) > MAX_USERID_LENGTH:
raise SynapseError( raise SynapseError(
400, 400,
"User ID may not be longer than %s characters" % (MAX_USERID_LENGTH,), "User ID may not be longer than %s characters" % (MAX_USERID_LENGTH,),
Codes.INVALID_USERNAME, Codes.INVALID_USERNAME,
) )
users = await self.store.get_users_by_id_case_insensitive(user_id) users = await self.store.get_users_by_id_case_insensitive(user_id)
if users: if users:
@ -200,7 +204,9 @@ class RegistrationHandler(BaseHandler):
await self.auth.check_auth_blocking(threepid=threepid) await self.auth.check_auth_blocking(threepid=threepid)
if localpart is not None: if localpart is not None:
await self.check_username(localpart, guest_access_token=guest_access_token) allow_invalid = by_admin and self.hs.config.meow.admin_api_register_invalid
await self.check_username(localpart, guest_access_token=guest_access_token,
allow_invalid=allow_invalid)
was_guest = guest_access_token is not None was_guest = guest_access_token is not None