Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zilencer: Rate limit the transfer_remote_server_registration endpoint.
Mateusz Mandera5b3bdbb3a347
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex 87f5ce742be..64c9307e39d 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -3139,6 +3139,28 @@ class RegistrationTakeoverFlowTest(ZulipTestCase):)self.assert_json_error(result, "unregistered.example.com not yet registered")+ @override_settings(+ RATE_LIMITING=True,+ RATE_LIMITING_RULES=settings.RATE_LIMITING_RULES+ | {"transfer_remote_server_registration_endpoint_by_ip": [(10, 2)]},+ )+ def test_transfer_endpoint_rate_limiting(self) -> None:+ for hostname in ["zulip1.example.com", "zulip2.example.com"]:+ result = self.client_post(+ "/api/v1/remotes/server/register/transfer",+ {"hostname": hostname},+ )+ self.assert_json_error(result, f"{hostname} not yet registered")++ result = self.client_post(+ "/api/v1/remotes/server/register/transfer",+ {"hostname": "zulip3.example.com"},+ )+ self.assertEqual(result.status_code, 429)+ data = result.json()+ self.assertEqual(data.get("result"), "error")+ self.assertIn("API usage exceeded rate limit", data.get("msg"))+def test_serve_verification_secret_endpoint(self) -> None:result = self.client_get("/api/v1/zulip-services/verify/sometoken/",diff --git a/zilencer/views.py b/zilencer/views.pyindex 4c49c1f0f2b..4962aac94b0 100644--- a/zilencer/views.py+++ b/zilencer/views.py@@ -69,7 +69,7 @@ from zerver.lib.push_notifications import (validate_token,)from zerver.lib.queue import queue_event_on_commit-from zerver.lib.rate_limiter import rate_limit_endpoint_absolute+from zerver.lib.rate_limiter import rate_limit_endpoint_absolute, rate_limit_request_by_ipfrom zerver.lib.remote_server import (InstallationCountDataForAnalytics,RealmAuditLogDataForAnalytics,@@ -178,6 +178,9 @@ def validate_hostname_or_raise_error(hostname: str) -> None:@require_post@typed_endpointdef transfer_remote_server_registration(request: HttpRequest, *, hostname: str) -> HttpResponse:+ # We need to rate limit this endpoint to prevent hostname enumeration.+ rate_limit_request_by_ip(request, domain="transfer_remote_server_registration_endpoint_by_ip")+validate_hostname_or_raise_error(hostname)if not RemoteZulipServer.objects.filter(hostname=hostname, deactivated=False).exists():diff --git a/zproject/default_settings.py b/zproject/default_settings.pyindex fb0f398c6dd..82ed8460017 100644--- a/zproject/default_settings.py+++ b/zproject/default_settings.py@@ -354,6 +354,10 @@ DEFAULT_RATE_LIMITING_RULES = {# 10 demos per day(86400, 10),],+ "transfer_remote_server_registration_endpoint_by_ip": [+ # 10 transfer registration requests per day per IP+ (86400, 10),+ ],}# Rate limiting defaults can be individually overridden by adding# entries in this object, which is merged withdiff --git a/zproject/test_extra_settings.py b/zproject/test_extra_settings.pyindex 1090ef50529..6a81ba501c8 100644--- a/zproject/test_extra_settings.py+++ b/zproject/test_extra_settings.py@@ -275,6 +275,7 @@ RATE_LIMITING_RULES: dict[str, list[tuple[int, int]]] = {"email_change_by_user": [],"password_reset_form_by_email": [],"sends_email_by_remote_server": [],+ "transfer_remote_server_registration_endpoint_by_ip": [],}CLOUD_FREE_TRIAL_DAYS: int | None = None