2021-07-15 16:54:22 +03:00
|
|
|
<h2 style="color:red">
|
|
|
|
This page of the Synapse documentation is now deprecated. For up to date
|
2021-06-18 14:15:52 +03:00
|
|
|
documentation on setting up or writing a spam checker module, please see
|
2021-07-15 16:54:22 +03:00
|
|
|
<a href="modules.md">this page</a>.
|
|
|
|
</h2>
|
2021-06-18 14:15:52 +03:00
|
|
|
|
2020-02-13 15:40:57 +03:00
|
|
|
# Handling spam in Synapse
|
|
|
|
|
|
|
|
Synapse has support to customize spam checking behavior. It can plug into a
|
|
|
|
variety of events and affect how they are presented to users on your homeserver.
|
|
|
|
|
|
|
|
The spam checking behavior is implemented as a Python class, which must be
|
|
|
|
able to be imported by the running Synapse.
|
|
|
|
|
|
|
|
## Python spam checker class
|
|
|
|
|
|
|
|
The Python class is instantiated with two objects:
|
|
|
|
|
|
|
|
* Any configuration (see below).
|
2020-10-07 14:03:26 +03:00
|
|
|
* An instance of `synapse.module_api.ModuleApi`.
|
2020-02-13 15:40:57 +03:00
|
|
|
|
|
|
|
It then implements methods which return a boolean to alter behavior in Synapse.
|
2021-03-10 18:42:51 +03:00
|
|
|
All the methods must be defined.
|
2020-02-13 15:40:57 +03:00
|
|
|
|
|
|
|
There's a generic method for checking every event (`check_event_for_spam`), as
|
|
|
|
well as some specific methods:
|
|
|
|
|
|
|
|
* `user_may_invite`
|
|
|
|
* `user_may_create_room`
|
|
|
|
* `user_may_create_room_alias`
|
|
|
|
* `user_may_publish_room`
|
2020-12-11 22:05:15 +03:00
|
|
|
* `check_username_for_spam`
|
|
|
|
* `check_registration_for_spam`
|
2021-03-10 18:42:51 +03:00
|
|
|
* `check_media_file_for_spam`
|
2020-02-13 15:40:57 +03:00
|
|
|
|
2021-02-24 20:51:52 +03:00
|
|
|
The details of each of these methods (as well as their inputs and outputs)
|
2020-02-13 15:40:57 +03:00
|
|
|
are documented in the `synapse.events.spamcheck.SpamChecker` class.
|
|
|
|
|
2020-10-07 14:03:26 +03:00
|
|
|
The `ModuleApi` class provides a way for the custom spam checker class to
|
|
|
|
call back into the homeserver internals.
|
2020-02-13 15:40:57 +03:00
|
|
|
|
2021-03-10 18:42:51 +03:00
|
|
|
Additionally, a `parse_config` method is mandatory and receives the plugin config
|
|
|
|
dictionary. After parsing, It must return an object which will be
|
|
|
|
passed to `__init__` later.
|
|
|
|
|
2020-02-13 15:40:57 +03:00
|
|
|
### Example
|
|
|
|
|
|
|
|
```python
|
2020-12-11 22:05:15 +03:00
|
|
|
from synapse.spam_checker_api import RegistrationBehaviour
|
|
|
|
|
2020-02-13 15:40:57 +03:00
|
|
|
class ExampleSpamChecker:
|
|
|
|
def __init__(self, config, api):
|
|
|
|
self.config = config
|
|
|
|
self.api = api
|
|
|
|
|
2021-03-10 18:42:51 +03:00
|
|
|
@staticmethod
|
|
|
|
def parse_config(config):
|
|
|
|
return config
|
|
|
|
|
2020-12-11 22:05:15 +03:00
|
|
|
async def check_event_for_spam(self, foo):
|
2020-02-13 15:40:57 +03:00
|
|
|
return False # allow all events
|
|
|
|
|
2020-12-11 22:05:15 +03:00
|
|
|
async def user_may_invite(self, inviter_userid, invitee_userid, room_id):
|
2020-02-13 15:40:57 +03:00
|
|
|
return True # allow all invites
|
|
|
|
|
2020-12-11 22:05:15 +03:00
|
|
|
async def user_may_create_room(self, userid):
|
2020-02-13 15:40:57 +03:00
|
|
|
return True # allow all room creations
|
|
|
|
|
2020-12-11 22:05:15 +03:00
|
|
|
async def user_may_create_room_alias(self, userid, room_alias):
|
2020-02-13 15:40:57 +03:00
|
|
|
return True # allow all room aliases
|
|
|
|
|
2020-12-11 22:05:15 +03:00
|
|
|
async def user_may_publish_room(self, userid, room_id):
|
2020-02-13 15:40:57 +03:00
|
|
|
return True # allow publishing of all rooms
|
2020-02-14 15:17:54 +03:00
|
|
|
|
2020-12-11 22:05:15 +03:00
|
|
|
async def check_username_for_spam(self, user_profile):
|
2020-02-14 15:17:54 +03:00
|
|
|
return False # allow all usernames
|
2020-12-11 22:05:15 +03:00
|
|
|
|
2021-03-16 15:41:41 +03:00
|
|
|
async def check_registration_for_spam(
|
|
|
|
self,
|
|
|
|
email_threepid,
|
|
|
|
username,
|
|
|
|
request_info,
|
|
|
|
auth_provider_id,
|
|
|
|
):
|
2020-12-11 22:05:15 +03:00
|
|
|
return RegistrationBehaviour.ALLOW # allow all registrations
|
2021-02-03 19:44:16 +03:00
|
|
|
|
|
|
|
async def check_media_file_for_spam(self, file_wrapper, file_info):
|
|
|
|
return False # allow all media
|
2020-02-13 15:40:57 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
Modify the `spam_checker` section of your `homeserver.yaml` in the following
|
|
|
|
manner:
|
|
|
|
|
2020-05-08 21:25:48 +03:00
|
|
|
Create a list entry with the keys `module` and `config`.
|
2020-02-13 15:40:57 +03:00
|
|
|
|
2020-05-08 21:25:48 +03:00
|
|
|
* `module` should point to the fully qualified Python class that implements your
|
|
|
|
custom logic, e.g. `my_module.ExampleSpamChecker`.
|
|
|
|
|
|
|
|
* `config` is a dictionary that gets passed to the spam checker class.
|
2020-02-13 15:40:57 +03:00
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
This section might look like:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
spam_checker:
|
2020-05-08 21:25:48 +03:00
|
|
|
- module: my_module.ExampleSpamChecker
|
|
|
|
config:
|
|
|
|
# Enable or disable a specific option in ExampleSpamChecker.
|
|
|
|
my_custom_option: true
|
2020-02-13 15:40:57 +03:00
|
|
|
```
|
|
|
|
|
2020-05-08 21:25:48 +03:00
|
|
|
More spam checkers can be added in tandem by appending more items to the list. An
|
|
|
|
action is blocked when at least one of the configured spam checkers flags it.
|
|
|
|
|
2020-02-13 15:40:57 +03:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
The [Mjolnir](https://github.com/matrix-org/mjolnir) project is a full fledged
|
|
|
|
example using the Synapse spam checking API, including a bot for dynamic
|
|
|
|
configuration.
|