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

Commit 7ffddd8

Browse files
Prevent historical state from being pushed to an application service via /transactions (MSC2716) (#11265)
Mark historical state from the MSC2716 `/batch_send` endpoint as `historical` which makes it `backfilled` and have a negative `stream_ordering` so it doesn't get queried by `/transactions`. Fix #11241 Complement tests: matrix-org/complement#221
1 parent 92b7538 commit 7ffddd8

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

changelog.d/11265.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent [MSC2716](https://github.com/matrix-org/matrix-doc/pull/2716) historical state events from being pushed to an application service via `/transactions`.

synapse/appservice/api.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,32 @@ async def push_bulk(
231231
json_body=body,
232232
args={"access_token": service.hs_token},
233233
)
234+
if logger.isEnabledFor(logging.DEBUG):
235+
logger.debug(
236+
"push_bulk to %s succeeded! events=%s",
237+
uri,
238+
[event.get("event_id") for event in events],
239+
)
234240
sent_transactions_counter.labels(service.id).inc()
235241
sent_events_counter.labels(service.id).inc(len(events))
236242
return True
237243
except CodeMessageException as e:
238-
logger.warning("push_bulk to %s received %s", uri, e.code)
244+
logger.warning(
245+
"push_bulk to %s received code=%s msg=%s",
246+
uri,
247+
e.code,
248+
e.msg,
249+
exc_info=logger.isEnabledFor(logging.DEBUG),
250+
)
239251
except Exception as ex:
240-
logger.warning("push_bulk to %s threw exception %s", uri, ex)
252+
logger.warning(
253+
"push_bulk to %s threw exception(%s) %s args=%s",
254+
uri,
255+
type(ex).__name__,
256+
ex,
257+
ex.args,
258+
exc_info=logger.isEnabledFor(logging.DEBUG),
259+
)
241260
failed_transactions_counter.labels(service.id).inc()
242261
return False
243262

synapse/handlers/room_batch.py

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ async def persist_state_events_at_start(
221221
action=membership,
222222
content=event_dict["content"],
223223
outlier=True,
224+
historical=True,
224225
prev_event_ids=[prev_event_id_for_state_chain],
225226
# Make sure to use a copy of this list because we modify it
226227
# later in the loop here. Otherwise it will be the same
@@ -240,6 +241,7 @@ async def persist_state_events_at_start(
240241
),
241242
event_dict,
242243
outlier=True,
244+
historical=True,
243245
prev_event_ids=[prev_event_id_for_state_chain],
244246
# Make sure to use a copy of this list because we modify it
245247
# later in the loop here. Otherwise it will be the same

synapse/handlers/room_member.py

+15
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ async def _local_membership_update(
268268
content: Optional[dict] = None,
269269
require_consent: bool = True,
270270
outlier: bool = False,
271+
historical: bool = False,
271272
) -> Tuple[str, int]:
272273
"""
273274
Internal membership update function to get an existing event or create
@@ -293,6 +294,9 @@ async def _local_membership_update(
293294
outlier: Indicates whether the event is an `outlier`, i.e. if
294295
it's from an arbitrary point and floating in the DAG as
295296
opposed to being inline with the current DAG.
297+
historical: Indicates whether the message is being inserted
298+
back in time around some existing events. This is used to skip
299+
a few checks and mark the event as backfilled.
296300
297301
Returns:
298302
Tuple of event ID and stream ordering position
@@ -337,6 +341,7 @@ async def _local_membership_update(
337341
auth_event_ids=auth_event_ids,
338342
require_consent=require_consent,
339343
outlier=outlier,
344+
historical=historical,
340345
)
341346

342347
prev_state_ids = await context.get_prev_state_ids()
@@ -433,6 +438,7 @@ async def update_membership(
433438
new_room: bool = False,
434439
require_consent: bool = True,
435440
outlier: bool = False,
441+
historical: bool = False,
436442
prev_event_ids: Optional[List[str]] = None,
437443
auth_event_ids: Optional[List[str]] = None,
438444
) -> Tuple[str, int]:
@@ -454,6 +460,9 @@ async def update_membership(
454460
outlier: Indicates whether the event is an `outlier`, i.e. if
455461
it's from an arbitrary point and floating in the DAG as
456462
opposed to being inline with the current DAG.
463+
historical: Indicates whether the message is being inserted
464+
back in time around some existing events. This is used to skip
465+
a few checks and mark the event as backfilled.
457466
prev_event_ids: The event IDs to use as the prev events
458467
auth_event_ids:
459468
The event ids to use as the auth_events for the new event.
@@ -487,6 +496,7 @@ async def update_membership(
487496
new_room=new_room,
488497
require_consent=require_consent,
489498
outlier=outlier,
499+
historical=historical,
490500
prev_event_ids=prev_event_ids,
491501
auth_event_ids=auth_event_ids,
492502
)
@@ -507,6 +517,7 @@ async def update_membership_locked(
507517
new_room: bool = False,
508518
require_consent: bool = True,
509519
outlier: bool = False,
520+
historical: bool = False,
510521
prev_event_ids: Optional[List[str]] = None,
511522
auth_event_ids: Optional[List[str]] = None,
512523
) -> Tuple[str, int]:
@@ -530,6 +541,9 @@ async def update_membership_locked(
530541
outlier: Indicates whether the event is an `outlier`, i.e. if
531542
it's from an arbitrary point and floating in the DAG as
532543
opposed to being inline with the current DAG.
544+
historical: Indicates whether the message is being inserted
545+
back in time around some existing events. This is used to skip
546+
a few checks and mark the event as backfilled.
533547
prev_event_ids: The event IDs to use as the prev events
534548
auth_event_ids:
535549
The event ids to use as the auth_events for the new event.
@@ -657,6 +671,7 @@ async def update_membership_locked(
657671
content=content,
658672
require_consent=require_consent,
659673
outlier=outlier,
674+
historical=historical,
660675
)
661676

662677
latest_event_ids = await self.store.get_prev_events_for_room(room_id)

0 commit comments

Comments
 (0)