Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
rate_limit: Merge two IP rate limits domains that send emails.
Alex Vandiver0cfb156545a2
diff --git a/zerver/tests/test_external.py b/zerver/tests/test_external.pyindex e81bcaa952c..51b17dff2de 100644--- a/zerver/tests/test_external.py+++ b/zerver/tests/test_external.py@@ -203,7 +203,7 @@ class RateLimitTests(ZulipTestCase):resp = self.send_unauthed_api_request(REMOTE_ADDR="127.0.0.2")self.assertNotEqual(resp.status_code, 429)- @rate_limit_rule(1, 5, domain="create_realm_by_ip")+ @rate_limit_rule(1, 5, domain="sends_email_by_ip")def test_create_realm_rate_limiting(self) -> None:with self.settings(OPEN_REALM_CREATION=True):self.do_test_hit_ratelimits(@@ -211,7 +211,7 @@ class RateLimitTests(ZulipTestCase):is_json=False,)- @rate_limit_rule(1, 5, domain="find_account_by_ip")+ @rate_limit_rule(1, 5, domain="sends_email_by_ip")def test_find_account_rate_limiting(self) -> None:self.do_test_hit_ratelimits(lambda: self.client_post("/accounts/find/", {"emails": "[email protected]"}),@@ -221,13 +221,28 @@ class RateLimitTests(ZulipTestCase):# Test whether submitting multiple emails is handled correctly.# The limit is set to 10 per second, so 5 requests with 2 emails# submitted in each should be allowed.- @rate_limit_rule(1, 10, domain="find_account_by_ip")+ @rate_limit_rule(1, 10, domain="sends_email_by_ip")def test_find_account_rate_limiting_multiple(self) -> None:self.do_test_hit_ratelimits(lambda: self.client_post("/accounts/find/", {"emails": "[email protected],[email protected]"}),is_json=False,)+ @rate_limit_rule(1, 5, domain="sends_email_by_ip")+ def test_combined_ip_limits(self) -> None:+ # Alternate requests to /new/ and /accounts/find/+ request_count = 0++ def alternate_requests() -> HttpResponse:+ nonlocal request_count+ request_count += 1+ if request_count % 2 == 1:+ return self.client_post("/new/", {"email": "[email protected]"})+ else:+ return self.client_post("/accounts/find/", {"emails": "[email protected]"})++ self.do_test_hit_ratelimits(alternate_requests, is_json=False)+@skipUnless(settings.ZILENCER_ENABLED, "requires zilencer")@rate_limit_rule(1, 5, domain="api_by_remote_server")def test_hit_ratelimits_as_remote_server(self) -> None:diff --git a/zerver/views/registration.py b/zerver/views/registration.pyindex 335de8f6762..0bd5b67ab73 100644--- a/zerver/views/registration.py+++ b/zerver/views/registration.py@@ -605,7 +605,7 @@ def create_realm(request: HttpRequest, creation_key: Optional[str] = None) -> Htform = RealmCreationForm(request.POST)if form.is_valid():try:- rate_limit_request_by_ip(request, domain="create_realm_by_ip")+ rate_limit_request_by_ip(request, domain="sends_email_by_ip")except RateLimited as e:assert e.secs_to_freedom is not Nonereturn render(@@ -725,7 +725,7 @@ def find_account(emails = form.cleaned_data["emails"]for i in range(len(emails)):try:- rate_limit_request_by_ip(request, domain="find_account_by_ip")+ rate_limit_request_by_ip(request, domain="sends_email_by_ip")except RateLimited as e:assert e.secs_to_freedom is not Nonereturn render(diff --git a/zproject/computed_settings.py b/zproject/computed_settings.pyindex 470471a6d56..ac2dd6c6bfd 100644--- a/zproject/computed_settings.py+++ b/zproject/computed_settings.py@@ -385,16 +385,13 @@ RATE_LIMITING_RULES = {"authenticate_by_username": [(1800, 5), # 5 login attempts within 30 minutes],- "create_realm_by_ip": [- (1800, 5),- ],- "find_account_by_ip": [- (3600, 10),- ],"password_reset_form_by_email": [(3600, 2), # 2 reset emails per hour(86400, 5), # 5 per day],+ "sends_email_by_ip": [+ (1800, 5),+ ],}# List of domains that, when applied to a request in a Tornado process,diff --git a/zproject/test_extra_settings.py b/zproject/test_extra_settings.pyindex e1382816bbf..448045a60b4 100644--- a/zproject/test_extra_settings.py+++ b/zproject/test_extra_settings.py@@ -266,8 +266,7 @@ RATE_LIMITING_RULES: Dict[str, List[Tuple[int, int]]] = {"api_by_ip": [],"api_by_remote_server": [],"authenticate_by_username": [],- "create_realm_by_ip": [],- "find_account_by_ip": [],+ "sends_email_by_ip": [],"password_reset_form_by_email": [],}