test_federation.py 4.9 KB

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