Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
register_remote_server: Don't allow duplicate hostnames.
Mateusz Mandera25f47bd7495c
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex 259b23759cd..7a24efbf5fc 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -4909,6 +4909,32 @@ class PushBouncerSignupTest(ZulipTestCase):result, f"Zulip server auth failure: key does not match role {zulip_org_id}")+ def test_register_duplicate_hostname(self) -> None:+ zulip_org_id = str(uuid.uuid4())+ zulip_org_key = get_random_string(64)+ request = dict(+ zulip_org_id=zulip_org_id,+ zulip_org_key=zulip_org_key,+ hostname="example.com",+ contact_email="[email protected]",+ )++ result = self.client_post("/api/v1/remotes/server/register", request)+ self.assert_json_success(result)+ server = RemoteZulipServer.objects.get(uuid=zulip_org_id)+ self.assertEqual(server.hostname, "example.com")++ new_zulip_org_id = str(uuid.uuid4())+ new_zulip_org_key = get_random_string(64)+ request = dict(+ zulip_org_id=new_zulip_org_id,+ zulip_org_key=new_zulip_org_key,+ hostname="example.com",+ contact_email="[email protected]",+ )+ result = self.client_post("/api/v1/remotes/server/register", request)+ self.assert_json_error(result, "A server with hostname example.com already exists")+class TestUserPushIdentityCompat(ZulipTestCase):def test_filter_q(self) -> None:diff --git a/zilencer/views.py b/zilencer/views.pyindex 3b13f4009f9..ac728151ca7 100644--- a/zilencer/views.py+++ b/zilencer/views.py@@ -137,29 +137,40 @@ def register_remote_server(except ValidationError as e:raise JsonableError(e.message)- with transaction.atomic():- remote_server, created = RemoteZulipServer.objects.get_or_create(- uuid=zulip_org_id,- defaults={- "hostname": hostname,- "contact_email": contact_email,- "api_key": zulip_org_key,- "last_request_datetime": timezone_now(),- },+ try:+ remote_server = RemoteZulipServer.objects.get(uuid=zulip_org_id)+ except RemoteZulipServer.DoesNotExist:+ remote_server = None++ if remote_server is not None:+ if not constant_time_compare(remote_server.api_key, zulip_org_key):+ raise InvalidZulipServerKeyError(zulip_org_id)++ if remote_server.deactivated:+ raise RemoteServerDeactivatedError++ if remote_server is None and RemoteZulipServer.objects.filter(hostname=hostname).exists():+ raise JsonableError(+ _("A server with hostname {hostname} already exists").format(hostname=hostname))- if created:++ with transaction.atomic():+ if remote_server is None:+ created = True+ remote_server = RemoteZulipServer.objects.create(+ uuid=zulip_org_id,+ hostname=hostname,+ contact_email=contact_email,+ api_key=zulip_org_key,+ last_request_datetime=timezone_now(),+ )RemoteZulipServerAuditLog.objects.create(event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,server=remote_server,event_time=remote_server.last_updated,)else:- if not constant_time_compare(remote_server.api_key, zulip_org_key):- raise InvalidZulipServerKeyError(zulip_org_id)-- if remote_server.deactivated:- raise RemoteServerDeactivatedError-+ created = Falseremote_server.hostname = hostnameremote_server.contact_email = contact_emailif new_org_key is not None: