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.

push_notifs: Check app ID has a plausible shape.

Greg Pricea9796ec503a3
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index f8ffc612069..be730df21ea 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -780,11 +780,16 @@ class PushBouncerNotificationTest(BouncerTestCase):
result = self.client_delete(endpoint, {"token": broken_token}, subdomain="zulip")
self.assert_json_error(result, "Empty or invalid length token")
- # Try adding without appid...
+ # Try adding with missing or invalid appid...
if appid:
result = self.client_post(endpoint, {"token": token}, subdomain="zulip")
self.assert_json_error(result, "Missing 'appid' argument")
+ result = self.client_post(
+ endpoint, {"token": token, "appid": "'; tables --"}, subdomain="zulip"
+ )
+ self.assert_json_error(result, "Invalid app ID")
+
# Try to remove a non-existent token...
result = self.client_delete(endpoint, {"token": "abcd1234"}, subdomain="zulip")
self.assert_json_error(result, "Token does not exist")
@@ -2811,11 +2816,14 @@ class TestPushApi(BouncerTestCase):
result = self.client_delete(endpoint, {"token": broken_token})
self.assert_json_error(result, "Empty or invalid length token")
- # Try adding without appid...
+ # Try adding with missing or invalid appid...
if appid:
result = self.client_post(endpoint, {"token": label})
self.assert_json_error(result, "Missing 'appid' argument")
+ result = self.client_post(endpoint, {"token": label, "appid": "'; tables --"})
+ self.assert_json_error(result, "Invalid app ID")
+
# Try to remove a non-existent token...
result = self.client_delete(endpoint, {"token": "abcd1234"})
self.assert_json_error(result, "Token does not exist")
diff --git a/zerver/views/push_notifications.py b/zerver/views/push_notifications.py
index 2abb8211848..c1c768135de 100644
--- a/zerver/views/push_notifications.py
+++ b/zerver/views/push_notifications.py
@@ -1,3 +1,4 @@
+import re
from typing import Optional
from django.http import HttpRequest, HttpResponse
@@ -14,9 +15,19 @@ from zerver.lib.push_notifications import (
)
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
+from zerver.lib.validator import check_string
from zerver.models import PushDeviceToken, UserProfile
+def check_app_id(var_name: str, val: object) -> str:
+ # Garbage values should be harmless, but we can be picky
+ # as insurance against bugs somewhere.
+ s = check_string(var_name, val)
+ if not re.fullmatch("[.a-zA-Z0-9-]+", s):
+ raise JsonableError(_("Invalid app ID"))
+ return s
+
+
def validate_token(token_str: str, kind: int) -> None:
if token_str == "" or len(token_str) > 4096:
raise JsonableError(_("Empty or invalid length token"))
@@ -34,7 +45,7 @@ def add_apns_device_token(
request: HttpRequest,
user_profile: UserProfile,
token: str = REQ(),
- appid: str = REQ(),
+ appid: str = REQ(str_validator=check_app_id),
) -> HttpResponse:
validate_token(token, PushDeviceToken.APNS)
add_push_device_token(user_profile, token, PushDeviceToken.APNS, ios_app_id=appid)