-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Check auth on received events' auth_events #11001
Changes from all commits
5cd2255
a53b1a0
2e64051
1dd6077
f2b011d
0ce88de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a long-standing bug which meant that events received over federation were sometimes incorrectly accepted into the room state. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a long-standing bug which meant that events received over federation were sometimes incorrectly accepted into the room state. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1197,7 +1197,7 @@ async def _auth_and_persist_fetched_events_inner( | |
|
||
def prep(event: EventBase) -> Optional[Tuple[EventBase, EventContext]]: | ||
with nested_logging_context(suffix=event.event_id): | ||
auth = {} | ||
auth = [] | ||
for auth_event_id in event.auth_event_ids(): | ||
ae = persisted_events.get(auth_event_id) | ||
if not ae: | ||
|
@@ -1210,7 +1210,7 @@ def prep(event: EventBase) -> Optional[Tuple[EventBase, EventContext]]: | |
# exist, which means it is premature to reject `event`. Instead we | ||
# just ignore it for now. | ||
return None | ||
auth[(ae.type, ae.state_key)] = ae | ||
auth.append(ae) | ||
|
||
context = EventContext.for_outlier() | ||
try: | ||
|
@@ -1250,6 +1250,10 @@ async def _check_event_auth( | |
|
||
Returns: | ||
The updated context object. | ||
|
||
Raises: | ||
AuthError if we were unable to find copies of the event's auth events. | ||
(Most other failures just cause us to set `context.rejected`.) | ||
""" | ||
# This method should only be used for non-outliers | ||
assert not event.internal_metadata.outlier | ||
|
@@ -1266,7 +1270,26 @@ async def _check_event_auth( | |
context.rejected = RejectedReason.AUTH_ERROR | ||
return context | ||
|
||
# calculate what the auth events *should* be, to use as a basis for auth. | ||
# next, check that we have all of the event's auth events. | ||
# | ||
# Note that this can raise AuthError, which we want to propagate to the | ||
# caller rather than swallow with `context.rejected` (since we cannot be | ||
# certain that there is a permanent problem with the event). | ||
claimed_auth_events = await self._load_or_fetch_auth_events_for_event( | ||
origin, event | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we only doing this for non-outliers? We want to have the full auth chain for outlier events too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we already do it for outlier events. They go through a completely different code path (_auth_and_persist_fetched_events which does the same sort of thing as this in |
||
|
||
# ... and check that the event passes auth at those auth events. | ||
try: | ||
check_auth_rules_for_event(room_version_obj, event, claimed_auth_events) | ||
except AuthError as e: | ||
logger.warning( | ||
"While checking auth of %r against auth_events: %s", event, e | ||
) | ||
context.rejected = RejectedReason.AUTH_ERROR | ||
return context | ||
|
||
# now check auth against what we think the auth events *should* be. | ||
prev_state_ids = await context.get_prev_state_ids() | ||
auth_events_ids = self._event_auth_handler.compute_auth_events( | ||
event, prev_state_ids, for_verification=True | ||
|
@@ -1299,7 +1322,9 @@ async def _check_event_auth( | |
auth_events_for_auth = calculated_auth_event_map | ||
|
||
try: | ||
check_auth_rules_for_event(room_version_obj, event, auth_events_for_auth) | ||
check_auth_rules_for_event( | ||
room_version_obj, event, auth_events_for_auth.values() | ||
) | ||
except AuthError as e: | ||
logger.warning("Failed auth resolution for %r because %s", event, e) | ||
context.rejected = RejectedReason.AUTH_ERROR | ||
|
@@ -1397,11 +1422,9 @@ async def _check_for_soft_fail( | |
current_state_ids_list = [ | ||
e for k, e in current_state_ids.items() if k in auth_types | ||
] | ||
|
||
auth_events_map = await self._store.get_events(current_state_ids_list) | ||
current_auth_events = { | ||
(e.type, e.state_key): e for e in auth_events_map.values() | ||
} | ||
current_auth_events = await self._store.get_events_as_list( | ||
current_state_ids_list | ||
) | ||
|
||
try: | ||
check_auth_rules_for_event(room_version_obj, event, current_auth_events) | ||
|
@@ -1466,6 +1489,9 @@ async def _update_auth_events_and_context_for_auth( | |
# if we have missing events, we need to fetch those events from somewhere. | ||
# | ||
# we start by checking if they are in the store, and then try calling /event_auth/. | ||
# | ||
# TODO: this code is now redundant, since it should be impossible for us to | ||
# get here without already having the auth events. | ||
Comment on lines
+1493
to
+1494
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to clean this up in another PR. |
||
if missing_auth: | ||
have_events = await self._store.have_seen_events( | ||
event.room_id, missing_auth | ||
|
@@ -1569,7 +1595,7 @@ async def _update_auth_events_and_context_for_auth( | |
logger.info( | ||
"After state res: updating auth_events with new state %s", | ||
{ | ||
(d.type, d.state_key): d.event_id | ||
d | ||
for d in new_state.values() | ||
if auth_events.get((d.type, d.state_key)) != d | ||
}, | ||
|
@@ -1583,6 +1609,75 @@ async def _update_auth_events_and_context_for_auth( | |
|
||
return context, auth_events | ||
|
||
async def _load_or_fetch_auth_events_for_event( | ||
self, destination: str, event: EventBase | ||
) -> Collection[EventBase]: | ||
"""Fetch this event's auth_events, from database or remote | ||
|
||
Loads any of the auth_events that we already have from the database/cache. If | ||
there are any that are missing, calls /event_auth to get the complete auth | ||
chain for the event (and then attempts to load the auth_events again). | ||
|
||
If any of the auth_events cannot be found, raises an AuthError. This can happen | ||
for a number of reasons; eg: the events don't exist, or we were unable to talk | ||
to `destination`, or we couldn't validate the signature on the event (which | ||
in turn has multiple potential causes). | ||
|
||
Args: | ||
destination: where to send the /event_auth request. Typically the server | ||
that sent us `event` in the first place. | ||
event: the event whose auth_events we want | ||
|
||
Returns: | ||
all of the events in `event.auth_events`, after deduplication | ||
|
||
Raises: | ||
AuthError if we were unable to fetch the auth_events for any reason. | ||
""" | ||
event_auth_event_ids = set(event.auth_event_ids()) | ||
event_auth_events = await self._store.get_events( | ||
event_auth_event_ids, allow_rejected=True | ||
) | ||
missing_auth_event_ids = event_auth_event_ids.difference( | ||
event_auth_events.keys() | ||
) | ||
if not missing_auth_event_ids: | ||
return event_auth_events.values() | ||
|
||
logger.info( | ||
"Event %s refers to unknown auth events %s: fetching auth chain", | ||
event, | ||
missing_auth_event_ids, | ||
) | ||
try: | ||
await self._get_remote_auth_chain_for_event( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Getting the entire auth chain again is fairly heavyweight. I sort of wonder if we should only fetch the missing auth events, falling back to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, probably, though as you say, I think it's a change for another day. |
||
destination, event.room_id, event.event_id | ||
) | ||
Comment on lines
+1652
to
+1655
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the use of |
||
except Exception as e: | ||
logger.warning("Failed to get auth chain for %s: %s", event, e) | ||
# in this case, it's very likely we still won't have all the auth | ||
# events - but we pick that up below. | ||
|
||
# try to fetch the auth events we missed list time. | ||
extra_auth_events = await self._store.get_events( | ||
missing_auth_event_ids, allow_rejected=True | ||
) | ||
missing_auth_event_ids.difference_update(extra_auth_events.keys()) | ||
event_auth_events.update(extra_auth_events) | ||
if not missing_auth_event_ids: | ||
return event_auth_events.values() | ||
|
||
# we still don't have all the auth events. | ||
logger.warning( | ||
"Missing auth events for %s: %s", | ||
event, | ||
shortstr(missing_auth_event_ids), | ||
) | ||
# the fact we can't find the auth event doesn't mean it doesn't | ||
# exist, which means it is premature to store `event` as rejected. | ||
# instead we raise an AuthError, which will make the caller ignore it. | ||
raise AuthError(code=HTTPStatus.FORBIDDEN, msg="Auth events could not be found") | ||
|
||
async def _get_remote_auth_chain_for_event( | ||
self, destination: str, room_id: str, event_id: str | ||
) -> None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm scared of this breaking more MSC2716 stuff (related #10764)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MadLittleMods I hope it won't :/
Can you explain your concern in more detail? I don't think this is directly related to #10764, which as I read it is about whether remote servers can provide the
prev_events
for a given event, rather than theauth_events
.The protocol in general, and Synapse in particular, already assume quite heavily that you must have copies of the
auth_events
for any event you end up serving over federation. This PR fixes some edge-cases where we weren't properly checking it, but doesn't change the fundamental philosophy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a bit squeamish about making sure the remote homeservers can still accept our imported historical events. It might be fine, just sounds kinda related to something that could make it harder. Our
auth_events
on historical events are real (some outliers) so this probably works. We only use fakeprev_events
.The other issue was just related in terms of other breaking changes.