From d26a94a0db4b1b9a0052e182355992ec7c224ab5 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 23 Aug 2023 15:00:21 -0700 Subject: [PATCH] management: Validate string_id when creating or renaming a realm. Signed-off-by: Anders Kaseorg --- .../management/commands/change_realm_subdomain.py | 14 ++++++++++++++ zerver/management/commands/create_realm.py | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/zerver/management/commands/change_realm_subdomain.py b/zerver/management/commands/change_realm_subdomain.py index fc3dd9a059..c9b98a39af 100644 --- a/zerver/management/commands/change_realm_subdomain.py +++ b/zerver/management/commands/change_realm_subdomain.py @@ -1,7 +1,11 @@ from argparse import ArgumentParser from typing import Any +from django.core.exceptions import ValidationError +from django.core.management.base import CommandError + from zerver.actions.create_realm import do_change_realm_subdomain +from zerver.forms import check_subdomain_available from zerver.lib.management import ZulipBaseCommand @@ -16,12 +20,22 @@ class Command(ZulipBaseCommand): action="store_true", help="Do not redirect the old name to the new one", ) + parser.add_argument( + "--allow-reserved-subdomain", + action="store_true", + help="Allow use of reserved subdomains", + ) def handle(self, *args: Any, **options: Any) -> None: realm = self.get_realm(options) assert realm is not None # Should be ensured by parser new_subdomain = options["new_subdomain"] + allow_reserved_subdomain = options["allow_reserved_subdomain"] + try: + check_subdomain_available(new_subdomain, allow_reserved_subdomain) + except ValidationError as error: + raise CommandError(error.message) do_change_realm_subdomain( realm, new_subdomain, diff --git a/zerver/management/commands/create_realm.py b/zerver/management/commands/create_realm.py index 62f9448365..28b3f927e3 100644 --- a/zerver/management/commands/create_realm.py +++ b/zerver/management/commands/create_realm.py @@ -1,10 +1,12 @@ import argparse from typing import Any +from django.core.exceptions import ValidationError from django.core.management.base import CommandError from zerver.actions.create_realm import do_create_realm from zerver.actions.create_user import do_create_user +from zerver.forms import check_subdomain_available from zerver.lib.management import ZulipBaseCommand from zerver.models import UserProfile @@ -35,11 +37,22 @@ workflow as `./manage.py create_user`. help="Subdomain for the new organization. Empty if root domain.", default="", ) + parser.add_argument( + "--allow-reserved-subdomain", + action="store_true", + help="Allow use of reserved subdomains", + ) self.add_create_user_args(parser) - def handle(self, *args: Any, **options: str) -> None: + def handle(self, *args: Any, **options: Any) -> None: realm_name = options["realm_name"] string_id = options["string_id"] + allow_reserved_subdomain = options["allow_reserved_subdomain"] + + try: + check_subdomain_available(string_id, allow_reserved_subdomain) + except ValidationError as error: + raise CommandError(error.message) create_user_params = self.get_create_user_params(options)