zilencer: Limit all hostname lookups with deactivated=False.

We enforce at registration time that hostnames with deactivated=False
must be unique, though we do not enforce it in the database because we
have historical data for which that is not true.

However, we should perform all hostname lookups with a
`deactivated=False` filter, as that will resolve the majority of
`RemoteZulipServer.MultipleObjectsReturned` errors.  Once the
production data is cleaned up, this can be enforced via a partial
unique index.
This commit is contained in:
Alex Vandiver
2025-08-18 13:51:29 +00:00
committed by Tim Abbott
parent 2dd8818517
commit 3d1a4e718e

View File

@@ -178,7 +178,7 @@ def validate_hostname_or_raise_error(hostname: str) -> None:
def transfer_remote_server_registration(request: HttpRequest, *, hostname: str) -> HttpResponse:
validate_hostname_or_raise_error(hostname)
if not RemoteZulipServer.objects.filter(hostname=hostname).exists():
if not RemoteZulipServer.objects.filter(hostname=hostname, deactivated=False).exists():
raise JsonableError(_("{hostname} not yet registered").format(hostname=hostname))
verification_secret = generate_registration_transfer_verification_secret(hostname)
@@ -271,7 +271,10 @@ def register_remote_server(
if remote_server.deactivated:
raise RemoteServerDeactivatedError
if remote_server is None and RemoteZulipServer.objects.filter(hostname=hostname).exists():
if (
remote_server is None
and RemoteZulipServer.objects.filter(hostname=hostname, deactivated=False).exists()
):
raise HostnameAlreadyInUseBouncerError(hostname)
with transaction.atomic(durable=True):
@@ -357,7 +360,7 @@ def verify_registration_transfer_challenge_ack_endpoint(
)
try:
remote_server = RemoteZulipServer.objects.get(hostname=hostname)
remote_server = RemoteZulipServer.objects.get(hostname=hostname, deactivated=False)
except RemoteZulipServer.DoesNotExist:
raise JsonableError(_("Registration not found for this hostname"))