Repository navigation
fix: parse request bodies after RequestVerification #1630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
779746c
08d277b
06b6a16
e18f19b
286c38f
eb5e885
3caef8b
1a21eeb
5f69d8a
6bd06b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from .request_parsing import RequestParsing | ||
|
|
||
| __all__ = [ | ||
| "RequestParsing", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from typing import Awaitable, Callable | ||
|
|
||
| from slack_bolt.middleware.async_middleware import AsyncMiddleware | ||
| from slack_bolt.middleware.request_parsing.request_parsing import attach_team_id_to_client | ||
| from slack_bolt.request.async_internals import build_async_context | ||
| from slack_bolt.request.async_request import AsyncBoltRequest | ||
| from slack_bolt.request.internals import parse_body | ||
| from slack_bolt.response import BoltResponse | ||
|
|
||
|
|
||
| class AsyncRequestParsing(AsyncMiddleware): | ||
| async def async_process( | ||
| self, | ||
| *, | ||
| req: AsyncBoltRequest, | ||
| resp: BoltResponse, | ||
| next: Callable[[], Awaitable[BoltResponse]], | ||
| ) -> BoltResponse: | ||
| if req.raw_body: | ||
| req.body = parse_body(req.raw_body, req.content_type) | ||
| set_before_parsing = dict(req.context) | ||
| build_async_context(req.context, req.body) | ||
| req.context.update(set_before_parsing) | ||
| attach_team_id_to_client(req.context) | ||
| return await next() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from typing import Callable | ||
|
|
||
| from slack_bolt.context.base_context import BaseContext | ||
| from slack_bolt.middleware.middleware import Middleware | ||
| from slack_bolt.request import BoltRequest | ||
| from slack_bolt.request.internals import build_context, parse_body | ||
| from slack_bolt.response import BoltResponse | ||
|
|
||
|
|
||
| class RequestParsing(Middleware): | ||
| def process( | ||
| self, | ||
| *, | ||
| req: BoltRequest, | ||
| resp: BoltResponse, | ||
| next: Callable[[], BoltResponse], | ||
| ) -> BoltResponse: | ||
| if req.raw_body: | ||
| req.body = parse_body(req.raw_body, req.content_type) | ||
| set_before_parsing = dict(req.context) | ||
| build_context(req.context, req.body) | ||
| req.context.update(set_before_parsing) | ||
| attach_team_id_to_client(req.context) | ||
| return next() | ||
|
|
||
|
|
||
| def attach_team_id_to_client(context: BaseContext) -> None: | ||
| if context.team_id is not None and context.get("client") is not None: | ||
| context["client"].default_params["team_id"] = context.team_id | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| from logging import Logger | ||||||
| from typing import Callable, Optional | ||||||
| from typing import Callable, Dict, Optional | ||||||
| from urllib.parse import parse_qsl | ||||||
|
|
||||||
| from slack_bolt.logger import get_bolt_logger | ||||||
| from slack_bolt.middleware.middleware import Middleware | ||||||
|
|
@@ -38,15 +39,22 @@ def process( | |||||
| # only the internals of this method | ||||||
| next: Callable[[], BoltResponse], | ||||||
| ) -> BoltResponse: | ||||||
| if self._is_ssl_check_request(req.body): | ||||||
| if self._verify_token_if_needed(req.body): | ||||||
| form_params = self._parse_form_params(req.raw_body) | ||||||
| if self._is_ssl_check_request(form_params): | ||||||
| if self._verify_token_if_needed(form_params): | ||||||
| return self._build_error_response() | ||||||
| return self._build_success_response() | ||||||
| else: | ||||||
| return next() | ||||||
|
|
||||||
| # ----------------------------------------- | ||||||
|
|
||||||
| @staticmethod | ||||||
| def _parse_form_params(raw_body: str) -> Dict[str, str]: | ||||||
| if raw_body.startswith("{"): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (non-blocking): A JSON body with leading whitespace still gets split on
Suggested change
|
||||||
| return {} | ||||||
| return dict(parse_qsl(raw_body, keep_blank_values=True)) | ||||||
|
WilliamBergamin marked this conversation as resolved.
Resolved
|
||||||
|
|
||||||
| @staticmethod | ||||||
| def _is_ssl_check_request(body: dict): | ||||||
| return "ssl_check" in body and body["ssl_check"] == "1" | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,11 @@ | |
| from slack_bolt.error import BoltError | ||
| from slack_bolt.request.internals import ( | ||
| parse_query, | ||
| parse_body, | ||
| build_normalized_headers, | ||
| build_context, | ||
| extract_content_type, | ||
| error_message_raw_body_required_in_http_mode, | ||
| ) | ||
| from slack_bolt.util.utils import create_copy | ||
|
|
||
|
|
||
| class BoltRequest: | ||
|
|
@@ -59,24 +58,20 @@ def __init__( | |
| self.headers = build_normalized_headers(headers) | ||
| self.content_type = extract_content_type(self.headers) | ||
|
|
||
| if isinstance(body, str): | ||
| self.body = parse_body(self.raw_body, self.content_type) | ||
| elif isinstance(body, dict): | ||
| self.body = body | ||
| else: | ||
| self.body = {} | ||
|
|
||
| self.context = build_context(BoltContext(context if context else {}), self.body) | ||
| self.body = body if isinstance(body, dict) else {} | ||
|
WilliamBergamin marked this conversation as resolved.
Resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question (blocking): Your earlier thread on this got resolved, but the PR is still |
||
| self.context = BoltContext(context if context else {}) | ||
| self.lazy_only = bool(self.headers.get("x-slack-bolt-lazy-only", [False])[0]) | ||
| self.lazy_function_name = self.headers.get("x-slack-bolt-lazy-function-name", [None])[0] | ||
| self.mode = mode | ||
|
|
||
| def to_copyable(self) -> "BoltRequest": | ||
| body: Union[str, dict] = self.raw_body if self.mode == "http" else self.body | ||
| return BoltRequest( | ||
| copied = BoltRequest( | ||
| body=body, | ||
| query=self.query, | ||
| headers=self.headers, | ||
| context=self.context.to_copyable(), | ||
| mode=self.mode, | ||
| ) | ||
| copied.body = create_copy(self.body) | ||
| return copied | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from slack_bolt.middleware.request_parsing.async_request_parsing import AsyncRequestParsing | ||
| from slack_bolt.request.async_request import AsyncBoltRequest | ||
| from slack_bolt.response import BoltResponse | ||
|
|
||
|
|
||
| async def async_parse_request(req: AsyncBoltRequest) -> AsyncBoltRequest: | ||
| async def next_(): | ||
| return BoltResponse(status=200) | ||
|
|
||
| await AsyncRequestParsing().async_process(req=req, resp=BoltResponse(status=200), next=next_) | ||
| return req |
Uh oh!
There was an error while loading. Please reload this page.