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

Commit d80d39b

Browse files
authored
Add a ratelimiter for 3pid invite (#11892)
1 parent 8332475 commit d80d39b

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

changelog.d/11892.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use a dedicated configurable rate limiter for 3PID invites.

docs/sample_config.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,9 @@ log_config: "CONFDIR/SERVERNAME.log.config"
857857
# - one for ratelimiting how often a user or IP can attempt to validate a 3PID.
858858
# - two for ratelimiting how often invites can be sent in a room or to a
859859
# specific user.
860+
# - one for ratelimiting 3PID invites (i.e. invites sent to a third-party ID
861+
# such as an email address or a phone number) based on the account that's
862+
# sending the invite.
860863
#
861864
# The defaults are as shown below.
862865
#
@@ -906,6 +909,10 @@ log_config: "CONFDIR/SERVERNAME.log.config"
906909
# per_user:
907910
# per_second: 0.003
908911
# burst_count: 5
912+
#
913+
#rc_third_party_invite:
914+
# per_second: 0.2
915+
# burst_count: 10
909916

910917
# Ratelimiting settings for incoming federation
911918
#

synapse/config/ratelimiting.py

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ def read_config(self, config, **kwargs):
134134
defaults={"per_second": 0.003, "burst_count": 5},
135135
)
136136

137+
self.rc_third_party_invite = RateLimitConfig(
138+
config.get("rc_third_party_invite", {}),
139+
defaults={
140+
"per_second": self.rc_message.per_second,
141+
"burst_count": self.rc_message.burst_count,
142+
},
143+
)
144+
137145
def generate_config_section(self, **kwargs):
138146
return """\
139147
## Ratelimiting ##
@@ -168,6 +176,9 @@ def generate_config_section(self, **kwargs):
168176
# - one for ratelimiting how often a user or IP can attempt to validate a 3PID.
169177
# - two for ratelimiting how often invites can be sent in a room or to a
170178
# specific user.
179+
# - one for ratelimiting 3PID invites (i.e. invites sent to a third-party ID
180+
# such as an email address or a phone number) based on the account that's
181+
# sending the invite.
171182
#
172183
# The defaults are as shown below.
173184
#
@@ -217,6 +228,10 @@ def generate_config_section(self, **kwargs):
217228
# per_user:
218229
# per_second: 0.003
219230
# burst_count: 5
231+
#
232+
#rc_third_party_invite:
233+
# per_second: 0.2
234+
# burst_count: 10
220235
221236
# Ratelimiting settings for incoming federation
222237
#

synapse/handlers/room_member.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ def __init__(self, hs: "HomeServer"):
116116
burst_count=hs.config.ratelimiting.rc_invites_per_user.burst_count,
117117
)
118118

119+
self._third_party_invite_limiter = Ratelimiter(
120+
store=self.store,
121+
clock=self.clock,
122+
rate_hz=hs.config.ratelimiting.rc_third_party_invite.per_second,
123+
burst_count=hs.config.ratelimiting.rc_third_party_invite.burst_count,
124+
)
125+
119126
self.request_ratelimiter = hs.get_request_ratelimiter()
120127

121128
@abc.abstractmethod
@@ -1295,7 +1302,7 @@ async def do_3pid_invite(
12951302

12961303
# We need to rate limit *before* we send out any 3PID invites, so we
12971304
# can't just rely on the standard ratelimiting of events.
1298-
await self.request_ratelimiter.ratelimit(requester)
1305+
await self._third_party_invite_limiter.ratelimit(requester)
12991306

13001307
can_invite = await self.third_party_event_rules.check_threepid_can_be_invited(
13011308
medium, address, room_id

0 commit comments

Comments
 (0)