federation_base.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  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 collections import namedtuple
  17. import six
  18. from twisted.internet import defer
  19. from twisted.internet.defer import DeferredList
  20. from synapse.api.constants import MAX_DEPTH, EventTypes, Membership
  21. from synapse.api.errors import Codes, SynapseError
  22. from synapse.crypto.event_signing import check_event_content_hash
  23. from synapse.events import FrozenEvent
  24. from synapse.events.utils import prune_event
  25. from synapse.http.servlet import assert_params_in_dict
  26. from synapse.types import get_domain_from_id
  27. from synapse.util import logcontext, unwrapFirstError
  28. logger = logging.getLogger(__name__)
  29. class FederationBase(object):
  30. def __init__(self, hs):
  31. self.hs = hs
  32. self.server_name = hs.hostname
  33. self.keyring = hs.get_keyring()
  34. self.spam_checker = hs.get_spam_checker()
  35. self.store = hs.get_datastore()
  36. self._clock = hs.get_clock()
  37. @defer.inlineCallbacks
  38. def _check_sigs_and_hash_and_fetch(self, origin, pdus, outlier=False,
  39. include_none=False):
  40. """Takes a list of PDUs and checks the signatures and hashs of each
  41. one. If a PDU fails its signature check then we check if we have it in
  42. the database and if not then request if from the originating server of
  43. that PDU.
  44. If a PDU fails its content hash check then it is redacted.
  45. The given list of PDUs are not modified, instead the function returns
  46. a new list.
  47. Args:
  48. pdu (list)
  49. outlier (bool)
  50. Returns:
  51. Deferred : A list of PDUs that have valid signatures and hashes.
  52. """
  53. deferreds = self._check_sigs_and_hashes(pdus)
  54. @defer.inlineCallbacks
  55. def handle_check_result(pdu, deferred):
  56. try:
  57. res = yield logcontext.make_deferred_yieldable(deferred)
  58. except SynapseError:
  59. res = None
  60. if not res:
  61. # Check local db.
  62. res = yield self.store.get_event(
  63. pdu.event_id,
  64. allow_rejected=True,
  65. allow_none=True,
  66. )
  67. if not res and pdu.origin != origin:
  68. try:
  69. res = yield self.get_pdu(
  70. destinations=[pdu.origin],
  71. event_id=pdu.event_id,
  72. outlier=outlier,
  73. timeout=10000,
  74. )
  75. except SynapseError:
  76. pass
  77. if not res:
  78. logger.warn(
  79. "Failed to find copy of %s with valid signature",
  80. pdu.event_id,
  81. )
  82. defer.returnValue(res)
  83. handle = logcontext.preserve_fn(handle_check_result)
  84. deferreds2 = [
  85. handle(pdu, deferred)
  86. for pdu, deferred in zip(pdus, deferreds)
  87. ]
  88. valid_pdus = yield logcontext.make_deferred_yieldable(
  89. defer.gatherResults(
  90. deferreds2,
  91. consumeErrors=True,
  92. )
  93. ).addErrback(unwrapFirstError)
  94. if include_none:
  95. defer.returnValue(valid_pdus)
  96. else:
  97. defer.returnValue([p for p in valid_pdus if p])
  98. def _check_sigs_and_hash(self, pdu):
  99. return logcontext.make_deferred_yieldable(
  100. self._check_sigs_and_hashes([pdu])[0],
  101. )
  102. def _check_sigs_and_hashes(self, pdus):
  103. """Checks that each of the received events is correctly signed by the
  104. sending server.
  105. Args:
  106. pdus (list[FrozenEvent]): the events to be checked
  107. Returns:
  108. list[Deferred]: for each input event, a deferred which:
  109. * returns the original event if the checks pass
  110. * returns a redacted version of the event (if the signature
  111. matched but the hash did not)
  112. * throws a SynapseError if the signature check failed.
  113. The deferreds run their callbacks in the sentinel logcontext.
  114. """
  115. deferreds = _check_sigs_on_pdus(self.keyring, pdus)
  116. ctx = logcontext.LoggingContext.current_context()
  117. def callback(_, pdu):
  118. with logcontext.PreserveLoggingContext(ctx):
  119. if not check_event_content_hash(pdu):
  120. logger.warn(
  121. "Event content has been tampered, redacting %s: %s",
  122. pdu.event_id, pdu.get_pdu_json()
  123. )
  124. return prune_event(pdu)
  125. if self.spam_checker.check_event_for_spam(pdu):
  126. logger.warn(
  127. "Event contains spam, redacting %s: %s",
  128. pdu.event_id, pdu.get_pdu_json()
  129. )
  130. return prune_event(pdu)
  131. return pdu
  132. def errback(failure, pdu):
  133. failure.trap(SynapseError)
  134. with logcontext.PreserveLoggingContext(ctx):
  135. logger.warn(
  136. "Signature check failed for %s",
  137. pdu.event_id,
  138. )
  139. return failure
  140. for deferred, pdu in zip(deferreds, pdus):
  141. deferred.addCallbacks(
  142. callback, errback,
  143. callbackArgs=[pdu],
  144. errbackArgs=[pdu],
  145. )
  146. return deferreds
  147. class PduToCheckSig(namedtuple("PduToCheckSig", [
  148. "pdu", "redacted_pdu_json", "event_id_domain", "sender_domain", "deferreds",
  149. ])):
  150. pass
  151. def _check_sigs_on_pdus(keyring, pdus):
  152. """Check that the given events are correctly signed
  153. Args:
  154. keyring (synapse.crypto.Keyring): keyring object to do the checks
  155. pdus (Collection[EventBase]): the events to be checked
  156. Returns:
  157. List[Deferred]: a Deferred for each event in pdus, which will either succeed if
  158. the signatures are valid, or fail (with a SynapseError) if not.
  159. """
  160. # (currently this is written assuming the v1 room structure; we'll probably want a
  161. # separate function for checking v2 rooms)
  162. # we want to check that the event is signed by:
  163. #
  164. # (a) the server which created the event_id
  165. #
  166. # (b) the sender's server.
  167. #
  168. # - except in the case of invites created from a 3pid invite, which are exempt
  169. # from this check, because the sender has to match that of the original 3pid
  170. # invite, but the event may come from a different HS, for reasons that I don't
  171. # entirely grok (why do the senders have to match? and if they do, why doesn't the
  172. # joining server ask the inviting server to do the switcheroo with
  173. # exchange_third_party_invite?).
  174. #
  175. # That's pretty awful, since redacting such an invite will render it invalid
  176. # (because it will then look like a regular invite without a valid signature),
  177. # and signatures are *supposed* to be valid whether or not an event has been
  178. # redacted. But this isn't the worst of the ways that 3pid invites are broken.
  179. #
  180. # let's start by getting the domain for each pdu, and flattening the event back
  181. # to JSON.
  182. pdus_to_check = [
  183. PduToCheckSig(
  184. pdu=p,
  185. redacted_pdu_json=prune_event(p).get_pdu_json(),
  186. event_id_domain=get_domain_from_id(p.event_id),
  187. sender_domain=get_domain_from_id(p.sender),
  188. deferreds=[],
  189. )
  190. for p in pdus
  191. ]
  192. # first make sure that the event is signed by the event_id's domain
  193. deferreds = keyring.verify_json_objects_for_server([
  194. (p.event_id_domain, p.redacted_pdu_json)
  195. for p in pdus_to_check
  196. ])
  197. for p, d in zip(pdus_to_check, deferreds):
  198. p.deferreds.append(d)
  199. # now let's look for events where the sender's domain is different to the
  200. # event id's domain (normally only the case for joins/leaves), and add additional
  201. # checks.
  202. pdus_to_check_sender = [
  203. p for p in pdus_to_check
  204. if p.sender_domain != p.event_id_domain and not _is_invite_via_3pid(p.pdu)
  205. ]
  206. more_deferreds = keyring.verify_json_objects_for_server([
  207. (p.sender_domain, p.redacted_pdu_json)
  208. for p in pdus_to_check_sender
  209. ])
  210. for p, d in zip(pdus_to_check_sender, more_deferreds):
  211. p.deferreds.append(d)
  212. # replace lists of deferreds with single Deferreds
  213. return [_flatten_deferred_list(p.deferreds) for p in pdus_to_check]
  214. def _flatten_deferred_list(deferreds):
  215. """Given a list of one or more deferreds, either return the single deferred, or
  216. combine into a DeferredList.
  217. """
  218. if len(deferreds) > 1:
  219. return DeferredList(deferreds, fireOnOneErrback=True, consumeErrors=True)
  220. else:
  221. assert len(deferreds) == 1
  222. return deferreds[0]
  223. def _is_invite_via_3pid(event):
  224. return (
  225. event.type == EventTypes.Member
  226. and event.membership == Membership.INVITE
  227. and "third_party_invite" in event.content
  228. )
  229. def event_from_pdu_json(pdu_json, outlier=False):
  230. """Construct a FrozenEvent from an event json received over federation
  231. Args:
  232. pdu_json (object): pdu as received over federation
  233. outlier (bool): True to mark this event as an outlier
  234. Returns:
  235. FrozenEvent
  236. Raises:
  237. SynapseError: if the pdu is missing required fields or is otherwise
  238. not a valid matrix event
  239. """
  240. # we could probably enforce a bunch of other fields here (room_id, sender,
  241. # origin, etc etc)
  242. assert_params_in_dict(pdu_json, ('event_id', 'type', 'depth'))
  243. depth = pdu_json['depth']
  244. if not isinstance(depth, six.integer_types):
  245. raise SynapseError(400, "Depth %r not an intger" % (depth, ),
  246. Codes.BAD_JSON)
  247. if depth < 0:
  248. raise SynapseError(400, "Depth too small", Codes.BAD_JSON)
  249. elif depth > MAX_DEPTH:
  250. raise SynapseError(400, "Depth too large", Codes.BAD_JSON)
  251. event = FrozenEvent(
  252. pdu_json
  253. )
  254. event.internal_metadata.outlier = outlier
  255. return event