2018-07-16 12:38:45 +02:00
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import logging
|
2021-04-08 23:38:54 +02:00
|
|
|
from typing import Optional
|
2022-03-04 11:31:19 +01:00
|
|
|
from unittest.mock import patch
|
2018-07-16 12:38:45 +02:00
|
|
|
|
2019-04-01 11:24:38 +02:00
|
|
|
from synapse.api.room_versions import RoomVersions
|
2022-03-04 11:31:19 +01:00
|
|
|
from synapse.events import EventBase, make_event_from_dict
|
2022-03-31 19:39:34 +02:00
|
|
|
from synapse.events.snapshot import EventContext
|
2022-03-04 11:31:19 +01:00
|
|
|
from synapse.types import JsonDict, create_requester
|
|
|
|
from synapse.visibility import filter_events_for_client, filter_events_for_server
|
2018-07-17 12:51:26 +02:00
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
from tests import unittest
|
|
|
|
from tests.utils import create_room
|
2018-07-16 12:38:45 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
TEST_ROOM_ID = "!TEST:ROOM"
|
|
|
|
|
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
class FilterEventsForServerTestCase(unittest.HomeserverTestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
super(FilterEventsForServerTestCase, self).setUp()
|
2018-07-16 12:38:45 +02:00
|
|
|
self.event_creation_handler = self.hs.get_event_creation_handler()
|
|
|
|
self.event_builder_factory = self.hs.get_event_builder_factory()
|
2022-05-31 14:17:50 +02:00
|
|
|
self._storage_controllers = self.hs.get_storage_controllers()
|
2018-07-16 12:38:45 +02:00
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
self.get_success(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM"))
|
2018-08-09 15:33:49 +02:00
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
def test_filtering(self) -> None:
|
2018-07-16 12:38:45 +02:00
|
|
|
#
|
|
|
|
# The events to be filtered consist of 10 membership events (it doesn't
|
|
|
|
# really matter if they are joins or leaves, so let's make them joins).
|
|
|
|
# One of those membership events is going to be for a user on the
|
|
|
|
# server we are filtering for (so we can check the filtering is doing
|
|
|
|
# the right thing).
|
|
|
|
#
|
|
|
|
|
|
|
|
# before we do that, we persist some other events to act as state.
|
2022-04-01 17:10:31 +02:00
|
|
|
self._inject_visibility("@admin:hs", "joined")
|
2018-07-16 12:38:45 +02:00
|
|
|
for i in range(0, 10):
|
2022-04-01 17:10:31 +02:00
|
|
|
self._inject_room_member("@resident%i:hs" % i)
|
2018-07-16 12:38:45 +02:00
|
|
|
|
|
|
|
events_to_filter = []
|
|
|
|
|
|
|
|
for i in range(0, 10):
|
|
|
|
user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server")
|
2022-04-01 17:10:31 +02:00
|
|
|
evt = self._inject_room_member(user, extra_content={"a": "b"})
|
2018-07-16 12:38:45 +02:00
|
|
|
events_to_filter.append(evt)
|
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
filtered = self.get_success(
|
2022-05-31 14:17:50 +02:00
|
|
|
filter_events_for_server(
|
2022-11-21 16:46:14 +01:00
|
|
|
self._storage_controllers, "test_server", "hs", events_to_filter
|
2022-05-31 14:17:50 +02:00
|
|
|
)
|
2018-07-16 12:38:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# the result should be 5 redacted events, and 5 unredacted events.
|
|
|
|
for i in range(0, 5):
|
|
|
|
self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
|
|
|
|
self.assertNotIn("a", filtered[i].content)
|
|
|
|
|
|
|
|
for i in range(5, 10):
|
|
|
|
self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
|
|
|
|
self.assertEqual(filtered[i].content["a"], "b")
|
|
|
|
|
2022-03-31 19:39:34 +02:00
|
|
|
def test_filter_outlier(self) -> None:
|
|
|
|
# outlier events must be returned, for the good of the collective federation
|
2022-04-01 17:10:31 +02:00
|
|
|
self._inject_room_member("@resident:remote_hs")
|
|
|
|
self._inject_visibility("@resident:remote_hs", "joined")
|
2022-03-31 19:39:34 +02:00
|
|
|
|
2022-04-01 17:10:31 +02:00
|
|
|
outlier = self._inject_outlier()
|
2022-03-31 19:39:34 +02:00
|
|
|
self.assertEqual(
|
|
|
|
self.get_success(
|
2022-05-31 14:17:50 +02:00
|
|
|
filter_events_for_server(
|
2022-11-21 16:46:14 +01:00
|
|
|
self._storage_controllers, "remote_hs", "hs", [outlier]
|
2022-05-31 14:17:50 +02:00
|
|
|
)
|
2022-03-31 19:39:34 +02:00
|
|
|
),
|
|
|
|
[outlier],
|
|
|
|
)
|
|
|
|
|
|
|
|
# it should also work when there are other events in the list
|
2022-04-01 17:10:31 +02:00
|
|
|
evt = self._inject_message("@unerased:local_hs")
|
2022-03-31 19:39:34 +02:00
|
|
|
|
|
|
|
filtered = self.get_success(
|
2022-05-31 14:17:50 +02:00
|
|
|
filter_events_for_server(
|
2022-11-21 16:46:14 +01:00
|
|
|
self._storage_controllers, "remote_hs", "local_hs", [outlier, evt]
|
2022-05-31 14:17:50 +02:00
|
|
|
)
|
2022-03-31 19:39:34 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(len(filtered), 2, f"expected 2 results, got: {filtered}")
|
|
|
|
self.assertEqual(filtered[0], outlier)
|
|
|
|
self.assertEqual(filtered[1].event_id, evt.event_id)
|
|
|
|
self.assertEqual(filtered[1].content, evt.content)
|
|
|
|
|
|
|
|
# ... but other servers should only be able to see the outlier (the other should
|
|
|
|
# be redacted)
|
|
|
|
filtered = self.get_success(
|
2022-05-31 14:17:50 +02:00
|
|
|
filter_events_for_server(
|
2022-11-21 16:46:14 +01:00
|
|
|
self._storage_controllers, "other_server", "local_hs", [outlier, evt]
|
2022-05-31 14:17:50 +02:00
|
|
|
)
|
2022-03-31 19:39:34 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(filtered[0], outlier)
|
|
|
|
self.assertEqual(filtered[1].event_id, evt.event_id)
|
|
|
|
self.assertNotIn("body", filtered[1].content)
|
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
def test_erased_user(self) -> None:
|
2018-07-16 16:22:27 +02:00
|
|
|
# 4 message events, from erased and unerased users, with a membership
|
|
|
|
# change in the middle of them.
|
|
|
|
events_to_filter = []
|
|
|
|
|
2022-04-01 17:10:31 +02:00
|
|
|
evt = self._inject_message("@unerased:local_hs")
|
2018-07-16 16:22:27 +02:00
|
|
|
events_to_filter.append(evt)
|
|
|
|
|
2022-04-01 17:10:31 +02:00
|
|
|
evt = self._inject_message("@erased:local_hs")
|
2018-07-16 16:22:27 +02:00
|
|
|
events_to_filter.append(evt)
|
|
|
|
|
2022-04-01 17:10:31 +02:00
|
|
|
evt = self._inject_room_member("@joiner:remote_hs")
|
2018-07-16 16:22:27 +02:00
|
|
|
events_to_filter.append(evt)
|
|
|
|
|
2022-04-01 17:10:31 +02:00
|
|
|
evt = self._inject_message("@unerased:local_hs")
|
2018-07-16 16:22:27 +02:00
|
|
|
events_to_filter.append(evt)
|
|
|
|
|
2022-04-01 17:10:31 +02:00
|
|
|
evt = self._inject_message("@erased:local_hs")
|
2018-07-16 16:22:27 +02:00
|
|
|
events_to_filter.append(evt)
|
|
|
|
|
|
|
|
# the erasey user gets erased
|
2022-02-23 12:04:02 +01:00
|
|
|
self.get_success(
|
|
|
|
self.hs.get_datastores().main.mark_user_erased("@erased:local_hs")
|
|
|
|
)
|
2018-07-16 16:22:27 +02:00
|
|
|
|
|
|
|
# ... and the filtering happens.
|
2021-12-02 19:13:30 +01:00
|
|
|
filtered = self.get_success(
|
2022-05-31 14:17:50 +02:00
|
|
|
filter_events_for_server(
|
2022-11-21 16:46:14 +01:00
|
|
|
self._storage_controllers, "test_server", "local_hs", events_to_filter
|
2022-05-31 14:17:50 +02:00
|
|
|
)
|
2018-07-16 16:22:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for i in range(0, len(events_to_filter)):
|
|
|
|
self.assertEqual(
|
|
|
|
events_to_filter[i].event_id,
|
|
|
|
filtered[i].event_id,
|
|
|
|
"Unexpected event at result position %i" % (i,),
|
|
|
|
)
|
|
|
|
|
|
|
|
for i in (0, 3):
|
|
|
|
self.assertEqual(
|
|
|
|
events_to_filter[i].content["body"],
|
|
|
|
filtered[i].content["body"],
|
|
|
|
"Unexpected event content at result position %i" % (i,),
|
|
|
|
)
|
|
|
|
|
|
|
|
for i in (1, 4):
|
|
|
|
self.assertNotIn("body", filtered[i].content)
|
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
def _inject_visibility(self, user_id: str, visibility: str) -> EventBase:
|
2018-07-16 12:38:45 +02:00
|
|
|
content = {"history_visibility": visibility}
|
2019-04-01 11:24:38 +02:00
|
|
|
builder = self.event_builder_factory.for_room_version(
|
2019-01-24 10:28:16 +01:00
|
|
|
RoomVersions.V1,
|
2018-07-16 12:38:45 +02:00
|
|
|
{
|
|
|
|
"type": "m.room.history_visibility",
|
|
|
|
"sender": user_id,
|
|
|
|
"state_key": "",
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"content": content,
|
2019-05-10 07:12:11 +02:00
|
|
|
},
|
2018-07-16 12:38:45 +02:00
|
|
|
)
|
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
event, context = self.get_success(
|
2020-07-22 18:29:15 +02:00
|
|
|
self.event_creation_handler.create_new_client_event(builder)
|
2018-07-16 12:38:45 +02:00
|
|
|
)
|
2022-05-31 14:17:50 +02:00
|
|
|
self.get_success(
|
|
|
|
self._storage_controllers.persistence.persist_event(event, context)
|
|
|
|
)
|
2019-07-23 15:00:55 +02:00
|
|
|
return event
|
2018-07-16 12:38:45 +02:00
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
def _inject_room_member(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
membership: str = "join",
|
|
|
|
extra_content: Optional[JsonDict] = None,
|
|
|
|
) -> EventBase:
|
2018-07-16 12:38:45 +02:00
|
|
|
content = {"membership": membership}
|
2021-04-08 23:38:54 +02:00
|
|
|
content.update(extra_content or {})
|
2019-04-01 11:24:38 +02:00
|
|
|
builder = self.event_builder_factory.for_room_version(
|
2019-01-24 10:28:16 +01:00
|
|
|
RoomVersions.V1,
|
2018-07-16 12:38:45 +02:00
|
|
|
{
|
|
|
|
"type": "m.room.member",
|
|
|
|
"sender": user_id,
|
|
|
|
"state_key": user_id,
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"content": content,
|
2019-05-10 07:12:11 +02:00
|
|
|
},
|
2018-07-16 12:38:45 +02:00
|
|
|
)
|
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
event, context = self.get_success(
|
2020-07-22 18:29:15 +02:00
|
|
|
self.event_creation_handler.create_new_client_event(builder)
|
2018-07-16 12:38:45 +02:00
|
|
|
)
|
|
|
|
|
2022-05-31 14:17:50 +02:00
|
|
|
self.get_success(
|
|
|
|
self._storage_controllers.persistence.persist_event(event, context)
|
|
|
|
)
|
2019-07-23 15:00:55 +02:00
|
|
|
return event
|
2018-07-16 15:05:31 +02:00
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
def _inject_message(
|
|
|
|
self, user_id: str, content: Optional[JsonDict] = None
|
|
|
|
) -> EventBase:
|
2018-07-16 16:22:27 +02:00
|
|
|
if content is None:
|
2019-01-29 12:19:50 +01:00
|
|
|
content = {"body": "testytest", "msgtype": "m.text"}
|
2019-04-01 11:24:38 +02:00
|
|
|
builder = self.event_builder_factory.for_room_version(
|
2019-01-24 10:28:16 +01:00
|
|
|
RoomVersions.V1,
|
2018-07-16 16:22:27 +02:00
|
|
|
{
|
|
|
|
"type": "m.room.message",
|
|
|
|
"sender": user_id,
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"content": content,
|
2019-05-10 07:12:11 +02:00
|
|
|
},
|
2018-07-16 16:22:27 +02:00
|
|
|
)
|
|
|
|
|
2021-12-02 19:13:30 +01:00
|
|
|
event, context = self.get_success(
|
2020-07-22 18:29:15 +02:00
|
|
|
self.event_creation_handler.create_new_client_event(builder)
|
2018-07-16 16:22:27 +02:00
|
|
|
)
|
|
|
|
|
2022-05-31 14:17:50 +02:00
|
|
|
self.get_success(
|
|
|
|
self._storage_controllers.persistence.persist_event(event, context)
|
|
|
|
)
|
2019-07-23 15:00:55 +02:00
|
|
|
return event
|
2022-03-04 11:31:19 +01:00
|
|
|
|
2022-03-31 19:39:34 +02:00
|
|
|
def _inject_outlier(self) -> EventBase:
|
|
|
|
builder = self.event_builder_factory.for_room_version(
|
|
|
|
RoomVersions.V1,
|
|
|
|
{
|
|
|
|
"type": "m.room.member",
|
|
|
|
"sender": "@test:user",
|
|
|
|
"state_key": "@test:user",
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"content": {"membership": "join"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
event = self.get_success(builder.build(prev_event_ids=[], auth_event_ids=[]))
|
|
|
|
event.internal_metadata.outlier = True
|
|
|
|
self.get_success(
|
2022-05-31 14:17:50 +02:00
|
|
|
self._storage_controllers.persistence.persist_event(
|
|
|
|
event, EventContext.for_outlier(self._storage_controllers)
|
2022-05-10 21:43:13 +02:00
|
|
|
)
|
2022-03-31 19:39:34 +02:00
|
|
|
)
|
|
|
|
return event
|
|
|
|
|
2022-03-04 11:31:19 +01:00
|
|
|
|
|
|
|
class FilterEventsForClientTestCase(unittest.FederatingHomeserverTestCase):
|
|
|
|
def test_out_of_band_invite_rejection(self):
|
|
|
|
# this is where we have received an invite event over federation, and then
|
|
|
|
# rejected it.
|
|
|
|
invite_pdu = {
|
|
|
|
"room_id": "!room:id",
|
|
|
|
"depth": 1,
|
|
|
|
"auth_events": [],
|
|
|
|
"prev_events": [],
|
|
|
|
"origin_server_ts": 1,
|
|
|
|
"sender": "@someone:" + self.OTHER_SERVER_NAME,
|
|
|
|
"type": "m.room.member",
|
|
|
|
"state_key": "@user:test",
|
|
|
|
"content": {"membership": "invite"},
|
|
|
|
}
|
2022-07-12 20:46:32 +02:00
|
|
|
self.add_hashes_and_signatures_from_other_server(invite_pdu)
|
2022-03-04 11:31:19 +01:00
|
|
|
invite_event_id = make_event_from_dict(invite_pdu, RoomVersions.V9).event_id
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
self.hs.get_federation_server().on_invite_request(
|
|
|
|
self.OTHER_SERVER_NAME,
|
|
|
|
invite_pdu,
|
|
|
|
"9",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# stub out do_remotely_reject_invite so that we fall back to a locally-
|
|
|
|
# generated rejection
|
|
|
|
with patch.object(
|
|
|
|
self.hs.get_federation_handler(),
|
|
|
|
"do_remotely_reject_invite",
|
|
|
|
side_effect=Exception(),
|
|
|
|
):
|
|
|
|
reject_event_id, _ = self.get_success(
|
|
|
|
self.hs.get_room_member_handler().remote_reject_invite(
|
|
|
|
invite_event_id,
|
|
|
|
txn_id=None,
|
|
|
|
requester=create_requester("@user:test"),
|
|
|
|
content={},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
invite_event, reject_event = self.get_success(
|
|
|
|
self.hs.get_datastores().main.get_events_as_list(
|
|
|
|
[invite_event_id, reject_event_id]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# the invited user should be able to see both the invite and the rejection
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_success(
|
|
|
|
filter_events_for_client(
|
2022-05-31 14:17:50 +02:00
|
|
|
self.hs.get_storage_controllers(),
|
|
|
|
"@user:test",
|
|
|
|
[invite_event, reject_event],
|
2022-03-04 11:31:19 +01:00
|
|
|
)
|
|
|
|
),
|
|
|
|
[invite_event, reject_event],
|
|
|
|
)
|
|
|
|
|
|
|
|
# other users should see neither
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_success(
|
|
|
|
filter_events_for_client(
|
2022-05-31 14:17:50 +02:00
|
|
|
self.hs.get_storage_controllers(),
|
|
|
|
"@other:test",
|
|
|
|
[invite_event, reject_event],
|
2022-03-04 11:31:19 +01:00
|
|
|
)
|
|
|
|
),
|
|
|
|
[],
|
|
|
|
)
|