1
0

test_federation_server.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. channel = self.make_request(
  40. "POST",
  41. "/_matrix/federation/v1/get_missing_events/%s" % (room_1,),
  42. query_content,
  43. )
  44. self.assertEquals(400, channel.code, channel.result)
  45. self.assertEqual(channel.json_body["errcode"], "M_NOT_JSON")
  46. class ServerACLsTestCase(unittest.TestCase):
  47. def test_blacklisted_server(self):
  48. e = _create_acl_event({"allow": ["*"], "deny": ["evil.com"]})
  49. logging.info("ACL event: %s", e.content)
  50. self.assertFalse(server_matches_acl_event("evil.com", e))
  51. self.assertFalse(server_matches_acl_event("EVIL.COM", e))
  52. self.assertTrue(server_matches_acl_event("evil.com.au", e))
  53. self.assertTrue(server_matches_acl_event("honestly.not.evil.com", e))
  54. def test_block_ip_literals(self):
  55. e = _create_acl_event({"allow_ip_literals": False, "allow": ["*"]})
  56. logging.info("ACL event: %s", e.content)
  57. self.assertFalse(server_matches_acl_event("1.2.3.4", e))
  58. self.assertTrue(server_matches_acl_event("1a.2.3.4", e))
  59. self.assertFalse(server_matches_acl_event("[1:2::]", e))
  60. self.assertTrue(server_matches_acl_event("1:2:3:4", e))
  61. class StateQueryTests(unittest.FederatingHomeserverTestCase):
  62. servlets = [
  63. admin.register_servlets,
  64. room.register_servlets,
  65. login.register_servlets,
  66. ]
  67. def test_without_event_id(self):
  68. """
  69. Querying v1/state/<room_id> without an event ID will return the current
  70. known state.
  71. """
  72. u1 = self.register_user("u1", "pass")
  73. u1_token = self.login("u1", "pass")
  74. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  75. self.inject_room_member(room_1, "@user:other.example.com", "join")
  76. channel = self.make_request(
  77. "GET", "/_matrix/federation/v1/state/%s" % (room_1,)
  78. )
  79. self.assertEquals(200, channel.code, channel.result)
  80. self.assertEqual(
  81. channel.json_body["room_version"],
  82. self.hs.config.default_room_version.identifier,
  83. )
  84. members = set(
  85. map(
  86. lambda x: x["state_key"],
  87. filter(
  88. lambda x: x["type"] == "m.room.member", channel.json_body["pdus"]
  89. ),
  90. )
  91. )
  92. self.assertEqual(members, {"@user:other.example.com", u1})
  93. self.assertEqual(len(channel.json_body["pdus"]), 6)
  94. def test_needs_to_be_in_room(self):
  95. """
  96. Querying v1/state/<room_id> requires the server
  97. be in the room to provide data.
  98. """
  99. u1 = self.register_user("u1", "pass")
  100. u1_token = self.login("u1", "pass")
  101. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  102. channel = self.make_request(
  103. "GET", "/_matrix/federation/v1/state/%s" % (room_1,)
  104. )
  105. self.assertEquals(403, channel.code, channel.result)
  106. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  107. def _create_acl_event(content):
  108. return make_event_from_dict(
  109. {
  110. "room_id": "!a:b",
  111. "event_id": "$a:b",
  112. "type": "m.room.server_acls",
  113. "sender": "@a:b",
  114. "content": content,
  115. }
  116. )