test_federation.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from mock import Mock
  2. from twisted.internet.defer import ensureDeferred, maybeDeferred, succeed
  3. from synapse.events import make_event_from_dict
  4. from synapse.logging.context import LoggingContext
  5. from synapse.types import Requester, UserID
  6. from synapse.util import Clock
  7. from tests import unittest
  8. from tests.server import ThreadedMemoryReactorClock, setup_test_homeserver
  9. class MessageAcceptTests(unittest.TestCase):
  10. def setUp(self):
  11. self.http_client = Mock()
  12. self.reactor = ThreadedMemoryReactorClock()
  13. self.hs_clock = Clock(self.reactor)
  14. self.homeserver = setup_test_homeserver(
  15. self.addCleanup,
  16. http_client=self.http_client,
  17. clock=self.hs_clock,
  18. reactor=self.reactor,
  19. )
  20. user_id = UserID("us", "test")
  21. our_user = Requester(user_id, None, False, None, None)
  22. room_creator = self.homeserver.get_room_creation_handler()
  23. room = ensureDeferred(
  24. room_creator.create_room(
  25. our_user, room_creator.PRESETS_DICT["public_chat"], ratelimit=False
  26. )
  27. )
  28. self.reactor.advance(0.1)
  29. self.room_id = self.successResultOf(room)["room_id"]
  30. self.store = self.homeserver.get_datastore()
  31. # Figure out what the most recent event is
  32. most_recent = self.successResultOf(
  33. maybeDeferred(
  34. self.homeserver.get_datastore().get_latest_event_ids_in_room,
  35. self.room_id,
  36. )
  37. )[0]
  38. join_event = make_event_from_dict(
  39. {
  40. "room_id": self.room_id,
  41. "sender": "@baduser:test.serv",
  42. "state_key": "@baduser:test.serv",
  43. "event_id": "$join:test.serv",
  44. "depth": 1000,
  45. "origin_server_ts": 1,
  46. "type": "m.room.member",
  47. "origin": "test.servx",
  48. "content": {"membership": "join"},
  49. "auth_events": [],
  50. "prev_state": [(most_recent, {})],
  51. "prev_events": [(most_recent, {})],
  52. }
  53. )
  54. self.handler = self.homeserver.get_handlers().federation_handler
  55. self.handler.do_auth = lambda origin, event, context, auth_events: succeed(
  56. context
  57. )
  58. self.client = self.homeserver.get_federation_client()
  59. self.client._check_sigs_and_hash_and_fetch = lambda dest, pdus, **k: succeed(
  60. pdus
  61. )
  62. # Send the join, it should return None (which is not an error)
  63. d = ensureDeferred(
  64. self.handler.on_receive_pdu(
  65. "test.serv", join_event, sent_to_us_directly=True
  66. )
  67. )
  68. self.reactor.advance(1)
  69. self.assertEqual(self.successResultOf(d), None)
  70. # Make sure we actually joined the room
  71. self.assertEqual(
  72. self.successResultOf(
  73. maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  74. )[0],
  75. "$join:test.serv",
  76. )
  77. def test_cant_hide_direct_ancestors(self):
  78. """
  79. If you send a message, you must be able to provide the direct
  80. prev_events that said event references.
  81. """
  82. def post_json(destination, path, data, headers=None, timeout=0):
  83. # If it asks us for new missing events, give them NOTHING
  84. if path.startswith("/_matrix/federation/v1/get_missing_events/"):
  85. return {"events": []}
  86. self.http_client.post_json = post_json
  87. # Figure out what the most recent event is
  88. most_recent = self.successResultOf(
  89. maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  90. )[0]
  91. # Now lie about an event
  92. lying_event = make_event_from_dict(
  93. {
  94. "room_id": self.room_id,
  95. "sender": "@baduser:test.serv",
  96. "event_id": "one:test.serv",
  97. "depth": 1000,
  98. "origin_server_ts": 1,
  99. "type": "m.room.message",
  100. "origin": "test.serv",
  101. "content": {"body": "hewwo?"},
  102. "auth_events": [],
  103. "prev_events": [("two:test.serv", {}), (most_recent, {})],
  104. }
  105. )
  106. with LoggingContext(request="lying_event"):
  107. d = ensureDeferred(
  108. self.handler.on_receive_pdu(
  109. "test.serv", lying_event, sent_to_us_directly=True
  110. )
  111. )
  112. # Step the reactor, so the database fetches come back
  113. self.reactor.advance(1)
  114. # on_receive_pdu should throw an error
  115. failure = self.failureResultOf(d)
  116. self.assertEqual(
  117. failure.value.args[0],
  118. (
  119. "ERROR 403: Your server isn't divulging details about prev_events "
  120. "referenced in this event."
  121. ),
  122. )
  123. # Make sure the invalid event isn't there
  124. extrem = maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  125. self.assertEqual(self.successResultOf(extrem)[0], "$join:test.serv")