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

Verify ?chunk_id actually corresponds to an insertion event that exists (MSC2716) #10776

Merged
merged 5 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10776.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only allow the [MSC2716](https://github.com/matrix-org/matrix-doc/pull/2716) `/batch_send?chunk_id=xxx` endpoint to connect to an already existing insertion event.
13 changes: 12 additions & 1 deletion synapse/rest/client/room_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,18 @@ async def on_POST(
# event, which causes the HS to ask for the state at the start of
# the chunk later.
prev_event_ids = [fake_prev_event_id]
# TODO: Verify the chunk_id_from_query corresponds to an insertion event

# Verify the chunk_id_from_query corresponds to an actual insertion event
# and have the chunk connected.
corresponding_insertion_event_id = (
await self.store.get_insertion_event_by_chunk_id(chunk_id_from_query)
)
if corresponding_insertion_event_id is None:
raise SynapseError(
400,
"No insertion event corresponds to the given ?chunk_id",
errcode=Codes.INVALID_PARAM,
)
pass
# Otherwise, create an insertion event to act as a starting point.
#
Expand Down
2 changes: 2 additions & 0 deletions synapse/storage/databases/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
from .rejections import RejectionsStore
from .relations import RelationsStore
from .room import RoomStore
from .room_batch import RoomBatchStore
from .roommember import RoomMemberStore
from .search import SearchStore
from .session import SessionStore
Expand All @@ -81,6 +82,7 @@ class DataStore(
EventsBackgroundUpdatesStore,
RoomMemberStore,
RoomStore,
RoomBatchStore,
RegistrationStore,
StreamStore,
ProfileStore,
Expand Down
36 changes: 36 additions & 0 deletions synapse/storage/databases/main/room_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional

from synapse.storage._base import SQLBaseStore


class RoomBatchStore(SQLBaseStore):
async def get_insertion_event_by_chunk_id(self, chunk_id: str) -> Optional[str]:
"""Retrieve a insertion event ID.

Args:
chunk_id: The chunk ID of the insertion event to retrieve.

Returns:
The event_id of an insertion event, or None if there is no known
insertion event for the given insertion event.
"""
return await self.db_pool.simple_select_one_onecol(
table="insertion_events",
keyvalues={"next_chunk_id": chunk_id},
retcol="event_id",
allow_none=True,
)