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

Commit 0fe8e5d

Browse files
committed
Verify ?chunk_id actually corresponds to an insertion event that exists
Part of #10737 Part of [MSC2716](matrix-org/matrix-spec-proposals#2716)
1 parent 857b000 commit 0fe8e5d

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

synapse/rest/client/room_batch.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,18 @@ async def on_POST(
299299
# event, which causes the HS to ask for the state at the start of
300300
# the chunk later.
301301
prev_event_ids = [fake_prev_event_id]
302-
# TODO: Verify the chunk_id_from_query corresponds to an insertion event
302+
303+
# Verify the chunk_id_from_query corresponds to an actual insertion event
304+
# and have the chunk connected.
305+
corresponding_insertion_event_id = (
306+
await self.store.get_insertion_event_by_chunk_id(chunk_id_from_query)
307+
)
308+
if corresponding_insertion_event_id is None:
309+
raise SynapseError(
310+
400,
311+
"No insertion event corresponds to the given ?chunk_id",
312+
errcode=Codes.INVALID_PARAM,
313+
)
303314
pass
304315
# Otherwise, create an insertion event to act as a starting point.
305316
#

synapse/storage/databases/main/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
from .rejections import RejectionsStore
6262
from .relations import RelationsStore
6363
from .room import RoomStore
64+
from .room_batch import RoomBatchStore
6465
from .roommember import RoomMemberStore
6566
from .search import SearchStore
6667
from .session import SessionStore
@@ -81,6 +82,7 @@ class DataStore(
8182
EventsBackgroundUpdatesStore,
8283
RoomMemberStore,
8384
RoomStore,
85+
RoomBatchStore,
8486
RegistrationStore,
8587
StreamStore,
8688
ProfileStore,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2021 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Optional
16+
17+
from synapse.storage._base import SQLBaseStore
18+
from synapse.storage.database import DatabasePool
19+
20+
21+
class RoomBatchStore(SQLBaseStore):
22+
def __init__(self, database: DatabasePool, db_conn, hs):
23+
super().__init__(database, db_conn, hs)
24+
25+
async def get_insertion_event_by_chunk_id(self, chunk_id: str) -> Optional[str]:
26+
"""Retrieve a insertion event ID.
27+
28+
Args:
29+
chunk_id: The chunk ID of the insertion event to retrieve.
30+
31+
Returns:
32+
The event_id of an insertion event, or None if there is no known
33+
insertion event for the given insertion event.
34+
"""
35+
return await self.db_pool.simple_select_one_onecol(
36+
table="insertion_events",
37+
keyvalues={"next_chunk_id": chunk_id},
38+
retcol="event_id",
39+
allow_none=True,
40+
)

0 commit comments

Comments
 (0)