Move message_limit into antispam. (#243)

* Move message_limit into antispam.

https://github.com/matrix-org/message_limit

Not ideal but we've had complaints about keeping them separate
and unfortunately the need for this module is not going to go away.
This commit is contained in:
Gnuxie 2022-03-18 11:07:21 +00:00 committed by GitHub
parent 1880287ac4
commit 48206a8524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 10 deletions

View File

@ -84,7 +84,7 @@ set up:
## Synapse Module
**This requires Synapse 1.37.0 or higher**
**This requires Synapse 1.53.0 or higher**
Using the bot to manage your rooms is great, however if you want to use your ban lists
(or someone else's) on your server to affect all of your users then a Synapse module
@ -120,6 +120,23 @@ modules:
# these rooms.
ban_lists:
- "!roomid:example.org"
message_max_length:
# Limit the characters in a message (event body) that a client can send in an event on this server.
# By default there is no limit (beyond the the limit the spec enforces on event size).
# Uncomment if you want messages to be limited to 510 characters.
#threshold: 510
# Limit messages only in certain rooms rooms.
# By default all rooms will enforce the limit.
# Uncomment if you want messages to only be subject to character limits in certain rooms.
#rooms:
# - "!vMvyOCeCxHsggkmALd:localhost:9999"
# Also hide messages from remote servers that are over the `message_limit`.
# By default only events from this server will be limited.
# WARNING: Remote users on other servers will still be able to messages over the limit.
# Uncomment to enforce the `message_limit` on events from remote servers.
#remote_servers: true
```
*Note*: Although this is described as a "spam checker", it does much more than fight

View File

@ -18,6 +18,7 @@ from typing import Dict, Union
from .list_rule import ALL_RULE_TYPES, RECOMMENDATION_BAN
from .ban_list import BanList
from synapse.module_api import UserID
from .message_max_length import MessageMaxLength
logger = logging.getLogger("synapse.contrib." + __name__)
@ -94,14 +95,12 @@ class AntiSpam(object):
self.get_list_for_room(room_id).build(with_event=event)
return False # Ban list updates aren't spam
if not self.block_messages:
return False # not spam (we aren't blocking 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
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
return False # not spam (as far as we're concerned)
@ -151,6 +150,7 @@ class Module:
def __init__(self, config, api):
self.antispam = AntiSpam(config, api)
self.message_max_length = MessageMaxLength(config.get("message_max_length", {}), api)
self.antispam.api.register_spam_checker_callbacks(
check_event_for_spam=self.check_event_for_spam,
user_may_invite=self.user_may_invite,
@ -162,7 +162,13 @@ class Module:
async def check_event_for_spam(
self, event: "synapse.events.EventBase"
) -> Union[bool, str]:
return self.antispam.check_event_for_spam(event)
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.
async def user_may_invite(
self, inviter_user_id: str, invitee_user_id: str, room_id: str

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2019-2022 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.
import logging
from synapse.module_api import UserID
logger = logging.getLogger("synapse.contrib." + __name__)
class MessageMaxLength(object):
"""
Limits the number of characters that can be in the body of an event.
"""
def __init__(self, config, api):
self.threshold: Option[int] = config.get("threshold", None)
self.rooms: Set[str] = set(config.get("rooms", []))
self.remote_servers: bool = config.get("remote_servers", False)
self.api = api
def check_event_for_spam(self, event: "synapse.events.EventBase") -> bool:
if self.threshold is None:
return False # not spam, MessageMaxLength hasn't been configured to do anything.
sender = UserID.from_string(event.get("sender", ""))
# check if the event is from us or we if we are limiting message length from remote servers too.
if sender.domain == self.api.server_name or self.remote_servers:
body = event.get("content", {}).get("body", "")
if len(body) > self.threshold:
room_id = event.get("room_id", "")
if len(self.rooms) == 0 or room_id in self.rooms:
return True # above the limit, spam
return False # not spam (as far as we're concerned)