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

Commit

Permalink
Use Pydantic to validate PUT /directory/list/room/{roomId}
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Oct 13, 2022
1 parent 55d4bf8 commit fb9b361
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import logging
import string
from typing import TYPE_CHECKING, Iterable, List, Optional
from typing import TYPE_CHECKING, Iterable, List, Literal, Optional

from synapse.api.constants import MAX_ALIAS_LENGTH, EventTypes
from synapse.api.errors import (
Expand Down Expand Up @@ -429,7 +429,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 +454,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
9 changes: 6 additions & 3 deletions synapse/rest/client/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import TYPE_CHECKING, List, Optional, Tuple

from pydantic import StrictStr
from typing_extensions import Literal

from twisted.web.server import Request

Expand Down Expand Up @@ -141,16 +142,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 Down

0 comments on commit fb9b361

Please sign in to comment.