Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions slack_sdk/oauth/authorize_url_generator/__init__.py
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:

@WilliamBergamin WilliamBergamin Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.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.
Comment on lines +6 to +7

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪓 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.

Suggested change
# ":", "/" 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=":/,")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚦 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?

Suggested change
return quote(value, safe=":/,")
return quote(str(value), safe=":/,")



class AuthorizeUrlGenerator:
Expand All @@ -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


Expand All @@ -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
49 changes: 49 additions & 0 deletions tests/slack_sdk/oauth/authorize_url_generator/test_generator.py
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

Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Parsing the URL back and checking env never leaks into its own parameter is exactly the test this bug needed. 🙌


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)
Loading