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.

remote_server: Validate zulip_org_id submitted by registering server.

Mateusz Manderae48120fd12f1
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index 9dd058ec091..a2112c48d6a 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -2455,6 +2455,18 @@ class PushBouncerSignupTest(ZulipTestCase):
result = self.client_post("/api/v1/remotes/server/register", request)
self.assert_json_error(result, "Enter a valid email address.")
+ def test_push_signup_invalid_zulip_org_id(self) -> None:
+ zulip_org_id = "x" * RemoteZulipServer.UUID_LENGTH
+ 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_error(result, "Invalid UUID")
+
def test_push_signup_success(self) -> None:
zulip_org_id = str(uuid.uuid4())
zulip_org_key = get_random_string(64)
diff --git a/zilencer/views.py b/zilencer/views.py
index e2322976a2c..432679727b6 100644
--- a/zilencer/views.py
+++ b/zilencer/views.py
@@ -1,6 +1,7 @@
import datetime
import logging
from typing import Any, Dict, List, Optional, Union
+from uuid import UUID
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator, validate_email
@@ -50,6 +51,13 @@ def validate_entity(entity: Union[UserProfile, RemoteZulipServer]) -> RemoteZuli
return entity
+def validate_uuid(uuid: str) -> None:
+ try:
+ UUID(uuid, version=4)
+ except ValueError:
+ raise ValidationError(err_("Invalid UUID"))
+
+
def validate_bouncer_token_request(
entity: Union[UserProfile, RemoteZulipServer], token: str, kind: int
) -> RemoteZulipServer:
@@ -89,6 +97,11 @@ def register_remote_server(
except ValidationError as e:
raise JsonableError(e.message)
+ try:
+ validate_uuid(zulip_org_id)
+ except ValidationError as e:
+ raise JsonableError(e.message)
+
remote_server, created = RemoteZulipServer.objects.get_or_create(
uuid=zulip_org_id,
defaults={"hostname": hostname, "contact_email": contact_email, "api_key": zulip_org_key},