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

Commit 5099bd6

Browse files
authored
Do not allow send_nonmember_event to be called with shadow-banned users. (#8158)
1 parent 6e1c64a commit 5099bd6

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

changelog.d/8158.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for shadow-banning users (ignoring any message send requests).

synapse/handlers/message.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,24 +647,35 @@ async def send_nonmember_event(
647647
event: EventBase,
648648
context: EventContext,
649649
ratelimit: bool = True,
650+
ignore_shadow_ban: bool = False,
650651
) -> int:
651652
"""
652653
Persists and notifies local clients and federation of an event.
653654
654655
Args:
655-
requester
656-
event the event to send.
657-
context: the context of the event.
656+
requester: The requester sending the event.
657+
event: The event to send.
658+
context: The context of the event.
658659
ratelimit: Whether to rate limit this send.
660+
ignore_shadow_ban: True if shadow-banned users should be allowed to
661+
send this event.
659662
660663
Return:
661664
The stream_id of the persisted event.
665+
666+
Raises:
667+
ShadowBanError if the requester has been shadow-banned.
662668
"""
663669
if event.type == EventTypes.Member:
664670
raise SynapseError(
665671
500, "Tried to send member event through non-member codepath"
666672
)
667673

674+
if not ignore_shadow_ban and requester.shadow_banned:
675+
# We randomly sleep a bit just to annoy the requester.
676+
await self.clock.sleep(random.randint(1, 10))
677+
raise ShadowBanError()
678+
668679
user = UserID.from_string(event.sender)
669680

670681
assert self.hs.is_mine(user), "User must be our own: %s" % (user,)
@@ -725,6 +736,14 @@ async def create_and_send_nonmember_event(
725736
726737
See self.create_event and self.send_nonmember_event.
727738
739+
Args:
740+
requester: The requester sending the event.
741+
event_dict: An entire event.
742+
ratelimit: Whether to rate limit this send.
743+
txn_id: The transaction ID.
744+
ignore_shadow_ban: True if shadow-banned users should be allowed to
745+
send this event.
746+
728747
Raises:
729748
ShadowBanError if the requester has been shadow-banned.
730749
"""
@@ -750,7 +769,11 @@ async def create_and_send_nonmember_event(
750769
raise SynapseError(403, spam_error, Codes.FORBIDDEN)
751770

752771
stream_id = await self.send_nonmember_event(
753-
requester, event, context, ratelimit=ratelimit
772+
requester,
773+
event,
774+
context,
775+
ratelimit=ratelimit,
776+
ignore_shadow_ban=ignore_shadow_ban,
754777
)
755778
return event, stream_id
756779

@@ -1190,8 +1213,14 @@ async def _send_dummy_events_to_fill_extremities(self):
11901213

11911214
event.internal_metadata.proactively_send = False
11921215

1216+
# Since this is a dummy-event it is OK if it is sent by a
1217+
# shadow-banned user.
11931218
await self.send_nonmember_event(
1194-
requester, event, context, ratelimit=False
1219+
requester,
1220+
event,
1221+
context,
1222+
ratelimit=False,
1223+
ignore_shadow_ban=True,
11951224
)
11961225
dummy_event_sent = True
11971226
break

0 commit comments

Comments
 (0)