From 139fb8621d86b87fc1f72c1843ae2adc53e6e6ed Mon Sep 17 00:00:00 2001 From: Satyam Bansal Date: Sun, 14 May 2023 13:05:26 +0530 Subject: [PATCH] webhooks: Strengthen types using WildValue in JSON Integration. --- zerver/webhooks/json/view.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/zerver/webhooks/json/view.py b/zerver/webhooks/json/view.py index 2e9a85e192a36..a45ed86e4d635 100644 --- a/zerver/webhooks/json/view.py +++ b/zerver/webhooks/json/view.py @@ -1,11 +1,15 @@ import json -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_anything, + to_wild_value, +) from zerver.lib.webhooks.common import check_send_webhook_message from zerver.models import UserProfile @@ -21,7 +25,7 @@ def api_json_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: body = get_body_for_http_request(payload) subject = get_subject_for_http_request(payload) @@ -30,10 +34,10 @@ def api_json_webhook( return json_success(request) -def get_subject_for_http_request(payload: Dict[str, Any]) -> str: +def get_subject_for_http_request(payload: WildValue) -> str: return "JSON" -def get_body_for_http_request(payload: Dict[str, Any]) -> str: - prettypayload = json.dumps(payload, indent=2) +def get_body_for_http_request(payload: WildValue) -> str: + prettypayload = json.dumps(payload.tame(check_anything), indent=2) return JSON_MESSAGE_TEMPLATE.format(webhook_payload=prettypayload, sort_keys=True)