Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zilencer: Better errors on non-200, non-JSON, or weird-JSON transfers.
Alex Vandiver294f5fc44815
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex 64c9307e39d..6c258dfe363 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -3050,7 +3050,7 @@ class RegistrationTakeoverFlowTest(ZulipTestCase):],)- # HttpError:+ # Status coderesponses.add(method=responses.GET,url=base_url,@@ -3061,7 +3061,7 @@ class RegistrationTakeoverFlowTest(ZulipTestCase):"/api/v1/remotes/server/register/verify_challenge",{"hostname": self.hostname, "access_token": access_token},)- self.assert_json_error(result, "Error response received from the host: 403")+ self.assert_json_error(result, "Unexpected status code received from the host: 403")self.assertIn("verify_registration_transfer:host:example.com|exception:", mock_log.output[0])@@ -3132,6 +3132,44 @@ class RegistrationTakeoverFlowTest(ZulipTestCase):"verify_registration_transfer:host:example.com|exception:", mock_log.output[0])+ # JSONDecodeError+ responses.add(+ method=responses.GET,+ url=base_url,+ status=200,+ body="",+ )+ with self.assertLogs("zilencer.views", level="INFO") as mock_log:+ result = self.client_post(+ "/api/v1/remotes/server/register/verify_challenge",+ {"hostname": self.hostname, "access_token": access_token},+ )+ self.assert_json_error(+ result, "An error occurred while parsing the response from the host."+ )+ self.assertIn(+ "verify_registration_transfer:host:example.com|exception:", mock_log.output[0]+ )++ # KeyError+ responses.add(+ method=responses.GET,+ url=base_url,+ status=200,+ body=orjson.dumps({"other": "response"}),+ )+ with self.assertLogs("zilencer.views", level="INFO") as mock_log:+ result = self.client_post(+ "/api/v1/remotes/server/register/verify_challenge",+ {"hostname": self.hostname, "access_token": access_token},+ )+ self.assert_json_error(+ result, "An error occurred while parsing the response from the host."+ )+ self.assertIn(+ "verify_registration_transfer:host:example.com|exception:", mock_log.output[0]+ )+def test_initiate_flow_for_unregistered_domain(self) -> None:result = self.client_post("/api/v1/remotes/server/register/transfer",diff --git a/zilencer/views.py b/zilencer/views.pyindex cfa3d45c34e..f13afbffd41 100644--- a/zilencer/views.py+++ b/zilencer/views.py@@ -1,5 +1,6 @@import base64import hashlib+import jsonimport loggingfrom collections import Counterfrom datetime import datetime, timedelta, timezone@@ -399,19 +400,20 @@ def verify_registration_transfer_challenge_ack_endpoint(session = RegistrationTransferVerificationSession()url = urljoin(f"https://{hostname}", f"/api/v1/zulip-services/verify/{access_token}/")- exception_and_error_message: tuple[Exception, str] | None = None+ exception_and_error_message: tuple[Exception | None, str] | None = Nonetry:response = session.get(url)- response.raise_for_status()- except requests.exceptions.HTTPError as e:- if check_transfer_challenge_response_secret_not_prepared(e.response):++ if check_transfer_challenge_response_secret_not_prepared(response):logger.info("verify_registration_transfer:host:%s|secret_not_prepared", hostname)raise JsonableError(_("The host reported it has no verification secret."))- error_message = _("Error response received from the host: {status_code}").format(- status_code=response.status_code- )- exception_and_error_message = (e, error_message)+ if response.status_code != 200:+ error_message = _(+ "Unexpected status code received from the host: {status_code}"+ ).format(status_code=response.status_code)+ exception_and_error_message = (None, error_message)+except requests.exceptions.SSLError as e:error_message = "SSL error occurred while communicating with the host."exception_and_error_message = (e, error_message)@@ -425,13 +427,18 @@ def verify_registration_transfer_challenge_ack_endpoint(error_message = "An error occurred while communicating with the host."exception_and_error_message = (e, error_message)+ try:+ if exception_and_error_message is None:+ verification_secret = response.json()["verification_secret"]+ except (json.JSONDecodeError, KeyError) as e:+ error_message = "An error occurred while parsing the response from the host."+ exception_and_error_message = (e, error_message)+if exception_and_error_message is not None:exception, error_message = exception_and_error_messagelogger.info("verify_registration_transfer:host:%s|exception:%s", hostname, exception)raise JsonableError(error_message)- data = response.json()- verification_secret = data["verification_secret"]validate_registration_transfer_verification_secret(verification_secret, hostname)logger.info("verify_registration_transfer:host:%s|success", hostname)