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

Commit

Permalink
Use Pydantic when PUTting room aliases (#14179)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson authored Oct 17, 2022
1 parent 2c2c3f8 commit ccce8cd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
1 change: 1 addition & 0 deletions changelog.d/14179.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve the validation of the following PUT endpoints: [`/directory/room/{roomAlias}`](https://spec.matrix.org/v1.4/client-server-api/#put_matrixclientv3directoryroomroomalias), [`/directory/list/room/{roomId}`](https://spec.matrix.org/v1.4/client-server-api/#put_matrixclientv3directorylistroomroomid) and [`/directory/list/appservice/{networkId}/{roomId}`](https://spec.matrix.org/v1.4/application-service-api/#put_matrixclientv3directorylistappservicenetworkidroomid).
19 changes: 11 additions & 8 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import string
from typing import TYPE_CHECKING, Iterable, List, Optional

from typing_extensions import Literal

from synapse.api.constants import MAX_ALIAS_LENGTH, EventTypes
from synapse.api.errors import (
AuthError,
Expand Down Expand Up @@ -429,7 +431,10 @@ async def _user_can_delete_alias(
return await self.auth.check_can_change_room_list(room_id, requester)

async def edit_published_room_list(
self, requester: Requester, room_id: str, visibility: str
self,
requester: Requester,
room_id: str,
visibility: Literal["public", "private"],
) -> None:
"""Edit the entry of the room in the published room list.
Expand All @@ -451,9 +456,6 @@ async def edit_published_room_list(
if requester.is_guest:
raise AuthError(403, "Guests cannot edit the published room list")

if visibility not in ["public", "private"]:
raise SynapseError(400, "Invalid visibility setting")

if visibility == "public" and not self.enable_room_list_search:
# The room list has been disabled.
raise AuthError(
Expand Down Expand Up @@ -505,7 +507,11 @@ async def edit_published_room_list(
await self.store.set_room_is_public(room_id, making_public)

async def edit_published_appservice_room_list(
self, appservice_id: str, network_id: str, room_id: str, visibility: str
self,
appservice_id: str,
network_id: str,
room_id: str,
visibility: Literal["public", "private"],
) -> None:
"""Add or remove a room from the appservice/network specific public
room list.
Expand All @@ -516,9 +522,6 @@ async def edit_published_appservice_room_list(
room_id
visibility: either "public" or "private"
"""
if visibility not in ["public", "private"]:
raise SynapseError(400, "Invalid visibility setting")

await self.store.set_room_is_public_appservice(
room_id, appservice_id, network_id, visibility == "public"
)
Expand Down
58 changes: 35 additions & 23 deletions synapse/rest/client/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@
# limitations under the License.

import logging
from typing import TYPE_CHECKING, Tuple
from typing import TYPE_CHECKING, List, Optional, Tuple

from pydantic import StrictStr
from typing_extensions import Literal

from twisted.web.server import Request

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.servlet import (
RestServlet,
parse_and_validate_json_object_from_request,
)
from synapse.http.site import SynapseRequest
from synapse.rest.client._base import client_patterns
from synapse.rest.models import RequestBodyModel
from synapse.types import JsonDict, RoomAlias

if TYPE_CHECKING:
Expand Down Expand Up @@ -54,38 +61,35 @@ async def on_GET(self, request: Request, room_alias: str) -> Tuple[int, JsonDict

return 200, res

class PutBody(RequestBodyModel):
# TODO: get Pydantic to validate that this is a valid room id?
room_id: StrictStr
# `servers` is unspecced
servers: Optional[List[StrictStr]] = None

async def on_PUT(
self, request: SynapseRequest, room_alias: str
) -> Tuple[int, JsonDict]:
if not RoomAlias.is_valid(room_alias):
raise SynapseError(400, "Room alias invalid", errcode=Codes.INVALID_PARAM)
room_alias_obj = RoomAlias.from_string(room_alias)

content = parse_json_object_from_request(request)
if "room_id" not in content:
raise SynapseError(
400, 'Missing params: ["room_id"]', errcode=Codes.BAD_JSON
)
content = parse_and_validate_json_object_from_request(request, self.PutBody)

logger.debug("Got content: %s", content)
logger.debug("Got room name: %s", room_alias_obj.to_string())

room_id = content["room_id"]
servers = content["servers"] if "servers" in content else None

logger.debug("Got room_id: %s", room_id)
logger.debug("Got servers: %s", servers)
logger.debug("Got room_id: %s", content.room_id)
logger.debug("Got servers: %s", content.servers)

# TODO(erikj): Check types.

room = await self.store.get_room(room_id)
room = await self.store.get_room(content.room_id)
if room is None:
raise SynapseError(400, "Room does not exist")

requester = await self.auth.get_user_by_req(request)

await self.directory_handler.create_association(
requester, room_alias_obj, room_id, servers
requester, room_alias_obj, content.room_id, content.servers
)

return 200, {}
Expand Down Expand Up @@ -137,16 +141,18 @@ async def on_GET(self, request: Request, room_id: str) -> Tuple[int, JsonDict]:

return 200, {"visibility": "public" if room["is_public"] else "private"}

class PutBody(RequestBodyModel):
visibility: Literal["public", "private"] = "public"

async def on_PUT(
self, request: SynapseRequest, room_id: str
) -> Tuple[int, JsonDict]:
requester = await self.auth.get_user_by_req(request)

content = parse_json_object_from_request(request)
visibility = content.get("visibility", "public")
content = parse_and_validate_json_object_from_request(request, self.PutBody)

await self.directory_handler.edit_published_room_list(
requester, room_id, visibility
requester, room_id, content.visibility
)

return 200, {}
Expand All @@ -163,20 +169,26 @@ def __init__(self, hs: "HomeServer"):
self.directory_handler = hs.get_directory_handler()
self.auth = hs.get_auth()

class PutBody(RequestBodyModel):
visibility: Literal["public", "private"] = "public"

async def on_PUT(
self, request: SynapseRequest, network_id: str, room_id: str
) -> Tuple[int, JsonDict]:
content = parse_json_object_from_request(request)
visibility = content.get("visibility", "public")
return await self._edit(request, network_id, room_id, visibility)
content = parse_and_validate_json_object_from_request(request, self.PutBody)
return await self._edit(request, network_id, room_id, content.visibility)

async def on_DELETE(
self, request: SynapseRequest, network_id: str, room_id: str
) -> Tuple[int, JsonDict]:
return await self._edit(request, network_id, room_id, "private")

async def _edit(
self, request: SynapseRequest, network_id: str, room_id: str, visibility: str
self,
request: SynapseRequest,
network_id: str,
room_id: str,
visibility: Literal["public", "private"],
) -> Tuple[int, JsonDict]:
requester = await self.auth.get_user_by_req(request)
if not requester.app_service:
Expand Down

0 comments on commit ccce8cd

Please sign in to comment.