|
22 | 22 |
|
23 | 23 | from synapse import types
|
24 | 24 | from synapse.api.constants import MAX_DEPTH, EventTypes, Membership
|
25 |
| -from synapse.api.errors import AuthError, Codes, SynapseError |
| 25 | +from synapse.api.errors import AuthError, Codes, LimitExceededError, SynapseError |
| 26 | +from synapse.api.ratelimiting import Ratelimiter |
26 | 27 | from synapse.api.room_versions import EventFormatVersions
|
27 | 28 | from synapse.crypto.event_signing import compute_event_reference_hash
|
28 | 29 | from synapse.events import EventBase
|
@@ -78,6 +79,17 @@ def __init__(self, hs):
|
78 | 79 | if self._is_on_event_persistence_instance:
|
79 | 80 | self.persist_event_storage = hs.get_storage().persistence
|
80 | 81 |
|
| 82 | + self._join_rate_limiter_local = Ratelimiter( |
| 83 | + clock=self.clock, |
| 84 | + rate_hz=hs.config.ratelimiting.rc_joins_local.per_second, |
| 85 | + burst_count=hs.config.ratelimiting.rc_joins_local.burst_count, |
| 86 | + ) |
| 87 | + self._join_rate_limiter_remote = Ratelimiter( |
| 88 | + clock=self.clock, |
| 89 | + rate_hz=hs.config.ratelimiting.rc_joins_remote.per_second, |
| 90 | + burst_count=hs.config.ratelimiting.rc_joins_remote.burst_count, |
| 91 | + ) |
| 92 | + |
81 | 93 | # This is only used to get at ratelimit function, and
|
82 | 94 | # maybe_kick_guest_users. It's fine there are multiple of these as
|
83 | 95 | # it doesn't store state.
|
@@ -472,7 +484,28 @@ async def _update_membership(
|
472 | 484 | ):
|
473 | 485 | raise SynapseError(403, "Not allowed to join this room")
|
474 | 486 |
|
475 |
| - if not is_host_in_room: |
| 487 | + if is_host_in_room: |
| 488 | + time_now_s = self.clock.time() |
| 489 | + allowed, time_allowed = self._join_rate_limiter_local.can_do_action( |
| 490 | + requester.user.to_string(), |
| 491 | + ) |
| 492 | + |
| 493 | + if not allowed: |
| 494 | + raise LimitExceededError( |
| 495 | + retry_after_ms=int(1000 * (time_allowed - time_now_s)) |
| 496 | + ) |
| 497 | + |
| 498 | + else: |
| 499 | + time_now_s = self.clock.time() |
| 500 | + allowed, time_allowed = self._join_rate_limiter_remote.can_do_action( |
| 501 | + requester.user.to_string(), |
| 502 | + ) |
| 503 | + |
| 504 | + if not allowed: |
| 505 | + raise LimitExceededError( |
| 506 | + retry_after_ms=int(1000 * (time_allowed - time_now_s)) |
| 507 | + ) |
| 508 | + |
476 | 509 | inviter = await self._get_inviter(target.to_string(), room_id)
|
477 | 510 | if inviter and not self.hs.is_mine(inviter):
|
478 | 511 | remote_room_hosts.append(inviter.domain)
|
|
0 commit comments