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

Allow spam checker to reject invites too #2492

Merged
merged 9 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions synapse/events/spamcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ def check_event_for_spam(self, event):
return False

return self.spam_checker.check_event_for_spam(event)

def user_may_invite(self, userid, room_id):
"""Checks if a given user may send an invite

If this method returns false, the invite will be rejected.

Args:
userid (string): The sender's user ID

Returns:
bool: True if the user may send an invite, otherwise False
"""
if self.spam_checker is None:
return True

return self.spam_checker.user_may_invite(userid, room_id)
6 changes: 6 additions & 0 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, hs):
self.action_generator = hs.get_action_generator()
self.is_mine_id = hs.is_mine_id
self.pusher_pool = hs.get_pusherpool()
self.spam_checker = hs.get_spam_checker()

self.replication_layer.set_handler(self)

Expand Down Expand Up @@ -1077,6 +1078,11 @@ def on_invite_request(self, origin, pdu):
if self.hs.config.block_non_admin_invites:
raise SynapseError(403, "This server does not accept room invites")

if not self.spam_checker.user_may_invite(event.sender, event.room_id):
raise SynapseError(
403, "This user is not permitted to send invites to this server"
)

membership = event.content.get("membership")
if event.type != EventTypes.Member or membership != Membership.INVITE:
raise SynapseError(400, "The event was not an m.room.member invite event")
Expand Down
19 changes: 17 additions & 2 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, hs):
self.member_linearizer = Linearizer(name="member")

self.clock = hs.get_clock()
self.spam_checker = hs.get_spam_checker()

self.distributor = hs.get_distributor()
self.distributor.declare("user_joined_room")
Expand Down Expand Up @@ -210,12 +211,26 @@ def _update_membership(
if is_blocked:
raise SynapseError(403, "This room has been blocked on this server")

if (effective_membership_state == "invite" and
self.hs.config.block_non_admin_invites):
if effective_membership_state == "invite":
block_invite = False
is_requester_admin = yield self.auth.is_server_admin(
requester.user,
)
if not is_requester_admin:
if self.hs.config.block_non_admin_invites:
logger.info(
"Blocking invite: user is not admin and non-admin "
"invites disabled"
)
block_invite = True

if not self.spam_checker.user_may_invite(
requester.user.to_string(), room_id,
):
logger.info("Blocking invite due to spam checker")
block_invite = True

if block_invite:
raise SynapseError(
403, "Invites have been disabled on this server",
)
Expand Down