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

Commit c186fda

Browse files
committed
Consistently use room_id from federation request body
Some federation APIs have a redundant `room_id` path param (see https://github.com/matrix-org/matrix-doc/issues/2330). We should make sure we consistently use either the path param or the body param, and the body param is easier.
1 parent f737368 commit c186fda

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

changelog.d/8776.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in some federation APIs which could lead to unexpected behaviour if different parameters were set in the URI and the request body.

synapse/federation/federation_server.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from synapse.federation.persistence import TransactionActions
5050
from synapse.federation.units import Edu, Transaction
5151
from synapse.http.endpoint import parse_server_name
52+
from synapse.http.servlet import assert_params_in_dict
5253
from synapse.logging.context import (
5354
make_deferred_yieldable,
5455
nested_logging_context,
@@ -514,11 +515,12 @@ async def on_invite_request(
514515
return {"event": ret_pdu.get_pdu_json(time_now)}
515516

516517
async def on_send_join_request(
517-
self, origin: str, content: JsonDict, room_id: str
518+
self, origin: str, content: JsonDict
518519
) -> Dict[str, Any]:
519520
logger.debug("on_send_join_request: content: %s", content)
520521

521-
room_version = await self.store.get_room_version(room_id)
522+
assert_params_in_dict(content, ["room_id"])
523+
room_version = await self.store.get_room_version(content["room_id"])
522524
pdu = event_from_pdu_json(content, room_version)
523525

524526
origin_host, _ = parse_server_name(origin)
@@ -547,12 +549,11 @@ async def on_make_leave_request(
547549
time_now = self._clock.time_msec()
548550
return {"event": pdu.get_pdu_json(time_now), "room_version": room_version}
549551

550-
async def on_send_leave_request(
551-
self, origin: str, content: JsonDict, room_id: str
552-
) -> dict:
552+
async def on_send_leave_request(self, origin: str, content: JsonDict) -> dict:
553553
logger.debug("on_send_leave_request: content: %s", content)
554554

555-
room_version = await self.store.get_room_version(room_id)
555+
assert_params_in_dict(content, ["room_id"])
556+
room_version = await self.store.get_room_version(content["room_id"])
556557
pdu = event_from_pdu_json(content, room_version)
557558

558559
origin_host, _ = parse_server_name(origin)
@@ -748,12 +749,8 @@ async def exchange_third_party_invite(
748749
)
749750
return ret
750751

751-
async def on_exchange_third_party_invite_request(
752-
self, room_id: str, event_dict: Dict
753-
):
754-
ret = await self.handler.on_exchange_third_party_invite_request(
755-
room_id, event_dict
756-
)
752+
async def on_exchange_third_party_invite_request(self, event_dict: Dict):
753+
ret = await self.handler.on_exchange_third_party_invite_request(event_dict)
757754
return ret
758755

759756
async def check_server_matches_acl(self, server_name: str, room_id: str):

synapse/federation/transport/server.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ class FederationV1SendLeaveServlet(BaseFederationServlet):
528528
PATH = "/send_leave/(?P<room_id>[^/]*)/(?P<event_id>[^/]*)"
529529

530530
async def on_PUT(self, origin, content, query, room_id, event_id):
531-
content = await self.handler.on_send_leave_request(origin, content, room_id)
531+
content = await self.handler.on_send_leave_request(origin, content)
532532
return 200, (200, content)
533533

534534

@@ -538,7 +538,7 @@ class FederationV2SendLeaveServlet(BaseFederationServlet):
538538
PREFIX = FEDERATION_V2_PREFIX
539539

540540
async def on_PUT(self, origin, content, query, room_id, event_id):
541-
content = await self.handler.on_send_leave_request(origin, content, room_id)
541+
content = await self.handler.on_send_leave_request(origin, content)
542542
return 200, content
543543

544544

@@ -550,24 +550,24 @@ async def on_GET(self, origin, content, query, context, event_id):
550550

551551

552552
class FederationV1SendJoinServlet(BaseFederationServlet):
553-
PATH = "/send_join/(?P<context>[^/]*)/(?P<event_id>[^/]*)"
553+
PATH = "/send_join/(?P<room_id>[^/]*)/(?P<event_id>[^/]*)"
554554

555-
async def on_PUT(self, origin, content, query, context, event_id):
555+
async def on_PUT(self, origin, content, query, room_id, event_id):
556556
# TODO(paul): assert that context/event_id parsed from path actually
557557
# match those given in content
558-
content = await self.handler.on_send_join_request(origin, content, context)
558+
content = await self.handler.on_send_join_request(origin, content)
559559
return 200, (200, content)
560560

561561

562562
class FederationV2SendJoinServlet(BaseFederationServlet):
563-
PATH = "/send_join/(?P<context>[^/]*)/(?P<event_id>[^/]*)"
563+
PATH = "/send_join/(?P<room_id>[^/]*)/(?P<event_id>[^/]*)"
564564

565565
PREFIX = FEDERATION_V2_PREFIX
566566

567-
async def on_PUT(self, origin, content, query, context, event_id):
567+
async def on_PUT(self, origin, content, query, room_id, event_id):
568568
# TODO(paul): assert that context/event_id parsed from path actually
569569
# match those given in content
570-
content = await self.handler.on_send_join_request(origin, content, context)
570+
content = await self.handler.on_send_join_request(origin, content)
571571
return 200, content
572572

573573

@@ -616,9 +616,7 @@ class FederationThirdPartyInviteExchangeServlet(BaseFederationServlet):
616616
PATH = "/exchange_third_party_invite/(?P<room_id>[^/]*)"
617617

618618
async def on_PUT(self, origin, content, query, room_id):
619-
content = await self.handler.on_exchange_third_party_invite_request(
620-
room_id, content
621-
)
619+
content = await self.handler.on_exchange_third_party_invite_request(content)
622620
return 200, content
623621

624622

synapse/handlers/federation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from synapse.events.snapshot import EventContext
5656
from synapse.events.validator import EventValidator
5757
from synapse.handlers._base import BaseHandler
58+
from synapse.http.servlet import assert_params_in_dict
5859
from synapse.logging.context import (
5960
make_deferred_yieldable,
6061
nested_logging_context,
@@ -2688,20 +2689,19 @@ async def exchange_third_party_invite(
26882689
)
26892690

26902691
async def on_exchange_third_party_invite_request(
2691-
self, room_id: str, event_dict: JsonDict
2692+
self, event_dict: JsonDict
26922693
) -> None:
26932694
"""Handle an exchange_third_party_invite request from a remote server
26942695
26952696
The remote server will call this when it wants to turn a 3pid invite
26962697
into a normal m.room.member invite.
26972698
26982699
Args:
2699-
room_id: The ID of the room.
2700-
2701-
event_dict (dict[str, Any]): Dictionary containing the event body.
2700+
event_dict: Dictionary containing the event body.
27022701
27032702
"""
2704-
room_version = await self.store.get_room_version_id(room_id)
2703+
assert_params_in_dict(event_dict, ["room_id"])
2704+
room_version = await self.store.get_room_version_id(event_dict["room_id"])
27052705

27062706
# NB: event_dict has a particular specced format we might need to fudge
27072707
# if we change event formats too much.

0 commit comments

Comments
 (0)