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.

airbrake: Strengthen types using WildValue.

Anders Kaseorgac9a5ba894dc
diff --git a/zerver/tests/test_integrations_dev_panel.py b/zerver/tests/test_integrations_dev_panel.py
index a1d859be82e..37e04213417 100644
--- a/zerver/tests/test_integrations_dev_panel.py
+++ b/zerver/tests/test_integrations_dev_panel.py
@@ -14,7 +14,7 @@ class TestIntegrationsDevPanel(ZulipTestCase):
bot = get_user("[email protected]", self.zulip_realm)
url = f"/api/v1/external/airbrake?api_key={bot.api_key}"
target_url = "/devtools/integrations/check_send_webhook_fixture_message"
- body = "{}" # This empty body should generate a KeyError on the webhook code side.
+ body = "{}" # This empty body should generate a ValidationError on the webhook code side.
data = {
"url": url,
@@ -29,9 +29,9 @@ class TestIntegrationsDevPanel(ZulipTestCase):
expected_response = {"result": "error", "msg": "Internal server error"}
self.assertEqual(orjson.loads(response.content), expected_response)
- # Intention of this test looks like to trigger keyError
- # so just testing KeyError is printed along with Traceback in logs
- self.assertTrue("KeyError" in logs.output[0])
+ # Intention of this test looks like to trigger ValidationError
+ # so just testing ValidationError is printed along with Traceback in logs
+ self.assertTrue("ValidationError" in logs.output[0])
self.assertTrue("Traceback (most recent call last)" in logs.output[0])
self.assertEqual(
logs.output[1], "ERROR:django.request:Internal Server Error: /api/v1/external/airbrake"
diff --git a/zerver/webhooks/airbrake/view.py b/zerver/webhooks/airbrake/view.py
index 62b5e18fe80..f49c685e649 100644
--- a/zerver/webhooks/airbrake/view.py
+++ b/zerver/webhooks/airbrake/view.py
@@ -1,11 +1,10 @@
# Webhooks for external integrations.
-from typing import Any, Dict
-
from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
+from zerver.lib.validator import WildValue, check_string, to_wild_value
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
@@ -18,7 +17,7 @@ AIRBRAKE_MESSAGE_TEMPLATE = '[{error_class}]({error_url}): "{error_message}" occ
def api_airbrake_webhook(
request: HttpRequest,
user_profile: UserProfile,
- payload: Dict[str, Any] = REQ(argument_type="body"),
+ payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
) -> HttpResponse:
subject = get_subject(payload)
body = get_body(payload)
@@ -26,14 +25,16 @@ def api_airbrake_webhook(
return json_success(request)
-def get_subject(payload: Dict[str, Any]) -> str:
- return AIRBRAKE_TOPIC_TEMPLATE.format(project_name=payload["error"]["project"]["name"])
+def get_subject(payload: WildValue) -> str:
+ return AIRBRAKE_TOPIC_TEMPLATE.format(
+ project_name=payload["error"]["project"]["name"].tame(check_string)
+ )
-def get_body(payload: Dict[str, Any]) -> str:
+def get_body(payload: WildValue) -> str:
data = {
- "error_url": payload["airbrake_error_url"],
- "error_class": payload["error"]["error_class"],
- "error_message": payload["error"]["error_message"],
+ "error_url": payload["airbrake_error_url"].tame(check_string),
+ "error_class": payload["error"]["error_class"].tame(check_string),
+ "error_message": payload["error"]["error_message"].tame(check_string),
}
return AIRBRAKE_MESSAGE_TEMPLATE.format(**data)