summaryrefslogtreecommitdiff
path: root/synapse/api
diff options
context:
space:
mode:
authorAndrej Shadura <andrewsh@debian.org>2022-06-19 15:20:00 +0200
committerAndrej Shadura <andrewsh@debian.org>2022-06-19 15:21:39 +0200
commit734a8e556ce00029d9d7ab0fed73336d24fa91f3 (patch)
treeb277733532b1b141d534133a4715a2fe765ab533 /synapse/api
parent7a966d08c8403bcff00ac636d977097602501a69 (diff)
parent6dc64c92c6991f09910f3e6db368e6eeb4b1981e (diff)
Update upstream source from tag 'upstream/1.61.0'
Update to upstream version '1.61.0' with Debian dir 5b9bb60cc861cbccd0027b7db7acf826071dc6a0
Diffstat (limited to 'synapse/api')
-rw-r--r--synapse/api/auth.py45
-rw-r--r--synapse/api/constants.py16
-rw-r--r--synapse/api/errors.py37
-rw-r--r--synapse/api/filtering.py9
-rw-r--r--synapse/api/room_versions.py32
5 files changed, 94 insertions, 45 deletions
diff --git a/synapse/api/auth.py b/synapse/api/auth.py
index 93175066..5a410f80 100644
--- a/synapse/api/auth.py
+++ b/synapse/api/auth.py
@@ -29,12 +29,11 @@ from synapse.api.errors import (
MissingClientTokenError,
)
from synapse.appservice import ApplicationService
-from synapse.events import EventBase
from synapse.http import get_request_user_agent
from synapse.http.site import SynapseRequest
from synapse.logging.opentracing import active_span, force_tracing, start_active_span
from synapse.storage.databases.main.registration import TokenLookupResult
-from synapse.types import Requester, StateMap, UserID, create_requester
+from synapse.types import Requester, UserID, create_requester
from synapse.util.caches.lrucache import LruCache
from synapse.util.macaroons import get_value_from_macaroon, satisfy_expiry
@@ -61,8 +60,8 @@ class Auth:
self.hs = hs
self.clock = hs.get_clock()
self.store = hs.get_datastores().main
- self.state = hs.get_state_handler()
self._account_validity_handler = hs.get_account_validity_handler()
+ self._storage_controllers = hs.get_storage_controllers()
self.token_cache: LruCache[str, Tuple[str, bool]] = LruCache(
10000, "token_cache"
@@ -79,9 +78,8 @@ class Auth:
self,
room_id: str,
user_id: str,
- current_state: Optional[StateMap[EventBase]] = None,
allow_departed_users: bool = False,
- ) -> EventBase:
+ ) -> Tuple[str, Optional[str]]:
"""Check if the user is in the room, or was at some point.
Args:
room_id: The room to check.
@@ -99,29 +97,28 @@ class Auth:
Raises:
AuthError if the user is/was not in the room.
Returns:
- Membership event for the user if the user was in the
- room. This will be the join event if they are currently joined to
- the room. This will be the leave event if they have left the room.
+ The current membership of the user in the room and the
+ membership event ID of the user.
"""
- if current_state:
- member = current_state.get((EventTypes.Member, user_id), None)
- else:
- member = await self.state.get_current_state(
- room_id=room_id, event_type=EventTypes.Member, state_key=user_id
- )
- if member:
- membership = member.membership
+ (
+ membership,
+ member_event_id,
+ ) = await self.store.get_local_current_membership_for_user_in_room(
+ user_id=user_id,
+ room_id=room_id,
+ )
+ if membership:
if membership == Membership.JOIN:
- return member
+ return membership, member_event_id
# XXX this looks totally bogus. Why do we not allow users who have been banned,
# or those who were members previously and have been re-invited?
if allow_departed_users and membership == Membership.LEAVE:
forgot = await self.store.did_forget(user_id, room_id)
if not forgot:
- return member
+ return membership, member_event_id
raise AuthError(403, "User %s not in room %s" % (user_id, room_id))
@@ -602,8 +599,11 @@ class Auth:
# We currently require the user is a "moderator" in the room. We do this
# by checking if they would (theoretically) be able to change the
# m.room.canonical_alias events
- power_level_event = await self.state.get_current_state(
- room_id, EventTypes.PowerLevels, ""
+
+ power_level_event = (
+ await self._storage_controllers.state.get_current_state_event(
+ room_id, EventTypes.PowerLevels, ""
+ )
)
auth_events = {}
@@ -693,12 +693,11 @@ class Auth:
# * The user is a non-guest user, and was ever in the room
# * The user is a guest user, and has joined the room
# else it will throw.
- member_event = await self.check_user_in_room(
+ return await self.check_user_in_room(
room_id, user_id, allow_departed_users=allow_departed_users
)
- return member_event.membership, member_event.event_id
except AuthError:
- visibility = await self.state.get_current_state(
+ visibility = await self._storage_controllers.state.get_current_state_event(
room_id, EventTypes.RoomHistoryVisibility, ""
)
if (
diff --git a/synapse/api/constants.py b/synapse/api/constants.py
index 0ccd4c95..e1d31cab 100644
--- a/synapse/api/constants.py
+++ b/synapse/api/constants.py
@@ -31,11 +31,6 @@ MAX_ALIAS_LENGTH = 255
# the maximum length for a user id is 255 characters
MAX_USERID_LENGTH = 255
-# The maximum length for a group id is 255 characters
-MAX_GROUPID_LENGTH = 255
-MAX_GROUP_CATEGORYID_LENGTH = 255
-MAX_GROUP_ROLEID_LENGTH = 255
-
class Membership:
@@ -65,6 +60,8 @@ class JoinRules:
PRIVATE: Final = "private"
# As defined for MSC3083.
RESTRICTED: Final = "restricted"
+ # As defined for MSC3787.
+ KNOCK_RESTRICTED: Final = "knock_restricted"
class RestrictedJoinRuleTypes:
@@ -98,7 +95,6 @@ class EventTypes:
Aliases: Final = "m.room.aliases"
Redaction: Final = "m.room.redaction"
ThirdPartyInvite: Final = "m.room.third_party_invite"
- RelatedGroups: Final = "m.room.related_groups"
RoomHistoryVisibility: Final = "m.room.history_visibility"
CanonicalAlias: Final = "m.room.canonical_alias"
@@ -140,7 +136,13 @@ class DeviceKeyAlgorithms:
class EduTypes:
- Presence: Final = "m.presence"
+ PRESENCE: Final = "m.presence"
+ TYPING: Final = "m.typing"
+ RECEIPT: Final = "m.receipt"
+ DEVICE_LIST_UPDATE: Final = "m.device_list_update"
+ SIGNING_KEY_UPDATE: Final = "m.signing_key_update"
+ UNSTABLE_SIGNING_KEY_UPDATE: Final = "org.matrix.signing_key_update"
+ DIRECT_TO_DEVICE: Final = "m.direct_to_device"
class RejectedReason:
diff --git a/synapse/api/errors.py b/synapse/api/errors.py
index cb3b7323..cc7b7854 100644
--- a/synapse/api/errors.py
+++ b/synapse/api/errors.py
@@ -17,6 +17,7 @@
import logging
import typing
+from enum import Enum
from http import HTTPStatus
from typing import Any, Dict, List, Optional, Union
@@ -30,7 +31,11 @@ if typing.TYPE_CHECKING:
logger = logging.getLogger(__name__)
-class Codes:
+class Codes(str, Enum):
+ """
+ All known error codes, as an enum of strings.
+ """
+
UNRECOGNIZED = "M_UNRECOGNIZED"
UNAUTHORIZED = "M_UNAUTHORIZED"
FORBIDDEN = "M_FORBIDDEN"
@@ -74,6 +79,13 @@ class Codes:
WEAK_PASSWORD = "M_WEAK_PASSWORD"
INVALID_SIGNATURE = "M_INVALID_SIGNATURE"
USER_DEACTIVATED = "M_USER_DEACTIVATED"
+
+ # The account has been suspended on the server.
+ # By opposition to `USER_DEACTIVATED`, this is a reversible measure
+ # that can possibly be appealed and reverted.
+ # Part of MSC3823.
+ USER_ACCOUNT_SUSPENDED = "ORG.MATRIX.MSC3823.USER_ACCOUNT_SUSPENDED"
+
BAD_ALIAS = "M_BAD_ALIAS"
# For restricted join rules.
UNABLE_AUTHORISE_JOIN = "M_UNABLE_TO_AUTHORISE_JOIN"
@@ -134,7 +146,13 @@ class SynapseError(CodeMessageException):
errcode: Matrix error code e.g 'M_FORBIDDEN'
"""
- def __init__(self, code: int, msg: str, errcode: str = Codes.UNKNOWN):
+ def __init__(
+ self,
+ code: int,
+ msg: str,
+ errcode: str = Codes.UNKNOWN,
+ additional_fields: Optional[Dict] = None,
+ ):
"""Constructs a synapse error.
Args:
@@ -144,9 +162,13 @@ class SynapseError(CodeMessageException):
"""
super().__init__(code, msg)
self.errcode = errcode
+ if additional_fields is None:
+ self._additional_fields: Dict = {}
+ else:
+ self._additional_fields = dict(additional_fields)
def error_dict(self) -> "JsonDict":
- return cs_error(self.msg, self.errcode)
+ return cs_error(self.msg, self.errcode, **self._additional_fields)
class InvalidAPICallError(SynapseError):
@@ -171,14 +193,7 @@ class ProxiedRequestError(SynapseError):
errcode: str = Codes.UNKNOWN,
additional_fields: Optional[Dict] = None,
):
- super().__init__(code, msg, errcode)
- if additional_fields is None:
- self._additional_fields: Dict = {}
- else:
- self._additional_fields = dict(additional_fields)
-
- def error_dict(self) -> "JsonDict":
- return cs_error(self.msg, self.errcode, **self._additional_fields)
+ super().__init__(code, msg, errcode, additional_fields)
class ConsentNotGivenError(SynapseError):
diff --git a/synapse/api/filtering.py b/synapse/api/filtering.py
index 4a808e33..b0071475 100644
--- a/synapse/api/filtering.py
+++ b/synapse/api/filtering.py
@@ -19,6 +19,7 @@ from typing import (
TYPE_CHECKING,
Awaitable,
Callable,
+ Collection,
Dict,
Iterable,
List,
@@ -32,7 +33,7 @@ from typing import (
import jsonschema
from jsonschema import FormatChecker
-from synapse.api.constants import EventContentFields
+from synapse.api.constants import EduTypes, EventContentFields
from synapse.api.errors import SynapseError
from synapse.api.presence import UserPresenceState
from synapse.events import EventBase
@@ -346,7 +347,7 @@ class Filter:
user_id = event.user_id
field_matchers = {
"senders": lambda v: user_id == v,
- "types": lambda v: "m.presence" == v,
+ "types": lambda v: EduTypes.PRESENCE == v,
}
return self._check_fields(field_matchers)
else:
@@ -444,9 +445,9 @@ class Filter:
return room_ids
async def _check_event_relations(
- self, events: Iterable[FilterEvent]
+ self, events: Collection[FilterEvent]
) -> List[FilterEvent]:
- # The event IDs to check, mypy doesn't understand the ifinstance check.
+ # The event IDs to check, mypy doesn't understand the isinstance check.
event_ids = [event.event_id for event in events if isinstance(event, EventBase)] # type: ignore[attr-defined]
event_ids_to_keep = set(
await self._store.events_have_relations(
diff --git a/synapse/api/room_versions.py b/synapse/api/room_versions.py
index a747a408..3f85d61b 100644
--- a/synapse/api/room_versions.py
+++ b/synapse/api/room_versions.py
@@ -81,6 +81,9 @@ class RoomVersion:
msc2716_historical: bool
# MSC2716: Adds support for redacting "insertion", "chunk", and "marker" events
msc2716_redactions: bool
+ # MSC3787: Adds support for a `knock_restricted` join rule, mixing concepts of
+ # knocks and restricted join rules into the same join condition.
+ msc3787_knock_restricted_join_rule: bool
class RoomVersions:
@@ -99,6 +102,7 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V2 = RoomVersion(
"2",
@@ -115,6 +119,7 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V3 = RoomVersion(
"3",
@@ -131,6 +136,7 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V4 = RoomVersion(
"4",
@@ -147,6 +153,7 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V5 = RoomVersion(
"5",
@@ -163,6 +170,7 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V6 = RoomVersion(
"6",
@@ -179,6 +187,7 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
MSC2176 = RoomVersion(
"org.matrix.msc2176",
@@ -195,6 +204,7 @@ class RoomVersions:
msc2403_knocking=False,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V7 = RoomVersion(
"7",
@@ -211,6 +221,7 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V8 = RoomVersion(
"8",
@@ -227,6 +238,7 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
V9 = RoomVersion(
"9",
@@ -243,6 +255,7 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=False,
msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=False,
)
MSC2716v3 = RoomVersion(
"org.matrix.msc2716v3",
@@ -259,6 +272,24 @@ class RoomVersions:
msc2403_knocking=True,
msc2716_historical=True,
msc2716_redactions=True,
+ msc3787_knock_restricted_join_rule=False,
+ )
+ MSC3787 = RoomVersion(
+ "org.matrix.msc3787",
+ RoomDisposition.UNSTABLE,
+ EventFormatVersions.V3,
+ StateResolutionVersions.V2,
+ enforce_key_validity=True,
+ special_case_aliases_auth=False,
+ strict_canonicaljson=True,
+ limit_notifications_power_levels=True,
+ msc2176_redaction_rules=False,
+ msc3083_join_rules=True,
+ msc3375_redaction_rules=True,
+ msc2403_knocking=True,
+ msc2716_historical=False,
+ msc2716_redactions=False,
+ msc3787_knock_restricted_join_rule=True,
)
@@ -276,6 +307,7 @@ KNOWN_ROOM_VERSIONS: Dict[str, RoomVersion] = {
RoomVersions.V8,
RoomVersions.V9,
RoomVersions.MSC2716v3,
+ RoomVersions.MSC3787,
)
}