Repository navigation
fix(oauth): URL-encode the query values of the authorize URL #2082
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
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,11 @@ | ||||||
| from typing import Optional, Sequence | ||||||
| from urllib.parse import quote | ||||||
|
|
||||||
|
|
||||||
| 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. | ||||||
|
Comment on lines
+6
to
+7
Contributor
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): Could we drop this comment? The
Suggested change
|
||||||
| return quote(value, safe=":/,") | ||||||
|
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. praise: Keeping
Contributor
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. 🚦 issue (blocking):
Suggested change
|
||||||
|
|
||||||
|
|
||||||
| class AuthorizeUrlGenerator: | ||||||
|
|
@@ -20,11 +27,17 @@ def __init__( | |||||
| def generate(self, state: str, team: Optional[str] = None) -> str: | ||||||
| scopes = ",".join(self.scopes) if self.scopes else "" | ||||||
| user_scopes = ",".join(self.user_scopes) if self.user_scopes else "" | ||||||
| url = f"{self.authorization_url}?state={state}&client_id={self.client_id}&scope={scopes}&user_scope={user_scopes}" | ||||||
| url = ( | ||||||
| f"{self.authorization_url}?" | ||||||
| f"state={_encode(state)}&" | ||||||
| f"client_id={_encode(self.client_id)}&" | ||||||
| f"scope={_encode(scopes)}&" | ||||||
| f"user_scope={_encode(user_scopes)}" | ||||||
| ) | ||||||
| if self.redirect_uri is not None: | ||||||
| url += f"&redirect_uri={self.redirect_uri}" | ||||||
| url += f"&redirect_uri={_encode(self.redirect_uri)}" | ||||||
| if team is not None: | ||||||
| url += f"&team={team}" | ||||||
| url += f"&team={_encode(team)}" | ||||||
| return url | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -49,13 +62,13 @@ def generate(self, state: str, nonce: Optional[str] = None, team: Optional[str] | |||||
| url = ( | ||||||
| f"{self.authorization_url}?" | ||||||
| "response_type=code&" | ||||||
| f"state={state}&" | ||||||
| f"client_id={self.client_id}&" | ||||||
| f"scope={scopes}&" | ||||||
| f"redirect_uri={self.redirect_uri}" | ||||||
| f"state={_encode(state)}&" | ||||||
| f"client_id={_encode(self.client_id)}&" | ||||||
| f"scope={_encode(scopes)}&" | ||||||
| f"redirect_uri={_encode(self.redirect_uri)}" | ||||||
| ) | ||||||
| if team is not None: | ||||||
| url += f"&team={team}" | ||||||
| url += f"&team={_encode(team)}" | ||||||
| if nonce is not None: | ||||||
| url += f"&nonce={nonce}" | ||||||
| url += f"&nonce={_encode(nonce)}" | ||||||
| return url | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import unittest | ||
| from urllib.parse import parse_qs, urlparse | ||
|
|
||
| from slack_sdk.oauth import AuthorizeUrlGenerator, OpenIDConnectAuthorizeUrlGenerator | ||
|
|
||
|
|
@@ -70,3 +71,51 @@ def test_openid_connect(self): | |
| "&nonce=nnn" | ||
| ) | ||
| self.assertEqual(expected, url) | ||
|
|
||
| def test_redirect_uri_with_query_string(self): | ||
| redirect_uri = "https://www.example.com/slack/oauth_redirect?tenant=acme&env=prod" | ||
| generator = AuthorizeUrlGenerator( | ||
| client_id="111.222", | ||
| scopes=["chat:write"], | ||
| redirect_uri=redirect_uri, | ||
| ) | ||
| url = generator.generate("state-value") | ||
| expected = ( | ||
| "https://slack.com/oauth/v2/authorize" | ||
| "?state=state-value" | ||
| "&client_id=111.222" | ||
| "&scope=chat:write" | ||
| "&user_scope=" | ||
| "&redirect_uri=https://www.example.com/slack/oauth_redirect%3Ftenant%3Dacme%26env%3Dprod" | ||
| ) | ||
| self.assertEqual(expected, url) | ||
|
|
||
| params = parse_qs(urlparse(url).query, keep_blank_values=True) | ||
| self.assertEqual([redirect_uri], params["redirect_uri"]) | ||
| self.assertNotIn("env", params) | ||
|
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. praise: Parsing the URL back and checking |
||
|
|
||
| def test_state_and_team_with_reserved_characters(self): | ||
| generator = AuthorizeUrlGenerator(client_id="111.222", scopes=["chat:write"]) | ||
| url = generator.generate(state="a+b/c==", team="T1&x=1") | ||
|
|
||
| params = parse_qs(urlparse(url).query, keep_blank_values=True) | ||
| self.assertEqual(["a+b/c=="], params["state"]) | ||
| self.assertEqual(["T1&x=1"], params["team"]) | ||
| self.assertNotIn("x", params) | ||
|
|
||
| def test_openid_connect_with_reserved_characters(self): | ||
| redirect_uri = "https://www.example.com/oidc/callback?tenant=acme&env=prod" | ||
| generator = OpenIDConnectAuthorizeUrlGenerator( | ||
| client_id="111.222", | ||
| redirect_uri=redirect_uri, | ||
| scopes=["openid"], | ||
| ) | ||
| url = generator.generate(state="a+b/c==", nonce="n+1&x=1", team="T12345") | ||
|
|
||
| params = parse_qs(urlparse(url).query, keep_blank_values=True) | ||
| self.assertEqual([redirect_uri], params["redirect_uri"]) | ||
| self.assertEqual(["a+b/c=="], params["state"]) | ||
| self.assertEqual(["n+1&x=1"], params["nonce"]) | ||
| self.assertEqual(["T12345"], params["team"]) | ||
| self.assertNotIn("env", params) | ||
| self.assertNotIn("x", params) | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 suggestion (blocking): Could
urllib.parse.urlencodebuild the whole query, likeWebClientdoes inbase_client.py? It encodes and joins in one call, and already callsstr()on non-str values.quote_via=quotekeeps spaces as%20.