1
0
Fork 0
mirror of https://github.com/element-hq/synapse.git synced 2025-03-31 03:45:13 +00:00
This commit is contained in:
hex5f 2025-03-29 18:05:47 -07:00 committed by GitHub
commit 16edc80599
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 46 additions and 1 deletions

View file

@ -0,0 +1 @@
Add option to allow registrations that begin with `_`. Contributed by `_` (@hex5f).

View file

@ -2887,6 +2887,20 @@ Example configuration:
inhibit_user_in_use_error: true
```
---
### `allow_underscore_prefixed_registration`
Whether users are allowed to register with a underscore-prefixed localpart.
By default, AppServices use prefixes like `_example` to namespace their
associated ghost users. If turned on, this may result in clashes or confusion.
Useful when provisioning users from an external identity provider.
Defaults to false.
Example configuration:
```yaml
allow_underscore_prefixed_registration: false
```
---
## User session management
---
### `session_lifetime`

View file

@ -162,6 +162,10 @@ class RegistrationConfig(Config):
"disable_msisdn_registration", False
)
self.allow_underscore_prefixed_localpart = config.get(
"allow_underscore_prefixed_localpart", False
)
session_lifetime = config.get("session_lifetime")
if session_lifetime is not None:
session_lifetime = self.parse_duration(session_lifetime)

View file

@ -159,7 +159,10 @@ class RegistrationHandler:
if not localpart:
raise SynapseError(400, "User ID cannot be empty", Codes.INVALID_USERNAME)
if localpart[0] == "_":
if (
localpart[0] == "_"
and not self.hs.config.registration.allow_underscore_prefixed_localpart
):
raise SynapseError(
400, "User ID may not begin with _", Codes.INVALID_USERNAME
)

View file

@ -588,6 +588,29 @@ class RegistrationTestCase(unittest.HomeserverTestCase):
d = self.store.is_support_user(user_id)
self.assertFalse(self.get_success(d))
def test_underscore_localpart_rejected_by_default(self) -> None:
for invalid_user_id in ("_", "_prefixed"):
with self.subTest(invalid_user_id=invalid_user_id):
self.get_failure(
self.handler.register_user(localpart=invalid_user_id),
SynapseError,
)
@override_config(
{
"allow_underscore_prefixed_localpart": "true",
}
)
def test_underscore_localpart_allowed_if_configured(self) -> None:
for valid_user_id in ("_", "_prefixed"):
with self.subTest(valid_user_id=valid_user_id):
user_id = self.get_success(
self.handler.register_user(
localpart=valid_user_id,
),
)
self.assertEqual(user_id, f"@{valid_user_id}:test")
def test_invalid_user_id(self) -> None:
invalid_user_id = "^abcd"
self.get_failure(