2018-08-07 10:22:40 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-08-07 10:22:40 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import logging
|
2020-08-24 14:25:27 -04:00
|
|
|
from typing import (
|
|
|
|
Awaitable,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Sequence,
|
|
|
|
Set,
|
|
|
|
Tuple,
|
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
from synapse import event_auth
|
2019-04-01 05:24:38 -04:00
|
|
|
from synapse.api.constants import EventTypes
|
2018-08-07 10:22:40 -04:00
|
|
|
from synapse.api.errors import AuthError
|
2022-06-10 05:48:25 -04:00
|
|
|
from synapse.api.room_versions import RoomVersion
|
2019-12-13 07:55:32 -05:00
|
|
|
from synapse.events import EventBase
|
2023-09-13 07:57:19 -04:00
|
|
|
from synapse.types import MutableStateMap, StateMap, StrCollection
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
POWER_KEY = (EventTypes.PowerLevels, "")
|
|
|
|
|
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
async def resolve_events_with_store(
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id: str,
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: RoomVersion,
|
2020-08-24 14:25:27 -04:00
|
|
|
state_sets: Sequence[StateMap[str]],
|
2019-12-13 07:55:32 -05:00
|
|
|
event_map: Optional[Dict[str, EventBase]],
|
2023-09-13 07:57:19 -04:00
|
|
|
state_map_factory: Callable[[StrCollection], Awaitable[Dict[str, EventBase]]],
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> StateMap[str]:
|
2018-08-07 10:22:40 -04:00
|
|
|
"""
|
|
|
|
Args:
|
2019-12-13 07:55:32 -05:00
|
|
|
room_id: the room we are working in
|
|
|
|
|
|
|
|
state_sets: List of dicts of (type, state_key) -> event_id,
|
2018-08-07 10:22:40 -04:00
|
|
|
which are the different state groups to resolve.
|
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
event_map:
|
2018-08-07 10:22:40 -04:00
|
|
|
a dict from event_id to event, for any events that we happen to
|
|
|
|
have in flight (eg, those currently being persisted). This will be
|
2020-10-23 12:38:40 -04:00
|
|
|
used as a starting point for finding the state we need; any missing
|
2018-08-07 10:22:40 -04:00
|
|
|
events will be requested via state_map_factory.
|
|
|
|
|
|
|
|
If None, all events will be fetched via state_map_factory.
|
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
state_map_factory: will be called
|
2018-08-07 10:22:40 -04:00
|
|
|
with a list of event_ids that are needed, and should return with
|
2020-07-24 10:59:51 -04:00
|
|
|
an Awaitable that resolves to a dict of event_id to event.
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
A map from (type, state_key) to event_id.
|
2018-08-07 10:22:40 -04:00
|
|
|
"""
|
|
|
|
if len(state_sets) == 1:
|
2019-07-23 09:00:55 -04:00
|
|
|
return state_sets[0]
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
unconflicted_state, conflicted_state = _seperate(state_sets)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2020-02-21 07:15:07 -05:00
|
|
|
needed_events = {
|
2020-06-15 07:03:36 -04:00
|
|
|
event_id for event_ids in conflicted_state.values() for event_id in event_ids
|
2020-02-21 07:15:07 -05:00
|
|
|
}
|
2018-09-26 02:56:06 -04:00
|
|
|
needed_event_count = len(needed_events)
|
2018-08-07 10:22:40 -04:00
|
|
|
if event_map is not None:
|
2020-06-15 07:03:36 -04:00
|
|
|
needed_events -= set(event_map.keys())
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2018-09-26 02:56:06 -04:00
|
|
|
logger.info(
|
2019-06-20 05:32:02 -04:00
|
|
|
"Asking for %d/%d conflicted events", len(needed_events), needed_event_count
|
2018-09-26 02:56:06 -04:00
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
# A map from state event id to event. Only includes the state events which
|
|
|
|
# are in conflict (and those in event_map).
|
2020-07-24 10:59:51 -04:00
|
|
|
state_map = await state_map_factory(needed_events)
|
2018-08-07 10:22:40 -04:00
|
|
|
if event_map is not None:
|
|
|
|
state_map.update(event_map)
|
|
|
|
|
2019-12-13 07:55:32 -05:00
|
|
|
# everything in the state map should be in the right room
|
|
|
|
for event in state_map.values():
|
|
|
|
if event.room_id != room_id:
|
|
|
|
raise Exception(
|
|
|
|
"Attempting to state-resolve for room %s with event %s which is in %s"
|
2021-02-16 17:32:34 -05:00
|
|
|
% (
|
|
|
|
room_id,
|
|
|
|
event.event_id,
|
|
|
|
event.room_id,
|
|
|
|
)
|
2019-12-13 07:55:32 -05:00
|
|
|
)
|
|
|
|
|
2018-08-07 10:22:40 -04:00
|
|
|
# get the ids of the auth events which allow us to authenticate the
|
|
|
|
# conflicted state, picking only from the unconflicting state.
|
|
|
|
auth_events = _create_auth_events_from_maps(
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version, unconflicted_state, conflicted_state, state_map
|
2018-08-07 10:22:40 -04:00
|
|
|
)
|
|
|
|
|
2020-06-15 07:03:36 -04:00
|
|
|
new_needed_events = set(auth_events.values())
|
2018-09-26 02:56:06 -04:00
|
|
|
new_needed_event_count = len(new_needed_events)
|
2018-08-07 10:22:40 -04:00
|
|
|
new_needed_events -= needed_events
|
|
|
|
if event_map is not None:
|
2020-06-15 07:03:36 -04:00
|
|
|
new_needed_events -= set(event_map.keys())
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2018-09-26 02:56:06 -04:00
|
|
|
logger.info(
|
2019-06-20 05:32:02 -04:00
|
|
|
"Asking for %d/%d auth events", len(new_needed_events), new_needed_event_count
|
2018-09-26 02:56:06 -04:00
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2020-07-24 10:59:51 -04:00
|
|
|
state_map_new = await state_map_factory(new_needed_events)
|
2019-12-13 07:55:32 -05:00
|
|
|
for event in state_map_new.values():
|
|
|
|
if event.room_id != room_id:
|
|
|
|
raise Exception(
|
|
|
|
"Attempting to state-resolve for room %s with event %s which is in %s"
|
2021-02-16 17:32:34 -05:00
|
|
|
% (
|
|
|
|
room_id,
|
|
|
|
event.event_id,
|
|
|
|
event.room_id,
|
|
|
|
)
|
2019-12-13 07:55:32 -05:00
|
|
|
)
|
|
|
|
|
2018-08-07 10:22:40 -04:00
|
|
|
state_map.update(state_map_new)
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return _resolve_with_state(
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version, unconflicted_state, conflicted_state, auth_events, state_map
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _seperate(
|
|
|
|
state_sets: Iterable[StateMap[str]],
|
2020-08-28 07:28:53 -04:00
|
|
|
) -> Tuple[MutableStateMap[str], MutableStateMap[Set[str]]]:
|
2018-08-07 10:22:40 -04:00
|
|
|
"""Takes the state_sets and figures out which keys are conflicted and
|
|
|
|
which aren't. i.e., which have multiple different event_ids associated
|
|
|
|
with them in different state sets.
|
|
|
|
|
|
|
|
Args:
|
2020-08-24 14:25:27 -04:00
|
|
|
state_sets:
|
2018-08-07 10:22:40 -04:00
|
|
|
List of dicts of (type, state_key) -> event_id, which are the
|
|
|
|
different state groups to resolve.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-24 14:25:27 -04:00
|
|
|
A tuple of (unconflicted_state, conflicted_state), where:
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
unconflicted_state is a dict mapping (type, state_key)->event_id
|
|
|
|
for unconflicted state keys.
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
conflicted_state is a dict mapping (type, state_key) to a set of
|
|
|
|
event ids for conflicted state keys.
|
2018-08-07 10:22:40 -04:00
|
|
|
"""
|
|
|
|
state_set_iterator = iter(state_sets)
|
|
|
|
unconflicted_state = dict(next(state_set_iterator))
|
2021-07-15 06:02:43 -04:00
|
|
|
conflicted_state: MutableStateMap[Set[str]] = {}
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
for state_set in state_set_iterator:
|
2020-06-15 07:03:36 -04:00
|
|
|
for key, value in state_set.items():
|
2018-08-07 10:22:40 -04:00
|
|
|
# Check if there is an unconflicted entry for the state key.
|
|
|
|
unconflicted_value = unconflicted_state.get(key)
|
|
|
|
if unconflicted_value is None:
|
|
|
|
# There isn't an unconflicted entry so check if there is a
|
|
|
|
# conflicted entry.
|
|
|
|
ls = conflicted_state.get(key)
|
|
|
|
if ls is None:
|
|
|
|
# There wasn't a conflicted entry so haven't seen this key before.
|
|
|
|
# Therefore it isn't conflicted yet.
|
|
|
|
unconflicted_state[key] = value
|
|
|
|
else:
|
|
|
|
# This key is already conflicted, add our value to the conflict set.
|
|
|
|
ls.add(value)
|
|
|
|
elif unconflicted_value != value:
|
|
|
|
# If the unconflicted value is not the same as our value then we
|
|
|
|
# have a new conflict. So move the key from the unconflicted_state
|
|
|
|
# to the conflicted state.
|
|
|
|
conflicted_state[key] = {value, unconflicted_value}
|
|
|
|
unconflicted_state.pop(key, None)
|
|
|
|
|
|
|
|
return unconflicted_state, conflicted_state
|
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _create_auth_events_from_maps(
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: RoomVersion,
|
2020-08-24 14:25:27 -04:00
|
|
|
unconflicted_state: StateMap[str],
|
|
|
|
conflicted_state: StateMap[Set[str]],
|
|
|
|
state_map: Dict[str, EventBase],
|
|
|
|
) -> StateMap[str]:
|
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: The room version.
|
2020-08-24 14:25:27 -04:00
|
|
|
unconflicted_state: The unconflicted state map.
|
|
|
|
conflicted_state: The conflicted state map.
|
|
|
|
state_map:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A map from state key to event id.
|
|
|
|
"""
|
2018-08-07 10:22:40 -04:00
|
|
|
auth_events = {}
|
2020-06-15 07:03:36 -04:00
|
|
|
for event_ids in conflicted_state.values():
|
2018-08-07 10:22:40 -04:00
|
|
|
for event_id in event_ids:
|
|
|
|
if event_id in state_map:
|
2021-07-26 12:17:00 -04:00
|
|
|
keys = event_auth.auth_types_for_event(
|
|
|
|
room_version, state_map[event_id]
|
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
for key in keys:
|
|
|
|
if key not in auth_events:
|
2020-08-24 14:25:27 -04:00
|
|
|
auth_event_id = unconflicted_state.get(key, None)
|
|
|
|
if auth_event_id:
|
|
|
|
auth_events[key] = auth_event_id
|
2018-08-07 10:22:40 -04:00
|
|
|
return auth_events
|
|
|
|
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
def _resolve_with_state(
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: RoomVersion,
|
2020-08-28 07:28:53 -04:00
|
|
|
unconflicted_state_ids: MutableStateMap[str],
|
2020-08-24 14:25:27 -04:00
|
|
|
conflicted_state_ids: StateMap[Set[str]],
|
|
|
|
auth_event_ids: StateMap[str],
|
|
|
|
state_map: Dict[str, EventBase],
|
2021-10-06 13:55:25 -04:00
|
|
|
) -> MutableStateMap[str]:
|
2018-08-07 10:22:40 -04:00
|
|
|
conflicted_state = {}
|
2020-06-15 07:03:36 -04:00
|
|
|
for key, event_ids in conflicted_state_ids.items():
|
2018-08-07 10:22:40 -04:00
|
|
|
events = [state_map[ev_id] for ev_id in event_ids if ev_id in state_map]
|
|
|
|
if len(events) > 1:
|
|
|
|
conflicted_state[key] = events
|
|
|
|
elif len(events) == 1:
|
|
|
|
unconflicted_state_ids[key] = events[0].event_id
|
|
|
|
|
|
|
|
auth_events = {
|
|
|
|
key: state_map[ev_id]
|
2020-06-15 07:03:36 -04:00
|
|
|
for key, ev_id in auth_event_ids.items()
|
2018-08-07 10:22:40 -04:00
|
|
|
if ev_id in state_map
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
2021-07-26 12:17:00 -04:00
|
|
|
resolved_state = _resolve_state_events(
|
|
|
|
room_version, conflicted_state, auth_events
|
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Failed to resolve state")
|
|
|
|
raise
|
|
|
|
|
|
|
|
new_state = unconflicted_state_ids
|
2020-06-15 07:03:36 -04:00
|
|
|
for key, event in resolved_state.items():
|
2018-08-07 10:22:40 -04:00
|
|
|
new_state[key] = event.event_id
|
|
|
|
|
|
|
|
return new_state
|
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _resolve_state_events(
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: RoomVersion,
|
|
|
|
conflicted_state: StateMap[List[EventBase]],
|
|
|
|
auth_events: MutableStateMap[EventBase],
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> StateMap[EventBase]:
|
2021-02-16 17:32:34 -05:00
|
|
|
"""This is where we actually decide which of the conflicted state to
|
2018-08-07 10:22:40 -04:00
|
|
|
use.
|
|
|
|
|
|
|
|
We resolve conflicts in the following order:
|
|
|
|
1. power levels
|
|
|
|
2. join rules
|
|
|
|
3. memberships
|
|
|
|
4. other events.
|
|
|
|
"""
|
|
|
|
resolved_state = {}
|
|
|
|
if POWER_KEY in conflicted_state:
|
|
|
|
events = conflicted_state[POWER_KEY]
|
|
|
|
logger.debug("Resolving conflicted power levels %r", events)
|
2021-07-26 12:17:00 -04:00
|
|
|
resolved_state[POWER_KEY] = _resolve_auth_events(
|
|
|
|
room_version, events, auth_events
|
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
auth_events.update(resolved_state)
|
|
|
|
|
2020-06-15 07:03:36 -04:00
|
|
|
for key, events in conflicted_state.items():
|
2018-08-07 10:22:40 -04:00
|
|
|
if key[0] == EventTypes.JoinRules:
|
|
|
|
logger.debug("Resolving conflicted join rules %r", events)
|
2021-07-26 12:17:00 -04:00
|
|
|
resolved_state[key] = _resolve_auth_events(
|
|
|
|
room_version, events, auth_events
|
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
auth_events.update(resolved_state)
|
|
|
|
|
2020-06-15 07:03:36 -04:00
|
|
|
for key, events in conflicted_state.items():
|
2018-08-07 10:22:40 -04:00
|
|
|
if key[0] == EventTypes.Member:
|
|
|
|
logger.debug("Resolving conflicted member lists %r", events)
|
2021-07-26 12:17:00 -04:00
|
|
|
resolved_state[key] = _resolve_auth_events(
|
|
|
|
room_version, events, auth_events
|
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
auth_events.update(resolved_state)
|
|
|
|
|
2020-06-15 07:03:36 -04:00
|
|
|
for key, events in conflicted_state.items():
|
2018-08-07 10:22:40 -04:00
|
|
|
if key not in resolved_state:
|
|
|
|
logger.debug("Resolving conflicted state %r:%r", key, events)
|
2019-06-20 05:32:02 -04:00
|
|
|
resolved_state[key] = _resolve_normal_events(events, auth_events)
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
return resolved_state
|
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _resolve_auth_events(
|
2021-07-26 12:17:00 -04:00
|
|
|
room_version: RoomVersion, events: List[EventBase], auth_events: StateMap[EventBase]
|
2020-08-24 14:25:27 -04:00
|
|
|
) -> EventBase:
|
2020-02-21 07:15:07 -05:00
|
|
|
reverse = list(reversed(_ordered_events(events)))
|
2018-08-07 10:22:40 -04:00
|
|
|
|
2020-02-21 07:15:07 -05:00
|
|
|
auth_keys = {
|
2021-07-26 12:17:00 -04:00
|
|
|
key
|
|
|
|
for event in events
|
|
|
|
for key in event_auth.auth_types_for_event(room_version, event)
|
2020-02-21 07:15:07 -05:00
|
|
|
}
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
new_auth_events = {}
|
|
|
|
for key in auth_keys:
|
|
|
|
auth_event = auth_events.get(key, None)
|
|
|
|
if auth_event:
|
|
|
|
new_auth_events[key] = auth_event
|
|
|
|
|
|
|
|
auth_events = new_auth_events
|
|
|
|
|
|
|
|
prev_event = reverse[0]
|
|
|
|
for event in reverse[1:]:
|
|
|
|
auth_events[(prev_event.type, prev_event.state_key)] = prev_event
|
|
|
|
try:
|
|
|
|
# The signatures have already been checked at this point
|
2022-06-15 14:48:22 -04:00
|
|
|
event_auth.check_state_dependent_auth_rules(
|
2019-04-01 05:24:38 -04:00
|
|
|
event,
|
2021-10-18 13:28:30 -04:00
|
|
|
auth_events.values(),
|
2019-01-25 13:31:41 -05:00
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
prev_event = event
|
|
|
|
except AuthError:
|
|
|
|
return prev_event
|
|
|
|
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _resolve_normal_events(
|
|
|
|
events: List[EventBase], auth_events: StateMap[EventBase]
|
|
|
|
) -> EventBase:
|
2018-08-07 10:22:40 -04:00
|
|
|
for event in _ordered_events(events):
|
|
|
|
try:
|
|
|
|
# The signatures have already been checked at this point
|
2022-06-15 14:48:22 -04:00
|
|
|
event_auth.check_state_dependent_auth_rules(
|
2019-04-01 05:24:38 -04:00
|
|
|
event,
|
2021-10-18 13:28:30 -04:00
|
|
|
auth_events.values(),
|
2019-01-25 13:31:41 -05:00
|
|
|
)
|
2018-08-07 10:22:40 -04:00
|
|
|
return event
|
|
|
|
except AuthError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the last event (the one with the least depth) if they all fail
|
|
|
|
# the auth check.
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
2020-08-24 14:25:27 -04:00
|
|
|
def _ordered_events(events: Iterable[EventBase]) -> List[EventBase]:
|
2021-10-06 13:55:25 -04:00
|
|
|
def key_func(e: EventBase) -> Tuple[int, str]:
|
2018-12-03 05:47:48 -05:00
|
|
|
# we have to use utf-8 rather than ascii here because it turns out we allow
|
|
|
|
# people to send us events with non-ascii event IDs :/
|
2019-06-20 05:32:02 -04:00
|
|
|
return -int(e.depth), hashlib.sha1(e.event_id.encode("utf-8")).hexdigest()
|
2018-08-07 10:22:40 -04:00
|
|
|
|
|
|
|
return sorted(events, key=key_func)
|