test_federation_server.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. # Copyright 2019 Matrix.org Federation C.I.C
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import logging
  17. from parameterized import parameterized
  18. from synapse.events import make_event_from_dict
  19. from synapse.federation.federation_server import server_matches_acl_event
  20. from synapse.rest import admin
  21. from synapse.rest.client.v1 import login, room
  22. from tests import unittest
  23. class FederationServerTests(unittest.FederatingHomeserverTestCase):
  24. servlets = [
  25. admin.register_servlets,
  26. room.register_servlets,
  27. login.register_servlets,
  28. ]
  29. @parameterized.expand([(b"",), (b"foo",), (b'{"limit": Infinity}',)])
  30. def test_bad_request(self, query_content):
  31. """
  32. Querying with bad data returns a reasonable error code.
  33. """
  34. u1 = self.register_user("u1", "pass")
  35. u1_token = self.login("u1", "pass")
  36. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  37. self.inject_room_member(room_1, "@user:other.example.com", "join")
  38. "/get_missing_events/(?P<room_id>[^/]*)/?"
  39. request, channel = self.make_request(
  40. "POST",
  41. "/_matrix/federation/v1/get_missing_events/%s" % (room_1,),
  42. query_content,
  43. )
  44. self.render(request)
  45. self.assertEquals(400, channel.code, channel.result)
  46. self.assertEqual(channel.json_body["errcode"], "M_NOT_JSON")
  47. class ServerACLsTestCase(unittest.TestCase):
  48. def test_blacklisted_server(self):
  49. e = _create_acl_event({"allow": ["*"], "deny": ["evil.com"]})
  50. logging.info("ACL event: %s", e.content)
  51. self.assertFalse(server_matches_acl_event("evil.com", e))
  52. self.assertFalse(server_matches_acl_event("EVIL.COM", e))
  53. self.assertTrue(server_matches_acl_event("evil.com.au", e))
  54. self.assertTrue(server_matches_acl_event("honestly.not.evil.com", e))
  55. def test_block_ip_literals(self):
  56. e = _create_acl_event({"allow_ip_literals": False, "allow": ["*"]})
  57. logging.info("ACL event: %s", e.content)
  58. self.assertFalse(server_matches_acl_event("1.2.3.4", e))
  59. self.assertTrue(server_matches_acl_event("1a.2.3.4", e))
  60. self.assertFalse(server_matches_acl_event("[1:2::]", e))
  61. self.assertTrue(server_matches_acl_event("1:2:3:4", e))
  62. class StateQueryTests(unittest.FederatingHomeserverTestCase):
  63. servlets = [
  64. admin.register_servlets,
  65. room.register_servlets,
  66. login.register_servlets,
  67. ]
  68. def test_without_event_id(self):
  69. """
  70. Querying v1/state/<room_id> without an event ID will return the current
  71. known state.
  72. """
  73. u1 = self.register_user("u1", "pass")
  74. u1_token = self.login("u1", "pass")
  75. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  76. self.inject_room_member(room_1, "@user:other.example.com", "join")
  77. request, channel = self.make_request(
  78. "GET", "/_matrix/federation/v1/state/%s" % (room_1,)
  79. )
  80. self.render(request)
  81. self.assertEquals(200, channel.code, channel.result)
  82. self.assertEqual(
  83. channel.json_body["room_version"],
  84. self.hs.config.default_room_version.identifier,
  85. )
  86. members = set(
  87. map(
  88. lambda x: x["state_key"],
  89. filter(
  90. lambda x: x["type"] == "m.room.member", channel.json_body["pdus"]
  91. ),
  92. )
  93. )
  94. self.assertEqual(members, {"@user:other.example.com", u1})
  95. self.assertEqual(len(channel.json_body["pdus"]), 6)
  96. def test_needs_to_be_in_room(self):
  97. """
  98. Querying v1/state/<room_id> requires the server
  99. be in the room to provide data.
  100. """
  101. u1 = self.register_user("u1", "pass")
  102. u1_token = self.login("u1", "pass")
  103. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  104. request, channel = self.make_request(
  105. "GET", "/_matrix/federation/v1/state/%s" % (room_1,)
  106. )
  107. self.render(request)
  108. self.assertEquals(403, channel.code, channel.result)
  109. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  110. def _create_acl_event(content):
  111. return make_event_from_dict(
  112. {
  113. "room_id": "!a:b",
  114. "event_id": "$a:b",
  115. "type": "m.room.server_acls",
  116. "sender": "@a:b",
  117. "content": content,
  118. }
  119. )