federation_base.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from typing import TYPE_CHECKING, Awaitable, Callable, Optional
  17. from synapse.api.constants import MAX_DEPTH, EventContentFields, EventTypes, Membership
  18. from synapse.api.errors import Codes, SynapseError
  19. from synapse.api.room_versions import EventFormatVersions, RoomVersion
  20. from synapse.crypto.event_signing import check_event_content_hash
  21. from synapse.crypto.keyring import Keyring
  22. from synapse.events import EventBase, make_event_from_dict
  23. from synapse.events.utils import prune_event, validate_canonicaljson
  24. from synapse.http.servlet import assert_params_in_dict
  25. from synapse.logging.opentracing import log_kv, trace
  26. from synapse.types import JsonDict, get_domain_from_id
  27. if TYPE_CHECKING:
  28. from synapse.server import HomeServer
  29. logger = logging.getLogger(__name__)
  30. class InvalidEventSignatureError(RuntimeError):
  31. """Raised when the signature on an event is invalid.
  32. The stringification of this exception is just the error message without reference
  33. to the event id. The event id is available as a property.
  34. """
  35. def __init__(self, message: str, event_id: str):
  36. super().__init__(message)
  37. self.event_id = event_id
  38. class FederationBase:
  39. def __init__(self, hs: "HomeServer"):
  40. self.hs = hs
  41. self.server_name = hs.hostname
  42. self.keyring = hs.get_keyring()
  43. self.spam_checker = hs.get_spam_checker()
  44. self.store = hs.get_datastores().main
  45. self._clock = hs.get_clock()
  46. self._storage_controllers = hs.get_storage_controllers()
  47. @trace
  48. async def _check_sigs_and_hash(
  49. self,
  50. room_version: RoomVersion,
  51. pdu: EventBase,
  52. record_failure_callback: Optional[
  53. Callable[[EventBase, str], Awaitable[None]]
  54. ] = None,
  55. ) -> EventBase:
  56. """Checks that event is correctly signed by the sending server.
  57. Also checks the content hash, and redacts the event if there is a mismatch.
  58. Also runs the event through the spam checker; if it fails, redacts the event
  59. and flags it as soft-failed.
  60. Args:
  61. room_version: The room version of the PDU
  62. pdu: the event to be checked
  63. record_failure_callback: A callback to run whenever the given event
  64. fails signature or hash checks. This includes exceptions
  65. that would be normally be thrown/raised but also things like
  66. checking for event tampering where we just return the redacted
  67. event.
  68. Returns:
  69. * the original event if the checks pass
  70. * a redacted version of the event (if the signature
  71. matched but the hash did not). In this case a warning will be logged.
  72. Raises:
  73. InvalidEventSignatureError if the signature check failed. Nothing
  74. will be logged in this case.
  75. """
  76. try:
  77. await _check_sigs_on_pdu(self.keyring, room_version, pdu)
  78. except InvalidEventSignatureError as exc:
  79. if record_failure_callback:
  80. await record_failure_callback(pdu, str(exc))
  81. raise exc
  82. if not check_event_content_hash(pdu):
  83. # let's try to distinguish between failures because the event was
  84. # redacted (which are somewhat expected) vs actual ball-tampering
  85. # incidents.
  86. #
  87. # This is just a heuristic, so we just assume that if the keys are
  88. # about the same between the redacted and received events, then the
  89. # received event was probably a redacted copy (but we then use our
  90. # *actual* redacted copy to be on the safe side.)
  91. redacted_event = prune_event(pdu)
  92. if set(redacted_event.keys()) == set(pdu.keys()) and set(
  93. redacted_event.content.keys()
  94. ) == set(pdu.content.keys()):
  95. logger.debug(
  96. "Event %s seems to have been redacted; using our redacted copy",
  97. pdu.event_id,
  98. )
  99. log_kv(
  100. {
  101. "message": "Event seems to have been redacted; using our redacted copy",
  102. "event_id": pdu.event_id,
  103. }
  104. )
  105. else:
  106. logger.warning(
  107. "Event %s content has been tampered, redacting",
  108. pdu.event_id,
  109. )
  110. log_kv(
  111. {
  112. "message": "Event content has been tampered, redacting",
  113. "event_id": pdu.event_id,
  114. }
  115. )
  116. if record_failure_callback:
  117. await record_failure_callback(
  118. pdu, "Event content has been tampered with"
  119. )
  120. return redacted_event
  121. spam_check = await self.spam_checker.check_event_for_spam(pdu)
  122. if spam_check != self.spam_checker.NOT_SPAM:
  123. logger.warning("Event contains spam, soft-failing %s", pdu.event_id)
  124. log_kv(
  125. {
  126. "message": "Event contains spam, redacting (to save disk space) "
  127. "as well as soft-failing (to stop using the event in prev_events)",
  128. "event_id": pdu.event_id,
  129. }
  130. )
  131. # we redact (to save disk space) as well as soft-failing (to stop
  132. # using the event in prev_events).
  133. redacted_event = prune_event(pdu)
  134. redacted_event.internal_metadata.soft_failed = True
  135. return redacted_event
  136. return pdu
  137. @trace
  138. async def _check_sigs_on_pdu(
  139. keyring: Keyring, room_version: RoomVersion, pdu: EventBase
  140. ) -> None:
  141. """Check that the given events are correctly signed
  142. Args:
  143. keyring: keyring object to do the checks
  144. room_version: the room version of the PDUs
  145. pdus: the events to be checked
  146. Raises:
  147. InvalidEventSignatureError if the event wasn't correctly signed.
  148. """
  149. # we want to check that the event is signed by:
  150. #
  151. # (a) the sender's server
  152. #
  153. # - except in the case of invites created from a 3pid invite, which are exempt
  154. # from this check, because the sender has to match that of the original 3pid
  155. # invite, but the event may come from a different HS, for reasons that I don't
  156. # entirely grok (why do the senders have to match? and if they do, why doesn't the
  157. # joining server ask the inviting server to do the switcheroo with
  158. # exchange_third_party_invite?).
  159. #
  160. # That's pretty awful, since redacting such an invite will render it invalid
  161. # (because it will then look like a regular invite without a valid signature),
  162. # and signatures are *supposed* to be valid whether or not an event has been
  163. # redacted. But this isn't the worst of the ways that 3pid invites are broken.
  164. #
  165. # (b) for V1 and V2 rooms, the server which created the event_id
  166. #
  167. # let's start by getting the domain for each pdu, and flattening the event back
  168. # to JSON.
  169. # First we check that the sender event is signed by the sender's domain
  170. # (except if its a 3pid invite, in which case it may be sent by any server)
  171. sender_domain = get_domain_from_id(pdu.sender)
  172. if not _is_invite_via_3pid(pdu):
  173. try:
  174. await keyring.verify_event_for_server(
  175. sender_domain,
  176. pdu,
  177. pdu.origin_server_ts if room_version.enforce_key_validity else 0,
  178. )
  179. except Exception as e:
  180. raise InvalidEventSignatureError(
  181. f"unable to verify signature for sender domain {sender_domain}: {e}",
  182. pdu.event_id,
  183. ) from None
  184. # now let's look for events where the sender's domain is different to the
  185. # event id's domain (normally only the case for joins/leaves), and add additional
  186. # checks. Only do this if the room version has a concept of event ID domain
  187. # (ie, the room version uses old-style non-hash event IDs).
  188. if room_version.event_format == EventFormatVersions.ROOM_V1_V2:
  189. event_domain = get_domain_from_id(pdu.event_id)
  190. if event_domain != sender_domain:
  191. try:
  192. await keyring.verify_event_for_server(
  193. event_domain,
  194. pdu,
  195. pdu.origin_server_ts if room_version.enforce_key_validity else 0,
  196. )
  197. except Exception as e:
  198. raise InvalidEventSignatureError(
  199. f"unable to verify signature for event domain {event_domain}: {e}",
  200. pdu.event_id,
  201. ) from None
  202. # If this is a join event for a restricted room it may have been authorised
  203. # via a different server from the sending server. Check those signatures.
  204. if (
  205. room_version.msc3083_join_rules
  206. and pdu.type == EventTypes.Member
  207. and pdu.membership == Membership.JOIN
  208. and EventContentFields.AUTHORISING_USER in pdu.content
  209. ):
  210. authorising_server = get_domain_from_id(
  211. pdu.content[EventContentFields.AUTHORISING_USER]
  212. )
  213. try:
  214. await keyring.verify_event_for_server(
  215. authorising_server,
  216. pdu,
  217. pdu.origin_server_ts if room_version.enforce_key_validity else 0,
  218. )
  219. except Exception as e:
  220. raise InvalidEventSignatureError(
  221. f"unable to verify signature for authorising serve {authorising_server}: {e}",
  222. pdu.event_id,
  223. ) from None
  224. def _is_invite_via_3pid(event: EventBase) -> bool:
  225. return (
  226. event.type == EventTypes.Member
  227. and event.membership == Membership.INVITE
  228. and "third_party_invite" in event.content
  229. )
  230. def event_from_pdu_json(pdu_json: JsonDict, room_version: RoomVersion) -> EventBase:
  231. """Construct an EventBase from an event json received over federation
  232. Args:
  233. pdu_json: pdu as received over federation
  234. room_version: The version of the room this event belongs to
  235. Raises:
  236. SynapseError: if the pdu is missing required fields or is otherwise
  237. not a valid matrix event
  238. """
  239. # we could probably enforce a bunch of other fields here (room_id, sender,
  240. # origin, etc etc)
  241. assert_params_in_dict(pdu_json, ("type", "depth"))
  242. # Strip any unauthorized values from "unsigned" if they exist
  243. if "unsigned" in pdu_json:
  244. _strip_unsigned_values(pdu_json)
  245. depth = pdu_json["depth"]
  246. if not isinstance(depth, int):
  247. raise SynapseError(400, "Depth %r not an intger" % (depth,), Codes.BAD_JSON)
  248. if depth < 0:
  249. raise SynapseError(400, "Depth too small", Codes.BAD_JSON)
  250. elif depth > MAX_DEPTH:
  251. raise SynapseError(400, "Depth too large", Codes.BAD_JSON)
  252. # Validate that the JSON conforms to the specification.
  253. if room_version.strict_canonicaljson:
  254. validate_canonicaljson(pdu_json)
  255. event = make_event_from_dict(pdu_json, room_version)
  256. return event
  257. def _strip_unsigned_values(pdu_dict: JsonDict) -> None:
  258. """
  259. Strip any unsigned values unless specifically allowed, as defined by the whitelist.
  260. pdu: the json dict to strip values from. Note that the dict is mutated by this
  261. function
  262. """
  263. unsigned = pdu_dict["unsigned"]
  264. if not isinstance(unsigned, dict):
  265. pdu_dict["unsigned"] = {}
  266. if pdu_dict["type"] == "m.room.member":
  267. whitelist = ["knock_room_state", "invite_room_state", "age"]
  268. else:
  269. whitelist = ["age"]
  270. filtered_unsigned = {k: v for k, v in unsigned.items() if k in whitelist}
  271. pdu_dict["unsigned"] = filtered_unsigned