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

Commit 575b296

Browse files
committed
Improve validation for annotations.
1 parent 52c88a3 commit 575b296

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

synapse/events/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@ async def _injected_bundled_aggregations(
459459
# The bundled aggregations to include.
460460
aggregations = {}
461461

462-
annotations = await self.store.get_aggregation_groups_for_event(event_id)
462+
annotations = await self.store.get_aggregation_groups_for_event(
463+
event_id, room_id
464+
)
463465
if annotations.chunk:
464466
aggregations[RelationTypes.ANNOTATION] = annotations.to_dict()
465467

synapse/rest/client/relations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ async def on_GET(
318318

319319
pagination_chunk = await self.store.get_aggregation_groups_for_event(
320320
event_id=parent_id,
321+
room_id=room_id,
321322
event_type=event_type,
322323
limit=limit,
323324
from_token=from_token,

synapse/storage/databases/main/relations.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ async def event_is_target_of_relation(self, parent_id: str) -> bool:
201201
async def get_aggregation_groups_for_event(
202202
self,
203203
event_id: str,
204+
room_id: str,
204205
event_type: Optional[str] = None,
205206
limit: int = 5,
206207
direction: str = "b",
@@ -215,6 +216,7 @@ async def get_aggregation_groups_for_event(
215216
216217
Args:
217218
event_id: Fetch events that relate to this event ID.
219+
room_id: The room the event belongs to.
218220
event_type: Only fetch events with this event type, if given.
219221
limit: Only fetch the `limit` groups.
220222
direction: Whether to fetch the highest count first (`"b"`) or
@@ -227,8 +229,12 @@ async def get_aggregation_groups_for_event(
227229
`type`, `key` and `count` fields.
228230
"""
229231

230-
where_clause = ["relates_to_id = ?", "relation_type = ?"]
231-
where_args: List[Union[str, int]] = [event_id, RelationTypes.ANNOTATION]
232+
where_clause = ["relates_to_id = ?", "room_id = ?", "relation_type = ?"]
233+
where_args: List[Union[str, int]] = [
234+
event_id,
235+
room_id,
236+
RelationTypes.ANNOTATION,
237+
]
232238

233239
if event_type:
234240
where_clause.append("type = ?")

tests/rest/client/test_relations.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ def test_ignore_invalid_room(self):
664664
with patch(
665665
"synapse.handlers.message.EventCreationHandler._validate_event_relation"
666666
):
667+
# Generate a reaction and reference relations from a different room.
667668
self.get_success(
668669
inject_event(
669670
self.hs,
@@ -680,6 +681,23 @@ def test_ignore_invalid_room(self):
680681
)
681682
)
682683

684+
self.get_success(
685+
inject_event(
686+
self.hs,
687+
room_id=self.room,
688+
type="m.room.message",
689+
sender=self.user_id,
690+
content={
691+
"body": "foo",
692+
"msgtype": "m.text",
693+
"m.relates_to": {
694+
"rel_type": RelationTypes.REFERENCE,
695+
"event_id": parent_id,
696+
},
697+
},
698+
)
699+
)
700+
683701
# They should be ignored when fetching relations.
684702
channel = self.make_request(
685703
"GET",

0 commit comments

Comments
 (0)