2018-06-14 07:28:36 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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.
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import unittest
|
|
|
|
|
2018-06-14 07:28:36 -04:00
|
|
|
from synapse import event_auth
|
|
|
|
from synapse.api.errors import AuthError
|
2019-04-01 05:24:38 -04:00
|
|
|
from synapse.api.room_versions import RoomVersions
|
2020-02-07 10:30:04 -05:00
|
|
|
from synapse.events import make_event_from_dict
|
2020-03-09 08:58:25 -04:00
|
|
|
from synapse.types import get_domain_from_id
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
class EventAuthTestCase(unittest.TestCase):
|
|
|
|
def test_random_users_cannot_send_state_before_first_pl(self):
|
|
|
|
"""
|
|
|
|
Check that, before the first PL lands, the creator is the only user
|
|
|
|
that can send a state event.
|
|
|
|
"""
|
|
|
|
creator = "@creator:example.com"
|
|
|
|
joiner = "@joiner:example.com"
|
|
|
|
auth_events = {
|
|
|
|
("m.room.create", ""): _create_event(creator),
|
|
|
|
("m.room.member", creator): _join_event(creator),
|
|
|
|
("m.room.member", joiner): _join_event(joiner),
|
|
|
|
}
|
|
|
|
|
|
|
|
# creator should be able to send state
|
2019-01-25 13:31:41 -05:00
|
|
|
event_auth.check(
|
2020-01-28 09:18:29 -05:00
|
|
|
RoomVersions.V1,
|
2019-05-10 01:12:11 -04:00
|
|
|
_random_state_event(creator),
|
|
|
|
auth_events,
|
2019-01-25 13:31:41 -05:00
|
|
|
do_sig_check=False,
|
|
|
|
)
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
# joiner should not be able to send state
|
|
|
|
self.assertRaises(
|
|
|
|
AuthError,
|
|
|
|
event_auth.check,
|
2020-01-28 09:18:29 -05:00
|
|
|
RoomVersions.V1,
|
2018-06-14 07:28:36 -04:00
|
|
|
_random_state_event(joiner),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
2020-03-09 08:58:25 -04:00
|
|
|
)
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
def test_state_default_level(self):
|
|
|
|
"""
|
|
|
|
Check that users above the state_default level can send state and
|
|
|
|
those below cannot
|
|
|
|
"""
|
|
|
|
creator = "@creator:example.com"
|
|
|
|
pleb = "@joiner:example.com"
|
|
|
|
king = "@joiner2:example.com"
|
|
|
|
|
|
|
|
auth_events = {
|
|
|
|
("m.room.create", ""): _create_event(creator),
|
|
|
|
("m.room.member", creator): _join_event(creator),
|
2018-08-10 09:54:09 -04:00
|
|
|
("m.room.power_levels", ""): _power_levels_event(
|
|
|
|
creator, {"state_default": "30", "users": {pleb: "29", king: "30"}}
|
|
|
|
),
|
2018-06-14 07:28:36 -04:00
|
|
|
("m.room.member", pleb): _join_event(pleb),
|
|
|
|
("m.room.member", king): _join_event(king),
|
|
|
|
}
|
|
|
|
|
|
|
|
# pleb should not be able to send state
|
|
|
|
self.assertRaises(
|
|
|
|
AuthError,
|
|
|
|
event_auth.check,
|
2020-01-28 09:18:29 -05:00
|
|
|
RoomVersions.V1,
|
2018-06-14 07:28:36 -04:00
|
|
|
_random_state_event(pleb),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
|
|
|
),
|
|
|
|
|
|
|
|
# king should be able to send state
|
2019-01-25 13:31:41 -05:00
|
|
|
event_auth.check(
|
2020-01-28 09:18:29 -05:00
|
|
|
RoomVersions.V1, _random_state_event(king), auth_events, do_sig_check=False,
|
2019-01-25 13:31:41 -05:00
|
|
|
)
|
2018-06-14 07:28:36 -04:00
|
|
|
|
2020-03-09 08:58:25 -04:00
|
|
|
def test_alias_event(self):
|
|
|
|
"""Alias events have special behavior up through room version 6."""
|
|
|
|
creator = "@creator:example.com"
|
|
|
|
other = "@other:example.com"
|
|
|
|
auth_events = {
|
|
|
|
("m.room.create", ""): _create_event(creator),
|
|
|
|
("m.room.member", creator): _join_event(creator),
|
|
|
|
}
|
|
|
|
|
|
|
|
# creator should be able to send aliases
|
|
|
|
event_auth.check(
|
|
|
|
RoomVersions.V1, _alias_event(creator), auth_events, do_sig_check=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Reject an event with no state key.
|
|
|
|
with self.assertRaises(AuthError):
|
|
|
|
event_auth.check(
|
|
|
|
RoomVersions.V1,
|
|
|
|
_alias_event(creator, state_key=""),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# If the domain of the sender does not match the state key, reject.
|
|
|
|
with self.assertRaises(AuthError):
|
|
|
|
event_auth.check(
|
|
|
|
RoomVersions.V1,
|
|
|
|
_alias_event(creator, state_key="test.com"),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Note that the member does *not* need to be in the room.
|
|
|
|
event_auth.check(
|
|
|
|
RoomVersions.V1, _alias_event(other), auth_events, do_sig_check=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_msc2432_alias_event(self):
|
|
|
|
"""After MSC2432, alias events have no special behavior."""
|
|
|
|
creator = "@creator:example.com"
|
|
|
|
other = "@other:example.com"
|
|
|
|
auth_events = {
|
|
|
|
("m.room.create", ""): _create_event(creator),
|
|
|
|
("m.room.member", creator): _join_event(creator),
|
|
|
|
}
|
|
|
|
|
|
|
|
# creator should be able to send aliases
|
|
|
|
event_auth.check(
|
2020-05-15 09:30:10 -04:00
|
|
|
RoomVersions.V6, _alias_event(creator), auth_events, do_sig_check=False,
|
2020-03-09 08:58:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# No particular checks are done on the state key.
|
|
|
|
event_auth.check(
|
2020-05-15 09:30:10 -04:00
|
|
|
RoomVersions.V6,
|
2020-03-09 08:58:25 -04:00
|
|
|
_alias_event(creator, state_key=""),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
|
|
|
)
|
|
|
|
event_auth.check(
|
2020-05-15 09:30:10 -04:00
|
|
|
RoomVersions.V6,
|
2020-03-09 08:58:25 -04:00
|
|
|
_alias_event(creator, state_key="test.com"),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Per standard auth rules, the member must be in the room.
|
|
|
|
with self.assertRaises(AuthError):
|
|
|
|
event_auth.check(
|
2020-05-15 09:30:10 -04:00
|
|
|
RoomVersions.V6, _alias_event(other), auth_events, do_sig_check=False,
|
2020-03-09 08:58:25 -04:00
|
|
|
)
|
|
|
|
|
2020-05-14 12:38:17 -04:00
|
|
|
def test_msc2209(self):
|
|
|
|
"""
|
|
|
|
Notifications power levels get checked due to MSC2209.
|
|
|
|
"""
|
|
|
|
creator = "@creator:example.com"
|
|
|
|
pleb = "@joiner:example.com"
|
|
|
|
|
|
|
|
auth_events = {
|
|
|
|
("m.room.create", ""): _create_event(creator),
|
|
|
|
("m.room.member", creator): _join_event(creator),
|
|
|
|
("m.room.power_levels", ""): _power_levels_event(
|
|
|
|
creator, {"state_default": "30", "users": {pleb: "30"}}
|
|
|
|
),
|
|
|
|
("m.room.member", pleb): _join_event(pleb),
|
|
|
|
}
|
|
|
|
|
|
|
|
# pleb should be able to modify the notifications power level.
|
|
|
|
event_auth.check(
|
|
|
|
RoomVersions.V1,
|
|
|
|
_power_levels_event(pleb, {"notifications": {"room": 100}}),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# But an MSC2209 room rejects this change.
|
|
|
|
with self.assertRaises(AuthError):
|
|
|
|
event_auth.check(
|
2020-05-15 09:30:10 -04:00
|
|
|
RoomVersions.V6,
|
2020-05-14 12:38:17 -04:00
|
|
|
_power_levels_event(pleb, {"notifications": {"room": 100}}),
|
|
|
|
auth_events,
|
|
|
|
do_sig_check=False,
|
|
|
|
)
|
|
|
|
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
# helpers for making events
|
|
|
|
|
|
|
|
TEST_ROOM_ID = "!test:room"
|
|
|
|
|
|
|
|
|
|
|
|
def _create_event(user_id):
|
2020-02-07 10:30:04 -05:00
|
|
|
return make_event_from_dict(
|
2018-08-10 09:54:09 -04:00
|
|
|
{
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"event_id": _get_event_id(),
|
|
|
|
"type": "m.room.create",
|
|
|
|
"sender": user_id,
|
|
|
|
"content": {"creator": user_id},
|
|
|
|
}
|
|
|
|
)
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _join_event(user_id):
|
2020-02-07 10:30:04 -05:00
|
|
|
return make_event_from_dict(
|
2018-08-10 09:54:09 -04:00
|
|
|
{
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"event_id": _get_event_id(),
|
|
|
|
"type": "m.room.member",
|
|
|
|
"sender": user_id,
|
|
|
|
"state_key": user_id,
|
|
|
|
"content": {"membership": "join"},
|
|
|
|
}
|
|
|
|
)
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _power_levels_event(sender, content):
|
2020-02-07 10:30:04 -05:00
|
|
|
return make_event_from_dict(
|
2018-08-10 09:54:09 -04:00
|
|
|
{
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"event_id": _get_event_id(),
|
|
|
|
"type": "m.room.power_levels",
|
|
|
|
"sender": sender,
|
|
|
|
"state_key": "",
|
|
|
|
"content": content,
|
|
|
|
}
|
|
|
|
)
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
|
2020-03-09 08:58:25 -04:00
|
|
|
def _alias_event(sender, **kwargs):
|
|
|
|
data = {
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"event_id": _get_event_id(),
|
|
|
|
"type": "m.room.aliases",
|
|
|
|
"sender": sender,
|
|
|
|
"state_key": get_domain_from_id(sender),
|
|
|
|
"content": {"aliases": []},
|
|
|
|
}
|
|
|
|
data.update(**kwargs)
|
|
|
|
return make_event_from_dict(data)
|
|
|
|
|
|
|
|
|
2018-06-14 07:28:36 -04:00
|
|
|
def _random_state_event(sender):
|
2020-02-07 10:30:04 -05:00
|
|
|
return make_event_from_dict(
|
2018-08-10 09:54:09 -04:00
|
|
|
{
|
|
|
|
"room_id": TEST_ROOM_ID,
|
|
|
|
"event_id": _get_event_id(),
|
|
|
|
"type": "test.state",
|
|
|
|
"sender": sender,
|
|
|
|
"state_key": "",
|
|
|
|
"content": {"membership": "join"},
|
|
|
|
}
|
|
|
|
)
|
2018-06-14 07:28:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
event_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
def _get_event_id():
|
|
|
|
global event_count
|
|
|
|
c = event_count
|
|
|
|
event_count += 1
|
2018-08-10 09:54:09 -04:00
|
|
|
return "!%i:example.com" % (c,)
|