Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
validate_token: Move `validate_token` to avoid dependency cycle.
Prakhar Pratyushe6bdf84363bf
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.pyindex 4032a88b3a3..11f9a153a79 100644--- a/zerver/lib/push_notifications.py+++ b/zerver/lib/push_notifications.py@@ -85,6 +85,17 @@ def hex_to_b64(data: str) -> str:return base64.b64encode(bytes.fromhex(data)).decode()+def validate_token(token_str: str, kind: int) -> None:+ if token_str == "" or len(token_str) > 4096:+ raise JsonableError(_("Empty or invalid length token"))+ if kind == PushDeviceToken.APNS:+ # Validate that we can actually decode the token.+ try:+ b64_to_hex(token_str)+ except Exception:+ raise JsonableError(_("Invalid APNS token"))++def get_message_stream_name_from_database(message: Message) -> str:"""Never use this function outside of the push-notificationsdiff --git a/zerver/views/push_notifications.py b/zerver/views/push_notifications.pyindex 20025cb7808..e34503dbe3e 100644--- a/zerver/views/push_notifications.py+++ b/zerver/views/push_notifications.py@@ -18,10 +18,10 @@ from zerver.lib.exceptions import (from zerver.lib.push_notifications import (InvalidPushDeviceTokenError,add_push_device_token,- b64_to_hex,remove_push_device_token,send_test_push_notification,uses_notification_bouncer,+ validate_token,)from zerver.lib.remote_server import (SELF_HOSTING_REGISTRATION_TAKEOVER_CHALLENGE_TOKEN_REDIS_KEY,@@ -38,17 +38,6 @@ from zerver.views.errors import config_errorredis_client = redis_utils.get_redis_client()-def validate_token(token_str: str, kind: int) -> None:- if token_str == "" or len(token_str) > 4096:- raise JsonableError(_("Empty or invalid length token"))- if kind == PushDeviceToken.APNS:- # Validate that we can actually decode the token.- try:- b64_to_hex(token_str)- except Exception:- raise JsonableError(_("Invalid APNS token"))--@human_users_only@typed_endpointdef add_apns_device_token(diff --git a/zilencer/views.py b/zilencer/views.pyindex 2a619709040..73309a70555 100644--- a/zilencer/views.py+++ b/zilencer/views.py@@ -51,6 +51,7 @@ from zerver.lib.push_notifications import (send_android_push_notification,send_apple_push_notification,send_test_push_notification_directly_to_devices,+ validate_token,)from zerver.lib.queue import queue_event_on_commitfrom zerver.lib.rate_limiter import rate_limit_endpoint_absolute@@ -75,7 +76,6 @@ from zerver.lib.typed_endpoint_validators import check_string_fixed_lengthfrom zerver.lib.types import RemoteRealmDictValuefrom zerver.models.realm_audit_logs import AuditLogEventTypefrom zerver.models.realms import DisposableEmailError-from zerver.views.push_notifications import validate_tokenfrom zilencer.auth import (InvalidZulipServerKeyError,generate_registration_transfer_verification_secret,