2019-10-02 07:44:58 -06:00
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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.
|
2019-11-06 03:01:39 -07:00
|
|
|
import logging
|
2020-05-14 11:24:01 -06:00
|
|
|
from unittest import TestCase
|
2019-11-06 03:01:39 -07:00
|
|
|
|
2019-10-02 07:44:58 -06:00
|
|
|
from synapse.api.constants import EventTypes
|
2021-01-29 09:38:29 -07:00
|
|
|
from synapse.api.errors import AuthError, Codes, LimitExceededError, SynapseError
|
2020-05-14 11:24:01 -06:00
|
|
|
from synapse.api.room_versions import RoomVersions
|
|
|
|
from synapse.events import EventBase
|
2019-11-06 03:01:39 -07:00
|
|
|
from synapse.federation.federation_base import event_from_pdu_json
|
|
|
|
from synapse.logging.context import LoggingContext, run_in_background
|
2019-10-02 07:44:58 -06:00
|
|
|
from synapse.rest import admin
|
|
|
|
from synapse.rest.client.v1 import login, room
|
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
|
2019-11-06 03:01:39 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-10-02 07:44:58 -06:00
|
|
|
|
|
|
|
class FederationTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
2020-12-02 09:09:24 -07:00
|
|
|
hs = self.setup_test_homeserver(federation_http_client=None)
|
2020-10-09 05:24:34 -06:00
|
|
|
self.handler = hs.get_federation_handler()
|
2019-10-02 07:44:58 -06:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
return hs
|
|
|
|
|
|
|
|
def test_exchange_revoked_invite(self):
|
|
|
|
user_id = self.register_user("kermit", "test")
|
|
|
|
tok = self.login("kermit", "test")
|
|
|
|
|
2019-10-03 04:30:43 -06:00
|
|
|
room_id = self.helper.create_room_as(room_creator=user_id, tok=tok)
|
2019-10-02 07:44:58 -06:00
|
|
|
|
|
|
|
# Send a 3PID invite event with an empty body so it's considered as a revoked one.
|
|
|
|
invite_token = "sometoken"
|
|
|
|
self.helper.send_state(
|
|
|
|
room_id=room_id,
|
|
|
|
event_type=EventTypes.ThirdPartyInvite,
|
|
|
|
state_key=invite_token,
|
|
|
|
body={},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
d = self.handler.on_exchange_third_party_invite_request(
|
|
|
|
event_dict={
|
|
|
|
"type": EventTypes.Member,
|
|
|
|
"room_id": room_id,
|
|
|
|
"sender": user_id,
|
|
|
|
"state_key": "@someone:example.org",
|
|
|
|
"content": {
|
|
|
|
"membership": "invite",
|
|
|
|
"third_party_invite": {
|
|
|
|
"display_name": "alice",
|
|
|
|
"signed": {
|
|
|
|
"mxid": "@alice:localhost",
|
|
|
|
"token": invite_token,
|
|
|
|
"signatures": {
|
|
|
|
"magic.forest": {
|
|
|
|
"ed25519:3": "fQpGIW1Snz+pwLZu6sTy2aHy/DYWWTspTJRPyNp0PKkymfIsNffysMl6ObMMFdIJhk6g6pwlIqZ54rxo8SLmAg"
|
|
|
|
}
|
2019-10-03 04:29:07 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-02 07:44:58 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
failure = self.get_failure(d, AuthError).value
|
|
|
|
|
|
|
|
self.assertEqual(failure.code, 403, failure)
|
|
|
|
self.assertEqual(failure.errcode, Codes.FORBIDDEN, failure)
|
|
|
|
self.assertEqual(failure.msg, "You are not invited to this room.")
|
2019-11-06 03:01:39 -07:00
|
|
|
|
|
|
|
def test_rejected_message_event_state(self):
|
|
|
|
"""
|
|
|
|
Check that we store the state group correctly for rejected non-state events.
|
|
|
|
|
|
|
|
Regression test for #6289.
|
|
|
|
"""
|
|
|
|
OTHER_SERVER = "otherserver"
|
|
|
|
OTHER_USER = "@otheruser:" + OTHER_SERVER
|
|
|
|
|
|
|
|
# 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)
|
2020-01-31 09:50:13 -07:00
|
|
|
room_version = self.get_success(self.store.get_room_version(room_id))
|
2019-11-06 03:01:39 -07:00
|
|
|
|
|
|
|
# pretend that another server has joined
|
|
|
|
join_event = self._build_and_send_join_event(OTHER_SERVER, OTHER_USER, room_id)
|
|
|
|
|
|
|
|
# check the state group
|
|
|
|
sg = self.successResultOf(
|
|
|
|
self.store._get_state_group_for_event(join_event.event_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
# build and send an event which will be rejected
|
|
|
|
ev = event_from_pdu_json(
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"content": {},
|
|
|
|
"room_id": room_id,
|
|
|
|
"sender": "@yetanotheruser:" + OTHER_SERVER,
|
|
|
|
"depth": join_event["depth"] + 1,
|
|
|
|
"prev_events": [join_event.event_id],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": self.clock.time_msec(),
|
|
|
|
},
|
2020-01-31 09:50:13 -07:00
|
|
|
room_version,
|
2019-11-06 03:01:39 -07:00
|
|
|
)
|
|
|
|
|
2020-12-14 12:19:47 -07:00
|
|
|
with LoggingContext("send_rejected"):
|
2019-11-06 03:01:39 -07:00
|
|
|
d = run_in_background(self.handler.on_receive_pdu, OTHER_SERVER, ev)
|
|
|
|
self.get_success(d)
|
|
|
|
|
|
|
|
# that should have been rejected
|
|
|
|
e = self.get_success(self.store.get_event(ev.event_id, allow_rejected=True))
|
|
|
|
self.assertIsNotNone(e.rejected_reason)
|
|
|
|
|
|
|
|
# ... and the state group should be the same as before
|
|
|
|
sg2 = self.successResultOf(self.store._get_state_group_for_event(ev.event_id))
|
|
|
|
|
|
|
|
self.assertEqual(sg, sg2)
|
|
|
|
|
|
|
|
def test_rejected_state_event_state(self):
|
|
|
|
"""
|
|
|
|
Check that we store the state group correctly for rejected state events.
|
|
|
|
|
|
|
|
Regression test for #6289.
|
|
|
|
"""
|
|
|
|
OTHER_SERVER = "otherserver"
|
|
|
|
OTHER_USER = "@otheruser:" + OTHER_SERVER
|
|
|
|
|
|
|
|
# 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)
|
2020-01-31 09:50:13 -07:00
|
|
|
room_version = self.get_success(self.store.get_room_version(room_id))
|
2019-11-06 03:01:39 -07:00
|
|
|
|
|
|
|
# pretend that another server has joined
|
|
|
|
join_event = self._build_and_send_join_event(OTHER_SERVER, OTHER_USER, room_id)
|
|
|
|
|
|
|
|
# check the state group
|
|
|
|
sg = self.successResultOf(
|
|
|
|
self.store._get_state_group_for_event(join_event.event_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
# build and send an event which will be rejected
|
|
|
|
ev = event_from_pdu_json(
|
|
|
|
{
|
|
|
|
"type": "org.matrix.test",
|
|
|
|
"state_key": "test_key",
|
|
|
|
"content": {},
|
|
|
|
"room_id": room_id,
|
|
|
|
"sender": "@yetanotheruser:" + OTHER_SERVER,
|
|
|
|
"depth": join_event["depth"] + 1,
|
|
|
|
"prev_events": [join_event.event_id],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": self.clock.time_msec(),
|
|
|
|
},
|
2020-01-31 09:50:13 -07:00
|
|
|
room_version,
|
2019-11-06 03:01:39 -07:00
|
|
|
)
|
|
|
|
|
2020-12-14 12:19:47 -07:00
|
|
|
with LoggingContext("send_rejected"):
|
2019-11-06 03:01:39 -07:00
|
|
|
d = run_in_background(self.handler.on_receive_pdu, OTHER_SERVER, ev)
|
|
|
|
self.get_success(d)
|
|
|
|
|
|
|
|
# that should have been rejected
|
|
|
|
e = self.get_success(self.store.get_event(ev.event_id, allow_rejected=True))
|
|
|
|
self.assertIsNotNone(e.rejected_reason)
|
|
|
|
|
|
|
|
# ... and the state group should be the same as before
|
|
|
|
sg2 = self.successResultOf(self.store._get_state_group_for_event(ev.event_id))
|
|
|
|
|
|
|
|
self.assertEqual(sg, sg2)
|
|
|
|
|
2021-01-29 09:38:29 -07:00
|
|
|
@unittest.override_config(
|
|
|
|
{"rc_invites": {"per_user": {"per_second": 0.5, "burst_count": 3}}}
|
|
|
|
)
|
|
|
|
def test_invite_by_user_ratelimit(self):
|
|
|
|
"""Tests that invites from federation to a particular user are
|
|
|
|
actually rate-limited.
|
|
|
|
"""
|
|
|
|
other_server = "otherserver"
|
|
|
|
other_user = "@otheruser:" + other_server
|
|
|
|
|
|
|
|
# create the room
|
|
|
|
user_id = self.register_user("kermit", "test")
|
|
|
|
tok = self.login("kermit", "test")
|
|
|
|
|
|
|
|
def create_invite():
|
|
|
|
room_id = self.helper.create_room_as(room_creator=user_id, tok=tok)
|
|
|
|
room_version = self.get_success(self.store.get_room_version(room_id))
|
|
|
|
return event_from_pdu_json(
|
|
|
|
{
|
|
|
|
"type": EventTypes.Member,
|
|
|
|
"content": {"membership": "invite"},
|
|
|
|
"room_id": room_id,
|
|
|
|
"sender": other_user,
|
|
|
|
"state_key": "@user:test",
|
|
|
|
"depth": 32,
|
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": self.clock.time_msec(),
|
|
|
|
},
|
|
|
|
room_version,
|
|
|
|
)
|
|
|
|
|
2021-04-20 04:50:49 -06:00
|
|
|
for _ in range(3):
|
2021-01-29 09:38:29 -07:00
|
|
|
event = create_invite()
|
|
|
|
self.get_success(
|
|
|
|
self.handler.on_invite_request(
|
|
|
|
other_server,
|
|
|
|
event,
|
|
|
|
event.room_version,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
event = create_invite()
|
|
|
|
self.get_failure(
|
|
|
|
self.handler.on_invite_request(
|
|
|
|
other_server,
|
|
|
|
event,
|
|
|
|
event.room_version,
|
|
|
|
),
|
|
|
|
exc=LimitExceededError,
|
|
|
|
)
|
|
|
|
|
2019-11-06 03:01:39 -07:00
|
|
|
def _build_and_send_join_event(self, other_server, other_user, room_id):
|
|
|
|
join_event = self.get_success(
|
|
|
|
self.handler.on_make_join_request(other_server, room_id, other_user)
|
|
|
|
)
|
|
|
|
# the auth code requires that a signature exists, but doesn't check that
|
|
|
|
# signature... go figure.
|
|
|
|
join_event.signatures[other_server] = {"x": "y"}
|
2020-12-14 12:19:47 -07:00
|
|
|
with LoggingContext("send_join"):
|
2019-11-06 03:01:39 -07:00
|
|
|
d = run_in_background(
|
2021-06-24 08:30:49 -06:00
|
|
|
self.handler.on_send_membership_event, other_server, join_event
|
2019-11-06 03:01:39 -07:00
|
|
|
)
|
|
|
|
self.get_success(d)
|
|
|
|
|
|
|
|
# sanity-check: the room should show that the new user is a member
|
|
|
|
r = self.get_success(self.store.get_current_state_ids(room_id))
|
|
|
|
self.assertEqual(r[(EventTypes.Member, other_user)], join_event.event_id)
|
|
|
|
|
|
|
|
return join_event
|
2020-05-14 11:24:01 -06:00
|
|
|
|
|
|
|
|
|
|
|
class EventFromPduTestCase(TestCase):
|
|
|
|
def test_valid_json(self):
|
|
|
|
"""Valid JSON should be turned into an event."""
|
|
|
|
ev = event_from_pdu_json(
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"content": {"bool": True, "null": None, "int": 1, "str": "foobar"},
|
|
|
|
"room_id": "!room:test",
|
|
|
|
"sender": "@user:test",
|
|
|
|
"depth": 1,
|
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": 1234,
|
|
|
|
},
|
2020-05-15 07:30:10 -06:00
|
|
|
RoomVersions.V6,
|
2020-05-14 11:24:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIsInstance(ev, EventBase)
|
|
|
|
|
|
|
|
def test_invalid_numbers(self):
|
|
|
|
"""Invalid values for an integer should be rejected, all floats should be rejected."""
|
|
|
|
for value in [
|
|
|
|
-(2 ** 53),
|
|
|
|
2 ** 53,
|
|
|
|
1.0,
|
|
|
|
float("inf"),
|
|
|
|
float("-inf"),
|
|
|
|
float("nan"),
|
|
|
|
]:
|
|
|
|
with self.assertRaises(SynapseError):
|
|
|
|
event_from_pdu_json(
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"content": {"foo": value},
|
|
|
|
"room_id": "!room:test",
|
|
|
|
"sender": "@user:test",
|
|
|
|
"depth": 1,
|
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": 1234,
|
|
|
|
},
|
2020-05-15 07:30:10 -06:00
|
|
|
RoomVersions.V6,
|
2020-05-14 11:24:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_invalid_nested(self):
|
|
|
|
"""List and dictionaries are recursively searched."""
|
|
|
|
with self.assertRaises(SynapseError):
|
|
|
|
event_from_pdu_json(
|
|
|
|
{
|
|
|
|
"type": EventTypes.Message,
|
|
|
|
"content": {"foo": [{"bar": 2 ** 56}]},
|
|
|
|
"room_id": "!room:test",
|
|
|
|
"sender": "@user:test",
|
|
|
|
"depth": 1,
|
|
|
|
"prev_events": [],
|
|
|
|
"auth_events": [],
|
|
|
|
"origin_server_ts": 1234,
|
|
|
|
},
|
2020-05-15 07:30:10 -06:00
|
|
|
RoomVersions.V6,
|
2020-05-14 11:24:01 -06:00
|
|
|
)
|