Skip to content

Add option to allow registrations that begin with '_' #18262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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/18262.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option to allow registrations that begin with `_`. Contributed by `_` (@hex5f).
14 changes: 14 additions & 0 deletions docs/usage/configuration/config_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions synapse/config/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def read_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)
Expand Down
5 changes: 4 additions & 1 deletion synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ async def check_username(
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
)
Expand Down
23 changes: 23 additions & 0 deletions tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,29 @@ def test_register_not_support_user(self) -> None:
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(
Expand Down