Repository navigation
fix(oauth): URL-encode the query values of the authorize URL - #2082
omkarbandikatte wants to merge 1 commit into
Conversation
AuthorizeUrlGenerator and OpenIDConnectAuthorizeUrlGenerator wrote state, redirect_uri, team, nonce and the other values into the query string as they were. A redirect_uri with its own query string was cut at the first "&", and a "+" in state was read back as a space. Percent-encode each value. ":", "/" and "," are kept as they are, so the URLs generated for values without reserved characters do not change. Fixes slackapi#2081
mwbrooks
left a comment
There was a problem hiding this comment.
✅ Thanks @omkarbandikatte! 🎉 Congrats on your first contribution to python-slack-sdk!
The fix and tests look solid to me, so CI is up next.
|
|
||
| params = parse_qs(urlparse(url).query, keep_blank_values=True) | ||
| self.assertEqual([redirect_uri], params["redirect_uri"]) | ||
| self.assertNotIn("env", params) |
There was a problem hiding this comment.
praise: Parsing the URL back and checking env never leaks into its own parameter is exactly the test this bug needed. 🙌
| def _encode(value: str) -> str: | ||
| # ":", "/" and "," are valid in a query component and are kept as they are, | ||
| # so URLs built from plain values stay the same as before. | ||
| return quote(value, safe=":/,") |
There was a problem hiding this comment.
praise: Keeping :, /, and , safe means every URL built from plain values comes out byte for byte the same, which keeps this patch-safe for existing apps.
|
🏃 CI is running now: https://github.com/slackapi/python-slack-sdk/actions/runs/45453560784 I'll follow up here once it finishes. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2082 +/- ##
==========================================
+ Coverage 84.69% 84.70% +0.01%
==========================================
Files 118 118
Lines 14116 14119 +3
==========================================
+ Hits 11955 11959 +4
+ Misses 2161 2160 -1 ☔ View full report in Codecov by Harness. |
|
✅ CI is green across every Python version, lint, and typecheck, with codecov passing too. Nothing for you to address. I'll follow up with the approval shortly. |
mwbrooks
left a comment
There was a problem hiding this comment.
✅ Thanks again @omkarbandikatte, the encoding fix and the round-trip tests look great. Approving!
|
Thanks for the quick review and the kind words, @mwbrooks! Glad the round-trip tests were useful. Happy to follow up if anything else comes up before the 3.48.1 release. |
WilliamBergamin
left a comment
There was a problem hiding this comment.
Temporarily blocking this just to figure out if there is another approach or python utility we can use here
WilliamBergamin
left a comment
There was a problem hiding this comment.
Hi @omkarbandikatte, thanks for your contribution, and congrats on your first PR here 🎉 The round-trip tests pin #2081 exactly.
One blocking comment: non-str values now raise TypeError. The rest are optional 🙏
| def _encode(value: str) -> str: | ||
| # ":", "/" and "," are valid in a query component and are kept as they are, | ||
| # so URLs built from plain values stay the same as before. | ||
| return quote(value, safe=":/,") |
There was a problem hiding this comment.
🚦 issue (blocking): quote() only takes str or bytes, so calls that worked before now raise TypeError, like generate(state=uuid.uuid4()) or an int client_id. Could we keep the old str() coercion, and add a test passing a UUID state?
| return quote(value, safe=":/,") | |
| return quote(str(value), safe=":/,") |
| from urllib.parse import quote | ||
|
|
||
|
|
||
| def _encode(value: str) -> str: |
There was a problem hiding this comment.
🧩 suggestion (blocking): Could urllib.parse.urlencode build the whole query, like WebClient does in base_client.py? It encodes and joins in one call, and already calls str() on non-str values. quote_via=quote keeps spaces as %20.
params = {"state": state, "client_id": self.client_id, "scope": scopes, "user_scope": user_scopes}
if self.redirect_uri is not None:
params["redirect_uri"] = self.redirect_uri
if team is not None:
params["team"] = team
return f"{self.authorization_url}?{urlencode(params, safe=':/,', quote_via=quote)}"| # ":", "/" and "," are valid in a query component and are kept as they are, | ||
| # so URLs built from plain values stay the same as before. |
There was a problem hiding this comment.
🪓 suggestion (non-blocking): Could we drop this comment? The safe argument and the existing exact-URL tests already show that plain values stay the same.
| # ":", "/" and "," are valid in a query component and are kept as they are, | |
| # so URLs built from plain values stay the same as before. |
There was a problem hiding this comment.
🧩 Quick follow-up @omkarbandikatte: I'm making the urlencode suggestion blocking, for a few reasons:
- It's the standard library tool for this, and the SDK already uses it in
WebClient(base_client.py), so there's one way to build query strings in the SDK. - It encodes and joins the whole query in one call, so the
_encodehelper, its comment, and the manual&/=joining all go away. - It calls
str()on non-str values, so it also fixes theTypeErrorfrom my earlier comment. - With
safe=":/,"andquote_via=quote, today's URLs come out byte for byte the same.
Details and a code sample are on the_encodecomment 🙏
Summary
Fixes #2081.
AuthorizeUrlGenerator.generate()andOpenIDConnectAuthorizeUrlGenerator.generate()wrotestate,redirect_uri,team,nonceand the other values into the query string without URL-encoding them. Aredirect_uriwith its own query string was cut at the first&(the rest became a separate parameter of the authorize request), and a+instatewas read back as a space.Each value is now percent-encoded.
:,/and,are valid in a query component and are kept as they are, so the URL generated for values without reserved characters is the same as before. The four existing tests intest_generator.pypass unchanged.Testing
Three new tests in
tests/slack_sdk/oauth/authorize_url_generator/test_generator.py:test_redirect_uri_with_query_stringtest_state_and_team_with_reserved_characterstest_openid_connect_with_reserved_charactersEach parses the generated URL and checks that every value comes back as it was passed in. All three fail on
mainand pass with this change.I ran the steps of
./scripts/run_validation.shlocally on Python 3.13: codegen (no changes),ruff format --check,ruff check, and the fulltests/suite (1087 passed, 5 skipped).mypypasses onslack_sdk/oauth.Category
/docs(Documents)/tutorial(PythOnBoardingBot tutorial)tests/integration_tests(Automated tests for this library)Requirements
python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.shafter making the changes.