This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add ratelimiting on joins #8008
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add rate limiting to users joining rooms. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,8 @@ | |
|
|
||
| from synapse import types | ||
| from synapse.api.constants import MAX_DEPTH, EventTypes, Membership | ||
| from synapse.api.errors import AuthError, Codes, SynapseError | ||
| from synapse.api.errors import AuthError, Codes, LimitExceededError, SynapseError | ||
| from synapse.api.ratelimiting import Ratelimiter | ||
| from synapse.api.room_versions import EventFormatVersions | ||
| from synapse.crypto.event_signing import compute_event_reference_hash | ||
| from synapse.events import EventBase | ||
|
|
@@ -77,6 +78,17 @@ def __init__(self, hs): | |
| if self._is_on_event_persistence_instance: | ||
| self.persist_event_storage = hs.get_storage().persistence | ||
|
|
||
| self._join_rate_limiter_local = Ratelimiter( | ||
| clock=self.clock, | ||
| rate_hz=hs.config.ratelimiting.rc_joins_local.per_second, | ||
| burst_count=hs.config.ratelimiting.rc_joins_local.burst_count, | ||
| ) | ||
| self._join_rate_limiter_remote = Ratelimiter( | ||
| clock=self.clock, | ||
| rate_hz=hs.config.ratelimiting.rc_joins_remote.per_second, | ||
| burst_count=hs.config.ratelimiting.rc_joins_remote.burst_count, | ||
| ) | ||
|
|
||
| # This is only used to get at ratelimit function, and | ||
| # maybe_kick_guest_users. It's fine there are multiple of these as | ||
| # it doesn't store state. | ||
|
|
@@ -441,7 +453,28 @@ async def _update_membership( | |
| # so don't really fit into the general auth process. | ||
| raise AuthError(403, "Guest access not allowed") | ||
|
|
||
| if not is_host_in_room: | ||
| if is_host_in_room: | ||
| time_now_s = self.clock.time() | ||
| allowed, time_allowed = self._join_rate_limiter_local.can_do_action( | ||
| requester.user.to_string(), | ||
| ) | ||
|
|
||
| if not allowed: | ||
| raise LimitExceededError( | ||
| retry_after_ms=int(1000 * (time_allowed - time_now_s)) | ||
| ) | ||
|
|
||
| else: | ||
| time_now_s = self.clock.time() | ||
| allowed, time_allowed = self._join_rate_limiter_remote.can_do_action( | ||
| requester.user.to_string(), | ||
| ) | ||
|
|
||
| if not allowed: | ||
| raise LimitExceededError( | ||
| retry_after_ms=int(1000 * (time_allowed - time_now_s)) | ||
| ) | ||
|
Comment on lines
+456
to
+476
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Part of me feels like there must be something here to factor out, but not really sure it would be that easy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I know right, but I think this is a case where its best not to go down that rabbit hole |
||
|
|
||
| inviter = await self._get_inviter(target.to_string(), room_id) | ||
| if inviter and not self.hs.is_mine(inviter): | ||
| remote_room_hosts.append(inviter.domain) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't we want to allow faster local joins than remote joins?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we do! The burst count is the same but the rate is larger for locals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I read these both as seconds per request instead of requests per second. Ignore me. 😢