test_federation.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. from mock import Mock
  2. from twisted.internet.defer import maybeDeferred, succeed
  3. from synapse.events import FrozenEvent
  4. from synapse.types import Requester, UserID
  5. from synapse.util import Clock
  6. from tests import unittest
  7. from tests.server import ThreadedMemoryReactorClock, setup_test_homeserver
  8. class MessageAcceptTests(unittest.TestCase):
  9. def setUp(self):
  10. self.http_client = Mock()
  11. self.reactor = ThreadedMemoryReactorClock()
  12. self.hs_clock = Clock(self.reactor)
  13. self.homeserver = setup_test_homeserver(
  14. http_client=self.http_client, clock=self.hs_clock, reactor=self.reactor
  15. )
  16. user_id = UserID("us", "test")
  17. our_user = Requester(user_id, None, False, None, None)
  18. room_creator = self.homeserver.get_room_creation_handler()
  19. room = room_creator.create_room(
  20. our_user, room_creator.PRESETS_DICT["public_chat"], ratelimit=False
  21. )
  22. self.reactor.advance(0.1)
  23. self.room_id = self.successResultOf(room)["room_id"]
  24. # Figure out what the most recent event is
  25. most_recent = self.successResultOf(
  26. maybeDeferred(
  27. self.homeserver.datastore.get_latest_event_ids_in_room, self.room_id
  28. )
  29. )[0]
  30. join_event = FrozenEvent(
  31. {
  32. "room_id": self.room_id,
  33. "sender": "@baduser:test.serv",
  34. "state_key": "@baduser:test.serv",
  35. "event_id": "$join:test.serv",
  36. "depth": 1000,
  37. "origin_server_ts": 1,
  38. "type": "m.room.member",
  39. "origin": "test.servx",
  40. "content": {"membership": "join"},
  41. "auth_events": [],
  42. "prev_state": [(most_recent, {})],
  43. "prev_events": [(most_recent, {})],
  44. }
  45. )
  46. self.handler = self.homeserver.get_handlers().federation_handler
  47. self.handler.do_auth = lambda *a, **b: succeed(True)
  48. self.client = self.homeserver.get_federation_client()
  49. self.client._check_sigs_and_hash_and_fetch = lambda dest, pdus, **k: succeed(
  50. pdus
  51. )
  52. # Send the join, it should return None (which is not an error)
  53. d = self.handler.on_receive_pdu(
  54. "test.serv", join_event, sent_to_us_directly=True
  55. )
  56. self.reactor.advance(1)
  57. self.assertEqual(self.successResultOf(d), None)
  58. # Make sure we actually joined the room
  59. self.assertEqual(
  60. self.successResultOf(
  61. maybeDeferred(
  62. self.homeserver.datastore.get_latest_event_ids_in_room, self.room_id
  63. )
  64. )[0],
  65. "$join:test.serv",
  66. )
  67. def test_cant_hide_direct_ancestors(self):
  68. """
  69. If you send a message, you must be able to provide the direct
  70. prev_events that said event references.
  71. """
  72. def post_json(destination, path, data, headers=None, timeout=0):
  73. # If it asks us for new missing events, give them NOTHING
  74. if path.startswith("/_matrix/federation/v1/get_missing_events/"):
  75. return {"events": []}
  76. self.http_client.post_json = post_json
  77. # Figure out what the most recent event is
  78. most_recent = self.successResultOf(
  79. maybeDeferred(
  80. self.homeserver.datastore.get_latest_event_ids_in_room, self.room_id
  81. )
  82. )[0]
  83. # Now lie about an event
  84. lying_event = FrozenEvent(
  85. {
  86. "room_id": self.room_id,
  87. "sender": "@baduser:test.serv",
  88. "event_id": "one:test.serv",
  89. "depth": 1000,
  90. "origin_server_ts": 1,
  91. "type": "m.room.message",
  92. "origin": "test.serv",
  93. "content": "hewwo?",
  94. "auth_events": [],
  95. "prev_events": [("two:test.serv", {}), (most_recent, {})],
  96. }
  97. )
  98. d = self.handler.on_receive_pdu(
  99. "test.serv", lying_event, sent_to_us_directly=True
  100. )
  101. # Step the reactor, so the database fetches come back
  102. self.reactor.advance(1)
  103. # on_receive_pdu should throw an error
  104. failure = self.failureResultOf(d)
  105. self.assertEqual(
  106. failure.value.args[0],
  107. (
  108. "ERROR 403: Your server isn't divulging details about prev_events "
  109. "referenced in this event."
  110. ),
  111. )
  112. # Make sure the invalid event isn't there
  113. extrem = maybeDeferred(
  114. self.homeserver.datastore.get_latest_event_ids_in_room, self.room_id
  115. )
  116. self.assertEqual(self.successResultOf(extrem)[0], "$join:test.serv")
  117. def test_cant_hide_past_history(self):
  118. """
  119. If you send a message, you must be able to provide the direct
  120. prev_events that said event references.
  121. """
  122. def post_json(destination, path, data, headers=None, timeout=0):
  123. if path.startswith("/_matrix/federation/v1/get_missing_events/"):
  124. return {
  125. "events": [
  126. {
  127. "room_id": self.room_id,
  128. "sender": "@baduser:test.serv",
  129. "event_id": "three:test.serv",
  130. "depth": 1000,
  131. "origin_server_ts": 1,
  132. "type": "m.room.message",
  133. "origin": "test.serv",
  134. "content": "hewwo?",
  135. "auth_events": [],
  136. "prev_events": [("four:test.serv", {})],
  137. }
  138. ]
  139. }
  140. self.http_client.post_json = post_json
  141. def get_json(destination, path, args, headers=None):
  142. if path.startswith("/_matrix/federation/v1/state_ids/"):
  143. d = self.successResultOf(
  144. self.homeserver.datastore.get_state_ids_for_event("one:test.serv")
  145. )
  146. return succeed(
  147. {
  148. "pdu_ids": [
  149. y
  150. for x, y in d.items()
  151. if x == ("m.room.member", "@us:test")
  152. ],
  153. "auth_chain_ids": list(d.values()),
  154. }
  155. )
  156. self.http_client.get_json = get_json
  157. # Figure out what the most recent event is
  158. most_recent = self.successResultOf(
  159. maybeDeferred(
  160. self.homeserver.datastore.get_latest_event_ids_in_room, self.room_id
  161. )
  162. )[0]
  163. # Make a good event
  164. good_event = FrozenEvent(
  165. {
  166. "room_id": self.room_id,
  167. "sender": "@baduser:test.serv",
  168. "event_id": "one:test.serv",
  169. "depth": 1000,
  170. "origin_server_ts": 1,
  171. "type": "m.room.message",
  172. "origin": "test.serv",
  173. "content": "hewwo?",
  174. "auth_events": [],
  175. "prev_events": [(most_recent, {})],
  176. }
  177. )
  178. d = self.handler.on_receive_pdu(
  179. "test.serv", good_event, sent_to_us_directly=True
  180. )
  181. self.reactor.advance(1)
  182. self.assertEqual(self.successResultOf(d), None)
  183. bad_event = FrozenEvent(
  184. {
  185. "room_id": self.room_id,
  186. "sender": "@baduser:test.serv",
  187. "event_id": "two:test.serv",
  188. "depth": 1000,
  189. "origin_server_ts": 1,
  190. "type": "m.room.message",
  191. "origin": "test.serv",
  192. "content": "hewwo?",
  193. "auth_events": [],
  194. "prev_events": [("one:test.serv", {}), ("three:test.serv", {})],
  195. }
  196. )
  197. d = self.handler.on_receive_pdu(
  198. "test.serv", bad_event, sent_to_us_directly=True
  199. )
  200. self.reactor.advance(1)
  201. extrem = maybeDeferred(
  202. self.homeserver.datastore.get_latest_event_ids_in_room, self.room_id
  203. )
  204. self.assertEqual(self.successResultOf(extrem)[0], "two:test.serv")
  205. state = self.homeserver.get_state_handler().get_current_state_ids(self.room_id)
  206. self.reactor.advance(1)
  207. self.assertIn(("m.room.member", "@us:test"), self.successResultOf(state).keys())