You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1204 lines
41 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import copy
  15. import itertools
  16. import logging
  17. from typing import (
  18. TYPE_CHECKING,
  19. Any,
  20. Awaitable,
  21. Callable,
  22. Dict,
  23. Iterable,
  24. List,
  25. Mapping,
  26. Optional,
  27. Sequence,
  28. Tuple,
  29. TypeVar,
  30. Union,
  31. )
  32. import attr
  33. from prometheus_client import Counter
  34. from twisted.internet import defer
  35. from twisted.internet.defer import Deferred
  36. from synapse.api.constants import EventTypes, Membership
  37. from synapse.api.errors import (
  38. CodeMessageException,
  39. Codes,
  40. FederationDeniedError,
  41. HttpResponseException,
  42. SynapseError,
  43. UnsupportedRoomVersionError,
  44. )
  45. from synapse.api.room_versions import (
  46. KNOWN_ROOM_VERSIONS,
  47. EventFormatVersions,
  48. RoomVersion,
  49. RoomVersions,
  50. )
  51. from synapse.events import EventBase, builder
  52. from synapse.federation.federation_base import FederationBase, event_from_pdu_json
  53. from synapse.logging.context import make_deferred_yieldable, preserve_fn
  54. from synapse.logging.utils import log_function
  55. from synapse.types import JsonDict, get_domain_from_id
  56. from synapse.util import unwrapFirstError
  57. from synapse.util.caches.expiringcache import ExpiringCache
  58. from synapse.util.retryutils import NotRetryingDestination
  59. if TYPE_CHECKING:
  60. from synapse.server import HomeServer
  61. logger = logging.getLogger(__name__)
  62. sent_queries_counter = Counter("synapse_federation_client_sent_queries", "", ["type"])
  63. PDU_RETRY_TIME_MS = 1 * 60 * 1000
  64. T = TypeVar("T")
  65. class InvalidResponseError(RuntimeError):
  66. """Helper for _try_destination_list: indicates that the server returned a response
  67. we couldn't parse
  68. """
  69. pass
  70. class FederationClient(FederationBase):
  71. def __init__(self, hs: "HomeServer"):
  72. super().__init__(hs)
  73. self.pdu_destination_tried = {} # type: Dict[str, Dict[str, int]]
  74. self._clock.looping_call(self._clear_tried_cache, 60 * 1000)
  75. self.state = hs.get_state_handler()
  76. self.transport_layer = hs.get_federation_transport_client()
  77. self.hostname = hs.hostname
  78. self.signing_key = hs.signing_key
  79. self._get_pdu_cache = ExpiringCache(
  80. cache_name="get_pdu_cache",
  81. clock=self._clock,
  82. max_len=1000,
  83. expiry_ms=120 * 1000,
  84. reset_expiry_on_get=False,
  85. ) # type: ExpiringCache[str, EventBase]
  86. def _clear_tried_cache(self):
  87. """Clear pdu_destination_tried cache"""
  88. now = self._clock.time_msec()
  89. old_dict = self.pdu_destination_tried
  90. self.pdu_destination_tried = {}
  91. for event_id, destination_dict in old_dict.items():
  92. destination_dict = {
  93. dest: time
  94. for dest, time in destination_dict.items()
  95. if time + PDU_RETRY_TIME_MS > now
  96. }
  97. if destination_dict:
  98. self.pdu_destination_tried[event_id] = destination_dict
  99. @log_function
  100. async def make_query(
  101. self,
  102. destination: str,
  103. query_type: str,
  104. args: dict,
  105. retry_on_dns_fail: bool = False,
  106. ignore_backoff: bool = False,
  107. ) -> JsonDict:
  108. """Sends a federation Query to a remote homeserver of the given type
  109. and arguments.
  110. Args:
  111. destination: Domain name of the remote homeserver
  112. query_type: Category of the query type; should match the
  113. handler name used in register_query_handler().
  114. args: Mapping of strings to strings containing the details
  115. of the query request.
  116. ignore_backoff: true to ignore the historical backoff data
  117. and try the request anyway.
  118. Returns:
  119. The JSON object from the response
  120. """
  121. sent_queries_counter.labels(query_type).inc()
  122. return await self.transport_layer.make_query(
  123. destination,
  124. query_type,
  125. args,
  126. retry_on_dns_fail=retry_on_dns_fail,
  127. ignore_backoff=ignore_backoff,
  128. )
  129. @log_function
  130. async def query_client_keys(
  131. self, destination: str, content: JsonDict, timeout: int
  132. ) -> JsonDict:
  133. """Query device keys for a device hosted on a remote server.
  134. Args:
  135. destination: Domain name of the remote homeserver
  136. content: The query content.
  137. Returns:
  138. The JSON object from the response
  139. """
  140. sent_queries_counter.labels("client_device_keys").inc()
  141. return await self.transport_layer.query_client_keys(
  142. destination, content, timeout
  143. )
  144. @log_function
  145. async def query_user_devices(
  146. self, destination: str, user_id: str, timeout: int = 30000
  147. ) -> JsonDict:
  148. """Query the device keys for a list of user ids hosted on a remote
  149. server.
  150. """
  151. sent_queries_counter.labels("user_devices").inc()
  152. return await self.transport_layer.query_user_devices(
  153. destination, user_id, timeout
  154. )
  155. @log_function
  156. async def claim_client_keys(
  157. self, destination: str, content: JsonDict, timeout: int
  158. ) -> JsonDict:
  159. """Claims one-time keys for a device hosted on a remote server.
  160. Args:
  161. destination: Domain name of the remote homeserver
  162. content: The query content.
  163. Returns:
  164. The JSON object from the response
  165. """
  166. sent_queries_counter.labels("client_one_time_keys").inc()
  167. return await self.transport_layer.claim_client_keys(
  168. destination, content, timeout
  169. )
  170. async def backfill(
  171. self, dest: str, room_id: str, limit: int, extremities: Iterable[str]
  172. ) -> Optional[List[EventBase]]:
  173. """Requests some more historic PDUs for the given room from the
  174. given destination server.
  175. Args:
  176. dest: The remote homeserver to ask.
  177. room_id: The room_id to backfill.
  178. limit: The maximum number of events to return.
  179. extremities: our current backwards extremities, to backfill from
  180. """
  181. logger.debug("backfill extrem=%s", extremities)
  182. # If there are no extremities then we've (probably) reached the start.
  183. if not extremities:
  184. return None
  185. transaction_data = await self.transport_layer.backfill(
  186. dest, room_id, extremities, limit
  187. )
  188. logger.debug("backfill transaction_data=%r", transaction_data)
  189. room_version = await self.store.get_room_version(room_id)
  190. pdus = [
  191. event_from_pdu_json(p, room_version, outlier=False)
  192. for p in transaction_data["pdus"]
  193. ]
  194. # Check signatures and hash of pdus, removing any from the list that fail checks
  195. pdus[:] = await self._check_sigs_and_hash_and_fetch(
  196. dest, pdus, outlier=True, room_version=room_version
  197. )
  198. return pdus
  199. async def get_pdu(
  200. self,
  201. destinations: Iterable[str],
  202. event_id: str,
  203. room_version: RoomVersion,
  204. outlier: bool = False,
  205. timeout: Optional[int] = None,
  206. ) -> Optional[EventBase]:
  207. """Requests the PDU with given origin and ID from the remote home
  208. servers.
  209. Will attempt to get the PDU from each destination in the list until
  210. one succeeds.
  211. Args:
  212. destinations: Which homeservers to query
  213. event_id: event to fetch
  214. room_version: version of the room
  215. outlier: Indicates whether the PDU is an `outlier`, i.e. if
  216. it's from an arbitrary point in the context as opposed to part
  217. of the current block of PDUs. Defaults to `False`
  218. timeout: How long to try (in ms) each destination for before
  219. moving to the next destination. None indicates no timeout.
  220. Returns:
  221. The requested PDU, or None if we were unable to find it.
  222. """
  223. # TODO: Rate limit the number of times we try and get the same event.
  224. ev = self._get_pdu_cache.get(event_id)
  225. if ev:
  226. return ev
  227. pdu_attempts = self.pdu_destination_tried.setdefault(event_id, {})
  228. signed_pdu = None
  229. for destination in destinations:
  230. now = self._clock.time_msec()
  231. last_attempt = pdu_attempts.get(destination, 0)
  232. if last_attempt + PDU_RETRY_TIME_MS > now:
  233. continue
  234. try:
  235. transaction_data = await self.transport_layer.get_event(
  236. destination, event_id, timeout=timeout
  237. )
  238. logger.debug(
  239. "retrieved event id %s from %s: %r",
  240. event_id,
  241. destination,
  242. transaction_data,
  243. )
  244. pdu_list = [
  245. event_from_pdu_json(p, room_version, outlier=outlier)
  246. for p in transaction_data["pdus"]
  247. ] # type: List[EventBase]
  248. if pdu_list and pdu_list[0]:
  249. pdu = pdu_list[0]
  250. # Check signatures are correct.
  251. signed_pdu = await self._check_sigs_and_hash(room_version, pdu)
  252. break
  253. pdu_attempts[destination] = now
  254. except SynapseError as e:
  255. logger.info(
  256. "Failed to get PDU %s from %s because %s", event_id, destination, e
  257. )
  258. continue
  259. except NotRetryingDestination as e:
  260. logger.info(str(e))
  261. continue
  262. except FederationDeniedError as e:
  263. logger.info(str(e))
  264. continue
  265. except Exception as e:
  266. pdu_attempts[destination] = now
  267. logger.info(
  268. "Failed to get PDU %s from %s because %s", event_id, destination, e
  269. )
  270. continue
  271. if signed_pdu:
  272. self._get_pdu_cache[event_id] = signed_pdu
  273. return signed_pdu
  274. async def get_room_state_ids(
  275. self, destination: str, room_id: str, event_id: str
  276. ) -> Tuple[List[str], List[str]]:
  277. """Calls the /state_ids endpoint to fetch the state at a particular point
  278. in the room, and the auth events for the given event
  279. Returns:
  280. a tuple of (state event_ids, auth event_ids)
  281. """
  282. result = await self.transport_layer.get_room_state_ids(
  283. destination, room_id, event_id=event_id
  284. )
  285. state_event_ids = result["pdu_ids"]
  286. auth_event_ids = result.get("auth_chain_ids", [])
  287. if not isinstance(state_event_ids, list) or not isinstance(
  288. auth_event_ids, list
  289. ):
  290. raise Exception("invalid response from /state_ids")
  291. return state_event_ids, auth_event_ids
  292. async def _check_sigs_and_hash_and_fetch(
  293. self,
  294. origin: str,
  295. pdus: List[EventBase],
  296. room_version: RoomVersion,
  297. outlier: bool = False,
  298. include_none: bool = False,
  299. ) -> List[EventBase]:
  300. """Takes a list of PDUs and checks the signatures and hashes of each
  301. one. If a PDU fails its signature check then we check if we have it in
  302. the database and if not then request if from the originating server of
  303. that PDU.
  304. If a PDU fails its content hash check then it is redacted.
  305. The given list of PDUs are not modified, instead the function returns
  306. a new list.
  307. Args:
  308. origin
  309. pdu
  310. room_version
  311. outlier: Whether the events are outliers or not
  312. include_none: Whether to include None in the returned list
  313. for events that have failed their checks
  314. Returns:
  315. A list of PDUs that have valid signatures and hashes.
  316. """
  317. deferreds = self._check_sigs_and_hashes(room_version, pdus)
  318. async def handle_check_result(pdu: EventBase, deferred: Deferred):
  319. try:
  320. res = await make_deferred_yieldable(deferred)
  321. except SynapseError:
  322. res = None
  323. if not res:
  324. # Check local db.
  325. res = await self.store.get_event(
  326. pdu.event_id, allow_rejected=True, allow_none=True
  327. )
  328. pdu_origin = get_domain_from_id(pdu.sender)
  329. if not res and pdu_origin != origin:
  330. try:
  331. res = await self.get_pdu(
  332. destinations=[pdu_origin],
  333. event_id=pdu.event_id,
  334. room_version=room_version,
  335. outlier=outlier,
  336. timeout=10000,
  337. )
  338. except SynapseError:
  339. pass
  340. if not res:
  341. logger.warning(
  342. "Failed to find copy of %s with valid signature", pdu.event_id
  343. )
  344. return res
  345. handle = preserve_fn(handle_check_result)
  346. deferreds2 = [handle(pdu, deferred) for pdu, deferred in zip(pdus, deferreds)]
  347. valid_pdus = await make_deferred_yieldable(
  348. defer.gatherResults(deferreds2, consumeErrors=True)
  349. ).addErrback(unwrapFirstError)
  350. if include_none:
  351. return valid_pdus
  352. else:
  353. return [p for p in valid_pdus if p]
  354. async def get_event_auth(
  355. self, destination: str, room_id: str, event_id: str
  356. ) -> List[EventBase]:
  357. res = await self.transport_layer.get_event_auth(destination, room_id, event_id)
  358. room_version = await self.store.get_room_version(room_id)
  359. auth_chain = [
  360. event_from_pdu_json(p, room_version, outlier=True)
  361. for p in res["auth_chain"]
  362. ]
  363. signed_auth = await self._check_sigs_and_hash_and_fetch(
  364. destination, auth_chain, outlier=True, room_version=room_version
  365. )
  366. signed_auth.sort(key=lambda e: e.depth)
  367. return signed_auth
  368. async def _try_destination_list(
  369. self,
  370. description: str,
  371. destinations: Iterable[str],
  372. callback: Callable[[str], Awaitable[T]],
  373. failover_on_unknown_endpoint: bool = False,
  374. ) -> T:
  375. """Try an operation on a series of servers, until it succeeds
  376. Args:
  377. description: description of the operation we're doing, for logging
  378. destinations: list of server_names to try
  379. callback: Function to run for each server. Passed a single
  380. argument: the server_name to try.
  381. If the callback raises a CodeMessageException with a 300/400 code,
  382. attempts to perform the operation stop immediately and the exception is
  383. reraised.
  384. Otherwise, if the callback raises an Exception the error is logged and the
  385. next server tried. Normally the stacktrace is logged but this is
  386. suppressed if the exception is an InvalidResponseError.
  387. failover_on_unknown_endpoint: if True, we will try other servers if it looks
  388. like a server doesn't support the endpoint. This is typically useful
  389. if the endpoint in question is new or experimental.
  390. Returns:
  391. The result of callback, if it succeeds
  392. Raises:
  393. SynapseError if the chosen remote server returns a 300/400 code, or
  394. no servers were reachable.
  395. """
  396. for destination in destinations:
  397. if destination == self.server_name:
  398. continue
  399. try:
  400. res = await callback(destination)
  401. return res
  402. except InvalidResponseError as e:
  403. logger.warning("Failed to %s via %s: %s", description, destination, e)
  404. except UnsupportedRoomVersionError:
  405. raise
  406. except HttpResponseException as e:
  407. synapse_error = e.to_synapse_error()
  408. failover = False
  409. if 500 <= e.code < 600:
  410. failover = True
  411. elif failover_on_unknown_endpoint:
  412. # there is no good way to detect an "unknown" endpoint. Dendrite
  413. # returns a 404 (with no body); synapse returns a 400
  414. # with M_UNRECOGNISED.
  415. if e.code == 404 or (
  416. e.code == 400 and synapse_error.errcode == Codes.UNRECOGNIZED
  417. ):
  418. failover = True
  419. if not failover:
  420. raise synapse_error from e
  421. logger.warning(
  422. "Failed to %s via %s: %i %s",
  423. description,
  424. destination,
  425. e.code,
  426. e.args[0],
  427. )
  428. except Exception:
  429. logger.warning(
  430. "Failed to %s via %s", description, destination, exc_info=True
  431. )
  432. raise SynapseError(502, "Failed to %s via any server" % (description,))
  433. async def make_membership_event(
  434. self,
  435. destinations: Iterable[str],
  436. room_id: str,
  437. user_id: str,
  438. membership: str,
  439. content: dict,
  440. params: Optional[Mapping[str, Union[str, Iterable[str]]]],
  441. ) -> Tuple[str, EventBase, RoomVersion]:
  442. """
  443. Creates an m.room.member event, with context, without participating in the room.
  444. Does so by asking one of the already participating servers to create an
  445. event with proper context.
  446. Returns a fully signed and hashed event.
  447. Note that this does not append any events to any graphs.
  448. Args:
  449. destinations: Candidate homeservers which are probably
  450. participating in the room.
  451. room_id: The room in which the event will happen.
  452. user_id: The user whose membership is being evented.
  453. membership: The "membership" property of the event. Must be one of
  454. "join" or "leave".
  455. content: Any additional data to put into the content field of the
  456. event.
  457. params: Query parameters to include in the request.
  458. Returns:
  459. `(origin, event, room_version)` where origin is the remote
  460. homeserver which generated the event, and room_version is the
  461. version of the room.
  462. Raises:
  463. UnsupportedRoomVersionError: if remote responds with
  464. a room version we don't understand.
  465. SynapseError: if the chosen remote server returns a 300/400 code.
  466. RuntimeError: if no servers were reachable.
  467. """
  468. valid_memberships = {Membership.JOIN, Membership.LEAVE}
  469. if membership not in valid_memberships:
  470. raise RuntimeError(
  471. "make_membership_event called with membership='%s', must be one of %s"
  472. % (membership, ",".join(valid_memberships))
  473. )
  474. async def send_request(destination: str) -> Tuple[str, EventBase, RoomVersion]:
  475. ret = await self.transport_layer.make_membership_event(
  476. destination, room_id, user_id, membership, params
  477. )
  478. # Note: If not supplied, the room version may be either v1 or v2,
  479. # however either way the event format version will be v1.
  480. room_version_id = ret.get("room_version", RoomVersions.V1.identifier)
  481. room_version = KNOWN_ROOM_VERSIONS.get(room_version_id)
  482. if not room_version:
  483. raise UnsupportedRoomVersionError()
  484. pdu_dict = ret.get("event", None)
  485. if not isinstance(pdu_dict, dict):
  486. raise InvalidResponseError("Bad 'event' field in response")
  487. logger.debug("Got response to make_%s: %s", membership, pdu_dict)
  488. pdu_dict["content"].update(content)
  489. # The protoevent received over the JSON wire may not have all
  490. # the required fields. Lets just gloss over that because
  491. # there's some we never care about
  492. if "prev_state" not in pdu_dict:
  493. pdu_dict["prev_state"] = []
  494. ev = builder.create_local_event_from_event_dict(
  495. self._clock,
  496. self.hostname,
  497. self.signing_key,
  498. room_version=room_version,
  499. event_dict=pdu_dict,
  500. )
  501. return destination, ev, room_version
  502. return await self._try_destination_list(
  503. "make_" + membership, destinations, send_request
  504. )
  505. async def send_join(
  506. self, destinations: Iterable[str], pdu: EventBase, room_version: RoomVersion
  507. ) -> Dict[str, Any]:
  508. """Sends a join event to one of a list of homeservers.
  509. Doing so will cause the remote server to add the event to the graph,
  510. and send the event out to the rest of the federation.
  511. Args:
  512. destinations: Candidate homeservers which are probably
  513. participating in the room.
  514. pdu: event to be sent
  515. room_version: the version of the room (according to the server that
  516. did the make_join)
  517. Returns:
  518. a dict with members ``origin`` (a string
  519. giving the server the event was sent to, ``state`` (?) and
  520. ``auth_chain``.
  521. Raises:
  522. SynapseError: if the chosen remote server returns a 300/400 code.
  523. RuntimeError: if no servers were reachable.
  524. """
  525. async def send_request(destination) -> Dict[str, Any]:
  526. content = await self._do_send_join(destination, pdu)
  527. logger.debug("Got content: %s", content)
  528. state = [
  529. event_from_pdu_json(p, room_version, outlier=True)
  530. for p in content.get("state", [])
  531. ]
  532. auth_chain = [
  533. event_from_pdu_json(p, room_version, outlier=True)
  534. for p in content.get("auth_chain", [])
  535. ]
  536. pdus = {p.event_id: p for p in itertools.chain(state, auth_chain)}
  537. create_event = None
  538. for e in state:
  539. if (e.type, e.state_key) == (EventTypes.Create, ""):
  540. create_event = e
  541. break
  542. if create_event is None:
  543. # If the state doesn't have a create event then the room is
  544. # invalid, and it would fail auth checks anyway.
  545. raise SynapseError(400, "No create event in state")
  546. # the room version should be sane.
  547. create_room_version = create_event.content.get(
  548. "room_version", RoomVersions.V1.identifier
  549. )
  550. if create_room_version != room_version.identifier:
  551. # either the server that fulfilled the make_join, or the server that is
  552. # handling the send_join, is lying.
  553. raise InvalidResponseError(
  554. "Unexpected room version %s in create event"
  555. % (create_room_version,)
  556. )
  557. valid_pdus = await self._check_sigs_and_hash_and_fetch(
  558. destination,
  559. list(pdus.values()),
  560. outlier=True,
  561. room_version=room_version,
  562. )
  563. valid_pdus_map = {p.event_id: p for p in valid_pdus}
  564. # NB: We *need* to copy to ensure that we don't have multiple
  565. # references being passed on, as that causes... issues.
  566. signed_state = [
  567. copy.copy(valid_pdus_map[p.event_id])
  568. for p in state
  569. if p.event_id in valid_pdus_map
  570. ]
  571. signed_auth = [
  572. valid_pdus_map[p.event_id]
  573. for p in auth_chain
  574. if p.event_id in valid_pdus_map
  575. ]
  576. # NB: We *need* to copy to ensure that we don't have multiple
  577. # references being passed on, as that causes... issues.
  578. for s in signed_state:
  579. s.internal_metadata = copy.deepcopy(s.internal_metadata)
  580. # double-check that the same create event has ended up in the auth chain
  581. auth_chain_create_events = [
  582. e.event_id
  583. for e in signed_auth
  584. if (e.type, e.state_key) == (EventTypes.Create, "")
  585. ]
  586. if auth_chain_create_events != [create_event.event_id]:
  587. raise InvalidResponseError(
  588. "Unexpected create event(s) in auth chain: %s"
  589. % (auth_chain_create_events,)
  590. )
  591. return {
  592. "state": signed_state,
  593. "auth_chain": signed_auth,
  594. "origin": destination,
  595. }
  596. return await self._try_destination_list("send_join", destinations, send_request)
  597. async def _do_send_join(self, destination: str, pdu: EventBase) -> JsonDict:
  598. time_now = self._clock.time_msec()
  599. try:
  600. return await self.transport_layer.send_join_v2(
  601. destination=destination,
  602. room_id=pdu.room_id,
  603. event_id=pdu.event_id,
  604. content=pdu.get_pdu_json(time_now),
  605. )
  606. except HttpResponseException as e:
  607. if e.code in [400, 404]:
  608. err = e.to_synapse_error()
  609. # If we receive an error response that isn't a generic error, or an
  610. # unrecognised endpoint error, we assume that the remote understands
  611. # the v2 invite API and this is a legitimate error.
  612. if err.errcode not in [Codes.UNKNOWN, Codes.UNRECOGNIZED]:
  613. raise err
  614. else:
  615. raise e.to_synapse_error()
  616. logger.debug("Couldn't send_join with the v2 API, falling back to the v1 API")
  617. resp = await self.transport_layer.send_join_v1(
  618. destination=destination,
  619. room_id=pdu.room_id,
  620. event_id=pdu.event_id,
  621. content=pdu.get_pdu_json(time_now),
  622. )
  623. # We expect the v1 API to respond with [200, content], so we only return the
  624. # content.
  625. return resp[1]
  626. async def send_invite(
  627. self,
  628. destination: str,
  629. room_id: str,
  630. event_id: str,
  631. pdu: EventBase,
  632. ) -> EventBase:
  633. room_version = await self.store.get_room_version(room_id)
  634. content = await self._do_send_invite(destination, pdu, room_version)
  635. pdu_dict = content["event"]
  636. logger.debug("Got response to send_invite: %s", pdu_dict)
  637. pdu = event_from_pdu_json(pdu_dict, room_version)
  638. # Check signatures are correct.
  639. pdu = await self._check_sigs_and_hash(room_version, pdu)
  640. # FIXME: We should handle signature failures more gracefully.
  641. return pdu
  642. async def _do_send_invite(
  643. self, destination: str, pdu: EventBase, room_version: RoomVersion
  644. ) -> JsonDict:
  645. """Actually sends the invite, first trying v2 API and falling back to
  646. v1 API if necessary.
  647. Returns:
  648. The event as a dict as returned by the remote server
  649. """
  650. time_now = self._clock.time_msec()
  651. try:
  652. return await self.transport_layer.send_invite_v2(
  653. destination=destination,
  654. room_id=pdu.room_id,
  655. event_id=pdu.event_id,
  656. content={
  657. "event": pdu.get_pdu_json(time_now),
  658. "room_version": room_version.identifier,
  659. "invite_room_state": pdu.unsigned.get("invite_room_state", []),
  660. },
  661. )
  662. except HttpResponseException as e:
  663. if e.code in [400, 404]:
  664. err = e.to_synapse_error()
  665. # If we receive an error response that isn't a generic error, we
  666. # assume that the remote understands the v2 invite API and this
  667. # is a legitimate error.
  668. if err.errcode != Codes.UNKNOWN:
  669. raise err
  670. # Otherwise, we assume that the remote server doesn't understand
  671. # the v2 invite API. That's ok provided the room uses old-style event
  672. # IDs.
  673. if room_version.event_format != EventFormatVersions.V1:
  674. raise SynapseError(
  675. 400,
  676. "User's homeserver does not support this room version",
  677. Codes.UNSUPPORTED_ROOM_VERSION,
  678. )
  679. elif e.code in (403, 429):
  680. raise e.to_synapse_error()
  681. else:
  682. raise
  683. # Didn't work, try v1 API.
  684. # Note the v1 API returns a tuple of `(200, content)`
  685. _, content = await self.transport_layer.send_invite_v1(
  686. destination=destination,
  687. room_id=pdu.room_id,
  688. event_id=pdu.event_id,
  689. content=pdu.get_pdu_json(time_now),
  690. )
  691. return content
  692. async def send_leave(self, destinations: Iterable[str], pdu: EventBase) -> None:
  693. """Sends a leave event to one of a list of homeservers.
  694. Doing so will cause the remote server to add the event to the graph,
  695. and send the event out to the rest of the federation.
  696. This is mostly useful to reject received invites.
  697. Args:
  698. destinations: Candidate homeservers which are probably
  699. participating in the room.
  700. pdu: event to be sent
  701. Raises:
  702. SynapseError if the chosen remote server returns a 300/400 code.
  703. RuntimeError if no servers were reachable.
  704. """
  705. async def send_request(destination: str) -> None:
  706. content = await self._do_send_leave(destination, pdu)
  707. logger.debug("Got content: %s", content)
  708. return await self._try_destination_list(
  709. "send_leave", destinations, send_request
  710. )
  711. async def _do_send_leave(self, destination: str, pdu: EventBase) -> JsonDict:
  712. time_now = self._clock.time_msec()
  713. try:
  714. return await self.transport_layer.send_leave_v2(
  715. destination=destination,
  716. room_id=pdu.room_id,
  717. event_id=pdu.event_id,
  718. content=pdu.get_pdu_json(time_now),
  719. )
  720. except HttpResponseException as e:
  721. if e.code in [400, 404]:
  722. err = e.to_synapse_error()
  723. # If we receive an error response that isn't a generic error, or an
  724. # unrecognised endpoint error, we assume that the remote understands
  725. # the v2 invite API and this is a legitimate error.
  726. if err.errcode not in [Codes.UNKNOWN, Codes.UNRECOGNIZED]:
  727. raise err
  728. else:
  729. raise e.to_synapse_error()
  730. logger.debug("Couldn't send_leave with the v2 API, falling back to the v1 API")
  731. resp = await self.transport_layer.send_leave_v1(
  732. destination=destination,
  733. room_id=pdu.room_id,
  734. event_id=pdu.event_id,
  735. content=pdu.get_pdu_json(time_now),
  736. )
  737. # We expect the v1 API to respond with [200, content], so we only return the
  738. # content.
  739. return resp[1]
  740. async def get_public_rooms(
  741. self,
  742. remote_server: str,
  743. limit: Optional[int] = None,
  744. since_token: Optional[str] = None,
  745. search_filter: Optional[Dict] = None,
  746. include_all_networks: bool = False,
  747. third_party_instance_id: Optional[str] = None,
  748. ) -> JsonDict:
  749. """Get the list of public rooms from a remote homeserver
  750. Args:
  751. remote_server: The name of the remote server
  752. limit: Maximum amount of rooms to return
  753. since_token: Used for result pagination
  754. search_filter: A filter dictionary to send the remote homeserver
  755. and filter the result set
  756. include_all_networks: Whether to include results from all third party instances
  757. third_party_instance_id: Whether to only include results from a specific third
  758. party instance
  759. Returns:
  760. The response from the remote server.
  761. Raises:
  762. HttpResponseException: There was an exception returned from the remote server
  763. SynapseException: M_FORBIDDEN when the remote server has disallowed publicRoom
  764. requests over federation
  765. """
  766. return await self.transport_layer.get_public_rooms(
  767. remote_server,
  768. limit,
  769. since_token,
  770. search_filter,
  771. include_all_networks=include_all_networks,
  772. third_party_instance_id=third_party_instance_id,
  773. )
  774. async def get_missing_events(
  775. self,
  776. destination: str,
  777. room_id: str,
  778. earliest_events_ids: Iterable[str],
  779. latest_events: Iterable[EventBase],
  780. limit: int,
  781. min_depth: int,
  782. timeout: int,
  783. ) -> List[EventBase]:
  784. """Tries to fetch events we are missing. This is called when we receive
  785. an event without having received all of its ancestors.
  786. Args:
  787. destination
  788. room_id
  789. earliest_events_ids: List of event ids. Effectively the
  790. events we expected to receive, but haven't. `get_missing_events`
  791. should only return events that didn't happen before these.
  792. latest_events: List of events we have received that we don't
  793. have all previous events for.
  794. limit: Maximum number of events to return.
  795. min_depth: Minimum depth of events to return.
  796. timeout: Max time to wait in ms
  797. """
  798. try:
  799. content = await self.transport_layer.get_missing_events(
  800. destination=destination,
  801. room_id=room_id,
  802. earliest_events=earliest_events_ids,
  803. latest_events=[e.event_id for e in latest_events],
  804. limit=limit,
  805. min_depth=min_depth,
  806. timeout=timeout,
  807. )
  808. room_version = await self.store.get_room_version(room_id)
  809. events = [
  810. event_from_pdu_json(e, room_version) for e in content.get("events", [])
  811. ]
  812. signed_events = await self._check_sigs_and_hash_and_fetch(
  813. destination, events, outlier=False, room_version=room_version
  814. )
  815. except HttpResponseException as e:
  816. if not e.code == 400:
  817. raise
  818. # We are probably hitting an old server that doesn't support
  819. # get_missing_events
  820. signed_events = []
  821. return signed_events
  822. async def forward_third_party_invite(
  823. self, destinations: Iterable[str], room_id: str, event_dict: JsonDict
  824. ) -> None:
  825. for destination in destinations:
  826. if destination == self.server_name:
  827. continue
  828. try:
  829. await self.transport_layer.exchange_third_party_invite(
  830. destination=destination, room_id=room_id, event_dict=event_dict
  831. )
  832. return
  833. except CodeMessageException:
  834. raise
  835. except Exception as e:
  836. logger.exception(
  837. "Failed to send_third_party_invite via %s: %s", destination, str(e)
  838. )
  839. raise RuntimeError("Failed to send to any server.")
  840. async def get_room_complexity(
  841. self, destination: str, room_id: str
  842. ) -> Optional[JsonDict]:
  843. """
  844. Fetch the complexity of a remote room from another server.
  845. Args:
  846. destination: The remote server
  847. room_id: The room ID to ask about.
  848. Returns:
  849. Dict contains the complexity metric versions, while None means we
  850. could not fetch the complexity.
  851. """
  852. try:
  853. return await self.transport_layer.get_room_complexity(
  854. destination=destination, room_id=room_id
  855. )
  856. except CodeMessageException as e:
  857. # We didn't manage to get it -- probably a 404. We are okay if other
  858. # servers don't give it to us.
  859. logger.debug(
  860. "Failed to fetch room complexity via %s for %s, got a %d",
  861. destination,
  862. room_id,
  863. e.code,
  864. )
  865. except Exception:
  866. logger.exception(
  867. "Failed to fetch room complexity via %s for %s", destination, room_id
  868. )
  869. # If we don't manage to find it, return None. It's not an error if a
  870. # server doesn't give it to us.
  871. return None
  872. async def get_space_summary(
  873. self,
  874. destinations: Iterable[str],
  875. room_id: str,
  876. suggested_only: bool,
  877. max_rooms_per_space: Optional[int],
  878. exclude_rooms: List[str],
  879. ) -> "FederationSpaceSummaryResult":
  880. """
  881. Call other servers to get a summary of the given space
  882. Args:
  883. destinations: The remote servers. We will try them in turn, omitting any
  884. that have been blacklisted.
  885. room_id: ID of the space to be queried
  886. suggested_only: If true, ask the remote server to only return children
  887. with the "suggested" flag set
  888. max_rooms_per_space: A limit on the number of children to return for each
  889. space
  890. exclude_rooms: A list of room IDs to tell the remote server to skip
  891. Returns:
  892. a parsed FederationSpaceSummaryResult
  893. Raises:
  894. SynapseError if we were unable to get a valid summary from any of the
  895. remote servers
  896. """
  897. async def send_request(destination: str) -> FederationSpaceSummaryResult:
  898. res = await self.transport_layer.get_space_summary(
  899. destination=destination,
  900. room_id=room_id,
  901. suggested_only=suggested_only,
  902. max_rooms_per_space=max_rooms_per_space,
  903. exclude_rooms=exclude_rooms,
  904. )
  905. try:
  906. return FederationSpaceSummaryResult.from_json_dict(res)
  907. except ValueError as e:
  908. raise InvalidResponseError(str(e))
  909. return await self._try_destination_list(
  910. "fetch space summary",
  911. destinations,
  912. send_request,
  913. failover_on_unknown_endpoint=True,
  914. )
  915. @attr.s(frozen=True, slots=True)
  916. class FederationSpaceSummaryEventResult:
  917. """Represents a single event in the result of a successful get_space_summary call.
  918. It's essentially just a serialised event object, but we do a bit of parsing and
  919. validation in `from_json_dict` and store some of the validated properties in
  920. object attributes.
  921. """
  922. event_type = attr.ib(type=str)
  923. state_key = attr.ib(type=str)
  924. via = attr.ib(type=Sequence[str])
  925. # the raw data, including the above keys
  926. data = attr.ib(type=JsonDict)
  927. @classmethod
  928. def from_json_dict(cls, d: JsonDict) -> "FederationSpaceSummaryEventResult":
  929. """Parse an event within the result of a /spaces/ request
  930. Args:
  931. d: json object to be parsed
  932. Raises:
  933. ValueError if d is not a valid event
  934. """
  935. event_type = d.get("type")
  936. if not isinstance(event_type, str):
  937. raise ValueError("Invalid event: 'event_type' must be a str")
  938. state_key = d.get("state_key")
  939. if not isinstance(state_key, str):
  940. raise ValueError("Invalid event: 'state_key' must be a str")
  941. content = d.get("content")
  942. if not isinstance(content, dict):
  943. raise ValueError("Invalid event: 'content' must be a dict")
  944. via = content.get("via")
  945. if not isinstance(via, Sequence):
  946. raise ValueError("Invalid event: 'via' must be a list")
  947. if any(not isinstance(v, str) for v in via):
  948. raise ValueError("Invalid event: 'via' must be a list of strings")
  949. return cls(event_type, state_key, via, d)
  950. @attr.s(frozen=True, slots=True)
  951. class FederationSpaceSummaryResult:
  952. """Represents the data returned by a successful get_space_summary call."""
  953. rooms = attr.ib(type=Sequence[JsonDict])
  954. events = attr.ib(type=Sequence[FederationSpaceSummaryEventResult])
  955. @classmethod
  956. def from_json_dict(cls, d: JsonDict) -> "FederationSpaceSummaryResult":
  957. """Parse the result of a /spaces/ request
  958. Args:
  959. d: json object to be parsed
  960. Raises:
  961. ValueError if d is not a valid /spaces/ response
  962. """
  963. rooms = d.get("rooms")
  964. if not isinstance(rooms, Sequence):
  965. raise ValueError("'rooms' must be a list")
  966. if any(not isinstance(r, dict) for r in rooms):
  967. raise ValueError("Invalid room in 'rooms' list")
  968. events = d.get("events")
  969. if not isinstance(events, Sequence):
  970. raise ValueError("'events' must be a list")
  971. if any(not isinstance(e, dict) for e in events):
  972. raise ValueError("Invalid event in 'events' list")
  973. parsed_events = [
  974. FederationSpaceSummaryEventResult.from_json_dict(e) for e in events
  975. ]
  976. return cls(rooms, parsed_events)