Repository navigation
fix(oauth): encode authorization URL parameters #1981
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
Open
fallintoplace
wants to merge
2
commits into
slackapi:main
Choose a base branch
from
fallintoplace:fix/oauth-authorize-url-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import unittest | ||
| from urllib.parse import parse_qs, urlsplit | ||
|
|
||
| from slack_sdk.oauth import AuthorizeUrlGenerator, OpenIDConnectAuthorizeUrlGenerator | ||
|
|
||
|
|
@@ -70,3 +71,83 @@ def test_openid_connect(self): | |
| "&nonce=nnn" | ||
| ) | ||
| self.assertEqual(expected, url) | ||
|
|
||
| def test_query_parameters_round_trip(self): | ||
| redirect_uri = "https://www.example.com/callback?view=home&lang=en" | ||
| generator = AuthorizeUrlGenerator( | ||
| client_id="111.222", | ||
| redirect_uri=redirect_uri, | ||
| scopes=["chat:write", "commands"], | ||
| user_scopes=["search:read"], | ||
| ) | ||
| for state in ( | ||
| "", | ||
| "plus+value", | ||
| "space value", | ||
| "key=value&other=value", | ||
| "percent%20value", | ||
| "fragment#value", | ||
| "日本語", | ||
| ): | ||
| with self.subTest(state=state): | ||
| url = generator.generate(state=state, team="T12345") | ||
| self.assertDictEqual( | ||
| { | ||
| "state": [state], | ||
| "client_id": ["111.222"], | ||
| "scope": ["chat:write,commands"], | ||
| "user_scope": ["search:read"], | ||
| "redirect_uri": [redirect_uri], | ||
| "team": ["T12345"], | ||
| }, | ||
| parse_qs(urlsplit(url).query, keep_blank_values=True), | ||
| ) | ||
|
|
||
| def test_openid_connect_query_parameters_round_trip(self): | ||
|
Comment on lines
+75
to
+106
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. thank you for adding tests! |
||
| redirect_uri = "https://www.example.com/oidc/callback?view=home%20page&lang=en" | ||
| generator = OpenIDConnectAuthorizeUrlGenerator( | ||
| client_id="111.222", | ||
| redirect_uri=redirect_uri, | ||
| scopes=["openid", "profile"], | ||
| ) | ||
| for value in ( | ||
| "", | ||
| "plus+value", | ||
| "space value", | ||
| "key=value&other=value", | ||
| "percent%20value", | ||
| "fragment#value", | ||
| "日本語", | ||
| ): | ||
| with self.subTest(value=value): | ||
| state = f"state-{value}" | ||
| url = generator.generate(state=state, nonce=value, team="T12345") | ||
| self.assertDictEqual( | ||
| { | ||
| "response_type": ["code"], | ||
| "state": [state], | ||
| "client_id": ["111.222"], | ||
| "scope": ["openid,profile"], | ||
| "redirect_uri": [redirect_uri], | ||
| "team": ["T12345"], | ||
| "nonce": [value], | ||
| }, | ||
| parse_qs(urlsplit(url).query, keep_blank_values=True), | ||
| ) | ||
|
|
||
| def test_pre_encoded_authorization_values_are_not_decoded(self): | ||
| redirect_uri = "https://www.example.com/callback%3Fa%3Db" | ||
| generator = AuthorizeUrlGenerator( | ||
| client_id="111.222", redirect_uri=redirect_uri | ||
| ) | ||
| url = generator.generate(state="abc%2B") | ||
| self.assertIn("state=abc%252B", url) | ||
| self.assertIn("redirect_uri=https://www.example.com/callback%253Fa%253Db", url) | ||
|
|
||
| openid_generator = OpenIDConnectAuthorizeUrlGenerator( | ||
| client_id="111.222", redirect_uri=redirect_uri | ||
| ) | ||
| url = openid_generator.generate(state="abc%2B", nonce="x%26y") | ||
| self.assertIn("state=abc%252B", url) | ||
| self.assertIn("redirect_uri=https://www.example.com/callback%253Fa%253Db", url) | ||
| self.assertIn("nonce=x%2526y", url) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this may change behavior for apps that were already percent-encoding these values themselves. before this change, that was the only way to pass a redirect_uri with a query string, or a state containing + or &. those apps will
now get the values encoded twice:
we should document this in the pr description!