Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

auth: Ensure a realm can't be created on SOCIAL_AUTH_SUBDOMAIN.

Mateusz Manderaf064e3ebac20
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index ce42259d615..18a67df7b42 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -4973,6 +4973,8 @@ def do_create_realm(
date_created: Optional[datetime.datetime] = None,
is_demo_organization: Optional[bool] = False,
) -> Realm:
+ if string_id == settings.SOCIAL_AUTH_SUBDOMAIN:
+ raise AssertionError("Creating a realm on SOCIAL_AUTH_SUBDOMAIN is not allowed!")
if Realm.objects.filter(string_id=string_id).exists():
raise AssertionError(f"Realm {string_id} already exists!")
if not server_initialized():
diff --git a/zerver/lib/name_restrictions.py b/zerver/lib/name_restrictions.py
index 85cd8371e5d..0bf6ad861e7 100644
--- a/zerver/lib/name_restrictions.py
+++ b/zerver/lib/name_restrictions.py
@@ -1,7 +1,10 @@
from disposable_email_domains import blacklist
+from django.conf import settings
def is_reserved_subdomain(subdomain: str) -> bool:
+ if subdomain == settings.SOCIAL_AUTH_SUBDOMAIN:
+ return True
if subdomain in ZULIP_RESERVED_SUBDOMAINS:
return True
if subdomain[-1] == "s" and subdomain[:-1] in ZULIP_RESERVED_SUBDOMAINS:
diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py
index 53aeba68a42..9f0c26b1f6e 100644
--- a/zerver/tests/test_realm.py
+++ b/zerver/tests/test_realm.py
@@ -59,6 +59,11 @@ class RealmTest(ZulipTestCase):
["INFO:root:Server not yet initialized. Creating the internal realm first."],
)
+ def test_realm_creation_on_social_auth_subdomain_disallowed(self) -> None:
+ with self.settings(SOCIAL_AUTH_SUBDOMAIN="zulipauth"):
+ with self.assertRaises(AssertionError):
+ do_create_realm("zulipauth", "Test Realm")
+
def test_do_set_realm_name_caching(self) -> None:
"""The main complicated thing about setting realm names is fighting the
cache, and we start by populating the cache for Hamlet, and we end
diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py
index e8ad2ffebed..1a208f4d4ac 100644
--- a/zerver/tests/test_signup.py
+++ b/zerver/tests/test_signup.py
@@ -3409,6 +3409,12 @@ class RealmCreationTest(ZulipTestCase):
["Subdomain can only have lowercase letters, numbers, and '-'s."], result
)
+ with self.settings(SOCIAL_AUTH_SUBDOMAIN="zulipauth"):
+ result = self.client_get("/json/realm/subdomain/zulipauth")
+ self.assert_in_success_response(
+ ["Subdomain unavailable. Please choose a different one."], result
+ )
+
result = self.client_get("/json/realm/subdomain/hufflepuff")
self.assert_in_success_response(["available"], result)
self.assert_not_in_success_response(["unavailable"], result)