2022-02-22 07:17:10 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2022-02-22 07:17:10 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import twisted.web.client
|
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
|
|
|
from synapse.api.room_versions import RoomVersions
|
2022-07-20 16:58:51 -04:00
|
|
|
from synapse.events import EventBase
|
2022-10-03 15:53:29 -04:00
|
|
|
from synapse.rest import admin
|
|
|
|
from synapse.rest.client import login, room
|
2022-02-22 07:17:10 -05:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
|
|
|
|
2022-10-25 10:25:02 -04:00
|
|
|
from tests.test_utils import FakeResponse, event_injection
|
2022-02-22 07:17:10 -05:00
|
|
|
from tests.unittest import FederatingHomeserverTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class FederationClientTest(FederatingHomeserverTestCase):
|
2022-10-03 15:53:29 -04:00
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2023-02-06 11:05:06 -05:00
|
|
|
def prepare(
|
|
|
|
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
|
|
|
|
) -> None:
|
2022-02-22 07:17:10 -05:00
|
|
|
super().prepare(reactor, clock, homeserver)
|
|
|
|
|
|
|
|
# mock out the Agent used by the federation client, which is easier than
|
|
|
|
# catching the HTTPS connection and do the TLS stuff.
|
|
|
|
self._mock_agent = mock.create_autospec(twisted.web.client.Agent, spec_set=True)
|
|
|
|
homeserver.get_federation_http_client().agent = self._mock_agent
|
|
|
|
|
2022-07-20 16:58:51 -04:00
|
|
|
# Move clock up to somewhat realistic time so the PDU destination retry
|
|
|
|
# works (`now` needs to be larger than `0 + PDU_RETRY_TIME_MS`).
|
|
|
|
self.reactor.advance(1000000000)
|
|
|
|
|
|
|
|
self.creator = f"@creator:{self.OTHER_SERVER_NAME}"
|
|
|
|
self.test_room_id = "!room_id"
|
2022-02-22 07:17:10 -05:00
|
|
|
|
2023-02-06 11:05:06 -05:00
|
|
|
def test_get_room_state(self) -> None:
|
2022-02-22 07:17:10 -05:00
|
|
|
# mock up some events to use in the response.
|
|
|
|
# In real life, these would have things in `prev_events` and `auth_events`, but that's
|
|
|
|
# a bit annoying to mock up, and the code under test doesn't care, so we don't bother.
|
2022-07-12 14:46:32 -04:00
|
|
|
create_event_dict = self.add_hashes_and_signatures_from_other_server(
|
2022-02-22 07:17:10 -05:00
|
|
|
{
|
2022-07-20 16:58:51 -04:00
|
|
|
"room_id": self.test_room_id,
|
2022-02-22 07:17:10 -05:00
|
|
|
"type": "m.room.create",
|
|
|
|
"state_key": "",
|
2022-07-20 16:58:51 -04:00
|
|
|
"sender": self.creator,
|
|
|
|
"content": {"creator": self.creator},
|
2022-02-22 07:17:10 -05:00
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": 500,
|
|
|
|
}
|
|
|
|
)
|
2022-07-12 14:46:32 -04:00
|
|
|
member_event_dict = self.add_hashes_and_signatures_from_other_server(
|
2022-02-22 07:17:10 -05:00
|
|
|
{
|
2022-07-20 16:58:51 -04:00
|
|
|
"room_id": self.test_room_id,
|
2022-02-22 07:17:10 -05:00
|
|
|
"type": "m.room.member",
|
2022-07-20 16:58:51 -04:00
|
|
|
"sender": self.creator,
|
|
|
|
"state_key": self.creator,
|
2022-02-22 07:17:10 -05:00
|
|
|
"content": {"membership": "join"},
|
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": 600,
|
|
|
|
}
|
|
|
|
)
|
2022-07-12 14:46:32 -04:00
|
|
|
pl_event_dict = self.add_hashes_and_signatures_from_other_server(
|
2022-02-22 07:17:10 -05:00
|
|
|
{
|
2022-07-20 16:58:51 -04:00
|
|
|
"room_id": self.test_room_id,
|
2022-02-22 07:17:10 -05:00
|
|
|
"type": "m.room.power_levels",
|
2022-07-20 16:58:51 -04:00
|
|
|
"sender": self.creator,
|
2022-02-22 07:17:10 -05:00
|
|
|
"state_key": "",
|
|
|
|
"content": {},
|
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": 700,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# mock up the response, and have the agent return it
|
2022-04-27 09:58:26 -04:00
|
|
|
self._mock_agent.request.side_effect = lambda *args, **kwargs: defer.succeed(
|
2022-10-25 10:25:02 -04:00
|
|
|
FakeResponse.json(
|
|
|
|
payload={
|
2022-02-22 07:17:10 -05:00
|
|
|
"pdus": [
|
|
|
|
create_event_dict,
|
|
|
|
member_event_dict,
|
|
|
|
pl_event_dict,
|
|
|
|
],
|
|
|
|
"auth_chain": [
|
|
|
|
create_event_dict,
|
|
|
|
member_event_dict,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# now fire off the request
|
|
|
|
state_resp, auth_resp = self.get_success(
|
|
|
|
self.hs.get_federation_client().get_room_state(
|
2022-07-20 14:17:26 -04:00
|
|
|
"yet.another.server",
|
2022-07-20 16:58:51 -04:00
|
|
|
self.test_room_id,
|
2022-02-22 07:17:10 -05:00
|
|
|
"event_id",
|
|
|
|
RoomVersions.V9,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# check the right call got made to the agent
|
|
|
|
self._mock_agent.request.assert_called_once_with(
|
|
|
|
b"GET",
|
2023-06-20 05:05:31 -04:00
|
|
|
b"matrix-federation://yet.another.server/_matrix/federation/v1/state/%21room_id?event_id=event_id",
|
2022-02-22 07:17:10 -05:00
|
|
|
headers=mock.ANY,
|
|
|
|
bodyProducer=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
# ... and that the response is correct.
|
|
|
|
|
|
|
|
# the auth_resp should be empty because all the events are also in state
|
|
|
|
self.assertEqual(auth_resp, [])
|
|
|
|
|
|
|
|
# all of the events should be returned in state_resp, though not necessarily
|
|
|
|
# in the same order. We just check the type on the assumption that if the type
|
|
|
|
# is right, so is the rest of the event.
|
|
|
|
self.assertCountEqual(
|
|
|
|
[e.type for e in state_resp],
|
|
|
|
["m.room.create", "m.room.member", "m.room.power_levels"],
|
|
|
|
)
|
|
|
|
|
2023-02-06 11:05:06 -05:00
|
|
|
def test_get_pdu_returns_nothing_when_event_does_not_exist(self) -> None:
|
2022-07-20 16:58:51 -04:00
|
|
|
"""No event should be returned when the event does not exist"""
|
2022-10-26 17:10:55 -04:00
|
|
|
pulled_pdu_info = self.get_success(
|
2022-07-20 16:58:51 -04:00
|
|
|
self.hs.get_federation_client().get_pdu(
|
|
|
|
["yet.another.server"],
|
|
|
|
"event_should_not_exist",
|
|
|
|
RoomVersions.V9,
|
|
|
|
)
|
|
|
|
)
|
2022-10-26 17:10:55 -04:00
|
|
|
self.assertEqual(pulled_pdu_info, None)
|
2022-07-20 16:58:51 -04:00
|
|
|
|
2023-02-06 11:05:06 -05:00
|
|
|
def test_get_pdu(self) -> None:
|
2022-07-20 16:58:51 -04:00
|
|
|
"""Test to make sure an event is returned by `get_pdu()`"""
|
|
|
|
self._get_pdu_once()
|
|
|
|
|
2023-02-06 11:05:06 -05:00
|
|
|
def test_get_pdu_event_from_cache_is_pristine(self) -> None:
|
2022-07-20 16:58:51 -04:00
|
|
|
"""Test that modifications made to events returned by `get_pdu()`
|
|
|
|
do not propagate back to to the internal cache (events returned should
|
|
|
|
be a copy).
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Get the PDU in the cache
|
|
|
|
remote_pdu = self._get_pdu_once()
|
|
|
|
|
|
|
|
# Modify the the event reference.
|
|
|
|
# This change should not make it back to the `_get_pdu_cache`.
|
|
|
|
remote_pdu.internal_metadata.outlier = True
|
|
|
|
|
|
|
|
# Get the event again. This time it should read it from cache.
|
2022-10-26 17:10:55 -04:00
|
|
|
pulled_pdu_info2 = self.get_success(
|
2022-07-20 16:58:51 -04:00
|
|
|
self.hs.get_federation_client().get_pdu(
|
|
|
|
["yet.another.server"],
|
|
|
|
remote_pdu.event_id,
|
|
|
|
RoomVersions.V9,
|
|
|
|
)
|
|
|
|
)
|
2023-02-14 14:03:35 -05:00
|
|
|
assert pulled_pdu_info2 is not None
|
2022-10-26 17:10:55 -04:00
|
|
|
remote_pdu2 = pulled_pdu_info2.pdu
|
2022-07-20 16:58:51 -04:00
|
|
|
|
|
|
|
# Sanity check that we are working against the same event
|
|
|
|
self.assertEqual(remote_pdu.event_id, remote_pdu2.event_id)
|
|
|
|
|
|
|
|
# Make sure the event does not include modification from earlier
|
|
|
|
self.assertIsNotNone(remote_pdu2)
|
|
|
|
self.assertEqual(remote_pdu2.internal_metadata.outlier, False)
|
|
|
|
|
|
|
|
def _get_pdu_once(self) -> EventBase:
|
|
|
|
"""Retrieve an event via `get_pdu()` and assert that an event was returned.
|
|
|
|
Also used to prime the cache for subsequent test logic.
|
|
|
|
"""
|
|
|
|
message_event_dict = self.add_hashes_and_signatures_from_other_server(
|
|
|
|
{
|
|
|
|
"room_id": self.test_room_id,
|
|
|
|
"type": "m.room.message",
|
|
|
|
"sender": self.creator,
|
|
|
|
"state_key": "",
|
|
|
|
"content": {},
|
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": 700,
|
|
|
|
"depth": 10,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# mock up the response, and have the agent return it
|
|
|
|
self._mock_agent.request.side_effect = lambda *args, **kwargs: defer.succeed(
|
2022-10-25 10:25:02 -04:00
|
|
|
FakeResponse.json(
|
|
|
|
payload={
|
2022-07-20 16:58:51 -04:00
|
|
|
"origin": "yet.another.server",
|
|
|
|
"origin_server_ts": 900,
|
|
|
|
"pdus": [
|
|
|
|
message_event_dict,
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-10-26 17:10:55 -04:00
|
|
|
pulled_pdu_info = self.get_success(
|
2022-07-20 16:58:51 -04:00
|
|
|
self.hs.get_federation_client().get_pdu(
|
|
|
|
["yet.another.server"],
|
|
|
|
"event_id",
|
|
|
|
RoomVersions.V9,
|
|
|
|
)
|
|
|
|
)
|
2023-02-14 14:03:35 -05:00
|
|
|
assert pulled_pdu_info is not None
|
2022-10-26 17:10:55 -04:00
|
|
|
remote_pdu = pulled_pdu_info.pdu
|
2022-07-20 16:58:51 -04:00
|
|
|
|
|
|
|
# check the right call got made to the agent
|
|
|
|
self._mock_agent.request.assert_called_once_with(
|
|
|
|
b"GET",
|
2023-06-20 05:05:31 -04:00
|
|
|
b"matrix-federation://yet.another.server/_matrix/federation/v1/event/event_id",
|
2022-07-20 16:58:51 -04:00
|
|
|
headers=mock.ANY,
|
|
|
|
bodyProducer=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIsNotNone(remote_pdu)
|
|
|
|
self.assertEqual(remote_pdu.internal_metadata.outlier, False)
|
|
|
|
|
|
|
|
return remote_pdu
|
|
|
|
|
2022-10-03 15:53:29 -04:00
|
|
|
def test_backfill_invalid_signature_records_failed_pull_attempts(
|
|
|
|
self,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Test to make sure that events from /backfill with invalid signatures get
|
|
|
|
recorded as failed pull attempts.
|
|
|
|
"""
|
|
|
|
OTHER_USER = f"@user:{self.OTHER_SERVER_NAME}"
|
|
|
|
main_store = self.hs.get_datastores().main
|
|
|
|
|
|
|
|
# Create the room
|
|
|
|
user_id = self.register_user("kermit", "test")
|
|
|
|
tok = self.login("kermit", "test")
|
|
|
|
room_id = self.helper.create_room_as(room_creator=user_id, tok=tok)
|
|
|
|
|
|
|
|
# We purposely don't run `add_hashes_and_signatures_from_other_server`
|
|
|
|
# over this because we want the signature check to fail.
|
|
|
|
pulled_event, _ = self.get_success(
|
|
|
|
event_injection.create_event(
|
|
|
|
self.hs,
|
|
|
|
room_id=room_id,
|
|
|
|
sender=OTHER_USER,
|
|
|
|
type="test_event_type",
|
|
|
|
content={"body": "garply"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# We expect an outbound request to /backfill, so stub that out
|
|
|
|
self._mock_agent.request.side_effect = lambda *args, **kwargs: defer.succeed(
|
2022-10-25 10:25:02 -04:00
|
|
|
FakeResponse.json(
|
|
|
|
payload={
|
2022-10-03 15:53:29 -04:00
|
|
|
"origin": "yet.another.server",
|
|
|
|
"origin_server_ts": 900,
|
|
|
|
# Mimic the other server returning our new `pulled_event`
|
|
|
|
"pdus": [pulled_event.get_pdu_json()],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
self.hs.get_federation_client().backfill(
|
|
|
|
# We use "yet.another.server" instead of
|
|
|
|
# `self.OTHER_SERVER_NAME` because we want to see the behavior
|
|
|
|
# from `_check_sigs_and_hash_and_fetch_one` where it tries to
|
|
|
|
# fetch the PDU again from the origin server if the signature
|
|
|
|
# fails. Just want to make sure that the failure is counted from
|
|
|
|
# both code paths.
|
|
|
|
dest="yet.another.server",
|
|
|
|
room_id=room_id,
|
|
|
|
limit=1,
|
|
|
|
extremities=[pulled_event.event_id],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make sure our failed pull attempt was recorded
|
|
|
|
backfill_num_attempts = self.get_success(
|
|
|
|
main_store.db_pool.simple_select_one_onecol(
|
|
|
|
table="event_failed_pull_attempts",
|
|
|
|
keyvalues={"event_id": pulled_event.event_id},
|
|
|
|
retcol="num_attempts",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# This is 2 because it failed once from `self.OTHER_SERVER_NAME` and the
|
|
|
|
# other from "yet.another.server"
|
|
|
|
self.assertEqual(backfill_num_attempts, 2)
|