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.

slack_incoming: Process request payload directly in view.py.

Christopher Chongb6f6f6db8d70
diff --git a/zerver/webhooks/slack_incoming/tests.py b/zerver/webhooks/slack_incoming/tests.py
index 2acaa5e345b..acc52cee2c7 100644
--- a/zerver/webhooks/slack_incoming/tests.py
+++ b/zerver/webhooks/slack_incoming/tests.py
@@ -37,6 +37,15 @@ Hello, world.
content_type="application/x-www-form-urlencoded",
)
+ def test_message_without_payload(self) -> None:
+ self.url = self.build_webhook_url()
+ result = self.client_post(
+ self.url,
+ None, # type: ignore[arg-type] # we need to simulate a http request that doesn't send any payload
+ content_type="multipart/form-data; boundary=14537ad40cab4c77b6699c9c0fa9c82f",
+ )
+ self.assert_json_error(result, "Missing 'payload' argument")
+
def test_message_with_actions(self) -> None:
expected_topic = "C1H9RESGL"
expected_message = """
diff --git a/zerver/webhooks/slack_incoming/view.py b/zerver/webhooks/slack_incoming/view.py
index 290f29e8007..cf85ab66af3 100644
--- a/zerver/webhooks/slack_incoming/view.py
+++ b/zerver/webhooks/slack_incoming/view.py
@@ -7,10 +7,9 @@ from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
from zerver.decorator import webhook_view
-from zerver.lib.exceptions import InvalidJSONError
-from zerver.lib.request import REQ, has_request_variables
+from zerver.lib.exceptions import InvalidJSONError, JsonableError
+from zerver.lib.request import REQ, RequestVariableMissingError, has_request_variables
from zerver.lib.response import json_success
-from zerver.lib.validator import check_dict
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
@@ -21,19 +20,28 @@ def api_slack_incoming_webhook(
request: HttpRequest,
user_profile: UserProfile,
user_specified_topic: Optional[str] = REQ("topic", default=None),
- payload: Optional[Dict[str, Any]] = REQ("payload", json_validator=check_dict(), default=None),
) -> HttpResponse:
# Slack accepts webhook payloads as payload="encoded json" as
# application/x-www-form-urlencoded, as well as in the body as
- # application/json. We use has_request_variables to try to get
- # the form encoded version, and parse the body out ourselves if
- # # we were given JSON.
- if payload is None:
+ # application/json.
+ if request.content_type == "application/json":
try:
- payload = orjson.loads(request.body)
- except orjson.JSONDecodeError: # nocoverage
- raise InvalidJSONError(_("Malformed JSON"))
+ val = request.body.decode(request.encoding or "utf-8")
+ except UnicodeDecodeError: # nocoverage
+ raise JsonableError(_("Malformed payload"))
+ else:
+ req_var = "payload"
+ if req_var in request.POST:
+ val = request.POST[req_var]
+ elif req_var in request.GET: # nocoverage
+ val = request.GET[req_var]
+ else:
+ raise RequestVariableMissingError(req_var)
+ try:
+ payload = orjson.loads(val)
+ except orjson.JSONDecodeError: # nocoverage
+ raise InvalidJSONError(_("Malformed JSON"))
if user_specified_topic is None and "channel" in payload:
user_specified_topic = re.sub("^[@#]", "", payload["channel"])