Skip to content

fix(oauth): return copies from the cacheable installation stores - #2091

Merged
WilliamBergamin merged 2 commits into
mainfrom
fix/cacheable-store-copy
Dec 1, 2026
Merged

WilliamBergamin merged 2 commits into
mainfrom
fix/cacheable-store-copy

Conversation

@WilliamBergamin

@WilliamBergamin WilliamBergamin commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This pull request makes the cacheable installation stores (sync and async) return a copy of each cached bot or installation, so a caller that changes the result no longer changes the cache.

  • Cause: find_bot() and find_installation() returned the cached object itself. Bolt's authorize.py sets user_token = None on the installation when the requester isn't the installer, which wiped the installer's user token from the cache for every later lookup.
  • Fix: both stores cache a copy on a miss and return a copy on a hit, through a shared _copy_model() in installation_store/internals.py.
  • Copy depth: it's a shallow copy with fresh list and dict attributes, so scope lists and custom_values come back as their own copies, at about a quarter of deepcopy's cost. A nested value inside custom_values is still shared: custom values are app-defined and may not be copyable, and a deep copy would run on every cached lookup.

Testing

The new sync and async tests fail on main and pass here. Each one changes the result of a cache miss and a cache hit (an attribute, a scope list, a custom value), then checks that the next lookup is unchanged.

To check by hand, wrap a FileInstallationStore in CacheableInstallationStore and save an installation with a user token. Call find_installation(), set user_token = None on the result, then call find_installation() again: the user token is still there.

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.

🤖 Generated with Claude Code

The cacheable stores handed out the cached Bot/Installation itself, so a
caller that changed the result changed the cache. Bolt's authorize clears
user_token on the installation when the requester isn't the installer,
which wiped the installer's user token for every later lookup.

Both stores now cache a copy on a miss and return a copy on a hit. The
copy is shallow with fresh list and dict attributes, so scope lists and
custom_values are independent too.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@WilliamBergamin WilliamBergamin added this to the 3.48.1 milestone Dec 1, 2026
@WilliamBergamin WilliamBergamin added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch oauth labels Dec 1, 2026
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.95%. Comparing base (df953d1) to head (bd35730).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2091      +/-   ##
==========================================
- Coverage   84.97%   84.95%   -0.03%     
==========================================
  Files         118      118              
  Lines       14232    14287      +55     
==========================================
+ Hits        12094    12137      +43     
- Misses       2138     2150      +12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@WilliamBergamin
WilliamBergamin marked this pull request as ready for review December 1, 2026 16:13
@WilliamBergamin
WilliamBergamin requested a review from a team as a code owner December 1, 2026 16:13

@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 @WilliamBergamin, copying on both the miss and the hit means Bolt clearing user_token for a non-installer can't wipe it for everyone else, and the tests that fail on main prove it.

duplicate = copy(value)
for name, field in vars(value).items():
if isinstance(field, (list, dict)):
setattr(duplicate, name, copy(field))

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.

suggestion (non-blocking): A nested value inside custom_values (a dict of settings) is still shared with the cache, since this copies one level deep. Could we deepcopy just custom_values? It is usually small and that would close the last gap, here and in the async store.

@zimeg zimeg 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.

🐍 @WilliamBergamin LGTM! Copying on both the miss and the hit keeps one caller's changes out of every later lookup. Thanks for tracking this down 🔐

@WilliamBergamin WilliamBergamin left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🧪 Deep review pass: the miss and hit tests pin exactly the user_token wipe Bolt's authorize caused, and they fail on main.
Left a few comments, nothing blocking 💯

from slack_sdk.oauth.installation_store import Bot, Installation


Model = TypeVar("Model", Bot, Installation)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🏷️ suggestion (non-blocking): Could this be _Model? Without the underscore it's an importable name, and dropping it later would count as removing public API.

Suggested change
Model = TypeVar("Model", Bot, Installation)
_Model = TypeVar("_Model", Bot, Installation)

)


Model = TypeVar("Model", Bot, Installation)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🏷️ suggestion (non-blocking): Same here.

Suggested change
Model = TypeVar("Model", Bot, Installation)
_Model = TypeVar("_Model", Bot, Installation)

return self.underlying.logger

@staticmethod
def _copy(value: Model) -> Model:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🪓 suggestion (non-blocking): _copy and Model now live in both stores. Could they live once in installation_store/internals.py and be imported by both?

duplicate = copy(value)
for name, field in vars(value).items():
if isinstance(field, (list, dict)):
setattr(duplicate, name, copy(field))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🤔 question (non-blocking): A nested value in custom_values (like custom_values["k"]["x"]) is still shared with the cache. Is that worth deepcopy, or is shallow enough since a custom value might not be copyable?

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.

Good point, a deepcopy that raises on an uncopyable custom value would break the lookup itself, which is worse than a shared nested value. Shallow works for me, so let's drop my suggestion 👍

self.assertEqual(i.bot_token, "xoxb-refreshed")
self.assertIn("E111-T1111-U111", store.cached_installations)

def test_find_installation_returns_a_copy(self):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

💯 praise: Replaying Bolt's user_token = None on both the miss and the hit, plus the scope list and custom value edits, pins every way the cache could leak.

Both cacheable stores defined the same _copy staticmethod and a public
Model TypeVar. They now import one private _copy_model from
installation_store/internals.py. The TypeVar is unconstrained because
internals.py cannot import the models without an import cycle.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@WilliamBergamin
WilliamBergamin merged commit eeda5bd into main Dec 1, 2026
19 checks passed
@WilliamBergamin
WilliamBergamin deleted the fix/cacheable-store-copy branch December 1, 2026 17:17
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 oauth semver:patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants