Fix room creation being rate limited too aggressively since Synapse v1.69.0. (#14314)

* Introduce a test for the old behaviour which we want to restore

* Reintroduce the old behaviour in a simpler way

* Newsfile

Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org>

* Use 1 credit instead of 2 for creating a room: be more lenient than before

Notably, the UI in Element Web was still broken after restoring to prior behaviour.

After discussion, we agreed that it would be sensible to increase the limit.

Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org>
This commit is contained in:
reivilibre 2022-10-28 10:53:34 +00:00 committed by GitHub
parent 04fd6221de
commit 6a6e1e8c07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 9 deletions

View file

@ -54,6 +54,7 @@ from tests.http.server._base import make_request_with_cancellation_test
from tests.storage.test_stream import PaginationTestCase
from tests.test_utils import make_awaitable
from tests.test_utils.event_injection import create_event
from tests.unittest import override_config
PATH_PREFIX = b"/_matrix/client/api/v1"
@ -871,6 +872,41 @@ class RoomsCreateTestCase(RoomBase):
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
self.assertEqual(join_mock.call_count, 0)
def _create_basic_room(self) -> Tuple[int, object]:
"""
Tries to create a basic room and returns the response code.
"""
channel = self.make_request(
"POST",
"/createRoom",
{},
)
return channel.code, channel.json_body
@override_config(
{
"rc_message": {"per_second": 0.2, "burst_count": 10},
}
)
def test_room_creation_ratelimiting(self) -> None:
"""
Regression test for #14312, where ratelimiting was made too strict.
Clients should be able to create 10 rooms in a row
without hitting rate limits, using default rate limit config.
(We override rate limiting config back to its default value.)
To ensure we don't make ratelimiting too generous accidentally,
also check that we can't create an 11th room.
"""
for _ in range(10):
code, json_body = self._create_basic_room()
self.assertEqual(code, HTTPStatus.OK, json_body)
# The 6th room hits the rate limit.
code, json_body = self._create_basic_room()
self.assertEqual(code, HTTPStatus.TOO_MANY_REQUESTS, json_body)
class RoomTopicTestCase(RoomBase):
"""Tests /rooms/$room_id/topic REST events."""
@ -1390,10 +1426,22 @@ class RoomJoinRatelimitTestCase(RoomBase):
)
def test_join_local_ratelimit(self) -> None:
"""Tests that local joins are actually rate-limited."""
for _ in range(3):
self.helper.create_room_as(self.user_id)
# Create 4 rooms
room_ids = [
self.helper.create_room_as(self.user_id, is_public=True) for _ in range(4)
]
self.helper.create_room_as(self.user_id, expect_code=429)
joiner_user_id = self.register_user("joiner", "secret")
# Now make a new user try to join some of them.
# The user can join 3 rooms
for room_id in room_ids[0:3]:
self.helper.join(room_id, joiner_user_id)
# But the user cannot join a 4th room
self.helper.join(
room_ids[3], joiner_user_id, expect_code=HTTPStatus.TOO_MANY_REQUESTS
)
@unittest.override_config(
{"rc_joins": {"local": {"per_second": 0.5, "burst_count": 3}}}