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

Commit abedf7d

Browse files
authored
Get rid of _auth_and_persist_event (#10781)
This is only called in two places, and the code seems much clearer without it.
1 parent 03caba6 commit abedf7d

File tree

3 files changed

+43
-63
lines changed

3 files changed

+43
-63
lines changed

changelog.d/10781.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: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -909,12 +909,18 @@ async def _process_received_pdu(
909909
context = await self._state_handler.compute_event_context(
910910
event, old_state=state
911911
)
912-
await self._auth_and_persist_event(
913-
origin, event, context, state=state, backfilled=backfilled
912+
context = await self._check_event_auth(
913+
origin,
914+
event,
915+
context,
916+
state=state,
917+
backfilled=backfilled,
914918
)
915919
except AuthError as e:
916920
raise FederationError("ERROR", e.code, e.msg, affected=event.event_id)
917921

922+
await self._run_push_actions_and_persist_event(event, context, backfilled)
923+
918924
if backfilled:
919925
return
920926

@@ -1239,51 +1245,6 @@ async def prep(ev_info: _NewEventInfo):
12391245
],
12401246
)
12411247

1242-
async def _auth_and_persist_event(
1243-
self,
1244-
origin: str,
1245-
event: EventBase,
1246-
context: EventContext,
1247-
state: Optional[Iterable[EventBase]] = None,
1248-
claimed_auth_event_map: Optional[StateMap[EventBase]] = None,
1249-
backfilled: bool = False,
1250-
) -> None:
1251-
"""
1252-
Process an event by performing auth checks and then persisting to the database.
1253-
1254-
Args:
1255-
origin: The host the event originates from.
1256-
event: The event itself.
1257-
context:
1258-
The event context.
1259-
1260-
state:
1261-
The state events used to check the event for soft-fail. If this is
1262-
not provided the current state events will be used.
1263-
1264-
claimed_auth_event_map:
1265-
A map of (type, state_key) => event for the event's claimed auth_events.
1266-
Possibly incomplete, and possibly including events that are not yet
1267-
persisted, or authed, or in the right room.
1268-
1269-
Only populated when populating outliers.
1270-
1271-
backfilled: True if the event was backfilled.
1272-
"""
1273-
# claimed_auth_event_map should be given iff the event is an outlier
1274-
assert bool(claimed_auth_event_map) == event.internal_metadata.outlier
1275-
1276-
context = await self._check_event_auth(
1277-
origin,
1278-
event,
1279-
context,
1280-
state=state,
1281-
claimed_auth_event_map=claimed_auth_event_map,
1282-
backfilled=backfilled,
1283-
)
1284-
1285-
await self._run_push_actions_and_persist_event(event, context, backfilled)
1286-
12871248
async def _check_event_auth(
12881249
self,
12891250
origin: str,
@@ -1558,39 +1519,45 @@ async def _update_auth_events_and_context_for_auth(
15581519
event.room_id, [e.event_id for e in remote_auth_chain]
15591520
)
15601521

1561-
for e in remote_auth_chain:
1562-
if e.event_id in seen_remotes:
1522+
for auth_event in remote_auth_chain:
1523+
if auth_event.event_id in seen_remotes:
15631524
continue
15641525

1565-
if e.event_id == event.event_id:
1526+
if auth_event.event_id == event.event_id:
15661527
continue
15671528

15681529
try:
1569-
auth_ids = e.auth_event_ids()
1530+
auth_ids = auth_event.auth_event_ids()
15701531
auth = {
15711532
(e.type, e.state_key): e
15721533
for e in remote_auth_chain
15731534
if e.event_id in auth_ids or e.type == EventTypes.Create
15741535
}
1575-
e.internal_metadata.outlier = True
1536+
auth_event.internal_metadata.outlier = True
15761537

15771538
logger.debug(
15781539
"_check_event_auth %s missing_auth: %s",
15791540
event.event_id,
1580-
e.event_id,
1541+
auth_event.event_id,
15811542
)
15821543
missing_auth_event_context = (
1583-
await self._state_handler.compute_event_context(e)
1544+
await self._state_handler.compute_event_context(auth_event)
15841545
)
1585-
await self._auth_and_persist_event(
1546+
1547+
missing_auth_event_context = await self._check_event_auth(
15861548
origin,
1587-
e,
1549+
auth_event,
15881550
missing_auth_event_context,
15891551
claimed_auth_event_map=auth,
15901552
)
1553+
await self.persist_events_and_notify(
1554+
event.room_id, [(auth_event, missing_auth_event_context)]
1555+
)
15911556

1592-
if e.event_id in event_auth_events:
1593-
auth_events[(e.type, e.state_key)] = e
1557+
if auth_event.event_id in event_auth_events:
1558+
auth_events[
1559+
(auth_event.type, auth_event.state_key)
1560+
] = auth_event
15941561
except AuthError:
15951562
pass
15961563

@@ -1733,10 +1700,13 @@ async def _run_push_actions_and_persist_event(
17331700
context: The event context.
17341701
backfilled: True if the event was backfilled.
17351702
"""
1703+
# this method should not be called on outliers (those code paths call
1704+
# persist_events_and_notify directly.)
1705+
assert not event.internal_metadata.outlier
1706+
17361707
try:
17371708
if (
1738-
not event.internal_metadata.is_outlier()
1739-
and not backfilled
1709+
not backfilled
17401710
and not context.rejected
17411711
and (await self._store.get_min_depth(event.room_id)) <= event.depth
17421712
):

tests/test_federation.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,18 @@ def setUp(self):
7676

7777
self.handler = self.homeserver.get_federation_handler()
7878
federation_event_handler = self.homeserver.get_federation_event_handler()
79-
federation_event_handler._check_event_auth = lambda origin, event, context, state, claimed_auth_event_map, backfilled: succeed(
80-
context
81-
)
79+
80+
async def _check_event_auth(
81+
origin,
82+
event,
83+
context,
84+
state=None,
85+
claimed_auth_event_map=None,
86+
backfilled=False,
87+
):
88+
return context
89+
90+
federation_event_handler._check_event_auth = _check_event_auth
8291
self.client = self.homeserver.get_federation_client()
8392
self.client._check_sigs_and_hash_and_fetch = lambda dest, pdus, **k: succeed(
8493
pdus

0 commit comments

Comments
 (0)