Skip to content

Commit 7b2ec46

Browse files
anoadragon453phil-flex
authored andcommitted
Further improvements to requesting the public rooms list on a homeserver which has it set to private (matrix-org#7368)
1 parent 8d74a62 commit 7b2ec46

3 files changed

Lines changed: 71 additions & 23 deletions

File tree

changelog.d/7368.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve error responses when accessing remote public room lists.

synapse/federation/federation_client.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -883,18 +883,37 @@ async def _do_send_leave(self, destination, pdu):
883883

884884
def get_public_rooms(
885885
self,
886-
destination,
887-
limit=None,
888-
since_token=None,
889-
search_filter=None,
890-
include_all_networks=False,
891-
third_party_instance_id=None,
886+
remote_server: str,
887+
limit: Optional[int] = None,
888+
since_token: Optional[str] = None,
889+
search_filter: Optional[Dict] = None,
890+
include_all_networks: bool = False,
891+
third_party_instance_id: Optional[str] = None,
892892
):
893-
if destination == self.server_name:
894-
return
893+
"""Get the list of public rooms from a remote homeserver
894+
895+
Args:
896+
remote_server: The name of the remote server
897+
limit: Maximum amount of rooms to return
898+
since_token: Used for result pagination
899+
search_filter: A filter dictionary to send the remote homeserver
900+
and filter the result set
901+
include_all_networks: Whether to include results from all third party instances
902+
third_party_instance_id: Whether to only include results from a specific third
903+
party instance
904+
905+
Returns:
906+
Deferred[Dict[str, Any]]: The response from the remote server, or None if
907+
`remote_server` is the same as the local server_name
895908
909+
Raises:
910+
HttpResponseException: There was an exception returned from the remote server
911+
SynapseException: M_FORBIDDEN when the remote server has disallowed publicRoom
912+
requests over federation
913+
914+
"""
896915
return self.transport_layer.get_public_rooms(
897-
destination,
916+
remote_server,
898917
limit,
899918
since_token,
900919
search_filter,

synapse/federation/transport/client.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
# limitations under the License.
1616

1717
import logging
18-
from typing import Any, Dict
18+
from typing import Any, Dict, Optional
1919

2020
from six.moves import urllib
2121

2222
from twisted.internet import defer
2323

2424
from synapse.api.constants import Membership
25+
from synapse.api.errors import Codes, HttpResponseException, SynapseError
2526
from synapse.api.urls import (
2627
FEDERATION_UNSTABLE_PREFIX,
2728
FEDERATION_V1_PREFIX,
@@ -326,18 +327,25 @@ def send_invite_v2(self, destination, room_id, event_id, content):
326327
@log_function
327328
def get_public_rooms(
328329
self,
329-
remote_server,
330-
limit,
331-
since_token,
332-
search_filter=None,
333-
include_all_networks=False,
334-
third_party_instance_id=None,
330+
remote_server: str,
331+
limit: Optional[int] = None,
332+
since_token: Optional[str] = None,
333+
search_filter: Optional[Dict] = None,
334+
include_all_networks: bool = False,
335+
third_party_instance_id: Optional[str] = None,
335336
):
337+
"""Get the list of public rooms from a remote homeserver
338+
339+
See synapse.federation.federation_client.FederationClient.get_public_rooms for
340+
more information.
341+
"""
336342
if search_filter:
337343
# this uses MSC2197 (Search Filtering over Federation)
338344
path = _create_v1_path("/publicRooms")
339345

340-
data = {"include_all_networks": "true" if include_all_networks else "false"}
346+
data = {
347+
"include_all_networks": "true" if include_all_networks else "false"
348+
} # type: Dict[str, Any]
341349
if third_party_instance_id:
342350
data["third_party_instance_id"] = third_party_instance_id
343351
if limit:
@@ -347,9 +355,19 @@ def get_public_rooms(
347355

348356
data["filter"] = search_filter
349357

350-
response = yield self.client.post_json(
351-
destination=remote_server, path=path, data=data, ignore_backoff=True
352-
)
358+
try:
359+
response = yield self.client.post_json(
360+
destination=remote_server, path=path, data=data, ignore_backoff=True
361+
)
362+
except HttpResponseException as e:
363+
if e.code == 403:
364+
raise SynapseError(
365+
403,
366+
"You are not allowed to view the public rooms list of %s"
367+
% (remote_server,),
368+
errcode=Codes.FORBIDDEN,
369+
)
370+
raise
353371
else:
354372
path = _create_v1_path("/publicRooms")
355373

@@ -363,9 +381,19 @@ def get_public_rooms(
363381
if since_token:
364382
args["since"] = [since_token]
365383

366-
response = yield self.client.get_json(
367-
destination=remote_server, path=path, args=args, ignore_backoff=True
368-
)
384+
try:
385+
response = yield self.client.get_json(
386+
destination=remote_server, path=path, args=args, ignore_backoff=True
387+
)
388+
except HttpResponseException as e:
389+
if e.code == 403:
390+
raise SynapseError(
391+
403,
392+
"You are not allowed to view the public rooms list of %s"
393+
% (remote_server,),
394+
errcode=Codes.FORBIDDEN,
395+
)
396+
raise
369397

370398
return response
371399

0 commit comments

Comments
 (0)