Repository navigation
fix: parse request bodies after RequestVerification - #1630
WilliamBergamin wants to merge 10 commits into
Conversation
Unsigned bodies that are invalid JSON, a JSON array, or oddly shaped crash BoltRequest construction before RequestVerification can return 401. Refs #1447 Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
SslCheck runs before RequestVerification, so it should not depend on the fully parsed request body. ssl_check requests are form encoded, and parse_qsl never raises on untrusted input. Refs #1447 Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
A request created with defer_parsing=True keeps its raw body unparsed and its context unbuilt. The new built-in RequestParsing middleware completes both right after RequestVerification, so unsigned payloads are never parsed. Context values set after the request was created still win, and the per-request client gets the team_id it could not see earlier. The default stays eager, so code that builds a BoltRequest directly is unchanged. Refs #1447 Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Every HTTP adapter and the built-in dev servers pass defer_parsing=True where they hand the request to dispatch. The public to_bolt_request helpers keep eager parsing by default, and OAuth and Socket Mode paths are unchanged. Fixes #1447 Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
SignatureVerifier.is_valid raises on a non-numeric timestamp or a non-ASCII signature, which turned unsigned junk into a 500. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1630 +/- ##
==========================================
+ Coverage 91.57% 91.63% +0.06%
==========================================
Files 228 231 +3
Lines 7287 7332 +45
==========================================
+ Hits 6673 6719 +46
+ Misses 614 613 -1 ☔ View full report in Codecov by Harness. |
BoltRequest and AsyncBoltRequest no longer parse the body or build the context at construction. The RequestParsing middleware, which runs right after RequestVerification, is now the only parser, so every request that reaches dispatch is protected and the defer_parsing opt-in, its adapter plumbing, and its docs are gone. A request that is read without being dispatched now has an empty body; tests that do so run RequestParsing through a small helper. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
It is independent of parsing after verification. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
WilliamBergamin
left a comment
There was a problem hiding this comment.
🧩 Moving parsing into one middleware right after RequestVerification is the right seam, and the test that dispatches 41 signed payloads and compares them against parsing before dispatch is a great way to show nothing changed.
One blocking bug in SslCheck and one blocking semver question, the rest are small simplifications 🙏
parse_qsl split JSON event bodies on "&", so a signed event whose text contained "&ssl_check=1&" was answered as an ssl_check and never reached its listener. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
They claimed unsigned payloads are never parsed, which is false with request verification disabled; test_parsing_without_request_verification pins the real behavior. Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
mwbrooks
left a comment
There was a problem hiding this comment.
Great direction @WilliamBergamin, verifying before parsing is the right seam for #1447. I'm holding the approval on the semver question below, since this changes what BoltRequest gives callers before dispatch. Small follow-up too: the pipeline list in AGENTS.md could gain the new Request Parsing step.
| self.body = {} | ||
|
|
||
| self.context = build_context(BoltContext(context if context else {}), self.body) | ||
| self.body = body if isinstance(body, dict) else {} |
There was a problem hiding this comment.
question (blocking): Your earlier thread on this got resolved, but the PR is still semver:minor while the release notes list BoltRequest(body=raw).body == {} before dispatch. Are we shipping this as a minor on the security-fix grounds, or should it carry the next major? Either works for me once the call and a migration note are in the release notes.
|
|
||
| @staticmethod | ||
| def _parse_form_params(raw_body: str) -> Dict[str, str]: | ||
| if raw_body.startswith("{"): |
There was a problem hiding this comment.
suggestion (non-blocking): A JSON body with leading whitespace still gets split on & here, so a link URL with ssl_check=1 returns a 200 before its listener runs. Stripping first keeps this in step with JSON detection elsewhere.
| if raw_body.startswith("{"): | |
| if raw_body.lstrip().startswith("{"): |
Summary
Fixes #1447. Bolt parsed the body and built the context in
BoltRequest.__init__, beforeRequestVerificationran, so unsigned scanner traffic could crash an app before it was rejected with a 401. As discussed when closing #1586, this moves parsing after verification instead of hardening the extractors further.BoltRequestandAsyncBoltRequestno longer parse anything. The new built-inRequestParsingmiddleware runs right afterRequestVerification(even when verification is disabled) and is the only place where the body is parsed and the context is built from it.dispatch, so they are all covered.SslCheckreadsssl_checkfrom the raw form body.Release notes:
BoltRequestthat is read without being dispatched (for example in a unit test of a custom matcher or middleware) now hasbody == {}and a context that wasn't built from the body. Dispatch it, or runRequestParsingon it.context=now take precedence over values from the body.RequestVerification._can_skip(mode, body)always receives{}asbody. A subclass that overrides_init_middleware_listwithout callingsuper()no longer gets parsing.Testing
tests/scenario_tests{,_async}/test_request_parsing.pydispatches 41 signed payloads and asserts the samecontext,bodyandclient.default_paramsas a request parsed before dispatch. It also covers unsigned malformed bodies (401), adapter-set context winning over the body, a lazy listener readingbody,to_copyable, and ssl_check.tests/adapter_tests/aws/test_aws_lambda.pyruns the PoC fromextract_team_idcrashes withTypeError when payload["user"]is a string (e.g.member_joined_channelevents) #1447 through the Lambda handler. It fails onmainand passes here../scripts/run_tests.sh: 976 passed. Lint, format and mypy are clean.Category
slack_bolt.Appand/or its core componentsslack_bolt.async_app.AsyncAppand/or its core componentsslack_bolt.adapter/docsRequirements
./scripts/install_all_and_run_tests.shafter making the changes.🤖 Generated with Claude Code