Skip to content

fix(oauth): URL-encode the query values of the authorize URL - #2082

Open
omkarbandikatte wants to merge 1 commit into
slackapi:mainfrom
omkarbandikatte:fix/authorize-url-encoding
Open

omkarbandikatte wants to merge 1 commit into
slackapi:mainfrom
omkarbandikatte:fix/authorize-url-encoding

Conversation

@omkarbandikatte

Copy link
Copy Markdown

Summary

Fixes #2081.

AuthorizeUrlGenerator.generate() and OpenIDConnectAuthorizeUrlGenerator.generate() wrote state, redirect_uri, team, nonce and the other values into the query string without URL-encoding them. A redirect_uri with its own query string was cut at the first & (the rest became a separate parameter of the authorize request), and a + in state was 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 in test_generator.py pass unchanged.

Testing

Three new tests in tests/slack_sdk/oauth/authorize_url_generator/test_generator.py:

  • test_redirect_uri_with_query_string
  • test_state_and_team_with_reserved_characters
  • test_openid_connect_with_reserved_characters

Each parses the generated URL and checks that every value comes back as it was passed in. All three fail on main and pass with this change.

./scripts/run_tests.sh tests/slack_sdk/oauth/authorize_url_generator/test_generator.py

I ran the steps of ./scripts/run_validation.sh locally on Python 3.13: codegen (no changes), ruff format --check, ruff check, and the full tests/ suite (1087 passed, 5 skipped). mypy passes on slack_sdk/oauth.

Category

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs (Documents)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I have signed the Salesforce CLA (required before your PR can be reviewed or merged).
  • I've run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

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
@omkarbandikatte
omkarbandikatte requested a review from a team as a code owner November 24, 2026 17:53
@mwbrooks mwbrooks added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch oauth labels Nov 24, 2026
@mwbrooks mwbrooks added this to the 3.48.1 milestone Nov 24, 2026

@mwbrooks mwbrooks left a comment

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.

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

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 _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=":/,")

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.

@mwbrooks

Copy link
Copy Markdown
Member

🏃 CI is running now: https://github.com/slackapi/python-slack-sdk/actions/runs/45453560784

I'll follow up here once it finishes.

@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.70%. Comparing base (80f2f22) to head (f3f6570).
✅ All tests successful. No failed tests found.

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.
📢 Have feedback on the report? Share it here.

@mwbrooks

Copy link
Copy Markdown
Member

✅ 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 mwbrooks left a comment

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.

✅ Thanks again @omkarbandikatte, the encoding fix and the round-trip tests look great. Approving!

@omkarbandikatte

Copy link
Copy Markdown
Author

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 WilliamBergamin left a comment

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.

Temporarily blocking this just to figure out if there is another approach or python utility we can use here

@WilliamBergamin WilliamBergamin left a comment

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.

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

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

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)}"

Comment on lines +6 to +7
# ":", "/" and "," are valid in a query component and are kept as they are,
# so URLs built from plain values stay the same as before.

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.

@WilliamBergamin WilliamBergamin left a comment •

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.

🧩 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 _encode helper, its comment, and the manual &/= joining all go away.
  • It calls str() on non-str values, so it also fixes the TypeError from my earlier comment.
  • With safe=":/," and quote_via=quote, today's URLs come out byte for byte the same.
    Details and a code sample are on the _encode comment 🙏

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented cla:signed oauth semver:patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AuthorizeUrlGenerator does not URL-encode redirect_uri, state and other query values

3 participants