2019-10-26 02:59:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-02-09 07:40:33 +00:00
|
|
|
# Copyright 2019-2022 The Matrix.org Foundation C.I.C.
|
2019-10-26 02:59:19 +00:00
|
|
|
#
|
|
|
|
# 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
|
2022-03-18 17:20:53 +00:00
|
|
|
from typing import Dict, Union, Optional
|
2019-10-26 02:59:19 +00:00
|
|
|
from .list_rule import ALL_RULE_TYPES, RECOMMENDATION_BAN
|
|
|
|
from .ban_list import BanList
|
2022-02-11 09:13:16 +00:00
|
|
|
from synapse.module_api import UserID
|
2022-03-18 11:07:21 +00:00
|
|
|
from .message_max_length import MessageMaxLength
|
2019-10-26 02:59:19 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger("synapse.contrib." + __name__)
|
|
|
|
|
2022-02-09 07:40:33 +00:00
|
|
|
|
2019-10-25 19:32:05 +00:00
|
|
|
class AntiSpam(object):
|
2022-02-09 07:40:33 +00:00
|
|
|
"""
|
|
|
|
Implements the old synapse spam-checker API, for compatibility with older configurations.
|
|
|
|
|
|
|
|
See https://github.com/matrix-org/synapse/blob/master/docs/spam_checker.md
|
|
|
|
"""
|
|
|
|
|
2019-10-28 20:55:32 +00:00
|
|
|
def __init__(self, config, api):
|
2019-10-26 02:59:19 +00:00
|
|
|
self.block_invites = config.get("block_invites", True)
|
|
|
|
self.block_messages = config.get("block_messages", False)
|
2020-02-13 18:08:09 +00:00
|
|
|
self.block_usernames = config.get("block_usernames", False)
|
2019-10-26 02:59:19 +00:00
|
|
|
self.list_room_ids = config.get("ban_lists", [])
|
|
|
|
self.rooms_to_lists = {} # type: Dict[str, BanList]
|
2019-10-28 20:55:32 +00:00
|
|
|
self.api = api
|
2019-10-26 02:59:19 +00:00
|
|
|
|
|
|
|
# Now we build the ban lists so we can match them
|
|
|
|
self.build_lists()
|
|
|
|
|
|
|
|
def build_lists(self):
|
|
|
|
for room_id in self.list_room_ids:
|
|
|
|
self.build_list(room_id)
|
|
|
|
|
|
|
|
def build_list(self, room_id):
|
|
|
|
logger.info("Rebuilding ban list for %s" % (room_id))
|
|
|
|
self.get_list_for_room(room_id).build()
|
|
|
|
|
|
|
|
def get_list_for_room(self, room_id):
|
|
|
|
if room_id not in self.rooms_to_lists:
|
2019-10-28 20:55:32 +00:00
|
|
|
self.rooms_to_lists[room_id] = BanList(api=self.api, room_id=room_id)
|
2019-10-26 02:59:19 +00:00
|
|
|
return self.rooms_to_lists[room_id]
|
|
|
|
|
|
|
|
def is_user_banned(self, user_id):
|
|
|
|
for room_id in self.rooms_to_lists:
|
|
|
|
ban_list = self.rooms_to_lists[room_id]
|
|
|
|
for rule in ban_list.user_rules:
|
|
|
|
if rule.matches(user_id):
|
|
|
|
return rule.action == RECOMMENDATION_BAN
|
|
|
|
return False
|
|
|
|
|
2020-02-01 09:09:54 +00:00
|
|
|
def is_room_banned(self, invite_room_id):
|
|
|
|
for room_id in self.rooms_to_lists:
|
|
|
|
ban_list = self.rooms_to_lists[room_id]
|
|
|
|
for rule in ban_list.room_rules:
|
|
|
|
if rule.matches(invite_room_id):
|
|
|
|
return rule.action == RECOMMENDATION_BAN
|
|
|
|
return False
|
|
|
|
|
2019-10-26 02:59:19 +00:00
|
|
|
def is_server_banned(self, server_name):
|
|
|
|
for room_id in self.rooms_to_lists:
|
|
|
|
ban_list = self.rooms_to_lists[room_id]
|
|
|
|
for rule in ban_list.server_rules:
|
|
|
|
if rule.matches(server_name):
|
|
|
|
return rule.action == RECOMMENDATION_BAN
|
|
|
|
return False
|
|
|
|
|
|
|
|
# --- spam checker interface below here ---
|
2019-10-25 19:32:05 +00:00
|
|
|
|
|
|
|
def check_event_for_spam(self, event):
|
2019-10-26 02:59:19 +00:00
|
|
|
room_id = event.get("room_id", "")
|
|
|
|
event_type = event.get("type", "")
|
|
|
|
state_key = event.get("state_key", None)
|
|
|
|
|
|
|
|
# Rebuild the rules if there's an event for our ban lists
|
2022-02-09 07:40:33 +00:00
|
|
|
if (
|
|
|
|
state_key is not None
|
|
|
|
and event_type in ALL_RULE_TYPES
|
|
|
|
and room_id in self.list_room_ids
|
|
|
|
):
|
2019-10-26 02:59:19 +00:00
|
|
|
logger.info("Received ban list event - updating list")
|
|
|
|
self.get_list_for_room(room_id).build(with_event=event)
|
|
|
|
return False # Ban list updates aren't spam
|
|
|
|
|
2022-03-18 11:07:21 +00:00
|
|
|
if self.block_messages:
|
|
|
|
sender = UserID.from_string(event.get("sender", ""))
|
|
|
|
if self.is_user_banned(sender.to_string()):
|
|
|
|
return True
|
|
|
|
if self.is_server_banned(sender.domain):
|
|
|
|
return True
|
2019-10-26 02:59:19 +00:00
|
|
|
|
|
|
|
return False # not spam (as far as we're concerned)
|
2019-10-25 19:32:05 +00:00
|
|
|
|
|
|
|
def user_may_invite(self, inviter_user_id, invitee_user_id, room_id):
|
2019-10-26 02:59:19 +00:00
|
|
|
if not self.block_invites:
|
|
|
|
return True # allowed (we aren't blocking invites)
|
|
|
|
|
|
|
|
sender = UserID.from_string(inviter_user_id)
|
|
|
|
if self.is_user_banned(sender.to_string()):
|
|
|
|
return False
|
2020-02-01 09:09:54 +00:00
|
|
|
if self.is_room_banned(room_id):
|
|
|
|
return False
|
2019-10-26 02:59:19 +00:00
|
|
|
if self.is_server_banned(sender.domain):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True # allowed (as far as we're concerned)
|
2019-10-25 19:32:05 +00:00
|
|
|
|
2020-02-13 18:08:09 +00:00
|
|
|
def check_username_for_spam(self, user_profile):
|
|
|
|
if not self.block_usernames:
|
2022-03-17 16:53:31 +00:00
|
|
|
# /!\ NB: unlike other checks, where `True` means allowed,
|
|
|
|
# here `True` means "this user is a spammer".
|
|
|
|
return False # allowed (we aren't blocking based on usernames)
|
2020-02-13 18:08:09 +00:00
|
|
|
|
|
|
|
# Check whether the user ID or display name matches any of the banned
|
|
|
|
# patterns.
|
2022-03-18 17:20:53 +00:00
|
|
|
if user_profile["display_name"] is not None and self.is_user_banned(user_profile["display_name"]):
|
|
|
|
return True # spam
|
|
|
|
if self.is_user_banned(user_profile["user_id"]):
|
|
|
|
return True # spam
|
|
|
|
|
|
|
|
return False # not spam.
|
2020-02-13 18:08:09 +00:00
|
|
|
|
2019-10-25 19:32:05 +00:00
|
|
|
def user_may_create_room(self, user_id):
|
2019-10-26 02:59:19 +00:00
|
|
|
return True # allowed
|
2019-10-25 19:32:05 +00:00
|
|
|
|
|
|
|
def user_may_create_room_alias(self, user_id, room_alias):
|
2019-10-26 02:59:19 +00:00
|
|
|
return True # allowed
|
2019-10-25 19:32:05 +00:00
|
|
|
|
|
|
|
def user_may_publish_room(self, user_id, room_id):
|
2019-10-26 02:59:19 +00:00
|
|
|
return True # allowed
|
2019-10-25 19:32:05 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_config(config):
|
2019-10-26 02:59:19 +00:00
|
|
|
return config # no parsing needed
|
2022-02-09 07:40:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
# New module API
|
|
|
|
class Module:
|
|
|
|
"""
|
|
|
|
Our main entry point. Implements the Synapse Module API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, config, api):
|
|
|
|
self.antispam = AntiSpam(config, api)
|
2022-03-18 11:07:21 +00:00
|
|
|
self.message_max_length = MessageMaxLength(config.get("message_max_length", {}), api)
|
2022-02-09 07:40:33 +00:00
|
|
|
self.antispam.api.register_spam_checker_callbacks(
|
|
|
|
check_event_for_spam=self.check_event_for_spam,
|
|
|
|
user_may_invite=self.user_may_invite,
|
|
|
|
check_username_for_spam=self.check_username_for_spam,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Callbacks for `register_spam_checker_callbacks`
|
|
|
|
# Note that these are `async`, by opposition to the APIs in `AntiSpam`.
|
|
|
|
async def check_event_for_spam(
|
|
|
|
self, event: "synapse.events.EventBase"
|
|
|
|
) -> Union[bool, str]:
|
2022-03-18 11:07:21 +00:00
|
|
|
if self.antispam.check_event_for_spam(event):
|
|
|
|
# The event was marked by a banlist rule.
|
|
|
|
return True
|
|
|
|
if self.message_max_length.check_event_for_spam(event):
|
|
|
|
# Message too long.
|
|
|
|
return True
|
|
|
|
return False # not spam.
|
2022-02-09 07:40:33 +00:00
|
|
|
|
|
|
|
async def user_may_invite(
|
|
|
|
self, inviter_user_id: str, invitee_user_id: str, room_id: str
|
|
|
|
) -> bool:
|
|
|
|
return self.antispam.user_may_invite(inviter_user_id, invitee_user_id, room_id)
|
|
|
|
|
2022-03-18 17:20:53 +00:00
|
|
|
async def check_username_for_spam(self, user_profile: Dict[str, Optional[str]]) -> bool:
|
2022-02-09 07:40:33 +00:00
|
|
|
return self.antispam.check_username_for_spam(user_profile)
|