2017-09-19 07:20:11 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2017-09-19 07:20:11 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2019-10-31 11:16:14 -04:00
|
|
|
import inspect
|
2021-03-16 08:41:41 -04:00
|
|
|
import logging
|
2021-06-18 07:15:52 -04:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
Any,
|
|
|
|
Awaitable,
|
|
|
|
Callable,
|
|
|
|
Collection,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Tuple,
|
|
|
|
Union,
|
|
|
|
)
|
2019-10-31 11:16:14 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
# `Literal` appears with Python 3.8.
|
|
|
|
from typing_extensions import Literal
|
|
|
|
|
|
|
|
import synapse
|
2022-07-11 12:52:10 -04:00
|
|
|
from synapse.api.errors import Codes
|
2022-08-23 22:53:37 -04:00
|
|
|
from synapse.logging.opentracing import trace
|
2023-02-27 08:26:05 -05:00
|
|
|
from synapse.media._base import FileInfo
|
|
|
|
from synapse.media.media_storage import ReadableFileWrapper
|
2022-05-31 06:04:53 -04:00
|
|
|
from synapse.spam_checker_api import RegistrationBehaviour
|
2022-07-11 12:52:10 -04:00
|
|
|
from synapse.types import JsonDict, RoomAlias, UserProfile
|
2022-05-09 07:31:14 -04:00
|
|
|
from synapse.util.async_helpers import delay_cancellation, maybe_awaitable
|
2022-05-13 07:17:38 -04:00
|
|
|
from synapse.util.metrics import Measure
|
2019-10-31 11:16:14 -04:00
|
|
|
|
2020-11-17 09:09:40 -05:00
|
|
|
if TYPE_CHECKING:
|
2020-10-07 07:03:26 -04:00
|
|
|
import synapse.events
|
2020-02-14 12:49:40 -05:00
|
|
|
import synapse.server
|
|
|
|
|
2021-03-16 08:41:41 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
CHECK_EVENT_FOR_SPAM_CALLBACK = Callable[
|
|
|
|
["synapse.events.EventBase"],
|
2022-05-23 13:27:39 -04:00
|
|
|
Awaitable[
|
|
|
|
Union[
|
2022-05-31 06:04:53 -04:00
|
|
|
str,
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
2022-05-30 12:24:56 -04:00
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
2022-07-11 12:52:10 -04:00
|
|
|
Tuple[Codes, JsonDict],
|
2022-05-23 13:27:39 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
2021-06-18 07:15:52 -04:00
|
|
|
]
|
2022-05-23 12:36:21 -04:00
|
|
|
SHOULD_DROP_FEDERATED_EVENT_CALLBACK = Callable[
|
|
|
|
["synapse.events.EventBase"],
|
|
|
|
Awaitable[Union[bool, str]],
|
|
|
|
]
|
2022-06-13 14:16:16 -04:00
|
|
|
USER_MAY_JOIN_ROOM_CALLBACK = Callable[
|
|
|
|
[str, str, bool],
|
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
2022-06-13 14:16:16 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
USER_MAY_INVITE_CALLBACK = Callable[
|
|
|
|
[str, str, str],
|
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
2022-06-13 14:16:16 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
USER_MAY_SEND_3PID_INVITE_CALLBACK = Callable[
|
|
|
|
[str, str, str, str],
|
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
2022-06-13 14:16:16 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
USER_MAY_CREATE_ROOM_CALLBACK = Callable[
|
|
|
|
[str],
|
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
2022-06-13 14:16:16 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK = Callable[
|
|
|
|
[str, RoomAlias],
|
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
2022-06-13 14:16:16 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
USER_MAY_PUBLISH_ROOM_CALLBACK = Callable[
|
|
|
|
[str, str],
|
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
2022-06-13 14:16:16 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
2022-03-18 09:51:41 -04:00
|
|
|
CHECK_USERNAME_FOR_SPAM_CALLBACK = Callable[[UserProfile], Awaitable[bool]]
|
2021-06-18 07:15:52 -04:00
|
|
|
LEGACY_CHECK_REGISTRATION_FOR_SPAM_CALLBACK = Callable[
|
|
|
|
[
|
|
|
|
Optional[dict],
|
|
|
|
Optional[str],
|
|
|
|
Collection[Tuple[str, str]],
|
|
|
|
],
|
|
|
|
Awaitable[RegistrationBehaviour],
|
|
|
|
]
|
|
|
|
CHECK_REGISTRATION_FOR_SPAM_CALLBACK = Callable[
|
|
|
|
[
|
|
|
|
Optional[dict],
|
|
|
|
Optional[str],
|
|
|
|
Collection[Tuple[str, str]],
|
|
|
|
Optional[str],
|
|
|
|
],
|
|
|
|
Awaitable[RegistrationBehaviour],
|
|
|
|
]
|
|
|
|
CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK = Callable[
|
|
|
|
[ReadableFileWrapper, FileInfo],
|
2022-06-13 14:16:16 -04:00
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
2022-07-11 12:52:10 -04:00
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
2022-06-13 14:16:16 -04:00
|
|
|
# Deprecated
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
],
|
2021-06-18 07:15:52 -04:00
|
|
|
]
|
2023-06-26 10:12:20 -04:00
|
|
|
CHECK_LOGIN_FOR_SPAM_CALLBACK = Callable[
|
|
|
|
[
|
|
|
|
str,
|
|
|
|
Optional[str],
|
|
|
|
Optional[str],
|
|
|
|
Collection[Tuple[Optional[str], str]],
|
|
|
|
Optional[str],
|
|
|
|
],
|
|
|
|
Awaitable[
|
|
|
|
Union[
|
|
|
|
Literal["NOT_SPAM"],
|
|
|
|
Codes,
|
|
|
|
# Highly experimental, not officially part of the spamchecker API, may
|
|
|
|
# disappear without warning depending on the results of ongoing
|
|
|
|
# experiments.
|
|
|
|
# Use this to return additional information as part of an error.
|
|
|
|
Tuple[Codes, JsonDict],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
2021-06-18 07:15:52 -04:00
|
|
|
|
|
|
|
|
2021-10-13 07:24:07 -04:00
|
|
|
def load_legacy_spam_checkers(hs: "synapse.server.HomeServer") -> None:
|
2021-06-18 07:15:52 -04:00
|
|
|
"""Wrapper that loads spam checkers configured using the old configuration, and
|
|
|
|
registers the spam checker hooks they implement.
|
|
|
|
"""
|
2021-07-15 06:02:43 -04:00
|
|
|
spam_checkers: List[Any] = []
|
2021-06-18 07:15:52 -04:00
|
|
|
api = hs.get_module_api()
|
2021-09-24 07:25:21 -04:00
|
|
|
for module, config in hs.config.spamchecker.spam_checkers:
|
2021-06-18 07:15:52 -04:00
|
|
|
# Older spam checkers don't accept the `api` argument, so we
|
|
|
|
# try and detect support.
|
|
|
|
spam_args = inspect.getfullargspec(module)
|
|
|
|
if "api" in spam_args.args:
|
|
|
|
spam_checkers.append(module(config=config, api=api))
|
|
|
|
else:
|
|
|
|
spam_checkers.append(module(config=config))
|
|
|
|
|
|
|
|
# The known spam checker hooks. If a spam checker module implements a method
|
|
|
|
# which name appears in this set, we'll want to register it.
|
|
|
|
spam_checker_methods = {
|
|
|
|
"check_event_for_spam",
|
|
|
|
"user_may_invite",
|
|
|
|
"user_may_create_room",
|
|
|
|
"user_may_create_room_alias",
|
|
|
|
"user_may_publish_room",
|
|
|
|
"check_username_for_spam",
|
|
|
|
"check_registration_for_spam",
|
|
|
|
"check_media_file_for_spam",
|
|
|
|
}
|
|
|
|
|
|
|
|
for spam_checker in spam_checkers:
|
|
|
|
# Methods on legacy spam checkers might not be async, so we wrap them around a
|
|
|
|
# wrapper that will call maybe_awaitable on the result.
|
|
|
|
def async_wrapper(f: Optional[Callable]) -> Optional[Callable[..., Awaitable]]:
|
|
|
|
# f might be None if the callback isn't implemented by the module. In this
|
|
|
|
# case we don't want to register a callback at all so we return None.
|
|
|
|
if f is None:
|
|
|
|
return None
|
|
|
|
|
2021-06-23 11:22:08 -04:00
|
|
|
wrapped_func = f
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
if f.__name__ == "check_registration_for_spam":
|
|
|
|
checker_args = inspect.signature(f)
|
|
|
|
if len(checker_args.parameters) == 3:
|
|
|
|
# Backwards compatibility; some modules might implement a hook that
|
|
|
|
# doesn't expect a 4th argument. In this case, wrap it in a function
|
|
|
|
# that gives it only 3 arguments and drops the auth_provider_id on
|
|
|
|
# the floor.
|
|
|
|
def wrapper(
|
|
|
|
email_threepid: Optional[dict],
|
|
|
|
username: Optional[str],
|
|
|
|
request_info: Collection[Tuple[str, str]],
|
|
|
|
auth_provider_id: Optional[str],
|
|
|
|
) -> Union[Awaitable[RegistrationBehaviour], RegistrationBehaviour]:
|
2021-10-13 07:24:07 -04:00
|
|
|
# Assertion required because mypy can't prove we won't
|
|
|
|
# change `f` back to `None`. See
|
|
|
|
# https://mypy.readthedocs.io/en/latest/common_issues.html#narrowing-and-inner-functions
|
2021-06-18 07:15:52 -04:00
|
|
|
assert f is not None
|
|
|
|
|
|
|
|
return f(
|
|
|
|
email_threepid,
|
|
|
|
username,
|
|
|
|
request_info,
|
|
|
|
)
|
|
|
|
|
2021-06-23 11:22:08 -04:00
|
|
|
wrapped_func = wrapper
|
2021-06-18 07:15:52 -04:00
|
|
|
elif len(checker_args.parameters) != 4:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Bad signature for callback check_registration_for_spam",
|
|
|
|
)
|
|
|
|
|
2021-10-13 07:24:07 -04:00
|
|
|
def run(*args: Any, **kwargs: Any) -> Awaitable:
|
|
|
|
# Assertion required because mypy can't prove we won't change `f`
|
|
|
|
# back to `None`. See
|
|
|
|
# https://mypy.readthedocs.io/en/latest/common_issues.html#narrowing-and-inner-functions
|
2021-06-23 11:22:08 -04:00
|
|
|
assert wrapped_func is not None
|
2021-06-18 07:15:52 -04:00
|
|
|
|
2021-06-23 11:22:08 -04:00
|
|
|
return maybe_awaitable(wrapped_func(*args, **kwargs))
|
2021-06-18 07:15:52 -04:00
|
|
|
|
|
|
|
return run
|
|
|
|
|
|
|
|
# Register the hooks through the module API.
|
|
|
|
hooks = {
|
|
|
|
hook: async_wrapper(getattr(spam_checker, hook, None))
|
|
|
|
for hook in spam_checker_methods
|
|
|
|
}
|
|
|
|
|
|
|
|
api.register_spam_checker_callbacks(**hooks)
|
|
|
|
|
2017-09-27 05:26:13 -04:00
|
|
|
|
2023-04-17 20:57:40 -04:00
|
|
|
class SpamCheckerModuleApiCallbacks:
|
2022-06-13 14:16:16 -04:00
|
|
|
NOT_SPAM: Literal["NOT_SPAM"] = "NOT_SPAM"
|
2022-05-31 06:04:53 -04:00
|
|
|
|
2022-05-13 07:17:38 -04:00
|
|
|
def __init__(self, hs: "synapse.server.HomeServer") -> None:
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
self._check_event_for_spam_callbacks: List[CHECK_EVENT_FOR_SPAM_CALLBACK] = []
|
2022-05-23 12:36:21 -04:00
|
|
|
self._should_drop_federated_event_callbacks: List[
|
|
|
|
SHOULD_DROP_FEDERATED_EVENT_CALLBACK
|
|
|
|
] = []
|
2021-10-06 10:32:16 -04:00
|
|
|
self._user_may_join_room_callbacks: List[USER_MAY_JOIN_ROOM_CALLBACK] = []
|
2021-06-18 07:15:52 -04:00
|
|
|
self._user_may_invite_callbacks: List[USER_MAY_INVITE_CALLBACK] = []
|
2021-10-06 11:18:13 -04:00
|
|
|
self._user_may_send_3pid_invite_callbacks: List[
|
|
|
|
USER_MAY_SEND_3PID_INVITE_CALLBACK
|
|
|
|
] = []
|
2021-06-18 07:15:52 -04:00
|
|
|
self._user_may_create_room_callbacks: List[USER_MAY_CREATE_ROOM_CALLBACK] = []
|
|
|
|
self._user_may_create_room_alias_callbacks: List[
|
|
|
|
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK
|
|
|
|
] = []
|
|
|
|
self._user_may_publish_room_callbacks: List[USER_MAY_PUBLISH_ROOM_CALLBACK] = []
|
|
|
|
self._check_username_for_spam_callbacks: List[
|
|
|
|
CHECK_USERNAME_FOR_SPAM_CALLBACK
|
|
|
|
] = []
|
|
|
|
self._check_registration_for_spam_callbacks: List[
|
|
|
|
CHECK_REGISTRATION_FOR_SPAM_CALLBACK
|
|
|
|
] = []
|
|
|
|
self._check_media_file_for_spam_callbacks: List[
|
|
|
|
CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK
|
|
|
|
] = []
|
2023-06-26 10:12:20 -04:00
|
|
|
self._check_login_for_spam_callbacks: List[CHECK_LOGIN_FOR_SPAM_CALLBACK] = []
|
2021-06-18 07:15:52 -04:00
|
|
|
|
|
|
|
def register_callbacks(
|
|
|
|
self,
|
|
|
|
check_event_for_spam: Optional[CHECK_EVENT_FOR_SPAM_CALLBACK] = None,
|
2022-05-23 12:36:21 -04:00
|
|
|
should_drop_federated_event: Optional[
|
|
|
|
SHOULD_DROP_FEDERATED_EVENT_CALLBACK
|
|
|
|
] = None,
|
2021-10-06 10:32:16 -04:00
|
|
|
user_may_join_room: Optional[USER_MAY_JOIN_ROOM_CALLBACK] = None,
|
2021-06-18 07:15:52 -04:00
|
|
|
user_may_invite: Optional[USER_MAY_INVITE_CALLBACK] = None,
|
2021-10-06 11:18:13 -04:00
|
|
|
user_may_send_3pid_invite: Optional[USER_MAY_SEND_3PID_INVITE_CALLBACK] = None,
|
2021-06-18 07:15:52 -04:00
|
|
|
user_may_create_room: Optional[USER_MAY_CREATE_ROOM_CALLBACK] = None,
|
|
|
|
user_may_create_room_alias: Optional[
|
|
|
|
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK
|
|
|
|
] = None,
|
|
|
|
user_may_publish_room: Optional[USER_MAY_PUBLISH_ROOM_CALLBACK] = None,
|
|
|
|
check_username_for_spam: Optional[CHECK_USERNAME_FOR_SPAM_CALLBACK] = None,
|
|
|
|
check_registration_for_spam: Optional[
|
|
|
|
CHECK_REGISTRATION_FOR_SPAM_CALLBACK
|
|
|
|
] = None,
|
|
|
|
check_media_file_for_spam: Optional[CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK] = None,
|
2023-06-26 10:12:20 -04:00
|
|
|
check_login_for_spam: Optional[CHECK_LOGIN_FOR_SPAM_CALLBACK] = None,
|
2021-10-13 07:24:07 -04:00
|
|
|
) -> None:
|
2021-06-18 07:15:52 -04:00
|
|
|
"""Register callbacks from module for each hook."""
|
|
|
|
if check_event_for_spam is not None:
|
|
|
|
self._check_event_for_spam_callbacks.append(check_event_for_spam)
|
|
|
|
|
2022-05-23 12:36:21 -04:00
|
|
|
if should_drop_federated_event is not None:
|
|
|
|
self._should_drop_federated_event_callbacks.append(
|
|
|
|
should_drop_federated_event
|
|
|
|
)
|
|
|
|
|
2021-10-06 10:32:16 -04:00
|
|
|
if user_may_join_room is not None:
|
|
|
|
self._user_may_join_room_callbacks.append(user_may_join_room)
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
if user_may_invite is not None:
|
|
|
|
self._user_may_invite_callbacks.append(user_may_invite)
|
|
|
|
|
2021-10-06 11:18:13 -04:00
|
|
|
if user_may_send_3pid_invite is not None:
|
|
|
|
self._user_may_send_3pid_invite_callbacks.append(
|
|
|
|
user_may_send_3pid_invite,
|
|
|
|
)
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
if user_may_create_room is not None:
|
|
|
|
self._user_may_create_room_callbacks.append(user_may_create_room)
|
|
|
|
|
|
|
|
if user_may_create_room_alias is not None:
|
|
|
|
self._user_may_create_room_alias_callbacks.append(
|
|
|
|
user_may_create_room_alias,
|
|
|
|
)
|
|
|
|
|
|
|
|
if user_may_publish_room is not None:
|
|
|
|
self._user_may_publish_room_callbacks.append(user_may_publish_room)
|
|
|
|
|
|
|
|
if check_username_for_spam is not None:
|
|
|
|
self._check_username_for_spam_callbacks.append(check_username_for_spam)
|
|
|
|
|
|
|
|
if check_registration_for_spam is not None:
|
|
|
|
self._check_registration_for_spam_callbacks.append(
|
|
|
|
check_registration_for_spam,
|
|
|
|
)
|
|
|
|
|
|
|
|
if check_media_file_for_spam is not None:
|
|
|
|
self._check_media_file_for_spam_callbacks.append(check_media_file_for_spam)
|
2017-09-19 07:20:11 -04:00
|
|
|
|
2023-06-26 10:12:20 -04:00
|
|
|
if check_login_for_spam is not None:
|
|
|
|
self._check_login_for_spam_callbacks.append(check_login_for_spam)
|
|
|
|
|
2022-08-23 22:53:37 -04:00
|
|
|
@trace
|
2020-12-11 14:05:15 -05:00
|
|
|
async def check_event_for_spam(
|
|
|
|
self, event: "synapse.events.EventBase"
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, JsonDict], str]:
|
2017-09-26 14:20:23 -04:00
|
|
|
"""Checks if a given event is considered "spammy" by this server.
|
2017-09-19 07:20:11 -04:00
|
|
|
|
2017-09-26 14:20:23 -04:00
|
|
|
If the server considers an event spammy, then it will be rejected if
|
2022-03-10 12:40:07 -05:00
|
|
|
sent by a local user. If it is sent by a user on another server, the
|
|
|
|
event is soft-failed.
|
2017-09-19 07:20:11 -04:00
|
|
|
|
2017-09-26 14:20:23 -04:00
|
|
|
Args:
|
2020-02-14 12:49:40 -05:00
|
|
|
event: the event to be checked
|
2017-09-19 07:20:11 -04:00
|
|
|
|
2017-09-26 14:20:23 -04:00
|
|
|
Returns:
|
2022-05-31 06:04:53 -04:00
|
|
|
- `NOT_SPAM` if the event is considered good (non-spammy) and should be let
|
|
|
|
through. Other spamcheck filters may still reject it.
|
|
|
|
- A `Code` if the event is considered spammy and is rejected with a specific
|
2022-05-23 13:27:39 -04:00
|
|
|
error message/code.
|
2022-05-31 06:04:53 -04:00
|
|
|
- A string that isn't `NOT_SPAM` if the event is considered spammy and the
|
|
|
|
string should be used as the client-facing error message. This usage is
|
|
|
|
generally discouraged as it doesn't support internationalization.
|
2017-09-26 14:20:23 -04:00
|
|
|
"""
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._check_event_for_spam_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-05-31 06:04:53 -04:00
|
|
|
res = await delay_cancellation(callback(event))
|
|
|
|
if res is False or res == self.NOT_SPAM:
|
2022-05-23 13:27:39 -04:00
|
|
|
# This spam-checker accepts the event.
|
|
|
|
# Other spam-checkers may reject it, though.
|
|
|
|
continue
|
|
|
|
elif res is True:
|
|
|
|
# This spam-checker rejects the event with deprecated
|
|
|
|
# return value `True`
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
|
|
|
return res
|
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
|
|
|
return res, {}
|
2022-05-31 06:04:53 -04:00
|
|
|
elif not isinstance(res, str):
|
|
|
|
# mypy complains that we can't reach this code because of the
|
|
|
|
# return type in CHECK_EVENT_FOR_SPAM_CALLBACK, but we don't know
|
|
|
|
# for sure that the module actually returns it.
|
2024-03-13 13:05:57 -04:00
|
|
|
logger.warning( # type: ignore[unreachable]
|
2022-05-31 06:04:53 -04:00
|
|
|
"Module returned invalid value, rejecting message as spam"
|
|
|
|
)
|
|
|
|
res = "This message has been rejected as probable spam"
|
2022-05-23 13:27:39 -04:00
|
|
|
else:
|
2022-05-31 06:04:53 -04:00
|
|
|
# The module rejected the event either with a `Codes`
|
|
|
|
# or some other `str`. In either case, we stop here.
|
|
|
|
pass
|
|
|
|
|
|
|
|
return res
|
2022-05-23 13:27:39 -04:00
|
|
|
|
|
|
|
# No spam-checker has rejected the event, let it pass.
|
2022-05-31 06:04:53 -04:00
|
|
|
return self.NOT_SPAM
|
2017-10-03 09:28:12 -04:00
|
|
|
|
2022-05-23 12:36:21 -04:00
|
|
|
async def should_drop_federated_event(
|
|
|
|
self, event: "synapse.events.EventBase"
|
|
|
|
) -> Union[bool, str]:
|
|
|
|
"""Checks if a given federated event is considered "spammy" by this
|
|
|
|
server.
|
|
|
|
|
|
|
|
If the server considers an event spammy, it will be silently dropped,
|
|
|
|
and in doing so will split-brain our view of the room's DAG.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
event: the event to be checked
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the event should be silently dropped
|
|
|
|
"""
|
|
|
|
for callback in self._should_drop_federated_event_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-05-23 12:36:21 -04:00
|
|
|
res: Union[bool, str] = await delay_cancellation(callback(event))
|
|
|
|
if res:
|
|
|
|
return res
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2021-10-13 07:24:07 -04:00
|
|
|
async def user_may_join_room(
|
|
|
|
self, user_id: str, room_id: str, is_invited: bool
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, JsonDict], Literal["NOT_SPAM"]]:
|
2021-10-06 10:32:16 -04:00
|
|
|
"""Checks if a given users is allowed to join a room.
|
|
|
|
Not called when a user creates a room.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
userid: The ID of the user wanting to join the room
|
|
|
|
room_id: The ID of the room the user wants to join
|
|
|
|
is_invited: Whether the user is invited into the room
|
|
|
|
|
|
|
|
Returns:
|
2022-07-11 12:52:10 -04:00
|
|
|
NOT_SPAM if the operation is permitted, [Codes, Dict] otherwise.
|
2021-10-06 10:32:16 -04:00
|
|
|
"""
|
|
|
|
for callback in self._user_may_join_room_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-06-13 14:16:16 -04:00
|
|
|
res = await delay_cancellation(callback(user_id, room_id, is_invited))
|
|
|
|
# Normalize return values to `Codes` or `"NOT_SPAM"`.
|
|
|
|
if res is True or res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif res is False:
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2022-06-13 14:16:16 -04:00
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
2022-07-11 12:52:10 -04:00
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
2022-06-13 14:16:16 -04:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting join as spam"
|
|
|
|
)
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2021-10-06 10:32:16 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
# No spam-checker has rejected the request, let it pass.
|
|
|
|
return self.NOT_SPAM
|
2021-10-06 10:32:16 -04:00
|
|
|
|
2020-12-11 14:05:15 -05:00
|
|
|
async def user_may_invite(
|
2020-02-14 12:49:40 -05:00
|
|
|
self, inviter_userid: str, invitee_userid: str, room_id: str
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
2017-10-03 09:28:12 -04:00
|
|
|
"""Checks if a given user may send an invite
|
|
|
|
|
|
|
|
Args:
|
2020-02-14 12:49:40 -05:00
|
|
|
inviter_userid: The user ID of the sender of the invitation
|
|
|
|
invitee_userid: The user ID targeted in the invitation
|
|
|
|
room_id: The room ID
|
2017-10-03 09:28:12 -04:00
|
|
|
|
|
|
|
Returns:
|
2022-06-13 14:16:16 -04:00
|
|
|
NOT_SPAM if the operation is permitted, Codes otherwise.
|
2017-10-03 09:28:12 -04:00
|
|
|
"""
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._user_may_invite_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-06-13 14:16:16 -04:00
|
|
|
res = await delay_cancellation(
|
2022-05-13 07:17:38 -04:00
|
|
|
callback(inviter_userid, invitee_userid, room_id)
|
|
|
|
)
|
2022-06-13 14:16:16 -04:00
|
|
|
# Normalize return values to `Codes` or `"NOT_SPAM"`.
|
|
|
|
if res is True or res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif res is False:
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2022-06-13 14:16:16 -04:00
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
2022-07-11 12:52:10 -04:00
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
2022-06-13 14:16:16 -04:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting invite as spam"
|
|
|
|
)
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2017-10-03 09:28:12 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
# No spam-checker has rejected the request, let it pass.
|
|
|
|
return self.NOT_SPAM
|
2017-10-04 05:47:54 -04:00
|
|
|
|
2021-10-06 11:18:13 -04:00
|
|
|
async def user_may_send_3pid_invite(
|
|
|
|
self, inviter_userid: str, medium: str, address: str, room_id: str
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
2021-10-06 11:18:13 -04:00
|
|
|
"""Checks if a given user may invite a given threepid into the room
|
|
|
|
|
|
|
|
Note that if the threepid is already associated with a Matrix user ID, Synapse
|
|
|
|
will call user_may_invite with said user ID instead.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
inviter_userid: The user ID of the sender of the invitation
|
|
|
|
medium: The 3PID's medium (e.g. "email")
|
|
|
|
address: The 3PID's address (e.g. "alice@example.com")
|
|
|
|
room_id: The room ID
|
|
|
|
|
|
|
|
Returns:
|
2022-06-13 14:16:16 -04:00
|
|
|
NOT_SPAM if the operation is permitted, Codes otherwise.
|
2021-10-06 11:18:13 -04:00
|
|
|
"""
|
|
|
|
for callback in self._user_may_send_3pid_invite_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-06-13 14:16:16 -04:00
|
|
|
res = await delay_cancellation(
|
2022-05-13 07:17:38 -04:00
|
|
|
callback(inviter_userid, medium, address, room_id)
|
|
|
|
)
|
2022-06-13 14:16:16 -04:00
|
|
|
# Normalize return values to `Codes` or `"NOT_SPAM"`.
|
|
|
|
if res is True or res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif res is False:
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2022-06-13 14:16:16 -04:00
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
2022-07-11 12:52:10 -04:00
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
2022-06-13 14:16:16 -04:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting 3pid invite as spam"
|
|
|
|
)
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2021-10-06 11:18:13 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
return self.NOT_SPAM
|
2021-10-06 11:18:13 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
async def user_may_create_room(
|
|
|
|
self, userid: str
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
2017-10-04 05:47:54 -04:00
|
|
|
"""Checks if a given user may create a room
|
|
|
|
|
|
|
|
Args:
|
2020-02-14 12:49:40 -05:00
|
|
|
userid: The ID of the user attempting to create a room
|
2017-10-04 05:47:54 -04:00
|
|
|
"""
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._user_may_create_room_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-06-13 14:16:16 -04:00
|
|
|
res = await delay_cancellation(callback(userid))
|
|
|
|
if res is True or res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif res is False:
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2022-06-13 14:16:16 -04:00
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
2022-07-11 12:52:10 -04:00
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
2022-06-13 14:16:16 -04:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting room creation as spam"
|
|
|
|
)
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2017-10-04 05:47:54 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
return self.NOT_SPAM
|
2017-10-04 05:47:54 -04:00
|
|
|
|
2021-04-29 07:17:28 -04:00
|
|
|
async def user_may_create_room_alias(
|
|
|
|
self, userid: str, room_alias: RoomAlias
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
2017-10-04 05:47:54 -04:00
|
|
|
"""Checks if a given user may create a room alias
|
|
|
|
|
|
|
|
Args:
|
2020-02-14 12:49:40 -05:00
|
|
|
userid: The ID of the user attempting to create a room alias
|
|
|
|
room_alias: The alias to be created
|
2017-10-04 05:47:54 -04:00
|
|
|
|
|
|
|
"""
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._user_may_create_room_alias_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-06-13 14:16:16 -04:00
|
|
|
res = await delay_cancellation(callback(userid, room_alias))
|
|
|
|
if res is True or res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif res is False:
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2022-06-13 14:16:16 -04:00
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
2022-07-11 12:52:10 -04:00
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
2022-06-13 14:16:16 -04:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting room create as spam"
|
|
|
|
)
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2017-10-04 05:47:54 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
return self.NOT_SPAM
|
2017-10-04 09:29:33 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
async def user_may_publish_room(
|
|
|
|
self, userid: str, room_id: str
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
2017-10-04 09:29:33 -04:00
|
|
|
"""Checks if a given user may publish a room to the directory
|
|
|
|
|
|
|
|
Args:
|
2020-02-14 12:49:40 -05:00
|
|
|
userid: The user ID attempting to publish the room
|
|
|
|
room_id: The ID of the room that would be published
|
2017-10-04 09:29:33 -04:00
|
|
|
"""
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._user_may_publish_room_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-06-13 14:16:16 -04:00
|
|
|
res = await delay_cancellation(callback(userid, room_id))
|
|
|
|
if res is True or res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif res is False:
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2022-06-13 14:16:16 -04:00
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
2022-07-11 12:52:10 -04:00
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
2022-06-13 14:16:16 -04:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting room publication as spam"
|
|
|
|
)
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2017-10-04 09:29:33 -04:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
return self.NOT_SPAM
|
2020-02-14 07:17:54 -05:00
|
|
|
|
2022-03-18 09:51:41 -04:00
|
|
|
async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
|
2020-02-14 07:17:54 -05:00
|
|
|
"""Checks if a user ID or display name are considered "spammy" by this server.
|
|
|
|
|
|
|
|
If the server considers a username spammy, then it will not be included in
|
|
|
|
user directory results.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_profile: The user information to check, it contains the keys:
|
|
|
|
* user_id
|
|
|
|
* display_name
|
|
|
|
* avatar_url
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the user is spammy.
|
|
|
|
"""
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._check_username_for_spam_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-05-13 07:17:38 -04:00
|
|
|
# Make a copy of the user profile object to ensure the spam checker cannot
|
|
|
|
# modify it.
|
|
|
|
res = await delay_cancellation(callback(user_profile.copy()))
|
|
|
|
if res:
|
2021-06-18 07:15:52 -04:00
|
|
|
return True
|
2020-05-08 14:25:48 -04:00
|
|
|
|
|
|
|
return False
|
2020-08-20 15:42:58 -04:00
|
|
|
|
2020-12-11 14:05:15 -05:00
|
|
|
async def check_registration_for_spam(
|
2020-08-20 15:42:58 -04:00
|
|
|
self,
|
|
|
|
email_threepid: Optional[dict],
|
|
|
|
username: Optional[str],
|
|
|
|
request_info: Collection[Tuple[str, str]],
|
2021-03-16 08:41:41 -04:00
|
|
|
auth_provider_id: Optional[str] = None,
|
2020-08-20 15:42:58 -04:00
|
|
|
) -> RegistrationBehaviour:
|
|
|
|
"""Checks if we should allow the given registration request.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
email_threepid: The email threepid used for registering, if any
|
|
|
|
username: The request user name, if any
|
|
|
|
request_info: List of tuples of user agent and IP that
|
|
|
|
were used during the registration process.
|
2021-03-16 08:41:41 -04:00
|
|
|
auth_provider_id: The SSO IdP the user used, e.g "oidc", "saml",
|
|
|
|
"cas". If any. Note this does not include users registered
|
|
|
|
via a password provider.
|
2020-08-20 15:42:58 -04:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Enum for how the request should be handled
|
|
|
|
"""
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._check_registration_for_spam_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-05-13 07:17:38 -04:00
|
|
|
behaviour = await delay_cancellation(
|
|
|
|
callback(email_threepid, username, request_info, auth_provider_id)
|
|
|
|
)
|
2021-06-18 07:15:52 -04:00
|
|
|
assert isinstance(behaviour, RegistrationBehaviour)
|
|
|
|
if behaviour != RegistrationBehaviour.ALLOW:
|
|
|
|
return behaviour
|
2020-08-20 15:42:58 -04:00
|
|
|
|
|
|
|
return RegistrationBehaviour.ALLOW
|
2021-02-03 11:44:16 -05:00
|
|
|
|
2023-07-05 11:22:21 -04:00
|
|
|
@trace
|
2021-02-03 11:44:16 -05:00
|
|
|
async def check_media_file_for_spam(
|
|
|
|
self, file_wrapper: ReadableFileWrapper, file_info: FileInfo
|
2022-07-11 12:52:10 -04:00
|
|
|
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
2021-02-03 11:44:16 -05:00
|
|
|
"""Checks if a piece of newly uploaded media should be blocked.
|
|
|
|
|
|
|
|
This will be called for local uploads, downloads of remote media, each
|
|
|
|
thumbnail generated for those, and web pages/images used for URL
|
|
|
|
previews.
|
|
|
|
|
|
|
|
Note that care should be taken to not do blocking IO operations in the
|
|
|
|
main thread. For example, to get the contents of a file a module
|
|
|
|
should do::
|
|
|
|
|
|
|
|
async def check_media_file_for_spam(
|
|
|
|
self, file: ReadableFileWrapper, file_info: FileInfo
|
2022-06-13 14:16:16 -04:00
|
|
|
) -> Union[Codes, Literal["NOT_SPAM"]]:
|
2021-02-03 11:44:16 -05:00
|
|
|
buffer = BytesIO()
|
|
|
|
await file.write_chunks_to(buffer.write)
|
|
|
|
|
|
|
|
if buffer.getvalue() == b"Hello World":
|
2022-06-13 14:16:16 -04:00
|
|
|
return synapse.module_api.NOT_SPAM
|
2021-02-03 11:44:16 -05:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
return Codes.FORBIDDEN
|
2021-02-03 11:44:16 -05:00
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
file: An object that allows reading the contents of the media.
|
|
|
|
file_info: Metadata about the file.
|
|
|
|
"""
|
|
|
|
|
2021-06-18 07:15:52 -04:00
|
|
|
for callback in self._check_media_file_for_spam_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2022-06-13 14:16:16 -04:00
|
|
|
res = await delay_cancellation(callback(file_wrapper, file_info))
|
|
|
|
# Normalize return values to `Codes` or `"NOT_SPAM"`.
|
|
|
|
if res is False or res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif res is True:
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2022-06-13 14:16:16 -04:00
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
2022-07-11 12:52:10 -04:00
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
2022-06-13 14:16:16 -04:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting media file as spam"
|
|
|
|
)
|
2022-07-11 12:52:10 -04:00
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
2021-02-03 11:44:16 -05:00
|
|
|
|
2022-06-13 14:16:16 -04:00
|
|
|
return self.NOT_SPAM
|
2023-06-26 10:12:20 -04:00
|
|
|
|
|
|
|
async def check_login_for_spam(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
device_id: Optional[str],
|
|
|
|
initial_display_name: Optional[str],
|
|
|
|
request_info: Collection[Tuple[Optional[str], str]],
|
|
|
|
auth_provider_id: Optional[str] = None,
|
|
|
|
) -> Union[Tuple[Codes, dict], Literal["NOT_SPAM"]]:
|
|
|
|
"""Checks if we should allow the given registration request.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: The request user ID
|
|
|
|
request_info: List of tuples of user agent and IP that
|
|
|
|
were used during the registration process.
|
|
|
|
auth_provider_id: The SSO IdP the user used, e.g "oidc", "saml",
|
|
|
|
"cas". If any. Note this does not include users registered
|
|
|
|
via a password provider.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Enum for how the request should be handled
|
|
|
|
"""
|
|
|
|
|
|
|
|
for callback in self._check_login_for_spam_callbacks:
|
2023-08-15 08:11:20 -04:00
|
|
|
with Measure(self.clock, f"{callback.__module__}.{callback.__qualname__}"):
|
2023-06-26 10:12:20 -04:00
|
|
|
res = await delay_cancellation(
|
|
|
|
callback(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
initial_display_name,
|
|
|
|
request_info,
|
|
|
|
auth_provider_id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# Normalize return values to `Codes` or `"NOT_SPAM"`.
|
|
|
|
if res is self.NOT_SPAM:
|
|
|
|
continue
|
|
|
|
elif isinstance(res, synapse.api.errors.Codes):
|
|
|
|
return res, {}
|
|
|
|
elif (
|
|
|
|
isinstance(res, tuple)
|
|
|
|
and len(res) == 2
|
|
|
|
and isinstance(res[0], synapse.api.errors.Codes)
|
|
|
|
and isinstance(res[1], dict)
|
|
|
|
):
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
logger.warning(
|
|
|
|
"Module returned invalid value, rejecting login as spam"
|
|
|
|
)
|
|
|
|
return synapse.api.errors.Codes.FORBIDDEN, {}
|
|
|
|
|
|
|
|
return self.NOT_SPAM
|