Skip to content

Commit

Permalink
Implements parse_json function at filter_json appearances.
Browse files Browse the repository at this point in the history
Validating the JSON query parameter should fix element-hq#16922
  • Loading branch information
TrevisGordan committed Feb 15, 2024
1 parent 9608394 commit a040208
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
34 changes: 12 additions & 22 deletions synapse/rest/admin/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
assert_params_in_dict,
parse_enum,
parse_integer,
parse_json,
parse_json_object_from_request,
parse_string,
)
Expand Down Expand Up @@ -776,14 +777,8 @@ async def on_GET(
limit = parse_integer(request, "limit", default=10)

# picking the API shape for symmetry with /messages
filter_str = parse_string(request, "filter", encoding="utf-8")
if filter_str:
filter_json = urlparse.unquote(filter_str)
event_filter: Optional[Filter] = Filter(
self._hs, json_decoder.decode(filter_json)
)
else:
event_filter = None
filter_json = parse_json(request, "filter", encoding="utf-8")
event_filter = Filter(self._hs, filter_json) if filter_json else None

event_context = await self.room_context_handler.get_event_context(
requester,
Expand Down Expand Up @@ -914,21 +909,16 @@ async def on_GET(
)
# Twisted will have processed the args by now.
assert request.args is not None

filter_json = parse_json(request, "filter", encoding="utf-8")
event_filter = Filter(self._hs, filter_json) if filter_json else None

as_client_event = b"raw" not in request.args
filter_str = parse_string(request, "filter", encoding="utf-8")
if filter_str:
filter_json = urlparse.unquote(filter_str)
event_filter: Optional[Filter] = Filter(
self._hs, json_decoder.decode(filter_json)
)
if (
event_filter
and event_filter.filter_json.get("event_format", "client")
== "federation"
):
as_client_event = False
else:
event_filter = None
if (
event_filter
and event_filter.filter_json.get("event_format", "client") == "federation"
):
as_client_event = False

msgs = await self._pagination_handler.get_messages(
room_id=room_id,
Expand Down
35 changes: 12 additions & 23 deletions synapse/rest/client/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
parse_boolean,
parse_enum,
parse_integer,
parse_json,
parse_json_object_from_request,
parse_string,
parse_strings_from_args,
Expand All @@ -65,7 +66,6 @@
from synapse.streams.config import PaginationConfig
from synapse.types import JsonDict, Requester, StreamToken, ThirdPartyInstanceID, UserID
from synapse.types.state import StateFilter
from synapse.util import json_decoder
from synapse.util.cancellation import cancellable
from synapse.util.stringutils import parse_and_validate_server_name, random_string

Expand Down Expand Up @@ -703,21 +703,16 @@ async def on_GET(
)
# Twisted will have processed the args by now.
assert request.args is not None

filter_json = parse_json(request, "filter", encoding="utf-8")
event_filter = Filter(self._hs, filter_json) if filter_json else None

as_client_event = b"raw" not in request.args
filter_str = parse_string(request, "filter", encoding="utf-8")
if filter_str:
filter_json = urlparse.unquote(filter_str)
event_filter: Optional[Filter] = Filter(
self._hs, json_decoder.decode(filter_json)
)
if (
event_filter
and event_filter.filter_json.get("event_format", "client")
== "federation"
):
as_client_event = False
else:
event_filter = None
if (
event_filter
and event_filter.filter_json.get("event_format", "client") == "federation"
):
as_client_event = False

msgs = await self.pagination_handler.get_messages(
room_id=room_id,
Expand Down Expand Up @@ -898,14 +893,8 @@ async def on_GET(
limit = parse_integer(request, "limit", default=10)

# picking the API shape for symmetry with /messages
filter_str = parse_string(request, "filter", encoding="utf-8")
if filter_str:
filter_json = urlparse.unquote(filter_str)
event_filter: Optional[Filter] = Filter(
self._hs, json_decoder.decode(filter_json)
)
else:
event_filter = None
filter_json = parse_json(request, "filter", encoding="utf-8")
event_filter = Filter(self._hs, filter_json) if filter_json else None

event_context = await self.room_context_handler.get_event_context(
requester, room_id, event_id, limit, event_filter
Expand Down

0 comments on commit a040208

Please sign in to comment.