This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Bring spamchecker to parity with mainline #108
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
262163c
Add a spamchecker callback to allow or deny room creation based on in…
babolivier 6bcb28b
Add a spamchecker callback to allow or deny room joins (#10910)
babolivier dfcb52c
Add a spamchecker method to allow or deny 3pid invites (#10894)
babolivier c9c1bed
Converge with mainline
babolivier 23e48a1
Add a module API method to retrieve state from a room (#11204)
babolivier 481fbb0
Update DomainRuleChecker to use the new interface
babolivier 58d6cdd
Update tests
babolivier 726f13e
Delete custom user_may_create_room
babolivier 1e73b59
Lint
babolivier ed31c6b
Remove now useless config option
babolivier d859a6a
Merge branch 'dinsic' into babolivier/update_spam_checker
babolivier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a `user_may_send_3pid_invite` spam checker callback for modules to allow or deny 3PID invites. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a `user_may_create_room_with_invites` spam checker callback to allow modules to allow or deny a room creation request based on the invites and/or 3PID invites it includes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a spam checker callback to allow or deny room joins. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a module API method to retrieve the current state of a room. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
# Spam checker callbacks | ||
|
||
Spam checker callbacks allow module developers to implement spam mitigation actions for | ||
Synapse instances. Spam checker callbacks can be registered using the module API's | ||
`register_spam_checker_callbacks` method. | ||
|
||
## Callbacks | ||
|
||
The available spam checker callbacks are: | ||
|
||
### `check_event_for_spam` | ||
|
||
```python | ||
async def check_event_for_spam(event: "synapse.events.EventBase") -> Union[bool, str] | ||
``` | ||
|
||
Called when receiving an event from a client or via federation. The module can return | ||
either a `bool` to indicate whether the event must be rejected because of spam, or a `str` | ||
to indicate the event must be rejected because of spam and to give a rejection reason to | ||
forward to clients. | ||
|
||
### `user_may_join_room` | ||
|
||
```python | ||
async def user_may_join_room(user: str, room: str, is_invited: bool) -> bool | ||
``` | ||
|
||
Called when a user is trying to join a room. The module must return a `bool` to indicate | ||
whether the user can join the room. The user is represented by their Matrix user ID (e.g. | ||
`@alice:example.com`) and the room is represented by its Matrix ID (e.g. | ||
`!room:example.com`). The module is also given a boolean to indicate whether the user | ||
currently has a pending invite in the room. | ||
|
||
This callback isn't called if the join is performed by a server administrator, or in the | ||
context of a room creation. | ||
|
||
### `user_may_invite` | ||
|
||
```python | ||
async def user_may_invite(inviter: str, invitee: str, room_id: str) -> bool | ||
``` | ||
|
||
Called when processing an invitation. The module must return a `bool` indicating whether | ||
the inviter can invite the invitee to the given room. Both inviter and invitee are | ||
represented by their Matrix user ID (e.g. `@alice:example.com`). | ||
|
||
### `user_may_send_3pid_invite` | ||
|
||
```python | ||
async def user_may_send_3pid_invite( | ||
inviter: str, | ||
medium: str, | ||
address: str, | ||
room_id: str, | ||
) -> bool | ||
``` | ||
|
||
Called when processing an invitation using a third-party identifier (also called a 3PID, | ||
e.g. an email address or a phone number). The module must return a `bool` indicating | ||
whether the inviter can invite the invitee to the given room. | ||
|
||
The inviter is represented by their Matrix user ID (e.g. `@alice:example.com`), and the | ||
invitee is represented by its medium (e.g. "email") and its address | ||
(e.g. `alice@example.com`). See [the Matrix specification](https://matrix.org/docs/spec/appendices#pid-types) | ||
for more information regarding third-party identifiers. | ||
|
||
For example, a call to this callback to send an invitation to the email address | ||
`alice@example.com` would look like this: | ||
|
||
```python | ||
await user_may_send_3pid_invite( | ||
"@bob:example.com", # The inviter's user ID | ||
"email", # The medium of the 3PID to invite | ||
"alice@example.com", # The address of the 3PID to invite | ||
"!some_room:example.com", # The ID of the room to send the invite into | ||
) | ||
``` | ||
|
||
**Note**: If the third-party identifier is already associated with a matrix user ID, | ||
[`user_may_invite`](#user_may_invite) will be used instead. | ||
|
||
### `user_may_create_room` | ||
|
||
```python | ||
async def user_may_create_room(user: str) -> bool | ||
``` | ||
|
||
Called when processing a room creation request. The module must return a `bool` indicating | ||
whether the given user (represented by their Matrix user ID) is allowed to create a room. | ||
|
||
### `user_may_create_room_with_invites` | ||
|
||
```python | ||
async def user_may_create_room_with_invites( | ||
user: str, | ||
invites: List[str], | ||
threepid_invites: List[Dict[str, str]], | ||
) -> bool | ||
``` | ||
|
||
Called when processing a room creation request (right after `user_may_create_room`). | ||
The module is given the Matrix user ID of the user trying to create a room, as well as a | ||
list of Matrix users to invite and a list of third-party identifiers (3PID, e.g. email | ||
addresses) to invite. | ||
|
||
An invited Matrix user to invite is represented by their Matrix user IDs, and an invited | ||
3PIDs is represented by a dict that includes the 3PID medium (e.g. "email") through its | ||
`medium` key and its address (e.g. "alice@example.com") through its `address` key. | ||
|
||
See [the Matrix specification](https://matrix.org/docs/spec/appendices#pid-types) for more | ||
information regarding third-party identifiers. | ||
|
||
If no invite and/or 3PID invite were specified in the room creation request, the | ||
corresponding list(s) will be empty. | ||
|
||
**Note**: This callback is not called when a room is cloned (e.g. during a room upgrade) | ||
since no invites are sent when cloning a room. To cover this case, modules also need to | ||
implement `user_may_create_room`. | ||
|
||
### `user_may_create_room_alias` | ||
|
||
```python | ||
async def user_may_create_room_alias(user: str, room_alias: "synapse.types.RoomAlias") -> bool | ||
``` | ||
|
||
Called when trying to associate an alias with an existing room. The module must return a | ||
`bool` indicating whether the given user (represented by their Matrix user ID) is allowed | ||
to set the given alias. | ||
|
||
### `user_may_publish_room` | ||
|
||
```python | ||
async def user_may_publish_room(user: str, room_id: str) -> bool | ||
``` | ||
|
||
Called when trying to publish a room to the homeserver's public rooms directory. The | ||
module must return a `bool` indicating whether the given user (represented by their | ||
Matrix user ID) is allowed to publish the given room. | ||
|
||
### `check_username_for_spam` | ||
|
||
```python | ||
async def check_username_for_spam(user_profile: Dict[str, str]) -> bool | ||
``` | ||
|
||
Called when computing search results in the user directory. The module must return a | ||
`bool` indicating whether the given user profile can appear in search results. The profile | ||
is represented as a dictionary with the following keys: | ||
|
||
* `user_id`: The Matrix ID for this user. | ||
* `display_name`: The user's display name. | ||
* `avatar_url`: The `mxc://` URL to the user's avatar. | ||
|
||
The module is given a copy of the original dictionary, so modifying it from within the | ||
module cannot modify a user's profile when included in user directory search results. | ||
|
||
### `check_registration_for_spam` | ||
|
||
```python | ||
async def check_registration_for_spam( | ||
email_threepid: Optional[dict], | ||
username: Optional[str], | ||
request_info: Collection[Tuple[str, str]], | ||
auth_provider_id: Optional[str] = None, | ||
) -> "synapse.spam_checker_api.RegistrationBehaviour" | ||
``` | ||
|
||
Called when registering a new user. The module must return a `RegistrationBehaviour` | ||
indicating whether the registration can go through or must be denied, or whether the user | ||
may be allowed to register but will be shadow banned. | ||
|
||
The arguments passed to this callback are: | ||
|
||
* `email_threepid`: The email address used for registering, if any. | ||
* `username`: The username the user would like to register. Can be `None`, meaning that | ||
Synapse will generate one later. | ||
* `request_info`: A collection of tuples, which first item is a user agent, and which | ||
second item is an IP address. These user agents and IP addresses are the ones that were | ||
used during the registration process. | ||
* `auth_provider_id`: The identifier of the SSO authentication provider, if any. | ||
|
||
### `check_media_file_for_spam` | ||
|
||
```python | ||
async def check_media_file_for_spam( | ||
file_wrapper: "synapse.rest.media.v1.media_storage.ReadableFileWrapper", | ||
file_info: "synapse.rest.media.v1._base.FileInfo", | ||
) -> bool | ||
``` | ||
|
||
Called when storing a local or remote file. The module must return a boolean indicating | ||
whether the given file can be stored in the homeserver's media store. | ||
|
||
## Example | ||
|
||
The example below is a module that implements the spam checker callback | ||
`check_event_for_spam` to deny any message sent by users whose Matrix user IDs are | ||
mentioned in a configured list, and registers a web resource to the path | ||
`/_synapse/client/list_spam_checker/is_evil` that returns a JSON object indicating | ||
whether the provided user appears in that list. | ||
|
||
```python | ||
import json | ||
from typing import Union | ||
|
||
from twisted.web.resource import Resource | ||
from twisted.web.server import Request | ||
|
||
from synapse.module_api import ModuleApi | ||
|
||
|
||
class IsUserEvilResource(Resource): | ||
def __init__(self, config): | ||
super(IsUserEvilResource, self).__init__() | ||
self.evil_users = config.get("evil_users") or [] | ||
|
||
def render_GET(self, request: Request): | ||
user = request.args.get(b"user")[0].decode() | ||
request.setHeader(b"Content-Type", b"application/json") | ||
return json.dumps({"evil": user in self.evil_users}).encode() | ||
|
||
|
||
class ListSpamChecker: | ||
def __init__(self, config: dict, api: ModuleApi): | ||
self.api = api | ||
self.evil_users = config.get("evil_users") or [] | ||
|
||
self.api.register_spam_checker_callbacks( | ||
check_event_for_spam=self.check_event_for_spam, | ||
) | ||
|
||
self.api.register_web_resource( | ||
path="/_synapse/client/list_spam_checker/is_evil", | ||
resource=IsUserEvilResource(config), | ||
) | ||
|
||
async def check_event_for_spam(self, event: "synapse.events.EventBase") -> Union[bool, str]: | ||
return event.sender not in self.evil_users | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to keep the changelogs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we care, we don't generate changelogs with towncrier for this repo.