Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
add Measure blocks all over SpamChecker
Browse files Browse the repository at this point in the history
Signed-off-by: jesopo <github@lolnerd.net>
  • Loading branch information
jesopo committed May 10, 2022
1 parent ecef741 commit 75f446d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog.d/12513.feature
@@ -0,0 +1 @@
Insert Measure blocks throughout SpamChecker to collect metrics.
71 changes: 56 additions & 15 deletions synapse/events/spamcheck.py
Expand Up @@ -32,6 +32,7 @@
from synapse.spam_checker_api import RegistrationBehaviour
from synapse.types import RoomAlias, UserProfile
from synapse.util.async_helpers import maybe_awaitable
from synapse.util.metrics import Measure

if TYPE_CHECKING:
import synapse.events
Expand Down Expand Up @@ -162,7 +163,10 @@ def run(*args: Any, **kwargs: Any) -> Awaitable:


class SpamChecker:
def __init__(self) -> None:
def __init__(self, hs: "synapse.server.HomeServer") -> None:
self.hs = hs
self.clock = hs.get_clock()

self._check_event_for_spam_callbacks: List[CHECK_EVENT_FOR_SPAM_CALLBACK] = []
self._user_may_join_room_callbacks: List[USER_MAY_JOIN_ROOM_CALLBACK] = []
self._user_may_invite_callbacks: List[USER_MAY_INVITE_CALLBACK] = []
Expand Down Expand Up @@ -255,7 +259,10 @@ async def check_event_for_spam(
will be used as the error message returned to the user.
"""
for callback in self._check_event_for_spam_callbacks:
res: Union[bool, str] = await callback(event)
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res: Union[bool, str] = await callback(event)
if res:
return res

Expand All @@ -276,7 +283,11 @@ async def user_may_join_room(
Whether the user may join the room
"""
for callback in self._user_may_join_room_callbacks:
if await callback(user_id, room_id, is_invited) is False:
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res = await callback(user_id, room_id, is_invited)
if res is False:
return False

return True
Expand All @@ -297,7 +308,11 @@ async def user_may_invite(
True if the user may send an invite, otherwise False
"""
for callback in self._user_may_invite_callbacks:
if await callback(inviter_userid, invitee_userid, room_id) is False:
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res = await callback(inviter_userid, invitee_userid, room_id)
if res is False:
return False

return True
Expand All @@ -322,7 +337,11 @@ async def user_may_send_3pid_invite(
True if the user may send the invite, otherwise False
"""
for callback in self._user_may_send_3pid_invite_callbacks:
if await callback(inviter_userid, medium, address, room_id) is False:
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res = await callback(inviter_userid, medium, address, room_id)
if res is False:
return False

return True
Expand All @@ -339,7 +358,11 @@ async def user_may_create_room(self, userid: str) -> bool:
True if the user may create a room, otherwise False
"""
for callback in self._user_may_create_room_callbacks:
if await callback(userid) is False:
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res = await callback(userid)
if res is False:
return False

return True
Expand All @@ -359,7 +382,11 @@ async def user_may_create_room_alias(
True if the user may create a room alias, otherwise False
"""
for callback in self._user_may_create_room_alias_callbacks:
if await callback(userid, room_alias) is False:
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res = await callback(userid, room_alias)
if res is False:
return False

return True
Expand All @@ -377,7 +404,11 @@ async def user_may_publish_room(self, userid: str, room_id: str) -> bool:
True if the user may publish the room, otherwise False
"""
for callback in self._user_may_publish_room_callbacks:
if await callback(userid, room_id) is False:
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res = await callback(userid, room_id)
if res is False:
return False

return True
Expand All @@ -398,9 +429,13 @@ async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
True if the user is spammy.
"""
for callback in self._check_username_for_spam_callbacks:
# Make a copy of the user profile object to ensure the spam checker cannot
# modify it.
if await callback(user_profile.copy()):
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
# Make a copy of the user profile object to ensure the spam checker cannot
# modify it.
res = await callback(user_profile.copy())
if res:
return True

return False
Expand Down Expand Up @@ -428,9 +463,12 @@ async def check_registration_for_spam(
"""

for callback in self._check_registration_for_spam_callbacks:
behaviour = await (
callback(email_threepid, username, request_info, auth_provider_id)
)
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
behaviour = await (
callback(email_threepid, username, request_info, auth_provider_id)
)
assert isinstance(behaviour, RegistrationBehaviour)
if behaviour != RegistrationBehaviour.ALLOW:
return behaviour
Expand Down Expand Up @@ -472,7 +510,10 @@ async def check_media_file_for_spam(
"""

for callback in self._check_media_file_for_spam_callbacks:
spam = await callback(file_wrapper, file_info)
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
spam = await callback(file_wrapper, file_info)
if spam:
return True

Expand Down
2 changes: 1 addition & 1 deletion synapse/server.py
Expand Up @@ -680,7 +680,7 @@ def get_stats_handler(self) -> StatsHandler:

@cache_in_self
def get_spam_checker(self) -> SpamChecker:
return SpamChecker()
return SpamChecker(self)

@cache_in_self
def get_third_party_event_rules(self) -> ThirdPartyEventRules:
Expand Down

0 comments on commit 75f446d

Please sign in to comment.