test_federation.py 5.0 KB

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