-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement MSC4009 to widen the allowed Matrix ID grammar #15536
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Implement [MSC4009](https://github.com/matrix-org/matrix-spec-proposals/pull/4009) to expand the supported characters in Matrix IDs. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ | |
ReplicationRegisterServlet, | ||
) | ||
from synapse.spam_checker_api import RegistrationBehaviour | ||
from synapse.types import RoomAlias, UserID, create_requester | ||
from synapse.types import GUEST_USER_ID_PATTERN, RoomAlias, UserID, create_requester | ||
from synapse.types.state import StateFilter | ||
|
||
if TYPE_CHECKING: | ||
|
@@ -143,10 +143,15 @@ async def check_username( | |
assigned_user_id: Optional[str] = None, | ||
inhibit_user_in_use_error: bool = False, | ||
) -> None: | ||
if types.contains_invalid_mxid_characters(localpart): | ||
if types.contains_invalid_mxid_characters( | ||
localpart, self.hs.config.experimental.msc4009_e164_mxids | ||
): | ||
extra_chars = ( | ||
"=_-./+" if self.hs.config.experimental.msc4009_e164_mxids else "=_-./" | ||
) | ||
raise SynapseError( | ||
400, | ||
"User ID can only contain characters a-z, 0-9, or '=_-./'", | ||
f"User ID can only contain characters a-z, 0-9, or '{extra_chars}'", | ||
Codes.INVALID_USERNAME, | ||
) | ||
|
||
|
@@ -195,16 +200,12 @@ async def check_username( | |
errcode=Codes.FORBIDDEN, | ||
) | ||
|
||
if guest_access_token is None: | ||
try: | ||
int(localpart) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hah, can't believe this is how we defined the grammar for guest localparts. Goodness help us if Python allowed things like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It did not, but it did allow things like It did also allow |
||
raise SynapseError( | ||
400, | ||
"Numeric user IDs are reserved for guest users.", | ||
errcode=Codes.INVALID_USERNAME, | ||
) | ||
except ValueError: | ||
pass | ||
if guest_access_token is None and GUEST_USER_ID_PATTERN.fullmatch(localpart): | ||
raise SynapseError( | ||
400, | ||
"Numeric user IDs are reserved for guest users.", | ||
errcode=Codes.INVALID_USERNAME, | ||
) | ||
|
||
async def register_user( | ||
self, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,8 @@ def __init__(self, hs: "HomeServer"): | |
|
||
self._consent_at_registration = hs.config.consent.user_consent_at_registration | ||
|
||
self._e164_mxids = hs.config.experimental.msc4009_e164_mxids | ||
|
||
def register_identity_provider(self, p: SsoIdentityProvider) -> None: | ||
p_id = p.idp_id | ||
assert p_id not in self._identity_providers | ||
|
@@ -710,7 +712,7 @@ async def _register_mapped_user( | |
# Since the localpart is provided via a potentially untrusted module, | ||
# ensure the MXID is valid before registering. | ||
if not attributes.localpart or contains_invalid_mxid_characters( | ||
attributes.localpart | ||
attributes.localpart, self._e164_mxids | ||
): | ||
Comment on lines
714
to
716
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SSO mapping code (done via the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that we don't need to worry about changing a currently registered user's mapping since we find those users via their remote ID. Changing the mapping could, however, cause conflicts where there didn't used to be one. |
||
raise MappingException("localpart is invalid: %s" % (attributes.localpart,)) | ||
|
||
|
@@ -943,7 +945,7 @@ async def check_username_availability( | |
localpart, | ||
) | ||
|
||
if contains_invalid_mxid_characters(localpart): | ||
if contains_invalid_mxid_characters(localpart, self._e164_mxids): | ||
raise SynapseError(400, "localpart is invalid: %s" % (localpart,)) | ||
user_id = UserID(localpart, self._server_name).to_string() | ||
user_infos = await self._store.get_users_by_id_case_insensitive(user_id) | ||
|
Uh oh!
There was an error while loading. Please reload this page.