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

Extra validation for rest/client/account_data #13148

Merged
merged 2 commits into from
Jul 1, 2022
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/13148.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve validation logic in Synapse's REST endpoints.
19 changes: 17 additions & 2 deletions synapse/rest/client/account_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import logging
from typing import TYPE_CHECKING, Tuple

from synapse.api.errors import AuthError, NotFoundError, SynapseError
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
from synapse.http.server import HttpServer
from synapse.http.servlet import RestServlet, parse_json_object_from_request
from synapse.http.site import SynapseRequest
from synapse.types import JsonDict
from synapse.types import JsonDict, RoomID

from ._base import client_patterns

Expand Down Expand Up @@ -104,13 +104,21 @@ async def on_PUT(
if user_id != requester.user.to_string():
raise AuthError(403, "Cannot add account data for other users.")

if not RoomID.is_valid(room_id):
raise SynapseError(
400,
f"{room_id} is not a valid room ID",
Codes.INVALID_PARAM,
)

body = parse_json_object_from_request(request)

if account_data_type == "m.fully_read":
raise SynapseError(
405,
"Cannot set m.fully_read through this API."
" Use /rooms/!roomId:server.name/read_markers",
Codes.BAD_JSON,
)

await self.handler.add_account_data_to_room(
Expand All @@ -130,6 +138,13 @@ async def on_GET(
if user_id != requester.user.to_string():
raise AuthError(403, "Cannot get account data for other users.")

if not RoomID.is_valid(room_id):
raise SynapseError(
400,
f"{room_id} is not a valid room ID",
Codes.INVALID_PARAM,
)

event = await self.store.get_account_data_for_room_and_type(
user_id, room_id, account_data_type
)
Expand Down