Skip to content

Add configurable allowlist for setting custom profile fields #18562

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 2 commits 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/18562.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `msc4133_key_allowlist` experimental option to configure a list of custom profile keys that users may set.
14 changes: 13 additions & 1 deletion synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import enum
from functools import cache
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Any, List, Optional

import attr
import attr.validators
Expand Down Expand Up @@ -552,6 +552,18 @@ def read_config(
# MSC4133: Custom profile fields
self.msc4133_enabled: bool = experimental.get("msc4133_enabled", False)

self.msc4133_key_allowlist: Optional[List[str]] = experimental.get(
"msc4133_key_allowlist"
)
if self.msc4133_key_allowlist is not None:
if not isinstance(self.msc4133_key_allowlist, list) or not all(
isinstance(k, str) for k in self.msc4133_key_allowlist
):
raise ConfigError(
"experimental_features.msc4133_key_allowlist must be a list of strings",
("experimental", "msc4133_key_allowlist"),
)

# MSC4210: Remove legacy mentions
self.msc4210_enabled: bool = experimental.get("msc4210_enabled", False)

Expand Down
8 changes: 8 additions & 0 deletions synapse/handlers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ async def set_profile_field(
if not by_admin and target_user != requester.user:
raise AuthError(403, "Cannot set another user's profile")

allowlist = self.hs.config.experimental.msc4133_key_allowlist
if allowlist is not None and field_name not in allowlist:
raise SynapseError(
403,
"Changing this profile field is disabled on this server",
Codes.FORBIDDEN,
)

await self.store.set_profile_field(target_user, field_name, new_value)

# Custom fields do not propagate into the user directory *or* rooms.
Expand Down
28 changes: 28 additions & 0 deletions tests/rest/client/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,34 @@ def test_set_custom_field_other(self) -> None:
self.assertEqual(channel.code, 403, channel.result)
self.assertEqual(channel.json_body["errcode"], Codes.FORBIDDEN)

@unittest.override_config(
{
"experimental_features": {
"msc4133_enabled": True,
"msc4133_key_allowlist": ["allowed_field"],
}
}
)
def test_set_custom_field_not_allowlisted(self) -> None:
"""Setting a field not in the allowlist should be rejected."""
channel = self.make_request(
"PUT",
f"/_matrix/client/unstable/uk.tcpip.msc4133/profile/{self.owner}/blocked",
content={"blocked": "test"},
access_token=self.owner_tok,
)
self.assertEqual(channel.code, 403, channel.result)
self.assertEqual(channel.json_body["errcode"], Codes.FORBIDDEN)

# Allowed field should succeed.
channel = self.make_request(
"PUT",
f"/_matrix/client/unstable/uk.tcpip.msc4133/profile/{self.owner}/allowed_field",
content={"allowed_field": "ok"},
access_token=self.owner_tok,
)
self.assertEqual(channel.code, 200, channel.result)

def _setup_local_files(self, names_and_props: Dict[str, Dict[str, Any]]) -> None:
"""Stores metadata about files in the database.

Expand Down
Loading