Don't ratelimit autojoining of rooms (#8921)

Fixes #8866
This commit is contained in:
Erik Johnston 2020-12-11 10:17:49 +00:00 committed by GitHub
parent dc016c66ae
commit 1d55c7b567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 11 deletions

1
changelog.d/8921.bugfix Normal file
View File

@ -0,0 +1 @@
Fix bug where we ratelimited auto joining of rooms on registration (using `auto_join_rooms` config).

View File

@ -440,6 +440,7 @@ class RoomCreationHandler(BaseHandler):
invite_list=[], invite_list=[],
initial_state=initial_state, initial_state=initial_state,
creation_content=creation_content, creation_content=creation_content,
ratelimit=False,
) )
# Transfer membership events # Transfer membership events
@ -735,6 +736,7 @@ class RoomCreationHandler(BaseHandler):
room_alias=room_alias, room_alias=room_alias,
power_level_content_override=power_level_content_override, power_level_content_override=power_level_content_override,
creator_join_profile=creator_join_profile, creator_join_profile=creator_join_profile,
ratelimit=ratelimit,
) )
if "name" in config: if "name" in config:
@ -838,6 +840,7 @@ class RoomCreationHandler(BaseHandler):
room_alias: Optional[RoomAlias] = None, room_alias: Optional[RoomAlias] = None,
power_level_content_override: Optional[JsonDict] = None, power_level_content_override: Optional[JsonDict] = None,
creator_join_profile: Optional[JsonDict] = None, creator_join_profile: Optional[JsonDict] = None,
ratelimit: bool = True,
) -> int: ) -> int:
"""Sends the initial events into a new room. """Sends the initial events into a new room.
@ -884,7 +887,7 @@ class RoomCreationHandler(BaseHandler):
creator.user, creator.user,
room_id, room_id,
"join", "join",
ratelimit=False, ratelimit=ratelimit,
content=creator_join_profile, content=creator_join_profile,
) )

View File

@ -203,7 +203,7 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
# Only rate-limit if the user actually joined the room, otherwise we'll end # Only rate-limit if the user actually joined the room, otherwise we'll end
# up blocking profile updates. # up blocking profile updates.
if newly_joined: if newly_joined and ratelimit:
time_now_s = self.clock.time() time_now_s = self.clock.time()
( (
allowed, allowed,
@ -488,17 +488,20 @@ class RoomMemberHandler(metaclass=abc.ABCMeta):
raise AuthError(403, "Guest access not allowed") raise AuthError(403, "Guest access not allowed")
if not is_host_in_room: if not is_host_in_room:
time_now_s = self.clock.time() if ratelimit:
( time_now_s = self.clock.time()
allowed, (
time_allowed, allowed,
) = self._join_rate_limiter_remote.can_requester_do_action(requester,) time_allowed,
) = self._join_rate_limiter_remote.can_requester_do_action(
if not allowed: requester,
raise LimitExceededError(
retry_after_ms=int(1000 * (time_allowed - time_now_s))
) )
if not allowed:
raise LimitExceededError(
retry_after_ms=int(1000 * (time_allowed - time_now_s))
)
inviter = await self._get_inviter(target.to_string(), room_id) inviter = await self._get_inviter(target.to_string(), room_id)
if inviter and not self.hs.is_mine(inviter): if inviter and not self.hs.is_mine(inviter):
remote_room_hosts.append(inviter.domain) remote_room_hosts.append(inviter.domain)

View File

@ -26,6 +26,7 @@ from mock import Mock
import synapse.rest.admin import synapse.rest.admin
from synapse.api.constants import EventContentFields, EventTypes, Membership from synapse.api.constants import EventContentFields, EventTypes, Membership
from synapse.handlers.pagination import PurgeStatus from synapse.handlers.pagination import PurgeStatus
from synapse.rest import admin
from synapse.rest.client.v1 import directory, login, profile, room from synapse.rest.client.v1 import directory, login, profile, room
from synapse.rest.client.v2_alpha import account from synapse.rest.client.v2_alpha import account
from synapse.types import JsonDict, RoomAlias, UserID from synapse.types import JsonDict, RoomAlias, UserID
@ -625,6 +626,7 @@ class RoomJoinRatelimitTestCase(RoomBase):
user_id = "@sid1:red" user_id = "@sid1:red"
servlets = [ servlets = [
admin.register_servlets,
profile.register_servlets, profile.register_servlets,
room.register_servlets, room.register_servlets,
] ]
@ -703,6 +705,20 @@ class RoomJoinRatelimitTestCase(RoomBase):
request, channel = self.make_request("POST", path % room_id, {}) request, channel = self.make_request("POST", path % room_id, {})
self.assertEquals(channel.code, 200) self.assertEquals(channel.code, 200)
@unittest.override_config(
{
"rc_joins": {"local": {"per_second": 0.5, "burst_count": 3}},
"auto_join_rooms": ["#room:red", "#room2:red", "#room3:red", "#room4:red"],
"autocreate_auto_join_rooms": True,
},
)
def test_autojoin_rooms(self):
user_id = self.register_user("testuser", "password")
# Check that the new user successfully joined the four rooms
rooms = self.get_success(self.hs.get_datastore().get_rooms_for_user(user_id))
self.assertEqual(len(rooms), 4)
class RoomMessagesTestCase(RoomBase): class RoomMessagesTestCase(RoomBase):
""" Tests /rooms/$room_id/messages/$user_id/$msg_id REST events. """ """ Tests /rooms/$room_id/messages/$user_id/$msg_id REST events. """