2020-10-13 07:07:56 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# 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]
|
2020-10-13 07:07:56 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import logging
|
|
|
|
from typing import Tuple
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2020-10-13 07:07:56 -04:00
|
|
|
from synapse.api.constants import EventTypes
|
2024-03-19 13:52:53 -04:00
|
|
|
from synapse.api.errors import SynapseError
|
2020-10-13 07:07:56 -04:00
|
|
|
from synapse.events import EventBase
|
2023-02-24 16:15:29 -05:00
|
|
|
from synapse.events.snapshot import EventContext, UnpersistedEventContextBase
|
2020-10-13 07:07:56 -04:00
|
|
|
from synapse.rest import admin
|
2021-08-17 07:57:58 -04:00
|
|
|
from synapse.rest.client import login, room
|
2022-12-16 06:53:01 -05:00
|
|
|
from synapse.server import HomeServer
|
2020-10-13 07:07:56 -04:00
|
|
|
from synapse.types import create_requester
|
2022-12-16 06:53:01 -05:00
|
|
|
from synapse.util import Clock
|
2020-10-13 07:07:56 -04:00
|
|
|
from synapse.util.stringutils import random_string
|
|
|
|
|
|
|
|
from tests import unittest
|
2021-12-11 00:08:51 -05:00
|
|
|
from tests.test_utils.event_injection import create_event
|
2020-10-13 07:07:56 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class EventCreationTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-10-13 07:07:56 -04:00
|
|
|
self.handler = self.hs.get_event_creation_handler()
|
2023-02-14 14:03:35 -05:00
|
|
|
persistence = self.hs.get_storage_controllers().persistence
|
|
|
|
assert persistence is not None
|
|
|
|
self._persist_event_storage_controller = persistence
|
2024-03-19 13:52:53 -04:00
|
|
|
self.store = self.hs.get_datastores().main
|
2020-10-13 07:07:56 -04:00
|
|
|
|
|
|
|
self.user_id = self.register_user("tester", "foobar")
|
2023-08-30 06:10:56 -04:00
|
|
|
device_id = "dev-1"
|
|
|
|
access_token = self.login("tester", "foobar", device_id=device_id)
|
|
|
|
self.room_id = self.helper.create_room_as(self.user_id, tok=access_token)
|
2024-03-19 13:52:53 -04:00
|
|
|
self.private_room_id = self.helper.create_room_as(
|
|
|
|
self.user_id, tok=access_token, extra_content={"preset": "private_chat"}
|
|
|
|
)
|
2020-10-13 07:07:56 -04:00
|
|
|
|
2023-08-30 06:10:56 -04:00
|
|
|
self.requester = create_requester(self.user_id, device_id=device_id)
|
2020-10-13 07:07:56 -04:00
|
|
|
|
2021-12-11 00:08:51 -05:00
|
|
|
def _create_and_persist_member_event(self) -> Tuple[EventBase, EventContext]:
|
|
|
|
# Create a member event we can use as an auth_event
|
|
|
|
memberEvent, memberEventContext = self.get_success(
|
|
|
|
create_event(
|
|
|
|
self.hs,
|
|
|
|
room_id=self.room_id,
|
|
|
|
type="m.room.member",
|
|
|
|
sender=self.requester.user.to_string(),
|
|
|
|
state_key=self.requester.user.to_string(),
|
|
|
|
content={"membership": "join"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._persist_event_storage_controller.persist_event(
|
|
|
|
memberEvent, memberEventContext
|
|
|
|
)
|
2021-12-11 00:08:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
return memberEvent, memberEventContext
|
|
|
|
|
2023-02-24 16:15:29 -05:00
|
|
|
def _create_duplicate_event(
|
|
|
|
self, txn_id: str
|
|
|
|
) -> Tuple[EventBase, UnpersistedEventContextBase]:
|
2020-10-13 07:07:56 -04:00
|
|
|
"""Create a new event with the given transaction ID. All events produced
|
|
|
|
by this method will be considered duplicates.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# We create a new event with a random body, as otherwise we'll produce
|
|
|
|
# *exactly* the same event with the same hash, and so same event ID.
|
|
|
|
return self.get_success(
|
|
|
|
self.handler.create_event(
|
|
|
|
self.requester,
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": self.requester.user.to_string(),
|
|
|
|
"content": {"msgtype": "m.text", "body": random_string(5)},
|
|
|
|
},
|
|
|
|
txn_id=txn_id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def test_duplicated_txn_id(self) -> None:
|
2020-10-13 07:07:56 -04:00
|
|
|
"""Test that attempting to handle/persist an event with a transaction ID
|
|
|
|
that has already been persisted correctly returns the old event and does
|
|
|
|
*not* produce duplicate messages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
txn_id = "something_suitably_random"
|
|
|
|
|
2023-02-24 16:15:29 -05:00
|
|
|
event1, unpersisted_context = self._create_duplicate_event(txn_id)
|
|
|
|
context = self.get_success(unpersisted_context.persist(event1))
|
2020-10-13 07:07:56 -04:00
|
|
|
|
|
|
|
ret_event1 = self.get_success(
|
2022-09-28 06:11:48 -04:00
|
|
|
self.handler.handle_new_client_event(
|
|
|
|
self.requester,
|
|
|
|
events_and_context=[(event1, context)],
|
|
|
|
)
|
2020-10-13 07:07:56 -04:00
|
|
|
)
|
|
|
|
stream_id1 = ret_event1.internal_metadata.stream_ordering
|
|
|
|
|
|
|
|
self.assertEqual(event1.event_id, ret_event1.event_id)
|
|
|
|
|
2023-02-24 16:15:29 -05:00
|
|
|
event2, unpersisted_context = self._create_duplicate_event(txn_id)
|
|
|
|
context = self.get_success(unpersisted_context.persist(event2))
|
2020-10-13 07:07:56 -04:00
|
|
|
|
|
|
|
# We want to test that the deduplication at the persit event end works,
|
|
|
|
# so we want to make sure we test with different events.
|
|
|
|
self.assertNotEqual(event1.event_id, event2.event_id)
|
|
|
|
|
|
|
|
ret_event2 = self.get_success(
|
2022-09-28 06:11:48 -04:00
|
|
|
self.handler.handle_new_client_event(
|
|
|
|
self.requester,
|
|
|
|
events_and_context=[(event2, context)],
|
|
|
|
)
|
2020-10-13 07:07:56 -04:00
|
|
|
)
|
|
|
|
stream_id2 = ret_event2.internal_metadata.stream_ordering
|
|
|
|
|
|
|
|
# Assert that the returned values match those from the initial event
|
|
|
|
# rather than the new one.
|
|
|
|
self.assertEqual(ret_event1.event_id, ret_event2.event_id)
|
|
|
|
self.assertEqual(stream_id1, stream_id2)
|
|
|
|
|
|
|
|
# Let's test that calling `persist_event` directly also does the right
|
|
|
|
# thing.
|
2023-02-24 16:15:29 -05:00
|
|
|
event3, unpersisted_context = self._create_duplicate_event(txn_id)
|
|
|
|
context = self.get_success(unpersisted_context.persist(event3))
|
|
|
|
|
2020-10-13 07:07:56 -04:00
|
|
|
self.assertNotEqual(event1.event_id, event3.event_id)
|
|
|
|
|
|
|
|
ret_event3, event_pos3, _ = self.get_success(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._persist_event_storage_controller.persist_event(event3, context)
|
2020-10-13 07:07:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Assert that the returned values match those from the initial event
|
|
|
|
# rather than the new one.
|
|
|
|
self.assertEqual(ret_event1.event_id, ret_event3.event_id)
|
|
|
|
self.assertEqual(stream_id1, event_pos3.stream)
|
|
|
|
|
|
|
|
# Let's test that calling `persist_events` directly also does the right
|
|
|
|
# thing.
|
2023-02-24 16:15:29 -05:00
|
|
|
event4, unpersisted_context = self._create_duplicate_event(txn_id)
|
|
|
|
context = self.get_success(unpersisted_context.persist(event4))
|
2020-10-13 07:07:56 -04:00
|
|
|
self.assertNotEqual(event1.event_id, event3.event_id)
|
|
|
|
|
|
|
|
events, _ = self.get_success(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._persist_event_storage_controller.persist_events([(event3, context)])
|
2020-10-13 07:07:56 -04:00
|
|
|
)
|
|
|
|
ret_event4 = events[0]
|
|
|
|
|
|
|
|
# Assert that the returned values match those from the initial event
|
|
|
|
# rather than the new one.
|
|
|
|
self.assertEqual(ret_event1.event_id, ret_event4.event_id)
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def test_duplicated_txn_id_one_call(self) -> None:
|
2020-10-13 07:07:56 -04:00
|
|
|
"""Test that we correctly handle duplicates that we try and persist at
|
|
|
|
the same time.
|
|
|
|
"""
|
|
|
|
|
|
|
|
txn_id = "something_else_suitably_random"
|
|
|
|
|
|
|
|
# Create two duplicate events to persist at the same time
|
2023-02-24 16:15:29 -05:00
|
|
|
event1, unpersisted_context1 = self._create_duplicate_event(txn_id)
|
|
|
|
context1 = self.get_success(unpersisted_context1.persist(event1))
|
|
|
|
event2, unpersisted_context2 = self._create_duplicate_event(txn_id)
|
|
|
|
context2 = self.get_success(unpersisted_context2.persist(event2))
|
2020-10-13 07:07:56 -04:00
|
|
|
|
|
|
|
# Ensure their event IDs are different to start with
|
|
|
|
self.assertNotEqual(event1.event_id, event2.event_id)
|
|
|
|
|
|
|
|
events, _ = self.get_success(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._persist_event_storage_controller.persist_events(
|
2020-10-13 07:07:56 -04:00
|
|
|
[(event1, context1), (event2, context2)]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that we've deduplicated the events.
|
|
|
|
self.assertEqual(len(events), 2)
|
|
|
|
self.assertEqual(events[0].event_id, events[1].event_id)
|
2020-11-03 07:13:48 -05:00
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def test_when_empty_prev_events_allowed_create_event_with_empty_prev_events(
|
|
|
|
self,
|
|
|
|
) -> None:
|
2021-12-11 00:08:51 -05:00
|
|
|
"""When we set allow_no_prev_events=True, should be able to create a
|
|
|
|
event without any prev_events (only auth_events).
|
|
|
|
"""
|
|
|
|
# Create a member event we can use as an auth_event
|
|
|
|
memberEvent, _ = self._create_and_persist_member_event()
|
|
|
|
|
|
|
|
# Try to create the event with empty prev_events bit with some auth_events
|
|
|
|
event, _ = self.get_success(
|
|
|
|
self.handler.create_event(
|
|
|
|
self.requester,
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": self.requester.user.to_string(),
|
|
|
|
"content": {"msgtype": "m.text", "body": random_string(5)},
|
|
|
|
},
|
|
|
|
# Empty prev_events is the key thing we're testing here
|
|
|
|
prev_event_ids=[],
|
|
|
|
# But with some auth_events
|
|
|
|
auth_event_ids=[memberEvent.event_id],
|
|
|
|
# Allow no prev_events!
|
|
|
|
allow_no_prev_events=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.assertIsNotNone(event)
|
|
|
|
|
|
|
|
def test_when_empty_prev_events_not_allowed_reject_event_with_empty_prev_events(
|
|
|
|
self,
|
2022-12-16 06:53:01 -05:00
|
|
|
) -> None:
|
2021-12-11 00:08:51 -05:00
|
|
|
"""When we set allow_no_prev_events=False, shouldn't be able to create a
|
|
|
|
event without any prev_events even if it has auth_events. Expect an
|
|
|
|
exception to be raised.
|
|
|
|
"""
|
|
|
|
# Create a member event we can use as an auth_event
|
|
|
|
memberEvent, _ = self._create_and_persist_member_event()
|
|
|
|
|
|
|
|
# Try to create the event with empty prev_events but with some auth_events
|
|
|
|
self.get_failure(
|
|
|
|
self.handler.create_event(
|
|
|
|
self.requester,
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": self.requester.user.to_string(),
|
|
|
|
"content": {"msgtype": "m.text", "body": random_string(5)},
|
|
|
|
},
|
|
|
|
# Empty prev_events is the key thing we're testing here
|
|
|
|
prev_event_ids=[],
|
|
|
|
# But with some auth_events
|
|
|
|
auth_event_ids=[memberEvent.event_id],
|
|
|
|
# We expect the test to fail because empty prev_events are not
|
|
|
|
# allowed here!
|
|
|
|
allow_no_prev_events=False,
|
|
|
|
),
|
|
|
|
AssertionError,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_when_empty_prev_events_allowed_reject_event_with_empty_prev_events_and_auth_events(
|
|
|
|
self,
|
2022-12-16 06:53:01 -05:00
|
|
|
) -> None:
|
2021-12-11 00:08:51 -05:00
|
|
|
"""When we set allow_no_prev_events=True, should be able to create a
|
|
|
|
event without any prev_events or auth_events. Expect an exception to be
|
|
|
|
raised.
|
|
|
|
"""
|
|
|
|
# Try to create the event with empty prev_events and empty auth_events
|
|
|
|
self.get_failure(
|
|
|
|
self.handler.create_event(
|
|
|
|
self.requester,
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": self.requester.user.to_string(),
|
|
|
|
"content": {"msgtype": "m.text", "body": random_string(5)},
|
|
|
|
},
|
|
|
|
prev_event_ids=[],
|
|
|
|
# The event should be rejected when there are no auth_events
|
|
|
|
auth_event_ids=[],
|
|
|
|
# Allow no prev_events!
|
|
|
|
allow_no_prev_events=True,
|
|
|
|
),
|
|
|
|
AssertionError,
|
|
|
|
)
|
|
|
|
|
2024-03-19 13:52:53 -04:00
|
|
|
def test_call_invite_event_creation_fails_in_public_room(self) -> None:
|
|
|
|
# get prev_events for room
|
|
|
|
prev_events = self.get_success(
|
|
|
|
self.store.get_prev_events_for_room(self.room_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
# the invite in a public room should fail
|
|
|
|
self.get_failure(
|
|
|
|
self.handler.create_event(
|
|
|
|
self.requester,
|
|
|
|
{
|
|
|
|
"type": EventTypes.CallInvite,
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": self.requester.user.to_string(),
|
|
|
|
},
|
|
|
|
prev_event_ids=prev_events,
|
|
|
|
auth_event_ids=prev_events,
|
|
|
|
),
|
|
|
|
SynapseError,
|
|
|
|
)
|
|
|
|
|
|
|
|
# but a call invite in a private room should succeed
|
|
|
|
self.get_success(
|
|
|
|
self.handler.create_event(
|
|
|
|
self.requester,
|
|
|
|
{
|
|
|
|
"type": EventTypes.CallInvite,
|
|
|
|
"room_id": self.private_room_id,
|
|
|
|
"sender": self.requester.user.to_string(),
|
|
|
|
},
|
|
|
|
prev_event_ids=prev_events,
|
|
|
|
auth_event_ids=prev_events,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-11-03 07:13:48 -05:00
|
|
|
|
|
|
|
class ServerAclValidationTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-11-03 07:13:48 -05:00
|
|
|
self.user_id = self.register_user("tester", "foobar")
|
|
|
|
self.access_token = self.login("tester", "foobar")
|
|
|
|
self.room_id = self.helper.create_room_as(self.user_id, tok=self.access_token)
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def test_allow_server_acl(self) -> None:
|
2020-11-03 07:13:48 -05:00
|
|
|
"""Test that sending an ACL that blocks everyone but ourselves works."""
|
|
|
|
|
|
|
|
self.helper.send_state(
|
|
|
|
self.room_id,
|
|
|
|
EventTypes.ServerACL,
|
|
|
|
body={"allow": [self.hs.hostname]},
|
|
|
|
tok=self.access_token,
|
|
|
|
expect_code=200,
|
|
|
|
)
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def test_deny_server_acl_block_outselves(self) -> None:
|
2020-11-03 07:13:48 -05:00
|
|
|
"""Test that sending an ACL that blocks ourselves does not work."""
|
|
|
|
self.helper.send_state(
|
|
|
|
self.room_id,
|
|
|
|
EventTypes.ServerACL,
|
|
|
|
body={},
|
|
|
|
tok=self.access_token,
|
|
|
|
expect_code=400,
|
|
|
|
)
|
|
|
|
|
2022-12-16 06:53:01 -05:00
|
|
|
def test_deny_redact_server_acl(self) -> None:
|
2020-11-03 07:13:48 -05:00
|
|
|
"""Test that attempting to redact an ACL is blocked."""
|
|
|
|
|
|
|
|
body = self.helper.send_state(
|
|
|
|
self.room_id,
|
|
|
|
EventTypes.ServerACL,
|
|
|
|
body={"allow": [self.hs.hostname]},
|
|
|
|
tok=self.access_token,
|
|
|
|
expect_code=200,
|
|
|
|
)
|
|
|
|
event_id = body["event_id"]
|
|
|
|
|
|
|
|
# Redaction of event should fail.
|
|
|
|
path = "/_matrix/client/r0/rooms/%s/redact/%s" % (self.room_id, event_id)
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-11-03 07:13:48 -05:00
|
|
|
"POST", path, content={}, access_token=self.access_token
|
|
|
|
)
|
2022-08-05 10:59:09 -04:00
|
|
|
self.assertEqual(channel.code, 403)
|