Compare commits

...

7 Commits

Author SHA1 Message Date
Mathieu Velten 010f36cd3d Fix stupid typo 1 year ago
Mathieu Velten e4a1e0f9ad Add user pattern matching 1 year ago
Richard van der Hoff fdca229e50 fix log 1 year ago
Richard van der Hoff 0a87d0f2c6 Soft-fail incoming spam from federation 1 year ago
Mathieu Velten eeaba8447f aaaaa 1 year ago
Mathieu Velten 42b43c51e4 fix stuffs 1 year ago
Mathieu Velten 8d79db4230 Move conf in a specific file and handle hot-reload of it 1 year ago
  1. 5
      synapse_simple_antispam/__init__.py
  2. 25
      synapse_simple_antispam/invites.py
  3. 91
      synapse_simple_antispam/messages.py

@ -1,4 +1 @@
from .invites import AntiSpamInvites
from .messages import AntiSpamHomeserverMessages
from .messages import AntiSpamText
from .messages import AntiSpamRegex
from .messages import SimpleAntiSpam

@ -1,25 +0,0 @@
class AntiSpamInvites(object):
def __init__(self, config):
self._blocked_homeservers = config.get("blocked_homeservers", [])
def check_event_for_spam(self, event):
return False # allowed
def user_may_invite(self, inviter_user_id, invitee_user_id, room_id):
for bad_hs in self._blocked_homeservers:
if inviter_user_id.endswith(":" + bad_hs):
return False # not allowed
return True # allowed
def user_may_create_room(self, user_id):
return True # allowed
def user_may_create_room_alias(self, user_id, room_alias):
return True # allowed
def user_may_publish_room(self, user_id, room_id):
return True # allowed
@staticmethod
def parse_config(config):
return config # no parsing needed

@ -1,70 +1,65 @@
import logging
import re
import yaml
class AntiSpamHomeserverMessages(object):
logger = logging.getLogger(__name__)
class SimpleAntiSpam:
def __init__(self, config):
self._blocked_homeservers = config.get("blocked_homeservers", [])
self.rules_file = config["rules_file"]
self._reload_config()
def check_event_for_spam(self, event):
for bad_hs in self._blocked_homeservers:
if event.get("sender", "").endswith(":" + bad_hs):
return True # not allowed (spam)
return False # not spam
try:
from synapse.app._base import register_sighup
register_sighup(self._reload_config)
except Exception:
logger.warning(
"Failed to install sighup handler for anti spam rule reloading"
)
def user_may_invite(self, inviter_user_id, invitee_user_id, room_id):
return True # allowed
def _reload_config(self, *args):
with open(self.rules_file) as f:
spam_config = yaml.safe_load(f)
def user_may_create_room(self, user_id):
return True # allowed
self._blocked_messages_by_homeserver = spam_config.get("blocked_messages_by_homeserver", [])
self._blocked_messages_by_user = spam_config.get("blocked_messages_by_user", [])
self._blocked_messages_by_content = spam_config.get("blocked_messages_by_content", [])
def user_may_create_room_alias(self, user_id, room_alias):
return True # allowed
self._blocked_messages_by_user_pattern = [re.compile(v) for v in spam_config.get("blocked_messages_by_user_pattern", [])]
self._blocked_messages_by_content_pattern = [re.compile(v) for v in spam_config.get("blocked_messages_by_content_pattern", [])]
def user_may_publish_room(self, user_id, room_id):
return True # allowed
@staticmethod
def parse_config(config):
return config # no parsing needed
class AntiSpamText(object):
def __init__(self, config):
self._blocked_texts = config.get("blocked_messages", [])
self._blocked_invites_by_homeserver = spam_config.get("blocked_invites_by_homeserver", [])
def check_event_for_spam(self, event):
for msg in self._blocked_texts:
if event.get("content", {}).get("body", "") == msg:
for bad_hs in self._blocked_messages_by_homeserver:
if event.sender.endswith(":" + bad_hs):
logger.info("Soft-failing event %s from %s", event.event_id,bad_hs)
event.internal_metadata.soft_failed = True
return True # not allowed (spam)
return False # not spam
def user_may_invite(self, inviter_user_id, invitee_user_id, room_id):
return True # allowed
def user_may_create_room(self, user_id):
return True # allowed
def user_may_create_room_alias(self, user_id, room_alias):
return True # allowed
def user_may_publish_room(self, user_id, room_id):
return True # allowed
@staticmethod
def parse_config(config):
return config # no parsing needed
for bad_user in self._blocked_messages_by_user:
if event.sender == bad_user:
return True # not allowed (spam)
for bad_user in self._blocked_messages_by_user_pattern:
if bad_user.search(event.sender):
return True # not allowed (spam)
class AntiSpamRegex(object):
def __init__(self, config):
self._blocked_texts = [re.compile(v) for v in config.get("blocked_messages", [])]
for msg in self._blocked_messages_by_content:
if event.content.get("body", "") == msg:
return True # not allowed (spam)
def check_event_for_spam(self, event):
for msg in self._blocked_texts:
if msg.search(event.get("content", {}).get("body", "")):
for msg in self._blocked_messages_by_content_pattern:
if msg.search(event.content.get("body", "")):
return True # not allowed (spam)
return False # not spam
def user_may_invite(self, inviter_user_id, invitee_user_id, room_id):
for bad_hs in self._blocked_invites_by_homeserver:
if inviter_user_id.endswith(":" + bad_hs):
return False # not allowed
return True # allowed
def user_may_create_room(self, user_id):

Loading…
Cancel
Save