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 8 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,31 @@ | ||
| 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): | ||
| """Parses the request body and builds the context from it. | ||
|
|
||
| Runs right after `AsyncRequestVerification`, so unsigned payloads are never parsed. | ||
| Values already in the context take precedence over the ones in the body. | ||
| """ | ||
|
|
||
| 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,35 @@ | ||
| 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): | ||
| """Parses the request body and builds the context from it. | ||
|
|
||
| Runs right after `RequestVerification`, so unsigned payloads are never parsed. | ||
| Values already in the context take precedence over the ones in the body. | ||
| """ | ||
|
|
||
| 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) | ||
|
WilliamBergamin marked this conversation as resolved.
Resolved
|
||
| 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 |
|---|---|---|
|
|
@@ -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: | ||
|
|
@@ -34,6 +33,9 @@ def __init__( | |
| ): | ||
| """Request to a Bolt app. | ||
|
|
||
| The body is parsed and the context is built from it when the app dispatches the request, | ||
| right after request verification, so unsigned payloads are never parsed. | ||
|
|
||
| Args: | ||
| body: The raw request body (only plain text is supported for "http" mode) | ||
| query: The query string data in any data format. | ||
|
|
@@ -59,24 +61,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.