Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 2622b28

Browse files
authored
Inline _check_event_auth for outliers (#10926)
* Inline `_check_event_auth` for outliers When we are persisting an outlier, most of `_check_event_auth` is redundant: * `_update_auth_events_and_context_for_auth` does nothing, because the `input_auth_events` are (now) exactly the event's auth_events, which means that `missing_auth` is empty. * we don't care about soft-fail, kicking guest users or `send_on_behalf_of` for outliers ... so the only thing that matters is the auth itself, so let's just do that. * `_auth_and_persist_fetched_events_inner`: de-async `prep` `prep` no longer calls any `async` methods, so let's make it synchronous. * Simplify `_check_event_auth` We no longer need to support outliers here, which makes things rather simpler. * changelog * lint
1 parent eb2c7e5 commit 2622b28

File tree

4 files changed

+38
-59
lines changed

4 files changed

+38
-59
lines changed

changelog.d/10896.misc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Clean up some of the federation event authentication code for clarity.
1+
Clean up some of the federation event authentication code for clarity.

changelog.d/10926.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean up some of the federation event authentication code for clarity.

synapse/handlers/federation_event.py

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@
6868
UserID,
6969
get_domain_from_id,
7070
)
71-
from synapse.util.async_helpers import (
72-
Linearizer,
73-
concurrently_execute,
74-
yieldable_gather_results,
75-
)
71+
from synapse.util.async_helpers import Linearizer, concurrently_execute
7672
from synapse.util.iterutils import batch_iter
7773
from synapse.util.retryutils import NotRetryingDestination
7874
from synapse.util.stringutils import shortstr
@@ -1189,7 +1185,10 @@ async def _auth_and_persist_fetched_events_inner(
11891185
allow_rejected=True,
11901186
)
11911187

1192-
async def prep(event: EventBase) -> Optional[Tuple[EventBase, EventContext]]:
1188+
room_version = await self._store.get_room_version_id(room_id)
1189+
room_version_obj = KNOWN_ROOM_VERSIONS[room_version]
1190+
1191+
def prep(event: EventBase) -> Optional[Tuple[EventBase, EventContext]]:
11931192
with nested_logging_context(suffix=event.event_id):
11941193
auth = {}
11951194
for auth_event_id in event.auth_event_ids():
@@ -1207,17 +1206,15 @@ async def prep(event: EventBase) -> Optional[Tuple[EventBase, EventContext]]:
12071206
auth[(ae.type, ae.state_key)] = ae
12081207

12091208
context = EventContext.for_outlier()
1210-
context = await self._check_event_auth(
1211-
origin,
1212-
event,
1213-
context,
1214-
claimed_auth_event_map=auth,
1215-
)
1209+
try:
1210+
event_auth.check(room_version_obj, event, auth_events=auth)
1211+
except AuthError as e:
1212+
logger.warning("Rejecting %r because %s", event, e)
1213+
context.rejected = RejectedReason.AUTH_ERROR
1214+
12161215
return event, context
12171216

1218-
events_to_persist = (
1219-
x for x in await yieldable_gather_results(prep, fetched_events) if x
1220-
)
1217+
events_to_persist = (x for x in (prep(event) for event in fetched_events) if x)
12211218
await self.persist_events_and_notify(room_id, tuple(events_to_persist))
12221219

12231220
async def _check_event_auth(
@@ -1226,7 +1223,6 @@ async def _check_event_auth(
12261223
event: EventBase,
12271224
context: EventContext,
12281225
state: Optional[Iterable[EventBase]] = None,
1229-
claimed_auth_event_map: Optional[StateMap[EventBase]] = None,
12301226
backfilled: bool = False,
12311227
) -> EventContext:
12321228
"""
@@ -1242,42 +1238,36 @@ async def _check_event_auth(
12421238
The state events used to check the event for soft-fail. If this is
12431239
not provided the current state events will be used.
12441240
1245-
claimed_auth_event_map:
1246-
A map of (type, state_key) => event for the event's claimed auth_events.
1247-
Possibly including events that were rejected, or are in the wrong room.
1248-
1249-
Only populated when populating outliers.
1250-
12511241
backfilled: True if the event was backfilled.
12521242
12531243
Returns:
12541244
The updated context object.
12551245
"""
1256-
# claimed_auth_event_map should be given iff the event is an outlier
1257-
assert bool(claimed_auth_event_map) == event.internal_metadata.outlier
1246+
# This method should only be used for non-outliers
1247+
assert not event.internal_metadata.outlier
12581248

12591249
room_version = await self._store.get_room_version_id(event.room_id)
12601250
room_version_obj = KNOWN_ROOM_VERSIONS[room_version]
12611251

1262-
if claimed_auth_event_map:
1263-
# if we have a copy of the auth events from the event, use that as the
1264-
# basis for auth.
1265-
auth_events = claimed_auth_event_map
1266-
else:
1267-
# otherwise, we calculate what the auth events *should* be, and use that
1268-
prev_state_ids = await context.get_prev_state_ids()
1269-
auth_events_ids = self._event_auth_handler.compute_auth_events(
1270-
event, prev_state_ids, for_verification=True
1271-
)
1272-
auth_events_x = await self._store.get_events(auth_events_ids)
1273-
auth_events = {(e.type, e.state_key): e for e in auth_events_x.values()}
1252+
# calculate what the auth events *should* be, to use as a basis for auth.
1253+
prev_state_ids = await context.get_prev_state_ids()
1254+
auth_events_ids = self._event_auth_handler.compute_auth_events(
1255+
event, prev_state_ids, for_verification=True
1256+
)
1257+
auth_events_x = await self._store.get_events(auth_events_ids)
1258+
calculated_auth_event_map = {
1259+
(e.type, e.state_key): e for e in auth_events_x.values()
1260+
}
12741261

12751262
try:
12761263
(
12771264
context,
12781265
auth_events_for_auth,
12791266
) = await self._update_auth_events_and_context_for_auth(
1280-
origin, event, context, auth_events
1267+
origin,
1268+
event,
1269+
context,
1270+
calculated_auth_event_map=calculated_auth_event_map,
12811271
)
12821272
except Exception:
12831273
# We don't really mind if the above fails, so lets not fail
@@ -1289,7 +1279,7 @@ async def _check_event_auth(
12891279
"Ignoring failure and continuing processing of event.",
12901280
event.event_id,
12911281
)
1292-
auth_events_for_auth = auth_events
1282+
auth_events_for_auth = calculated_auth_event_map
12931283

12941284
try:
12951285
event_auth.check(room_version_obj, event, auth_events=auth_events_for_auth)
@@ -1425,7 +1415,7 @@ async def _update_auth_events_and_context_for_auth(
14251415
origin: str,
14261416
event: EventBase,
14271417
context: EventContext,
1428-
input_auth_events: StateMap[EventBase],
1418+
calculated_auth_event_map: StateMap[EventBase],
14291419
) -> Tuple[EventContext, StateMap[EventBase]]:
14301420
"""Helper for _check_event_auth. See there for docs.
14311421
@@ -1443,19 +1433,17 @@ async def _update_auth_events_and_context_for_auth(
14431433
event:
14441434
context:
14451435
1446-
input_auth_events:
1447-
Map from (event_type, state_key) to event
1448-
1449-
Normally, our calculated auth_events based on the state of the room
1450-
at the event's position in the DAG, though occasionally (eg if the
1451-
event is an outlier), may be the auth events claimed by the remote
1452-
server.
1436+
calculated_auth_event_map:
1437+
Our calculated auth_events based on the state of the room
1438+
at the event's position in the DAG.
14531439
14541440
Returns:
14551441
updated context, updated auth event map
14561442
"""
1457-
# take a copy of input_auth_events before we modify it.
1458-
auth_events: MutableStateMap[EventBase] = dict(input_auth_events)
1443+
assert not event.internal_metadata.outlier
1444+
1445+
# take a copy of calculated_auth_event_map before we modify it.
1446+
auth_events: MutableStateMap[EventBase] = dict(calculated_auth_event_map)
14591447

14601448
event_auth_events = set(event.auth_event_ids())
14611449

@@ -1496,15 +1484,6 @@ async def _update_auth_events_and_context_for_auth(
14961484
}
14971485
)
14981486

1499-
if event.internal_metadata.is_outlier():
1500-
# XXX: given that, for an outlier, we'll be working with the
1501-
# event's *claimed* auth events rather than those we calculated:
1502-
# (a) is there any point in this test, since different_auth below will
1503-
# obviously be empty
1504-
# (b) alternatively, why don't we do it earlier?
1505-
logger.info("Skipping auth_event fetch for outlier")
1506-
return context, auth_events
1507-
15081487
different_auth = event_auth_events.difference(
15091488
e.event_id for e in auth_events.values()
15101489
)

tests/test_federation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ async def _check_event_auth(
8282
event,
8383
context,
8484
state=None,
85-
claimed_auth_event_map=None,
8685
backfilled=False,
8786
):
8887
return context

0 commit comments

Comments
 (0)