Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
realm: Enfore length restriction on jitsi_server_url at API level.
Sahil Batra633ec698f5c0
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.pyindex a6b82eaa5c9..5824ae3d538 100644--- a/zerver/lib/validator.py+++ b/zerver/lib/validator.py@@ -389,6 +389,22 @@ def check_url(var_name: str, val: object) -> str:raise ValidationError(_("{var_name} is not a URL").format(var_name=var_name))+def check_capped_url(max_length: int) -> Validator[str]:+ def validator(var_name: str, val: object) -> str:+ # Ensure val is a string and length of the string does not+ # exceed max_length.+ s = check_capped_string(max_length)(var_name, val)+ # Validate as URL.+ validate = URLValidator()+ try:+ validate(s)+ return s+ except ValidationError:+ raise ValidationError(_("{var_name} is not a URL").format(var_name=var_name))++ return validator++def check_external_account_url_pattern(var_name: str, val: object) -> str:s = check_string(var_name, val)diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.pyindex bdd6c59dca9..b5baf22be0b 100644--- a/zerver/tests/test_realm.py+++ b/zerver/tests/test_realm.py@@ -1,6 +1,8 @@import jsonimport os+import randomimport re+import stringfrom datetime import datetime, timedeltafrom typing import Any, Dict, List, Unionfrom unittest import mock@@ -927,6 +929,12 @@ class RealmTest(ZulipTestCase):result = self.client_patch("/json/realm", req)self.assert_json_error(result, "jitsi_server_url is not an allowed_type")+ url_string = "".join(random.choices(string.ascii_lowercase, k=180))+ long_url = "https://jitsi.example.com/" + url_string+ req = dict(jitsi_server_url=orjson.dumps(long_url).decode())+ result = self.client_patch("/json/realm", req)+ self.assert_json_error(result, "jitsi_server_url is not an allowed_type")+valid_url = "https://jitsi.example.com"req = dict(jitsi_server_url=orjson.dumps(valid_url).decode())result = self.client_patch("/json/realm", req)diff --git a/zerver/views/realm.py b/zerver/views/realm.pyindex fce8ddbdd8d..366b280ee18 100644--- a/zerver/views/realm.py+++ b/zerver/views/realm.py@@ -32,13 +32,13 @@ from zerver.lib.user_groups import access_user_group_for_settingfrom zerver.lib.validator import (check_bool,check_capped_string,+ check_capped_url,check_dict,check_int,check_int_in,check_string_in,check_string_or_int,check_union,- check_url,to_non_negative_int,)from zerver.models import Realm, RealmReactivationStatus, RealmUserDefault, UserProfile@@ -54,6 +54,9 @@ def parse_jitsi_server_url(return value+JITSI_SERVER_URL_MAX_LENGTH = 200++@require_realm_admin@has_request_variablesdef update_realm(@@ -147,7 +150,10 @@ def update_realm(jitsi_server_url_raw: Optional[str] = REQ("jitsi_server_url",json_validator=check_union(- [check_string_in(list(Realm.JITSI_SERVER_SPECIAL_VALUES_MAP.keys())), check_url]+ [+ check_string_in(list(Realm.JITSI_SERVER_SPECIAL_VALUES_MAP.keys())),+ check_capped_url(JITSI_SERVER_URL_MAX_LENGTH),+ ]),default=None,),